##// END OF EJS Templates
Added some minor info
Added some minor info

File last commit:

r1238:a83ac0a9 decentral
r1238:a83ac0a9 decentral
Show More
sync.py
164 lines | 5.4 KiB | text/x-python | PythonLexer
neko259
Updated sync method for requesting and getting a post
r1177 import xml.etree.ElementTree as et
neko259
Sync-import of a single post is working
r1229 from django.db import transaction
neko259
Updated sync method for requesting and getting a post
r1177 from boards.models import KeyPair, GlobalId, Signature, Post
ENCODING_UNICODE = 'unicode'
TAG_MODEL = 'model'
TAG_REQUEST = 'request'
TAG_RESPONSE = 'response'
TAG_ID = 'id'
TAG_STATUS = 'status'
TAG_MODELS = 'models'
TAG_TITLE = 'title'
TAG_TEXT = 'text'
TAG_THREAD = 'thread'
TAG_PUB_TIME = 'pub-time'
TAG_SIGNATURES = 'signatures'
TAG_SIGNATURE = 'signature'
TAG_CONTENT = 'content'
TAG_ATTACHMENTS = 'attachments'
TAG_ATTACHMENT = 'attachment'
TYPE_GET = 'get'
ATTR_VERSION = 'version'
ATTR_TYPE = 'type'
ATTR_NAME = 'name'
ATTR_VALUE = 'value'
ATTR_MIMETYPE = 'mimetype'
neko259
Added signature verification for a post
r1237 ATTR_KEY = 'key'
neko259
Updated sync method for requesting and getting a post
r1177
STATUS_SUCCESS = 'success'
class SyncManager:
neko259
Made SyncManager's methods static
r1236 @staticmethod
def generate_response_get(model_list: list):
neko259
Updated sync method for requesting and getting a post
r1177 response = et.Element(TAG_RESPONSE)
status = et.SubElement(response, TAG_STATUS)
status.text = STATUS_SUCCESS
models = et.SubElement(response, TAG_MODELS)
for post in model_list:
model = et.SubElement(models, TAG_MODEL)
model.set(ATTR_NAME, 'post')
content_tag = et.SubElement(model, TAG_CONTENT)
tag_id = et.SubElement(content_tag, TAG_ID)
post.global_id.to_xml_element(tag_id)
title = et.SubElement(content_tag, TAG_TITLE)
title.text = post.title
text = et.SubElement(content_tag, TAG_TEXT)
neko259
Convert local post IDs to global when generating responses
r1228 text.text = post.get_sync_text()
neko259
Updated sync method for requesting and getting a post
r1177
if not post.is_opening():
thread = et.SubElement(content_tag, TAG_THREAD)
thread_id = et.SubElement(thread, TAG_ID)
post.get_thread().get_opening_post().global_id.to_xml_element(thread_id)
else:
# TODO Output tags here
pass
pub_time = et.SubElement(content_tag, TAG_PUB_TIME)
neko259
Sync-import of a single post is working
r1229 pub_time.text = str(post.get_pub_time_str())
neko259
Updated sync method for requesting and getting a post
r1177
signatures_tag = et.SubElement(model, TAG_SIGNATURES)
post_signatures = post.signature.all()
if post_signatures:
neko259
Added some minor info
r1238 signatures = post_signatures
# TODO Adding signature to a post is not yet added. For now this
# block is useless
neko259
Updated sync method for requesting and getting a post
r1177 else:
# TODO Maybe the signature can be computed only once after
# the post is added? Need to add some on_save signal queue
# and add this there.
key = KeyPair.objects.get(public_key=post.global_id.key)
signatures = [Signature(
key_type=key.key_type,
key=key.public_key,
neko259
Added signature verification for a post
r1237 signature=key.sign(et.tostring(content_tag, ENCODING_UNICODE)),
neko259
Updated sync method for requesting and getting a post
r1177 )]
for signature in signatures:
signature_tag = et.SubElement(signatures_tag, TAG_SIGNATURE)
signature_tag.set(ATTR_TYPE, signature.key_type)
signature_tag.set(ATTR_VALUE, signature.signature)
neko259
Added signature verification for a post
r1237 signature_tag.set(ATTR_KEY, signature.key)
neko259
Updated sync method for requesting and getting a post
r1177
return et.tostring(response, ENCODING_UNICODE)
neko259
Made SyncManager's methods static
r1236 @staticmethod
neko259
Sync-import of a single post is working
r1229 @transaction.atomic
neko259
Made SyncManager's methods static
r1236 def parse_response_get(response_xml):
neko259
Updated sync method for requesting and getting a post
r1177 tag_root = et.fromstring(response_xml)
tag_status = tag_root.find(TAG_STATUS)
if STATUS_SUCCESS == tag_status.text:
tag_models = tag_root.find(TAG_MODELS)
for tag_model in tag_models:
tag_content = tag_model.find(TAG_CONTENT)
neko259
Added signature verification for a post
r1237
neko259
Added some minor info
r1238 valid = SyncManager._verify_model(tag_content, tag_model)
neko259
Added signature verification for a post
r1237
if not valid:
raise Exception('Invalid model signature')
neko259
Updated sync method for requesting and getting a post
r1177 tag_id = tag_content.find(TAG_ID)
neko259
Refactored code for getting existing and new global ids
r1233 global_id, exists = GlobalId.from_xml_element(tag_id)
neko259
Don't allow to import the same post twice
r1232
neko259
Refactored code for getting existing and new global ids
r1233 if exists:
print('Post with same ID already exists')
else:
global_id.save()
neko259
Updated sync method for requesting and getting a post
r1177
neko259
Refactored code for getting existing and new global ids
r1233 title = tag_content.find(TAG_TITLE).text
text = tag_content.find(TAG_TEXT).text
pub_time = tag_content.find(TAG_PUB_TIME).text
neko259
Don't allow to import the same post twice
r1232
neko259
Refactored code for getting existing and new global ids
r1233 thread = tag_content.find(TAG_THREAD)
if thread:
opening_post = Post.objects.get(
id=thread.find(TAG_ID).text)
else:
opening_post = None
neko259
Moved the TODO item to the proper section
r1235 # TODO Get tags here
neko259
Updated sync method for requesting and getting a post
r1177
neko259
Refactored code for getting existing and new global ids
r1233 # TODO Check that the replied posts are already present
# before adding new ones
neko259
Sync-import of a single post is working
r1229
neko259
Added some minor info
r1238 # TODO Get images
neko259
Refactored code for getting existing and new global ids
r1233 post = Post.objects.import_post(
title=title, text=text, pub_time=pub_time,
opening_post=opening_post)
post.global_id = global_id
neko259
Updated sync method for requesting and getting a post
r1177 else:
# TODO Throw an exception?
pass
neko259
Added signature verification for a post
r1237
@staticmethod
neko259
Added some minor info
r1238 def _verify_model(tag_content, tag_model):
neko259
Added signature verification for a post
r1237 """
Verifies all signatures for a single model.
"""
valid = True
tag_signatures = tag_model.find(TAG_SIGNATURES)
for tag_signature in tag_signatures:
signature_type = tag_signature.get(ATTR_TYPE)
signature_value = tag_signature.get(ATTR_VALUE)
signature_key = tag_signature.get(ATTR_KEY)
if not KeyPair.objects.verify(
signature_key,
et.tostring(tag_content, ENCODING_UNICODE),
signature_value, signature_type):
valid = False
break
return valid