# HG changeset patch # User neko259 # Date 2015-07-12 11:19:59 # Node ID 52a453869c78c1d2c8ee4f1a3b31186f7e78a871 # Parent db37f36a3792481bd88655c3a2142bbc39c75e89 Don't allow to import the same post twice 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 @@ -102,24 +102,27 @@ class SyncManager: print('Post with same ID already exists') except GlobalId.DoesNotExist: global_id = GlobalId.from_xml_element(tag_id) - global_id.save() + + if not GlobalId.objects.global_id_exists(global_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 + 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 + # TODO thread, tags - # FIXME This prints are for testing purposes only, they must - # be removed after sync is implemented - print(title) - print(text) + # FIXME This prints are for testing purposes only, they must + # be removed after sync is implemented + print(title) + print(text) - post = Post.objects.import_post(title=title, text=text, - pub_time=pub_time) - post.global_id = global_id + 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/models/signature.py b/boards/models/signature.py --- a/boards/models/signature.py +++ b/boards/models/signature.py @@ -37,6 +37,15 @@ class GlobalIdManager(models.Manager): return et.tostring(request, 'unicode') + def global_id_exists(self, global_id): + """ + Checks if the same global id already exists in the system. + """ + + return self.filter(key=global_id.key, + key_type=global_id.key_type, + local_id=global_id.local_id).exists() + class GlobalId(models.Model): class Meta: 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 @@ -54,3 +54,7 @@ class SyncTest(TestCase): SyncManager().parse_response_get(response) self.assertEqual(1, Post.objects.count(), 'Post was not created from XML response.') + + SyncManager().parse_response_get(response) + self.assertEqual(1, Post.objects.count(), + 'The same post was imported twice.')