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 @@ -1,6 +1,6 @@ import xml.etree.ElementTree as et from django.db import transaction -from boards.models import KeyPair, GlobalId, Signature, Post +from boards.models import KeyPair, GlobalId, Signature, Post, Tag ENCODING_UNICODE = 'unicode' @@ -19,6 +19,8 @@ TAG_SIGNATURE = 'signature' TAG_CONTENT = 'content' TAG_ATTACHMENTS = 'attachments' TAG_ATTACHMENT = 'attachment' +TAG_TAGS = 'tags' +TAG_TAG = 'tag' TYPE_GET = 'get' @@ -57,13 +59,16 @@ class SyncManager: text = et.SubElement(content_tag, TAG_TEXT) text.text = post.get_sync_text() - 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) + thread = post.get_thread() + if post.is_opening(): + tag_tags = et.SubElement(content_tag, TAG_TAGS) + for tag in thread.get_tags(): + tag_tag = et.SubElement(tag_tags, TAG_TAG) + tag_tag.text = tag.name else: - # TODO Output tags here - pass + tag_thread = et.SubElement(content_tag, TAG_THREAD) + thread_id = et.SubElement(tag_thread, TAG_ID) + thread.get_opening_post().global_id.to_xml_element(thread_id) pub_time = et.SubElement(content_tag, TAG_PUB_TIME) pub_time.text = str(post.get_pub_time_str()) @@ -120,12 +125,16 @@ class SyncManager: pub_time = tag_content.find(TAG_PUB_TIME).text thread = tag_content.find(TAG_THREAD) + tags = [] if thread: opening_post = Post.objects.get( id=thread.find(TAG_ID).text) else: opening_post = None - # TODO Get tags here + tag_tags = tag_content.find(TAG_TAGS) + for tag_tag in tag_tags: + tag, created = Tag.objects.get_or_create(name=tag_tag.text) + tags.append(tag) # TODO Check that the replied posts are already present # before adding new ones @@ -134,7 +143,7 @@ class SyncManager: post = Post.objects.import_post( title=title, text=text, pub_time=pub_time, - opening_post=opening_post) + opening_post=opening_post, tags=tags) post.global_id = global_id else: # TODO Throw an exception? diff --git a/boards/tests/test_sync.py b/boards/tests/test_sync.py --- a/boards/tests/test_sync.py +++ b/boards/tests/test_sync.py @@ -1,4 +1,4 @@ -from boards.models import KeyPair, Post +from boards.models import KeyPair, Post, Tag from boards.models.post.sync import SyncManager from boards.tests.mocks import MockRequest from boards.views.sync import response_get @@ -16,7 +16,9 @@ class SyncTest(TestCase): """ KeyPair.objects.generate_key(primary=True) - post = Post.objects.create_post(title='test_title', text='test_text') + tag = Tag.objects.create(name='tag1') + post = Post.objects.create_post(title='test_title', text='test_text', + tags=[tag]) request = MockRequest() request.body = ( @@ -38,6 +40,7 @@ class SyncTest(TestCase): '' '%s' '%s' + '%s' '%s' '' % ( post.global_id.key, @@ -45,6 +48,7 @@ class SyncTest(TestCase): post.global_id.key_type, post.title, post.get_raw_text(), + post.get_thread().get_tags().first().name, post.get_pub_time_str(), ) in response_get(request).content.decode(), 'Wrong response generated for the GET request.') @@ -55,6 +59,11 @@ class SyncTest(TestCase): self.assertEqual(1, Post.objects.count(), 'Post was not created from XML response.') + parsed_post = Post.objects.first() + self.assertEqual('tag1', + parsed_post.get_thread().get_tags().first().name, + 'Invalid tag was parsed.') + SyncManager.parse_response_get(response) self.assertEqual(1, Post.objects.count(), 'The same post was imported twice.')