##// 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 models = et.SubElement(response, TAG_MODELS)
56 models = et.SubElement(response, TAG_MODELS)
57
57
58 # TODO Put global id's content into XML instad of manual serialization
59 for post in model_list:
58 for post in model_list:
60 model = et.SubElement(models, TAG_MODEL)
59 model = et.SubElement(models, TAG_MODEL)
61 model.set(ATTR_NAME, 'post')
60 model.set(ATTR_NAME, 'post')
62
61
63 content_tag = et.SubElement(model, TAG_CONTENT)
62 global_id = post.global_id
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
70
63
71 text = et.SubElement(content_tag, TAG_TEXT)
64 if global_id.content:
72 text.text = post.get_sync_text()
65 model.append(et.fromstring(global_id.content))
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
80 else:
66 else:
81 tag_thread = et.SubElement(content_tag, TAG_THREAD)
67 content_tag = et.SubElement(model, TAG_CONTENT)
82 thread_id = et.SubElement(tag_thread, TAG_ID)
68
83 thread.get_opening_post().global_id.to_xml_element(thread_id)
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)
72 title = et.SubElement(content_tag, TAG_TITLE)
86 pub_time.text = str(post.get_pub_time_str())
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()
78 thread = post.get_thread()
89 attachments = post.attachments.all()
79 if post.is_opening():
90 if len(images) > 0 or len(attachments) > 0:
80 tag_tags = et.SubElement(content_tag, TAG_TAGS)
91 attachments_tag = et.SubElement(content_tag, TAG_ATTACHMENTS)
81 for tag in thread.get_tags():
92 attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS)
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:
92 images = post.images.all()
95 SyncManager._attachment_to_xml(
93 attachments = post.attachments.all()
96 attachments_tag, attachment_refs, image.image.file,
94 if len(images) > 0 or len(attachments) > 0:
97 image.hash, image.image.url)
95 attachments_tag = et.SubElement(content_tag, TAG_ATTACHMENTS)
98 for file in attachments:
96 attachment_refs = et.SubElement(model, TAG_ATTACHMENT_REFS)
99 SyncManager._attachment_to_xml(
97
100 attachments_tag, attachment_refs, file.file.file,
98 for image in images:
101 file.hash, file.file.url)
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 signatures_tag = et.SubElement(model, TAG_SIGNATURES)
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 if post_signatures:
112 if post_signatures:
106 signatures = post_signatures
113 signatures = post_signatures
107 else:
114 else:
108 key = KeyPair.objects.get(public_key=post.global_id.key)
115 key = KeyPair.objects.get(public_key=global_id.key)
109 signature = Signature(
116 signature = Signature(
110 key_type=key.key_type,
117 key_type=key.key_type,
111 key=key.public_key,
118 key=key.public_key,
112 signature=key.sign(et.tostring(content_tag, encoding=ENCODING_UNICODE)),
119 signature=key.sign(global_id.content),
113 global_id=post.global_id,
120 global_id=global_id,
114 )
121 )
115 signature.save()
122 signature.save()
116 signatures = [signature]
123 signatures = [signature]
@@ -140,8 +147,8 b' class SyncManager:'
140 if exists:
147 if exists:
141 print('Post with same ID already exists')
148 print('Post with same ID already exists')
142 else:
149 else:
143 global_id.content = et.to_string(tag_content,
150 global_id.content = et.tostring(tag_content,
144 ENCODING_UNICODE)
151 ENCODING_UNICODE)
145 global_id.save()
152 global_id.save()
146 for signature in signatures:
153 for signature in signatures:
147 signature.global_id = global_id
154 signature.global_id = global_id
@@ -64,6 +64,12 b' class GlobalIdManager(models.Manager):'
64
64
65
65
66 class GlobalId(models.Model):
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 class Meta:
73 class Meta:
68 app_label = 'boards'
74 app_label = 'boards'
69
75
@@ -79,3 +79,9 b' def update_thread_on_delete(instance, **'
79 thread = instance.get_thread()
79 thread = instance.get_thread()
80 thread.last_edit_time = timezone.now()
80 thread.last_edit_time = timezone.now()
81 thread.save()
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