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