##// 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 TITLE_MAX_LENGTH = 200
37 TITLE_MAX_LENGTH = 200
38
38
39 # TODO This should be removed
40 NO_IP = '0.0.0.0'
39 NO_IP = '0.0.0.0'
41
40
42 REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]')
41 REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]')
@@ -129,7 +128,29 b' class PostManager(models.Manager):'
129 post.connect_threads(opening_posts)
128 post.connect_threads(opening_posts)
130 post.connect_notifications()
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 post.build_url()
151 post.build_url()
152 post.connect_replies()
153 post.connect_notifications()
133
154
134 return post
155 return post
135
156
@@ -346,15 +367,15 b' class Post(models.Model, Viewable):'
346 return
367 return
347 global_id = GlobalId(key_type=key.key_type,
368 global_id = GlobalId(key_type=key.key_type,
348 key=key.public_key,
369 key=key.public_key,
349 local_id = self.id)
370 local_id=self.id)
350 global_id.save()
371 global_id.save()
351
372
352 self.global_id = global_id
373 self.global_id = global_id
353
374
354 self.save(update_fields=['global_id'])
375 self.save(update_fields=['global_id'])
355
376
356 def get_pub_time_epoch(self):
377 def get_pub_time_str(self):
357 return utils.datetime_to_epoch(self.pub_time)
378 return str(self.pub_time)
358
379
359 def get_replied_ids(self):
380 def get_replied_ids(self):
360 """
381 """
@@ -1,4 +1,5 b''
1 import xml.etree.ElementTree as et
1 import xml.etree.ElementTree as et
2 from django.db import transaction
2 from boards.models import KeyPair, GlobalId, Signature, Post
3 from boards.models import KeyPair, GlobalId, Signature, Post
3
4
4 ENCODING_UNICODE = 'unicode'
5 ENCODING_UNICODE = 'unicode'
@@ -64,7 +65,7 b' class SyncManager:'
64 pass
65 pass
65
66
66 pub_time = et.SubElement(content_tag, TAG_PUB_TIME)
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 signatures_tag = et.SubElement(model, TAG_SIGNATURES)
70 signatures_tag = et.SubElement(model, TAG_SIGNATURES)
70 post_signatures = post.signature.all()
71 post_signatures = post.signature.all()
@@ -87,6 +88,7 b' class SyncManager:'
87
88
88 return et.tostring(response, ENCODING_UNICODE)
89 return et.tostring(response, ENCODING_UNICODE)
89
90
91 @transaction.atomic
90 def parse_response_get(self, response_xml):
92 def parse_response_get(self, response_xml):
91 tag_root = et.fromstring(response_xml)
93 tag_root = et.fromstring(response_xml)
92 tag_status = tag_root.find(TAG_STATUS)
94 tag_status = tag_root.find(TAG_STATUS)
@@ -100,17 +102,24 b' class SyncManager:'
100 print('Post with same ID already exists')
102 print('Post with same ID already exists')
101 except GlobalId.DoesNotExist:
103 except GlobalId.DoesNotExist:
102 global_id = GlobalId.from_xml_element(tag_id)
104 global_id = GlobalId.from_xml_element(tag_id)
105 global_id.save()
103
106
104 title = tag_content.find(TAG_TITLE).text
107 title = tag_content.find(TAG_TITLE).text
105 text = tag_content.find(TAG_TEXT).text
108 text = tag_content.find(TAG_TEXT).text
109 pub_time = tag_content.find(TAG_PUB_TIME).text
106 # TODO Check that the replied posts are already present
110 # TODO Check that the replied posts are already present
107 # before adding new ones
111 # before adding new ones
108
112
109 # TODO Pub time, thread, tags
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 print(title)
117 print(title)
112 print(text)
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 else:
123 else:
115 # TODO Throw an exception?
124 # TODO Throw an exception?
116 pass
125 pass
@@ -77,7 +77,7 b' class KeyTest(TestCase):'
77 key.public_key,
77 key.public_key,
78 post.id,
78 post.id,
79 key.key_type,
79 key.key_type,
80 str(reply_post.get_pub_time_epoch()),
80 str(reply_post.get_pub_time_str()),
81 ) in response,
81 ) in response,
82 'Wrong XML generated for the GET response.')
82 'Wrong XML generated for the GET response.')
83
83
@@ -1,4 +1,5 b''
1 from boards.models import KeyPair, Post
1 from boards.models import KeyPair, Post
2 from boards.models.post.sync import SyncManager
2 from boards.tests.mocks import MockRequest
3 from boards.tests.mocks import MockRequest
3 from boards.views.sync import response_get
4 from boards.views.sync import response_get
4
5
@@ -28,6 +29,7 b' class SyncTest(TestCase):'
28 post.global_id.key_type)
29 post.global_id.key_type)
29 )
30 )
30
31
32 response = response_get(request).content.decode()
31 self.assertTrue(
33 self.assertTrue(
32 '<status>success</status>'
34 '<status>success</status>'
33 '<models>'
35 '<models>'
@@ -36,13 +38,19 b' class SyncTest(TestCase):'
36 '<id key="%s" local-id="%d" type="%s" />'
38 '<id key="%s" local-id="%d" type="%s" />'
37 '<title>%s</title>'
39 '<title>%s</title>'
38 '<text>%s</text>'
40 '<text>%s</text>'
39 '<pub-time>%d</pub-time>'
41 '<pub-time>%s</pub-time>'
40 '</content>' % (
42 '</content>' % (
41 post.global_id.key,
43 post.global_id.key,
42 post.id,
44 post.id,
43 post.global_id.key_type,
45 post.global_id.key_type,
44 post.title,
46 post.title,
45 post.get_raw_text(),
47 post.get_raw_text(),
46 post.get_pub_time_epoch(),
48 post.get_pub_time_str(),
47 ) in response_get(request).content.decode(),
49 ) in response_get(request).content.decode(),
48 'Wrong response generated for the GET request.')
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