##// END OF EJS Templates
Delete global ID when deleting post. Cache model's content XML tag into global ID
neko259 -
r1520:ecaafe92 decentral
parent child Browse files
Show More
@@ -55,62 +55,69 b' class SyncManager:'
55 55
56 56 models = et.SubElement(response, TAG_MODELS)
57 57
58 # TODO Put global id's content into XML instad of manual serialization
59 58 for post in model_list:
60 59 model = et.SubElement(models, TAG_MODEL)
61 60 model.set(ATTR_NAME, 'post')
62 61
63 content_tag = et.SubElement(model, TAG_CONTENT)
64
65 tag_id = et.SubElement(content_tag, TAG_ID)
66 post.global_id.to_xml_element(tag_id)
67
68 title = et.SubElement(content_tag, TAG_TITLE)
69 title.text = post.title
62 global_id = post.global_id
70 63
71 text = et.SubElement(content_tag, TAG_TEXT)
72 text.text = post.get_sync_text()
73
74 thread = post.get_thread()
75 if post.is_opening():
76 tag_tags = et.SubElement(content_tag, TAG_TAGS)
77 for tag in thread.get_tags():
78 tag_tag = et.SubElement(tag_tags, TAG_TAG)
79 tag_tag.text = tag.name
64 if global_id.content:
65 model.append(et.fromstring(global_id.content))
80 66 else:
81 tag_thread = et.SubElement(content_tag, TAG_THREAD)
82 thread_id = et.SubElement(tag_thread, TAG_ID)
83 thread.get_opening_post().global_id.to_xml_element(thread_id)
67 content_tag = et.SubElement(model, TAG_CONTENT)
68
69 tag_id = et.SubElement(content_tag, TAG_ID)
70 global_id.to_xml_element(tag_id)
84 71
85 pub_time = et.SubElement(content_tag, TAG_PUB_TIME)
86 pub_time.text = str(post.get_pub_time_str())
72 title = et.SubElement(content_tag, TAG_TITLE)
73 title.text = post.title
74
75 text = et.SubElement(content_tag, TAG_TEXT)
76 text.text = post.get_sync_text()
87 77
88 images = post.images.all()
89 attachments = post.attachments.all()
90 if len(images) > 0 or len(attachments) > 0:
91 attachments_tag = et.SubElement(content_tag, TAG_ATTACHMENTS)
92 attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS)
78 thread = post.get_thread()
79 if post.is_opening():
80 tag_tags = et.SubElement(content_tag, TAG_TAGS)
81 for tag in thread.get_tags():
82 tag_tag = et.SubElement(tag_tags, TAG_TAG)
83 tag_tag.text = tag.name
84 else:
85 tag_thread = et.SubElement(content_tag, TAG_THREAD)
86 thread_id = et.SubElement(tag_thread, TAG_ID)
87 thread.get_opening_post().global_id.to_xml_element(thread_id)
88
89 pub_time = et.SubElement(content_tag, TAG_PUB_TIME)
90 pub_time.text = str(post.get_pub_time_str())
93 91
94 for image in images:
95 SyncManager._attachment_to_xml(
96 attachments_tag, attachment_refs, image.image.file,
97 image.hash, image.image.url)
98 for file in attachments:
99 SyncManager._attachment_to_xml(
100 attachments_tag, attachment_refs, file.file.file,
101 file.hash, file.file.url)
92 images = post.images.all()
93 attachments = post.attachments.all()
94 if len(images) > 0 or len(attachments) > 0:
95 attachments_tag = et.SubElement(content_tag, TAG_ATTACHMENTS)
96 attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS)
97
98 for image in images:
99 SyncManager._attachment_to_xml(
100 attachments_tag, attachment_refs, image.image.file,
101 image.hash, image.image.url)
102 for file in attachments:
103 SyncManager._attachment_to_xml(
104 attachments_tag, attachment_refs, file.file.file,
105 file.hash, file.file.url)
106
107 global_id.content = et.tostring(content_tag, ENCODING_UNICODE)
108 global_id.save()
102 109
103 110 signatures_tag = et.SubElement(model, TAG_SIGNATURES)
104 post_signatures = post.global_id.signature_set.all()
111 post_signatures = global_id.signature_set.all()
105 112 if post_signatures:
106 113 signatures = post_signatures
107 114 else:
108 key = KeyPair.objects.get(public_key=post.global_id.key)
115 key = KeyPair.objects.get(public_key=global_id.key)
109 116 signature = Signature(
110 117 key_type=key.key_type,
111 118 key=key.public_key,
112 signature=key.sign(et.tostring(content_tag, encoding=ENCODING_UNICODE)),
113 global_id=post.global_id,
119 signature=key.sign(global_id.content),
120 global_id=global_id,
114 121 )
115 122 signature.save()
116 123 signatures = [signature]
@@ -140,8 +147,8 b' class SyncManager:'
140 147 if exists:
141 148 print('Post with same ID already exists')
142 149 else:
143 global_id.content = et.to_string(tag_content,
144 ENCODING_UNICODE)
150 global_id.content = et.tostring(tag_content,
151 ENCODING_UNICODE)
145 152 global_id.save()
146 153 for signature in signatures:
147 154 signature.global_id = global_id
@@ -64,6 +64,12 b' class GlobalIdManager(models.Manager):'
64 64
65 65
66 66 class GlobalId(models.Model):
67 """
68 Global model ID and cache.
69 Key, key type and local ID make a single global identificator of the model.
70 Content is an XML cache of the model that can be passed along between nodes
71 without manual serialization each time.
72 """
67 73 class Meta:
68 74 app_label = 'boards'
69 75
@@ -79,3 +79,9 b' def update_thread_on_delete(instance, **'
79 79 thread = instance.get_thread()
80 80 thread.last_edit_time = timezone.now()
81 81 thread.save()
82
83
84 @receiver(post_delete, sender=Post)
85 def delete_global_id(instance, **kwargs):
86 if instance.global_id and instance.global_id.id:
87 instance.global_id.delete()
General Comments 0
You need to be logged in to leave comments. Login now