Show More
@@ -1,6 +1,6 b'' | |||
|
1 | 1 | import xml.etree.ElementTree as et |
|
2 | 2 | from django.db import transaction |
|
3 | from boards.models import KeyPair, GlobalId, Signature, Post | |
|
3 | from boards.models import KeyPair, GlobalId, Signature, Post, Tag | |
|
4 | 4 | |
|
5 | 5 | ENCODING_UNICODE = 'unicode' |
|
6 | 6 | |
@@ -19,6 +19,8 b" TAG_SIGNATURE = 'signature'" | |||
|
19 | 19 | TAG_CONTENT = 'content' |
|
20 | 20 | TAG_ATTACHMENTS = 'attachments' |
|
21 | 21 | TAG_ATTACHMENT = 'attachment' |
|
22 | TAG_TAGS = 'tags' | |
|
23 | TAG_TAG = 'tag' | |
|
22 | 24 | |
|
23 | 25 | TYPE_GET = 'get' |
|
24 | 26 | |
@@ -57,13 +59,16 b' class SyncManager:' | |||
|
57 | 59 | text = et.SubElement(content_tag, TAG_TEXT) |
|
58 | 60 | text.text = post.get_sync_text() |
|
59 | 61 | |
|
60 |
|
|
|
61 | thread = et.SubElement(content_tag, TAG_THREAD) | |
|
62 |
t |
|
|
63 | post.get_thread().get_opening_post().global_id.to_xml_element(thread_id) | |
|
62 | thread = post.get_thread() | |
|
63 | if post.is_opening(): | |
|
64 | tag_tags = et.SubElement(content_tag, TAG_TAGS) | |
|
65 | for tag in thread.get_tags(): | |
|
66 | tag_tag = et.SubElement(tag_tags, TAG_TAG) | |
|
67 | tag_tag.text = tag.name | |
|
64 | 68 | else: |
|
65 | # TODO Output tags here | |
|
66 | pass | |
|
69 | tag_thread = et.SubElement(content_tag, TAG_THREAD) | |
|
70 | thread_id = et.SubElement(tag_thread, TAG_ID) | |
|
71 | thread.get_opening_post().global_id.to_xml_element(thread_id) | |
|
67 | 72 | |
|
68 | 73 | pub_time = et.SubElement(content_tag, TAG_PUB_TIME) |
|
69 | 74 | pub_time.text = str(post.get_pub_time_str()) |
@@ -120,12 +125,16 b' class SyncManager:' | |||
|
120 | 125 | pub_time = tag_content.find(TAG_PUB_TIME).text |
|
121 | 126 | |
|
122 | 127 | thread = tag_content.find(TAG_THREAD) |
|
128 | tags = [] | |
|
123 | 129 | if thread: |
|
124 | 130 | opening_post = Post.objects.get( |
|
125 | 131 | id=thread.find(TAG_ID).text) |
|
126 | 132 | else: |
|
127 | 133 | opening_post = None |
|
128 | # TODO Get tags here | |
|
134 | tag_tags = tag_content.find(TAG_TAGS) | |
|
135 | for tag_tag in tag_tags: | |
|
136 | tag, created = Tag.objects.get_or_create(name=tag_tag.text) | |
|
137 | tags.append(tag) | |
|
129 | 138 | |
|
130 | 139 | # TODO Check that the replied posts are already present |
|
131 | 140 | # before adding new ones |
@@ -134,7 +143,7 b' class SyncManager:' | |||
|
134 | 143 | |
|
135 | 144 | post = Post.objects.import_post( |
|
136 | 145 | title=title, text=text, pub_time=pub_time, |
|
137 | opening_post=opening_post) | |
|
146 | opening_post=opening_post, tags=tags) | |
|
138 | 147 | post.global_id = global_id |
|
139 | 148 | else: |
|
140 | 149 | # TODO Throw an exception? |
@@ -1,4 +1,4 b'' | |||
|
1 | from boards.models import KeyPair, Post | |
|
1 | from boards.models import KeyPair, Post, Tag | |
|
2 | 2 | from boards.models.post.sync import SyncManager |
|
3 | 3 | from boards.tests.mocks import MockRequest |
|
4 | 4 | from boards.views.sync import response_get |
@@ -16,7 +16,9 b' class SyncTest(TestCase):' | |||
|
16 | 16 | """ |
|
17 | 17 | |
|
18 | 18 | KeyPair.objects.generate_key(primary=True) |
|
19 | post = Post.objects.create_post(title='test_title', text='test_text') | |
|
19 | tag = Tag.objects.create(name='tag1') | |
|
20 | post = Post.objects.create_post(title='test_title', text='test_text', | |
|
21 | tags=[tag]) | |
|
20 | 22 | |
|
21 | 23 | request = MockRequest() |
|
22 | 24 | request.body = ( |
@@ -38,6 +40,7 b' class SyncTest(TestCase):' | |||
|
38 | 40 | '<id key="%s" local-id="%d" type="%s" />' |
|
39 | 41 | '<title>%s</title>' |
|
40 | 42 | '<text>%s</text>' |
|
43 | '<tags><tag>%s</tag></tags>' | |
|
41 | 44 | '<pub-time>%s</pub-time>' |
|
42 | 45 | '</content>' % ( |
|
43 | 46 | post.global_id.key, |
@@ -45,6 +48,7 b' class SyncTest(TestCase):' | |||
|
45 | 48 | post.global_id.key_type, |
|
46 | 49 | post.title, |
|
47 | 50 | post.get_raw_text(), |
|
51 | post.get_thread().get_tags().first().name, | |
|
48 | 52 | post.get_pub_time_str(), |
|
49 | 53 | ) in response_get(request).content.decode(), |
|
50 | 54 | 'Wrong response generated for the GET request.') |
@@ -55,6 +59,11 b' class SyncTest(TestCase):' | |||
|
55 | 59 | self.assertEqual(1, Post.objects.count(), |
|
56 | 60 | 'Post was not created from XML response.') |
|
57 | 61 | |
|
62 | parsed_post = Post.objects.first() | |
|
63 | self.assertEqual('tag1', | |
|
64 | parsed_post.get_thread().get_tags().first().name, | |
|
65 | 'Invalid tag was parsed.') | |
|
66 | ||
|
58 | 67 | SyncManager.parse_response_get(response) |
|
59 | 68 | self.assertEqual(1, Post.objects.count(), |
|
60 | 69 | 'The same post was imported twice.') |
General Comments 0
You need to be logged in to leave comments.
Login now