# HG changeset patch # User neko259 # Date 2016-11-27 19:34:28 # Node ID 9abb9926b6a2ca874fc04f8cde59ee3d50003609 # Parent d6dc78904e286a034d783bf1cfeeb8c6d1896751 Allow syncing URL attachments diff --git a/boards/models/attachment/__init__.py b/boards/models/attachment/__init__.py --- a/boards/models/attachment/__init__.py +++ b/boards/models/attachment/__init__.py @@ -86,3 +86,6 @@ class Attachment(models.Model): return get_image_dimensions(preview_path) else: return 200, 150 + + def is_internal(self): + return self.url is None or len(self.url) == 0 diff --git a/boards/models/post/sync.py b/boards/models/post/sync.py --- a/boards/models/post/sync.py +++ b/boards/models/post/sync.py @@ -50,6 +50,7 @@ ATTR_URL = 'url' ATTR_ID_TYPE = 'id-type' ID_TYPE_MD5 = 'md5' +ID_TYPE_URL = 'url' STATUS_SUCCESS = 'success' @@ -77,11 +78,17 @@ class SyncManager: if global_id.content: model.append(et.fromstring(global_id.content)) if len(attachments) > 0: - attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS) - for file in attachments: - SyncManager._attachment_to_xml( - None, attachment_refs, file.file.file, - file.hash, file.file.url) + internal_attachments = False + for attachment in attachments: + if attachment.is_internal(): + internal_attachments = True + break + + if internal_attachments: + attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS) + for file in attachments: + SyncManager._attachment_to_xml( + None, attachment_refs, file) else: content_tag = et.SubElement(model, TAG_CONTENT) @@ -114,12 +121,21 @@ class SyncManager: if len(attachments) > 0: attachments_tag = et.SubElement(content_tag, TAG_ATTACHMENTS) - attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS) + + internal_attachments = False + for attachment in attachments: + if attachment.is_internal(): + internal_attachments = True + break + + if internal_attachments: + attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS) + else: + attachment_refs = None for file in attachments: SyncManager._attachment_to_xml( - attachments_tag, attachment_refs, file.file.file, - file.hash, file.file.url) + attachments_tag, attachment_refs, file) version_tag = et.SubElement(content_tag, TAG_VERSION) version_tag.text = str(post.version) @@ -289,14 +305,19 @@ class SyncManager: return signatures @staticmethod - def _attachment_to_xml(tag_attachments, tag_refs, file, hash, url): + def _attachment_to_xml(tag_attachments, tag_refs, attachment): if tag_attachments is not None: - mimetype = get_file_mimetype(file) - attachment = et.SubElement(tag_attachments, TAG_ATTACHMENT) - attachment.set(ATTR_MIMETYPE, mimetype) - attachment.set(ATTR_ID_TYPE, ID_TYPE_MD5) - attachment.text = hash + attachment_tag = et.SubElement(tag_attachments, TAG_ATTACHMENT) + if attachment.is_internal(): + mimetype = get_file_mimetype(attachment.file.file) + attachment_tag.set(ATTR_MIMETYPE, mimetype) + attachment_tag.set(ATTR_ID_TYPE, ID_TYPE_MD5) + attachment_tag.text = attachment.hash + else: + attachment_tag.set(ATTR_ID_TYPE, ID_TYPE_URL) + attachment_tag.text = attachment.url - attachment_ref = et.SubElement(tag_refs, TAG_ATTACHMENT_REF) - attachment_ref.set(ATTR_REF, hash) - attachment_ref.set(ATTR_URL, url) + if tag_refs is not None: + attachment_ref = et.SubElement(tag_refs, TAG_ATTACHMENT_REF) + attachment_ref.set(ATTR_REF, attachment.hash) + attachment_ref.set(ATTR_URL, attachment.url)