Skip to content

Commit

Permalink
Add basic support for email templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rehoumir committed Aug 15, 2024
1 parent 3e748c2 commit f9cef05
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 7 deletions.
57 changes: 57 additions & 0 deletions project_rossum_deploy/commands/download/email_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import asyncio
from anyio import Path
from rossum_api import ElisAPIClient
from rossum_api.api_client import Resource

from project_rossum_deploy.commands.download.helpers import (
should_write_object,
)

from project_rossum_deploy.common.read_write import write_json
from project_rossum_deploy.utils.functions import (
templatize_name_id,
)


async def download_email_templates_for_queue(
client: ElisAPIClient,
queue_id: int,
parent_dir: Path,
changed_files: list = [],
download_all: bool = False,
):
# email_templates = []

paginated_email_templates = [
email_template
async for email_template in client.list_all_email_templates(queue=queue_id)
]

# Refetch in case the paginated fields don't include everything
# Use raw dicts and not dataclasses in case of fields not defined in the Rossum API lib
full_email_templates = await asyncio.gather(
*[
client._http_client.fetch_one(Resource.EmailTemplate, email_template.id)
for email_template in paginated_email_templates
]
)

for email_template in full_email_templates:
email_template_path = (
parent_dir
/ "email_templates"
/ f'{templatize_name_id(email_template["name"], email_template["id"])}.json'
)
if download_all or await should_write_object(
email_template_path, email_template, changed_files
):
await write_json(
email_template_path,
email_template,
Resource.EmailTemplate,
log_message=f"Pulled {email_template_path}",
)

# email_templates.append((destination_local, email_template))

# return email_templates
16 changes: 14 additions & 2 deletions project_rossum_deploy/commands/download/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from rossum_api import ElisAPIClient
from rossum_api.api_client import Resource

