|
4 | 4 | from temba.msgs.models import Attachment, Media, Q
|
5 | 5 |
|
6 | 6 |
|
7 |
| -def compose_serialize(translation=None, json_encode=False, *, base_language=None, optin=None): |
| 7 | +def compose_serialize(translations, *, json_encode=False, base_language=None, optin=None) -> dict: |
8 | 8 | """
|
9 |
| - Serializes attachments from db to compose widget for populating initial widget values |
| 9 | + Serializes translations to format for compose widget |
10 | 10 | """
|
11 | 11 |
|
12 |
| - if not translation: |
| 12 | + if not translations: |
13 | 13 | return {}
|
14 | 14 |
|
15 |
| - translation = copy.deepcopy(translation) |
| 15 | + translations = copy.deepcopy(translations) |
16 | 16 |
|
17 | 17 | if base_language and optin:
|
18 |
| - translation[base_language]["optin"] = {"uuid": str(optin.uuid), "name": optin.name} |
| 18 | + translations[base_language]["optin"] = {"uuid": str(optin.uuid), "name": optin.name} |
19 | 19 |
|
20 |
| - for details in translation.values(): |
21 |
| - if "attachments" in details: |
22 |
| - details["attachments"] = compose_serialize_attachments(details["attachments"]) |
| 20 | + for translation in translations.values(): |
| 21 | + if "attachments" in translation: |
| 22 | + translation["attachments"] = compose_serialize_attachments(translation["attachments"]) |
23 | 23 |
|
24 |
| - if json_encode: |
25 |
| - return json.dumps(translation) |
| 24 | + # for now compose widget only supports simple text quick replies |
| 25 | + if "quick_replies" in translation: |
| 26 | + translation["quick_replies"] = [qr["text"] for qr in translation["quick_replies"]] |
26 | 27 |
|
27 |
| - return translation |
| 28 | + return json.dumps(translations) if json_encode else translations |
28 | 29 |
|
29 | 30 |
|
30 |
| -def compose_serialize_attachments(attachments): |
31 |
| - if not attachments: |
32 |
| - return [] |
33 |
| - parsed_attachments = Attachment.parse_all(attachments) |
34 |
| - serialized_attachments = [] |
35 |
| - for parsed_attachment in parsed_attachments: |
36 |
| - media = Media.objects.filter( |
37 |
| - Q(content_type=parsed_attachment.content_type) and Q(url=parsed_attachment.url) |
38 |
| - ).first() |
39 |
| - serialized_attachment = { |
40 |
| - "uuid": str(media.uuid), |
41 |
| - "content_type": media.content_type, |
42 |
| - "url": media.url, |
43 |
| - "filename": media.filename, |
44 |
| - "size": str(media.size), |
45 |
| - } |
46 |
| - serialized_attachments.append(serialized_attachment) |
47 |
| - return serialized_attachments |
48 |
| - |
49 |
| - |
50 |
| -def compose_deserialize(compose): |
| 31 | +def compose_serialize_attachments(attachments: list) -> list: |
| 32 | + serialized = [] |
| 33 | + |
| 34 | + for parsed in Attachment.parse_all(attachments): |
| 35 | + media = Media.objects.filter(Q(content_type=parsed.content_type) and Q(url=parsed.url)).first() |
| 36 | + serialized.append( |
| 37 | + { |
| 38 | + "uuid": str(media.uuid), |
| 39 | + "content_type": media.content_type, |
| 40 | + "url": media.url, |
| 41 | + "filename": media.filename, |
| 42 | + "size": str(media.size), |
| 43 | + } |
| 44 | + ) |
| 45 | + return serialized |
| 46 | + |
| 47 | + |
| 48 | +def compose_deserialize(compose: dict) -> dict: |
51 | 49 | """
|
52 | 50 | Deserializes attachments from compose widget to db for saving final db values
|
53 | 51 | """
|
54 |
| - for details in compose.values(): |
55 |
| - details["attachments"] = compose_deserialize_attachments(details.get("attachments", [])) |
| 52 | + for translation in compose.values(): |
| 53 | + translation["attachments"] = compose_deserialize_attachments(translation.get("attachments", [])) |
| 54 | + |
| 55 | + # for now compose widget only supports simple text quick replies |
| 56 | + if "quick_replies" in translation: |
| 57 | + translation["quick_replies"] = [{"text": qr} for qr in translation["quick_replies"]] |
56 | 58 | return compose
|
57 | 59 |
|
58 | 60 |
|
59 |
| -def compose_deserialize_attachments(attachments): |
| 61 | +def compose_deserialize_attachments(attachments: list) -> list: |
60 | 62 | if not attachments:
|
61 | 63 | return []
|
62 |
| - return [f"{a['content_type']}:{a['url']}" for a in attachments] |
| 64 | + |
| 65 | + return [str(Attachment(a["content_type"], a["url"])) for a in attachments] |
0 commit comments