##// END OF EJS Templates
Sync-import of a single post is working
neko259 -
r1229:3cb2475c decentral
parent child Browse files
Show More
@@ -36,7 +36,6 b' IMAGE_THUMB_SIZE = (200, 150)'
36 36
37 37 TITLE_MAX_LENGTH = 200
38 38
39 # TODO This should be removed
40 39 NO_IP = '0.0.0.0'
41 40
42 41 REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]')
@@ -129,7 +128,29 b' class PostManager(models.Manager):'
129 128 post.connect_threads(opening_posts)
130 129 post.connect_notifications()
131 130
131 return post
132
133 @transaction.atomic
134 def import_post(self, title: str, text:str, pub_time: str,
135 opening_post=None):
136 if opening_post is None:
137 thread = boards.models.thread.Thread.objects.create(
138 bump_time=pub_time, last_edit_time=pub_time)
139 # list(map(thread.tags.add, tags))
140 new_thread = True
141 else:
142 thread = opening_post.get_thread()
143 new_thread = False
144
145 post = Post.objects.create(title=title, text=text,
146 pub_time=pub_time,
147 poster_ip=NO_IP,
148 last_edit_time=pub_time,
149 thread_id=thread.id)
150
132 151 post.build_url()
152 post.connect_replies()
153 post.connect_notifications()
133 154
134 155 return post
135 156
@@ -346,15 +367,15 b' class Post(models.Model, Viewable):'
346 367 return
347 368 global_id = GlobalId(key_type=key.key_type,
348 369 key=key.public_key,
349 local_id = self.id)
370 local_id=self.id)
350 371 global_id.save()
351 372
352 373 self.global_id = global_id
353 374
354 375 self.save(update_fields=['global_id'])
355 376
356 def get_pub_time_epoch(self):
357 return utils.datetime_to_epoch(self.pub_time)
377 def get_pub_time_str(self):
378 return str(self.pub_time)
358 379
359 380 def get_replied_ids(self):
360 381 """
@@ -1,4 +1,5 b''
1 1 import xml.etree.ElementTree as et
2 from django.db import transaction
2 3 from boards.models import KeyPair, GlobalId, Signature, Post
3 4
4 5 ENCODING_UNICODE = 'unicode'
@@ -64,7 +65,7 b' class SyncManager:'
64 65 pass
65 66
66 67 pub_time = et.SubElement(content_tag, TAG_PUB_TIME)
67 pub_time.text = str(post.get_pub_time_epoch())
68 pub_time.text = str(post.get_pub_time_str())
68 69
69 70 signatures_tag = et.SubElement(model, TAG_SIGNATURES)
70 71 post_signatures = post.signature.all()
@@ -87,6 +88,7 b' class SyncManager:'
87 88
88 89 return et.tostring(response, ENCODING_UNICODE)
89 90
91 @transaction.atomic
90 92 def parse_response_get(self, response_xml):
91 93 tag_root = et.fromstring(response_xml)
92 94 tag_status = tag_root.find(TAG_STATUS)
@@ -100,17 +102,24 b' class SyncManager:'
100 102 print('Post with same ID already exists')
101 103 except GlobalId.DoesNotExist:
102 104 global_id = GlobalId.from_xml_element(tag_id)
105 global_id.save()
103 106
104 107 title = tag_content.find(TAG_TITLE).text
105 108 text = tag_content.find(TAG_TEXT).text
109 pub_time = tag_content.find(TAG_PUB_TIME).text
106 110 # TODO Check that the replied posts are already present
107 111 # before adding new ones
108 112
109 113 # TODO Pub time, thread, tags
110 114
115 # FIXME This prints are for testing purposes only, they must
116 # be removed after sync is implemented
111 117 print(title)
112 118 print(text)
113 # post = Post.objects.create(title=title, text=text)
119
120 post = Post.objects.import_post(title=title, text=text,
121 pub_time=pub_time)
122 post.global_id = global_id
114 123 else:
115 124 # TODO Throw an exception?
116 125 pass
@@ -77,7 +77,7 b' class KeyTest(TestCase):'
77 77 key.public_key,
78 78 post.id,
79 79 key.key_type,
80 str(reply_post.get_pub_time_epoch()),
80 str(reply_post.get_pub_time_str()),
81 81 ) in response,
82 82 'Wrong XML generated for the GET response.')
83 83
@@ -1,4 +1,5 b''
1 1 from boards.models import KeyPair, Post
2 from boards.models.post.sync import SyncManager
2 3 from boards.tests.mocks import MockRequest
3 4 from boards.views.sync import response_get
4 5
@@ -28,6 +29,7 b' class SyncTest(TestCase):'
28 29 post.global_id.key_type)
29 30 )
30 31
32 response = response_get(request).content.decode()
31 33 self.assertTrue(
32 34 '<status>success</status>'
33 35 '<models>'
@@ -36,13 +38,19 b' class SyncTest(TestCase):'
36 38 '<id key="%s" local-id="%d" type="%s" />'
37 39 '<title>%s</title>'
38 40 '<text>%s</text>'
39 '<pub-time>%d</pub-time>'
41 '<pub-time>%s</pub-time>'
40 42 '</content>' % (
41 43 post.global_id.key,
42 44 post.id,
43 45 post.global_id.key_type,
44 46 post.title,
45 47 post.get_raw_text(),
46 post.get_pub_time_epoch(),
48 post.get_pub_time_str(),
47 49 ) in response_get(request).content.decode(),
48 50 'Wrong response generated for the GET request.')
51
52 post.delete()
53
54 SyncManager().parse_response_get(response)
55 self.assertEqual(1, Post.objects.count(),
56 'Post was not created from XML response.')
General Comments 0
You need to be logged in to leave comments. Login now