from project_rossum_deploy.commands.download.email_templates import (
download_email_templates_for_queue,
)
from project_rossum_deploy.commands.download.helpers import (
determine_object_destination,
should_write_object,
Expand Down Expand Up @@ -116,19 +119,28 @@ async def download_queues_for_workspace(

inbox_id = extract_id_from_url(queue["inbox"])
if inbox_id:
queue["inbox"] = await download_inbox(
queue["inbox"] = await download_inbox_for_queue(
client=client,
parent_dir=queue_path,
inbox_id=inbox_id,
changed_files=changed_files,
download_all=download_all,
)

await download_email_templates_for_queue(
client=client,
parent_dir=queue_path,
queue_id=queue["id"],
changed_files=changed_files,
download_all=download_all,
)

queues.append(queue)

return queues


async def download_inbox(
async def download_inbox_for_queue(
client: ElisAPIClient,
parent_dir: Path,
inbox_id: int,
Expand Down
2 changes: 1 addition & 1 deletion project_rossum_deploy/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def detemplatize_name_id(path: Path | str) -> tuple[str, int]:
if str(path.stem) in ["queue", "workspace", "inbox"]:
parts = path.parent.name.split("_")
return "_".join(parts[:-1]), int(parts[-1].removeprefix("[").removesuffix("]"))
elif str(path.parent.stem) in ("hooks, schemas"):
elif str(path.parent.stem) in ["hooks", "schemas", "email_templates"]:
parts = path.stem.split("_")
return "_".join(parts[:-1]), int(parts[-1].removeprefix("[").removesuffix("]"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
"token_lifetime_s": null,
"hook_template": "https://shared-eu2.rossum.app/api/v1/hook_templates/30",
"modified_by": "https://shared-eu2.rossum.app/api/v1/users/285991",
"modified_at": "2024-07-12T09:49:33.794716Z"
"modified_at": "2024-08-14T13:42:23.661462Z"
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
"token_lifetime_s": null,
"hook_template": null,
"modified_by": "https://shared-eu2.rossum.app/api/v1/users/285991",
"modified_at": "2024-07-12T10:06:50.465784Z"
"modified_at": "2024-08-13T09:44:28.066752Z"
}
3 changes: 2 additions & 1 deletion tests/reference_project/source/organization.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"creator": "https://shared-eu2.rossum.app/api/v1/users/285991",
"settings": {},
"modified_by": "https://shared-eu2.rossum.app/api/v1/users/285991",
"modified_at": "2024-07-12T10:06:09.185846Z"
"modified_at": "2024-08-14T13:41:40.413544Z",
"sandbox": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -1202,5 +1202,5 @@
],
"metadata": {},
"modified_by": "https://shared-eu2.rossum.app/api/v1/users/285991",
"modified_at": "2024-07-12T10:12:41.185402Z"
"modified_at": "2024-08-14T13:39:55.612296Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851992,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851992",
"name": "Annotation status change - confirmed",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162844",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Verified documents: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Your documents have been checked by annotator.<br><br>{{ document_list }}<br><br>Regards</p>\n",
"type": "custom",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687095"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851994,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851994",
"name": "Annotation status change - exported",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162844",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Documents exported: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Your documents have been successfully exported.<br><br>{{ document_list }}<br><br>Regards</p>\n",
"type": "custom",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687097"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851990,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851990",
"name": "Annotation status change - received",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162844",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Documents received: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Your documents have been successfully received.<br><br>{{ document_list }}<br><br>Regards</p>\n",
"type": "custom",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687093"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": 5851989,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851989",
"name": "Default rejection template",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162844",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Rejected document {{parent_email_subject}}",
"message": "<p>Dear sender,<br><br>The attached document has been rejected.<br><br><br>Best regards,<br>{{ user_name }}</p>\n",
"type": "rejection_default",
"enabled": true,
"automate": true,
"triggers": [],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851996,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851996",
"name": "Email with no processable attachments",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162844",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "No processable documents: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Unfortunately, we have not received any document in the email that we can process. Please send a corrected version if appropriate.<br><br>Regards</p>",
"type": "email_with_no_processable_attachments",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687099"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851991,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851991",
"name": "Annotation status change - confirmed",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162843",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Verified documents: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Your documents have been checked by annotator.<br><br>{{ document_list }}<br><br>Regards</p>\n",
"type": "custom",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687094"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851993,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851993",
"name": "Annotation status change - exported",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162843",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Documents exported: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Your documents have been successfully exported.<br><br>{{ document_list }}<br><br>Regards</p>\n",
"type": "custom",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687096"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851988,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851988",
"name": "Annotation status change - received",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162843",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Documents received: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Your documents have been successfully received.<br><br>{{ document_list }}<br><br>Regards</p>\n",
"type": "custom",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687092"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": 5851987,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851987",
"name": "Default rejection template",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162843",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "Rejected document {{parent_email_subject}}",
"message": "<p>Dear sender,<br><br>The attached document has been rejected.<br><br><br>Best regards,<br>{{ user_name }}</p>\n",
"type": "rejection_default",
"enabled": true,
"automate": true,
"triggers": [],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": 5851995,
"url": "https://shared-eu2.rossum.app/api/v1/email_templates/5851995",
"name": "Email with no processable attachments",
"queue": "https://shared-eu2.rossum.app/api/v1/queues/1162843",
"organization": "https://shared-eu2.rossum.app/api/v1/organizations/263865",
"subject": "No processable documents: {{ parent_email_subject }}",
"message": "<p>Dear sender,<br><br>Unfortunately, we have not received any document in the email that we can process. Please send a corrected version if appropriate.<br><br>Regards</p>",
"type": "email_with_no_processable_attachments",
"enabled": false,
"automate": false,
"triggers": [
"https://shared-eu2.rossum.app/api/v1/triggers/4687098"
],
"to": [
{
"email": "{{sender_email}}"
}
],
"cc": [],
"bcc": []
}

0 comments on commit f9cef05

Please sign in to comment.