Show More
@@ -127,15 +127,13 b' class PostManager(models.Manager):' | |||||
127 |
|
127 | |||
128 | @transaction.atomic |
|
128 | @transaction.atomic | |
129 | def import_post(self, title: str, text:str, pub_time: str, |
|
129 | def import_post(self, title: str, text:str, pub_time: str, | |
130 | opening_post=None): |
|
130 | opening_post=None, tags=list()): | |
131 | if opening_post is None: |
|
131 | if opening_post is None: | |
132 | thread = boards.models.thread.Thread.objects.create( |
|
132 | thread = boards.models.thread.Thread.objects.create( | |
133 | bump_time=pub_time, last_edit_time=pub_time) |
|
133 | bump_time=pub_time, last_edit_time=pub_time) | |
134 |
|
|
134 | list(map(thread.tags.add, tags)) | |
135 | new_thread = True |
|
|||
136 | else: |
|
135 | else: | |
137 | thread = opening_post.get_thread() |
|
136 | thread = opening_post.get_thread() | |
138 | new_thread = False |
|
|||
139 |
|
137 | |||
140 | post = Post.objects.create(title=title, text=text, |
|
138 | post = Post.objects.create(title=title, text=text, | |
141 | pub_time=pub_time, |
|
139 | pub_time=pub_time, |
@@ -97,32 +97,32 b' class SyncManager:' | |||||
97 | for tag_model in tag_models: |
|
97 | for tag_model in tag_models: | |
98 | tag_content = tag_model.find(TAG_CONTENT) |
|
98 | tag_content = tag_model.find(TAG_CONTENT) | |
99 | tag_id = tag_content.find(TAG_ID) |
|
99 | tag_id = tag_content.find(TAG_ID) | |
100 | try: |
|
100 | global_id, exists = GlobalId.from_xml_element(tag_id) | |
101 | GlobalId.from_xml_element(tag_id, existing=True) |
|
|||
102 | print('Post with same ID already exists') |
|
|||
103 | except GlobalId.DoesNotExist: |
|
|||
104 | global_id = GlobalId.from_xml_element(tag_id) |
|
|||
105 |
|
101 | |||
106 | if not GlobalId.objects.global_id_exists(global_id): |
|
102 | if exists: | |
107 | global_id.save() |
|
103 | print('Post with same ID already exists') | |
|
104 | else: | |||
|
105 | global_id.save() | |||
108 |
|
106 | |||
109 |
|
|
107 | title = tag_content.find(TAG_TITLE).text | |
110 |
|
|
108 | text = tag_content.find(TAG_TEXT).text | |
111 |
|
|
109 | pub_time = tag_content.find(TAG_PUB_TIME).text | |
112 |
|
110 | |||
113 | # TODO Check that the replied posts are already present |
|
111 | thread = tag_content.find(TAG_THREAD) | |
114 |
|
|
112 | if thread: | |
115 |
|
113 | opening_post = Post.objects.get( | ||
116 |
|
|
114 | id=thread.find(TAG_ID).text) | |
|
115 | # TODO Get tags here | |||
|
116 | else: | |||
|
117 | opening_post = None | |||
117 |
|
118 | |||
118 | # FIXME This prints are for testing purposes only, they must |
|
119 | # TODO Check that the replied posts are already present | |
119 | # be removed after sync is implemented |
|
120 | # before adding new ones | |
120 | print(title) |
|
|||
121 | print(text) |
|
|||
122 |
|
121 | |||
123 |
|
|
122 | post = Post.objects.import_post( | |
124 |
|
|
123 | title=title, text=text, pub_time=pub_time, | |
125 |
post |
|
124 | opening_post=opening_post) | |
|
125 | post.global_id = global_id | |||
126 | else: |
|
126 | else: | |
127 | # TODO Throw an exception? |
|
127 | # TODO Throw an exception? | |
128 | pass |
|
128 | pass |
@@ -78,26 +78,28 b' class GlobalId(models.Model):' | |||||
78 | element.set(ATTR_LOCAL_ID, str(self.local_id)) |
|
78 | element.set(ATTR_LOCAL_ID, str(self.local_id)) | |
79 |
|
79 | |||
80 | @staticmethod |
|
80 | @staticmethod | |
81 |
def from_xml_element(element: et.Element |
|
81 | def from_xml_element(element: et.Element): | |
82 | """ |
|
82 | """ | |
83 | Parses XML id tag and gets global id from it. |
|
83 | Parses XML id tag and gets global id from it. | |
84 |
|
84 | |||
85 | Arguments: |
|
85 | Arguments: | |
86 | element -- the XML 'id' element |
|
86 | element -- the XML 'id' element | |
87 | existing -- if this is False, a new instance of GlobalId will be |
|
87 | ||
88 | created. Otherwise, we will search for an existing GlobalId instance |
|
88 | Returns: | |
89 | and throw DoesNotExist if there isn't one. |
|
89 | global_id -- id itself | |
|
90 | exists -- True if the global id was taken from database, False if it | |||
|
91 | did not exist and was created. | |||
90 | """ |
|
92 | """ | |
91 |
|
93 | |||
92 |
|
|
94 | try: | |
93 | return GlobalId.objects.get(key=element.get(ATTR_KEY), |
|
95 | return GlobalId.objects.get(key=element.get(ATTR_KEY), | |
94 | key_type=element.get(ATTR_KEY_TYPE), |
|
96 | key_type=element.get(ATTR_KEY_TYPE), | |
95 | local_id=int(element.get( |
|
97 | local_id=int(element.get( | |
96 | ATTR_LOCAL_ID))) |
|
98 | ATTR_LOCAL_ID))), True | |
97 | else: |
|
99 | except GlobalId.DoesNotExist: | |
98 | return GlobalId(key=element.get(ATTR_KEY), |
|
100 | return GlobalId(key=element.get(ATTR_KEY), | |
99 | key_type=element.get(ATTR_KEY_TYPE), |
|
101 | key_type=element.get(ATTR_KEY_TYPE), | |
100 | local_id=int(element.get(ATTR_LOCAL_ID))) |
|
102 | local_id=int(element.get(ATTR_LOCAL_ID))), False | |
101 |
|
103 | |||
102 |
|
104 | |||
103 | class Signature(models.Model): |
|
105 | class Signature(models.Model): |
@@ -24,13 +24,9 b' def response_get(request):' | |||||
24 | root_tag = et.fromstring(request_xml) |
|
24 | root_tag = et.fromstring(request_xml) | |
25 | model_tag = root_tag[0] |
|
25 | model_tag = root_tag[0] | |
26 | for id_tag in model_tag: |
|
26 | for id_tag in model_tag: | |
27 | try: |
|
27 | global_id, exists = GlobalId.from_xml_element(id_tag) | |
28 | global_id = GlobalId.from_xml_element(id_tag, existing=True) |
|
28 | if exists: | |
29 | posts.append(Post.objects.get(global_id=global_id)) |
|
29 | posts.append(Post.objects.get(global_id=global_id)) | |
30 | except GlobalId.DoesNotExist: |
|
|||
31 | # This is normal. If we don't have such GlobalId in the system, |
|
|||
32 | # just ignore this ID and proceed to the next one. |
|
|||
33 | pass |
|
|||
34 |
|
30 | |||
35 | response_xml = SyncManager().generate_response_get(posts) |
|
31 | response_xml = SyncManager().generate_response_get(posts) | |
36 |
|
32 |
General Comments 0
You need to be logged in to leave comments.
Login now