diff --git a/boards/models/post/__init__.py b/boards/models/post/__init__.py
--- a/boards/models/post/__init__.py
+++ b/boards/models/post/__init__.py
@@ -36,7 +36,6 @@ IMAGE_THUMB_SIZE = (200, 150)
TITLE_MAX_LENGTH = 200
-# TODO This should be removed
NO_IP = '0.0.0.0'
REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]')
@@ -129,7 +128,29 @@ class PostManager(models.Manager):
post.connect_threads(opening_posts)
post.connect_notifications()
+ return post
+
+ @transaction.atomic
+ def import_post(self, title: str, text:str, pub_time: str,
+ opening_post=None):
+ if opening_post is None:
+ thread = boards.models.thread.Thread.objects.create(
+ bump_time=pub_time, last_edit_time=pub_time)
+ # list(map(thread.tags.add, tags))
+ new_thread = True
+ else:
+ thread = opening_post.get_thread()
+ new_thread = False
+
+ post = Post.objects.create(title=title, text=text,
+ pub_time=pub_time,
+ poster_ip=NO_IP,
+ last_edit_time=pub_time,
+ thread_id=thread.id)
+
post.build_url()
+ post.connect_replies()
+ post.connect_notifications()
return post
@@ -346,15 +367,15 @@ class Post(models.Model, Viewable):
return
global_id = GlobalId(key_type=key.key_type,
key=key.public_key,
- local_id = self.id)
+ local_id=self.id)
global_id.save()
self.global_id = global_id
self.save(update_fields=['global_id'])
- def get_pub_time_epoch(self):
- return utils.datetime_to_epoch(self.pub_time)
+ def get_pub_time_str(self):
+ return str(self.pub_time)
def get_replied_ids(self):
"""
diff --git a/boards/models/post/sync.py b/boards/models/post/sync.py
--- a/boards/models/post/sync.py
+++ b/boards/models/post/sync.py
@@ -1,4 +1,5 @@
import xml.etree.ElementTree as et
+from django.db import transaction
from boards.models import KeyPair, GlobalId, Signature, Post
ENCODING_UNICODE = 'unicode'
@@ -64,7 +65,7 @@ class SyncManager:
pass
pub_time = et.SubElement(content_tag, TAG_PUB_TIME)
- pub_time.text = str(post.get_pub_time_epoch())
+ pub_time.text = str(post.get_pub_time_str())
signatures_tag = et.SubElement(model, TAG_SIGNATURES)
post_signatures = post.signature.all()
@@ -87,6 +88,7 @@ class SyncManager:
return et.tostring(response, ENCODING_UNICODE)
+ @transaction.atomic
def parse_response_get(self, response_xml):
tag_root = et.fromstring(response_xml)
tag_status = tag_root.find(TAG_STATUS)
@@ -100,17 +102,24 @@ class SyncManager:
print('Post with same ID already exists')
except GlobalId.DoesNotExist:
global_id = GlobalId.from_xml_element(tag_id)
+ global_id.save()
title = tag_content.find(TAG_TITLE).text
text = tag_content.find(TAG_TEXT).text
+ pub_time = tag_content.find(TAG_PUB_TIME).text
# TODO Check that the replied posts are already present
# before adding new ones
# TODO Pub time, thread, tags
+ # FIXME This prints are for testing purposes only, they must
+ # be removed after sync is implemented
print(title)
print(text)
- # post = Post.objects.create(title=title, text=text)
+
+ post = Post.objects.import_post(title=title, text=text,
+ pub_time=pub_time)
+ post.global_id = global_id
else:
# TODO Throw an exception?
pass
diff --git a/boards/tests/test_keys.py b/boards/tests/test_keys.py
--- a/boards/tests/test_keys.py
+++ b/boards/tests/test_keys.py
@@ -77,7 +77,7 @@ class KeyTest(TestCase):
key.public_key,
post.id,
key.key_type,
- str(reply_post.get_pub_time_epoch()),
+ str(reply_post.get_pub_time_str()),
) in response,
'Wrong XML generated for the GET response.')
diff --git a/boards/tests/test_sync.py b/boards/tests/test_sync.py
--- a/boards/tests/test_sync.py
+++ b/boards/tests/test_sync.py
@@ -1,4 +1,5 @@
from boards.models import KeyPair, Post
+from boards.models.post.sync import SyncManager
from boards.tests.mocks import MockRequest
from boards.views.sync import response_get
@@ -28,6 +29,7 @@ class SyncTest(TestCase):
post.global_id.key_type)
)
+ response = response_get(request).content.decode()
self.assertTrue(
'success'
''
@@ -36,13 +38,19 @@ class SyncTest(TestCase):
''
'%s'
'%s'
- '%d'
+ '%s'
'' % (
post.global_id.key,
post.id,
post.global_id.key_type,
post.title,
post.get_raw_text(),
- post.get_pub_time_epoch(),
+ post.get_pub_time_str(),
) in response_get(request).content.decode(),
'Wrong response generated for the GET request.')
+
+ post.delete()
+
+ SyncManager().parse_response_get(response)
+ self.assertEqual(1, Post.objects.count(),
+ 'Post was not created from XML response.')