##// END OF EJS Templates
Added next id list, previous id list and thread to the post XML output
neko259 -
r829:5301b1d8 decentral
parent child Browse files
Show More
@@ -52,6 +52,8 b" TAG_TEXT = 'text'"
52 52 TAG_THREAD = 'thread'
53 53 TAG_PUB_TIME = 'pub-time'
54 54 TAG_EDIT_TIME = 'edit-time'
55 TAG_PREVIOUS = 'previous'
56 TAG_NEXT = 'next'
55 57
56 58 TYPE_GET = 'get'
57 59
@@ -110,8 +112,8 b' class PostManager(models.Manager):'
110 112 Thread.objects.process_oldest_threads()
111 113 self.connect_replies(post)
112 114
113 logger.info('Created post #%d with title %s' % (post.id,
114 post.get_title()))
115 logger.info('Created post #%d with title %s and key %s'
116 % (post.id, post.get_title(), post.global_id.key))
115 117
116 118 return post
117 119
@@ -148,9 +150,8 b' class PostManager(models.Manager):'
148 150 Connects replies to a post to show them as a reflink map
149 151 """
150 152
151 for reply_number in re.finditer(REGEX_REPLY, post.text.raw):
152 post_id = reply_number.group(1)
153 ref_post = self.filter(id=post_id)
153 for reply_number in post.get_replied_ids():
154 ref_post = self.filter(id=reply_number)
154 155 if ref_post.count() > 0:
155 156 referenced_post = ref_post[0]
156 157 referenced_post.referenced_posts.add(post)
@@ -235,7 +236,7 b' class PostManager(models.Manager):'
235 236
236 237 if not post.is_opening():
237 238 thread = et.SubElement(model, TAG_THREAD)
238 thread.text = post.get_opening_post_id()
239 thread.text = str(post.get_thread().get_opening_post_id())
239 240
240 241 pub_time = et.SubElement(model, TAG_PUB_TIME)
241 242 pub_time.text = str(post.get_pub_time_epoch())
@@ -243,6 +244,22 b' class PostManager(models.Manager):'
243 244 edit_time = et.SubElement(model, TAG_EDIT_TIME)
244 245 edit_time.text = str(post.get_edit_time_epoch())
245 246
247 previous_ids = post.get_replied_ids()
248 if len(previous_ids) > 0:
249 previous = et.SubElement(model, TAG_PREVIOUS)
250 for id in previous_ids:
251 prev_id = et.SubElement(previous, TAG_ID)
252 replied_post = Post.objects.get(id=id)
253 replied_post.global_id.to_xml_element(prev_id)
254
255
256 next_ids = post.referenced_posts.order_by('id').all()
257 if len(next_ids) > 0:
258 next_el = et.SubElement(model, TAG_NEXT)
259 for ref_post in next_ids:
260 next_id = et.SubElement(next_el, TAG_ID)
261 ref_post.global_id.to_xml_element(next_id)
262
246 263 return et.tostring(response, 'unicode')
247 264
248 265
@@ -435,6 +452,9 b' class Post(models.Model, Viewable):'
435 452 """
436 453
437 454 self.images.all().delete()
455 self.signature.all().delete()
456 if self.global_id:
457 self.global_id.delete()
438 458
439 459 super(Post, self).delete(using)
440 460
@@ -455,6 +475,7 b' class Post(models.Model, Viewable):'
455 475 global_id = GlobalId(key_type=key.key_type,
456 476 key=key.public_key,
457 477 local_id = self.id)
478 global_id.save()
458 479
459 480 self.global_id = global_id
460 481
@@ -465,3 +486,6 b' class Post(models.Model, Viewable):'
465 486
466 487 def get_edit_time_epoch(self):
467 488 return utils.datetime_to_epoch(self.last_edit_time)
489
490 def get_replied_ids(self):
491 return re.findall(REGEX_REPLY, self.text.raw)
@@ -51,24 +51,41 b' class KeyTest(TestCase):'
51 51
52 52 def test_response_get(self):
53 53 post = self._create_post_with_key()
54 reply_post = Post.objects.create_post(title='test_title',
55 text='[post]%d[/post]' % post.id, thread=post.get_thread())
56 reply_reply_post = Post.objects.create_post(title='',
57 text='[post]%d[/post]' % reply_post.id,
58 thread=post.get_thread())
54 59
55 response = Post.objects.generate_response_get([post])
60 response = Post.objects.generate_response_get([reply_post])
56 61 logger.debug(response)
57 62
58 63 self.assertTrue('<response>'
59 64 '<status>success</status>'
60 65 '<models>'
61 66 '<model name="post" ref-id="1">'
62 '<id key="pubkey" local-id="1" type="test_key_type" />'
67 '<id key="pubkey" local-id="%d" type="test_key_type" />'
63 68 '<title>test_title</title>'
64 '<text>test_text</text>'
69 '<text>[post]%d[/post]</text>'
70 '<thread>%d</thread>'
65 71 '<pub-time>%s</pub-time>'
66 72 '<edit-time>%s</edit-time>'
73 '<previous>'
74 '<id key="pubkey" local-id="%d" type="test_key_type" />'
75 '</previous>'
76 '<next>'
77 '<id key="pubkey" local-id="%d" type="test_key_type" />'
78 '</next>'
67 79 '</model>'
68 80 '</models>'
69 81 '</response>' % (
70 str(post.get_edit_time_epoch()),
71 str(post.get_pub_time_epoch())
82 reply_post.id,
83 post.id,
84 post.id,
85 str(reply_post.get_edit_time_epoch()),
86 str(reply_post.get_pub_time_epoch()),
87 post.id,
88 reply_reply_post.id,
72 89 ) in response,
73 90 'Wrong XML generated for the GET response.')
74 91
General Comments 0
You need to be logged in to leave comments. Login now