Hi everyone,
I am having a problem with creating a parent Infolog and child Infologs.
- I have attached an email to a group to create with Infolog but there is a problem that it does not work with the group email.
- When I create the parent Infolog, it has a UID but creating subs by UID is completely impossible to attach.
def create_task_on_egroupware(request, title, egroupware_group_id, description, parent_uid=None):
API_URL = settings.EGROUPWARE_API_URL
username = request.user.username
password = settings.EGROUPWARE_CREDENTIALS.get('password')
api_url = f"{API_URL}/{username}/infolog/"
headers = {
"Content-Type": "application/json",
"Prefer": "return=representation",
"Accept": "application/pretty+json"
}
# 🔹 Lấy thời gian hiện tại theo chuẩn UTC, định dạng ISO 8601
start_time = datetime.now(timezone.utc).isoformat()
# 🔹 Lấy thông tin nhóm từ ExtendedGroup dựa vào egroupware_group_id
try:
group = ExtendedGroup.objects.get(egroupware_group_id=egroupware_group_id)
group_name = group.group.name # Lấy tên group
group_email = group.email_manager # Lấy email manager
group_principal_url = f"principals/groups/{group_name.lower()}/" # Dùng principal-URL
except ExtendedGroup.DoesNotExist:
print(f"❌ Group not found for egroupware_group_id = {egroupware_group_id}")
return None
# 🔹 Định nghĩa payload
payload = {
"title": title,
"start": start_time, # Thời gian tạo task
"timeZone": "Europe/Warsaw", # Múi giờ
"description": description, # Mô tả task
"priority": 5, # Độ ưu tiên mặc định
"participants": [
{
"@type": "Participant",
"name": group_name,
"uri": group_principal_url, # 🟢 Dùng principal-URL thay vì email
"email": group_email,
"kind": "group",
"roles": {"attendee": True}
}
],
"status": "confirmed",
"progress": "needs-action",
}
# Nếu có nhiệm vụ cha, thêm `relatedTo` đúng định dạng
if parent_uid:
payload["relatedTo"] = {
parent_uid: {
"@type": "Relation",
"relation": "parent"
}
}
print(f"🔹 Sending request to EGroupware: {json.dumps(payload, indent=2)}")
# 🔹 Gửi request lên EGroupware
response = requests.post(api_url, auth=(username, password), headers=headers, json=payload)
print(f"🔹 Response status: {response.status_code}")
print(f"🔹 Response text: {response.text}")
# 🔹 Xử lý lỗi JSONDecodeError nếu phản hồi rỗng
if response.status_code == 201:
try:
result = response.json()
return result.get("uid", None) # Trả về UID của task vừa tạo
except json.JSONDecodeError:
print(f"⚠️ JSONDecodeError but task created successfully.")
return "Task Created (No JSON Response)" # Giả lập UID
else:
print(f"⚠️ Error creating task: {response.text}")
return None
def wait_for_task_creation(api_url, auth, task_uid, headers, max_retries=10, wait_time=5):
for i in range(max_retries):
print(f"⏳ Check if parent task exists... Attempt {i+1}/{max_retries}")
response = requests.get(api_url, auth=auth, headers=headers)
if response.status_code == 200:
try:
tasks = response.json().get("responses", {})
# Kiểm tra nếu task cha đã xuất hiện
for task_url, task in tasks.items():
if task.get("uid") == task_uid:
print(f"✅ Task parent {task_uid} exists in EGroupware!")
return True
# 🔹 Nếu chưa tìm thấy, thử tìm theo title
print("⚠️ Task parent {task_uid} not found, searching by title...")
title_search_url = f"{api_url}?filter[title]=Room 101"
search_response = requests.get(title_search_url, auth=auth, headers=headers)
if search_response.status_code == 200:
task_data = search_response.json().get("responses", {})
for task_url, task in task_data.items():
if task.get("title") == f"Room {room_number}":
found_uid = task.get("uid")
print(f"✅ Task parent {found_uid} exists in EGroupware!")
return found_uid # Trả về UID mới
except json.JSONDecodeError:
print("⚠️ JSONDecodeError - Response is not valid JSON. Try again...")
time.sleep(wait_time)
print(f"❌ Task parent {task_uid} does not exist after {max_retries} retries!")
return False
def create_checkout_room_task(request, room_number):
extended_group = get_object_or_404(ExtendedGroup, group__name="housekeeping")
egroupware_group_id = extended_group.egroupware_group_id
# Tạo Task cha
parent_uid = create_task_on_egroupware(
request,
title=f"Room {room_number}",
egroupware_group_id=egroupware_group_id,
description=f"Room {room_number}"
)
if not parent_uid:
return "❌ Error creating Task Room on EGroupware!"
# Chờ task cha xuất hiện trước khi tạo sub-task
API_URL = settings.EGROUPWARE_API_URL
username = request.user.username
password = settings.EGROUPWARE_CREDENTIALS.get('password')
headers = {"Accept": "application/pretty+json"}
if not wait_for_task_creation(f"{API_URL}/{username}/infolog/", auth=(username, password), task_uid=parent_uid, headers=headers):
print(f"⚠️ The parent task {parent_uid} does not exist. Try retrieving the task list from the EGroupware API...")
response = requests.get(f"{API_URL}/{username}/infolog/?filter[title]=Room {room_number}", auth=(username, password), headers=headers)
if response.status_code == 200:
try:
task_data = response.json().get("responses", {})
for task_url, task in task_data.items():
if task.get("title") == f"Room {room_number}":
parent_uid = task.get("uid")
print(f"✅ Found parent task again: {parent_uid}")
break
except json.JSONDecodeError:
print(f"❌ JSONDecodeError - Response is not valid JSON. Try again...")
if not parent_uid:
return f"❌ The parent task {parent_uid} has not yet been recorded in the system.!"
# Lấy danh sách sub-task từ TaskTemplate
sub_tasks = TaskTemplate.objects.filter(task_type="Cleaning vacant rooms")
for task in sub_tasks:
subtask_description = f"""
Task Parent: Room {room_number}
Area: {task.area}
Task: {task.name}
"""
create_task_on_egroupware(
request,
title=f"Room {room_number}: {task.name}",
egroupware_group_id=egroupware_group_id,
description=subtask_description.strip(),
parent_uid=parent_uid
)
return f"✅ Task Room {room_number} and sub-tasks have been created on EGroupware!"
Sending request to EGroupware: {
“title”: “Room 101”,
“start”: “2025-03-05T12:05:22.232169+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Room 101”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”
}
Response status: 201
Response text: {
"@type": “Task”,
“prodId”: “EGroupware InfoLog 23.1.011”,
“uid”: “urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”,
“created”: “2025-03-05T12:05:22Z”,
“title”: “Room 101”,
“start”: “2025-03-05T13:05:22”,
“timeZone”: “Europe/Warsaw”,
“description”: “Room 101”,
“participants”: {
“1067”: {
"@type": “Participant”,
“name”: “Tuan Nguyen Le”,
“email”: "ngletuan94@gmail.com",
“kind”: “individual”,
“roles”: { “owner”: true }
},
“housekeeping -1008@lists.egroupware.org”: {
"@type": “Participant”,
“name”: “housekeeping”,
“email”: "-1008@lists.egroupware.org",
“kind”: “individual”,
“roles”: { “informational”: true }
}
},
“status”: “confirmed”,
“progress”: “needs-action”,
“priority”: 9,
“privacy”: “public”,
“egroupware.org:type”: “task”
}
Check if parent task exists… Attempt 1/10
Task parent urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5 exists in EGroupware!
Sending request to EGroupware: {
“title”: “Room 101: Change bed linens and sheets”,
“start”: “2025-03-05T12:05:22.470393+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Change bed linens and sheets”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Vacuum the entire room and bathroom”,
“start”: “2025-03-05T12:05:22.541383+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Vacuum the entire room and bathroom”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Dust the desk, wardrobe, doors, windowsills, lamps”,
“start”: “2025-03-05T12:05:22.610553+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Dust the desk, wardrobe, doors, windowsills, lamps”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Clean the kettle and cups”,
“start”: “2025-03-05T12:05:22.682884+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Clean the kettle and cups”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Refill the coffee and tea set, leave a bottle of water”,
“start”: “2025-03-05T12:05:22.759663+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Refill the coffee and tea set, leave a bottle of water”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Check the hotel service brochure set, restock if necessary”,
“start”: “2025-03-05T12:05:22.828724+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Check the hotel service brochure set, restock if necessary”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Clean the ceiling and remove cobwebs”,
“start”: “2025-03-05T12:05:22.893179+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Clean the ceiling and remove cobwebs”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Clean the fridge, discard any leftover products”,
“start”: “2025-03-05T12:05:22.969534+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Clean the fridge, discard any leftover products”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Clean windows, frames, and windowsills (both sides)”,
“start”: “2025-03-05T12:05:23.035995+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Clean windows, frames, and windowsills (both sides)”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
Sending request to EGroupware: {
“title”: “Room 101: Wipe the entrance door to the room and bathroom”,
“start”: “2025-03-05T12:05:23.114756+00:00”,
“timeZone”: “Europe/Warsaw”,
“description”: “Task Parent: Room 101\n Area: Hotel\n Task: Wipe the entrance door to the room and bathroom”,
“priority”: 5,
“participants”: [
{
"@type": “Participant”,
“name”: “housekeeping”,
“uri”: “principals/groups/housekeeping/”,
“email”: "-1008@lists.egroupware.org",
“kind”: “group”,
“roles”: {
“attendee”: true
}
}
],
“status”: “confirmed”,
“progress”: “needs-action”,
“relatedTo”: {
“urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5”: {
"@type": “Relation”,
“relation”: “parent”
}
}
}
Response status: 422
Response text: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
️ Error creating task: {
“error”: 422,
“message”: “Error parsing JsTask attribute ‘relatedTo’: UID ‘urn:uuid:f8e4616e-22b3-43f2-8d82-30c69ff10ca5’ NOT found!”
}
- I have tried all ways to fix the error but still not working.
created
last reply
- 7
replies
- 152
views
- 3
users
- 7
links