Show More
@@ -102,24 +102,27 b' class SyncManager:' | |||||
102 | print('Post with same ID already exists') |
|
102 | print('Post with same ID already exists') | |
103 | except GlobalId.DoesNotExist: |
|
103 | except GlobalId.DoesNotExist: | |
104 | global_id = GlobalId.from_xml_element(tag_id) |
|
104 | global_id = GlobalId.from_xml_element(tag_id) | |
105 | global_id.save() |
|
105 | ||
|
106 | if not GlobalId.objects.global_id_exists(global_id): | |||
|
107 | global_id.save() | |||
106 |
|
108 | |||
107 | title = tag_content.find(TAG_TITLE).text |
|
109 | title = tag_content.find(TAG_TITLE).text | |
108 | text = tag_content.find(TAG_TEXT).text |
|
110 | text = tag_content.find(TAG_TEXT).text | |
109 | pub_time = tag_content.find(TAG_PUB_TIME).text |
|
111 | pub_time = tag_content.find(TAG_PUB_TIME).text | |
110 | # TODO Check that the replied posts are already present |
|
112 | ||
111 | # before adding new ones |
|
113 | # TODO Check that the replied posts are already present | |
|
114 | # before adding new ones | |||
112 |
|
115 | |||
113 |
# TODO |
|
116 | # TODO thread, tags | |
114 |
|
117 | |||
115 | # FIXME This prints are for testing purposes only, they must |
|
118 | # FIXME This prints are for testing purposes only, they must | |
116 | # be removed after sync is implemented |
|
119 | # be removed after sync is implemented | |
117 | print(title) |
|
120 | print(title) | |
118 | print(text) |
|
121 | print(text) | |
119 |
|
122 | |||
120 | post = Post.objects.import_post(title=title, text=text, |
|
123 | post = Post.objects.import_post(title=title, text=text, | |
121 | pub_time=pub_time) |
|
124 | pub_time=pub_time) | |
122 | post.global_id = global_id |
|
125 | post.global_id = global_id | |
123 | else: |
|
126 | else: | |
124 | # TODO Throw an exception? |
|
127 | # TODO Throw an exception? | |
125 | pass |
|
128 | pass |
@@ -37,6 +37,15 b' class GlobalIdManager(models.Manager):' | |||||
37 |
|
37 | |||
38 | return et.tostring(request, 'unicode') |
|
38 | return et.tostring(request, 'unicode') | |
39 |
|
39 | |||
|
40 | def global_id_exists(self, global_id): | |||
|
41 | """ | |||
|
42 | Checks if the same global id already exists in the system. | |||
|
43 | """ | |||
|
44 | ||||
|
45 | return self.filter(key=global_id.key, | |||
|
46 | key_type=global_id.key_type, | |||
|
47 | local_id=global_id.local_id).exists() | |||
|
48 | ||||
40 |
|
49 | |||
41 | class GlobalId(models.Model): |
|
50 | class GlobalId(models.Model): | |
42 | class Meta: |
|
51 | class Meta: |
@@ -54,3 +54,7 b' class SyncTest(TestCase):' | |||||
54 | SyncManager().parse_response_get(response) |
|
54 | SyncManager().parse_response_get(response) | |
55 | self.assertEqual(1, Post.objects.count(), |
|
55 | self.assertEqual(1, Post.objects.count(), | |
56 | 'Post was not created from XML response.') |
|
56 | 'Post was not created from XML response.') | |
|
57 | ||||
|
58 | SyncManager().parse_response_get(response) | |||
|
59 | self.assertEqual(1, Post.objects.count(), | |||
|
60 | 'The same post was imported twice.') |
General Comments 0
You need to be logged in to leave comments.
Login now