Show More
@@ -15,8 +15,6 b' class Command(BaseCommand):' | |||||
15 | for global_id in GlobalId.objects.exclude(content__isnull=True).exclude( |
|
15 | for global_id in GlobalId.objects.exclude(content__isnull=True).exclude( | |
16 | content=''): |
|
16 | content=''): | |
17 | if global_id.is_local(): |
|
17 | if global_id.is_local(): | |
18 |
global_id.c |
|
18 | global_id.clear_cache() | |
19 | global_id.save() |
|
|||
20 | global_id.signature_set.all().delete() |
|
|||
21 | count += 1 |
|
19 | count += 1 | |
22 | print('Invalidated {} caches.'.format(count)) |
|
20 | print('Invalidated {} caches.'.format(count)) |
@@ -50,10 +50,11 b' class Command(BaseCommand):' | |||||
50 | else: |
|
50 | else: | |
51 | raise Exception('Invalid global ID') |
|
51 | raise Exception('Invalid global ID') | |
52 | else: |
|
52 | else: | |
53 | print('Running LIST request') |
|
53 | print('Running LIST request...') | |
54 | h = httplib2.Http() |
|
54 | h = httplib2.Http() | |
55 | xml = GlobalId.objects.generate_request_list() |
|
55 | xml = GlobalId.objects.generate_request_list() | |
56 | response, content = h.request(list_url, method="POST", body=xml) |
|
56 | response, content = h.request(list_url, method="POST", body=xml) | |
|
57 | print('Processing response...') | |||
57 |
|
58 | |||
58 | root = ET.fromstring(content) |
|
59 | root = ET.fromstring(content) | |
59 | status = root.findall('status')[0].text |
|
60 | status = root.findall('status')[0].text | |
@@ -78,8 +79,9 b' class Command(BaseCommand):' | |||||
78 | for offset in range(0, len(ids_to_sync), limit): |
|
79 | for offset in range(0, len(ids_to_sync), limit): | |
79 | xml = GlobalId.objects.generate_request_get(ids_to_sync[offset:offset+limit]) |
|
80 | xml = GlobalId.objects.generate_request_get(ids_to_sync[offset:offset+limit]) | |
80 | h = httplib2.Http() |
|
81 | h = httplib2.Http() | |
81 | print('Running GET request') |
|
82 | print('Running GET request...') | |
82 | response, content = h.request(get_url, method="POST", body=xml) |
|
83 | response, content = h.request(get_url, method="POST", body=xml) | |
|
84 | print('Processing response...') | |||
83 |
|
85 | |||
84 | SyncManager.parse_response_get(content, file_url) |
|
86 | SyncManager.parse_response_get(content, file_url) | |
85 | else: |
|
87 | else: |
@@ -388,9 +388,10 b' class Post(models.Model, Viewable):' | |||||
388 | self.version = F('version') + 1 |
|
388 | self.version = F('version') + 1 | |
389 |
|
389 | |||
390 | def clear_cache(self): |
|
390 | def clear_cache(self): | |
|
391 | """ | |||
|
392 | Clears sync data (content cache, signatures etc). | |||
|
393 | """ | |||
391 | global_id = self.global_id |
|
394 | global_id = self.global_id | |
392 | if global_id is not None and global_id.is_local()\ |
|
395 | if global_id is not None and global_id.is_local()\ | |
393 | and global_id.content is not None: |
|
396 | and global_id.content is not None: | |
394 |
global_id.c |
|
397 | global_id.clear_cache() | |
395 | global_id.save() |
|
|||
396 | global_id.signature_set.all().delete() |
|
@@ -130,7 +130,7 b' class PostManager(models.Manager):' | |||||
130 | @transaction.atomic |
|
130 | @transaction.atomic | |
131 | def import_post(self, title: str, text: str, pub_time: str, global_id, |
|
131 | def import_post(self, title: str, text: str, pub_time: str, global_id, | |
132 | opening_post=None, tags=list(), files=list(), |
|
132 | opening_post=None, tags=list(), files=list(), | |
133 | tripcode=None): |
|
133 | tripcode=None, version=1): | |
134 | is_opening = opening_post is None |
|
134 | is_opening = opening_post is None | |
135 | if is_opening: |
|
135 | if is_opening: | |
136 | thread = boards.models.thread.Thread.objects.create( |
|
136 | thread = boards.models.thread.Thread.objects.create( | |
@@ -139,20 +139,43 b' class PostManager(models.Manager):' | |||||
139 | else: |
|
139 | else: | |
140 | thread = opening_post.get_thread() |
|
140 | thread = opening_post.get_thread() | |
141 |
|
141 | |||
142 |
post = self.create(title=title, |
|
142 | post = self.create(title=title, | |
|
143 | text=text, | |||
143 | pub_time=pub_time, |
|
144 | pub_time=pub_time, | |
144 | poster_ip=NO_IP, |
|
145 | poster_ip=NO_IP, | |
145 | last_edit_time=pub_time, |
|
146 | last_edit_time=pub_time, | |
146 | global_id=global_id, |
|
147 | global_id=global_id, | |
147 | opening=is_opening, |
|
148 | opening=is_opening, | |
148 |
thread=thread, |
|
149 | thread=thread, | |
|
150 | tripcode=tripcode, | |||
|
151 | version=version) | |||
149 |
|
152 | |||
150 | # TODO Add files |
|
|||
151 | for file in files: |
|
153 | for file in files: | |
152 | self._add_file_to_post(file, post) |
|
154 | self._add_file_to_post(file, post) | |
153 |
|
155 | |||
154 | post.threads.add(thread) |
|
156 | post.threads.add(thread) | |
155 |
|
157 | |||
|
158 | @transaction.atomic | |||
|
159 | def update_post(self, post, title: str, text: str, pub_time: str, | |||
|
160 | tags=list(), files=list(), tripcode=None, version=1): | |||
|
161 | post.title = title | |||
|
162 | post.text = text | |||
|
163 | post.pub_time = pub_time | |||
|
164 | post.tripcode = tripcode | |||
|
165 | post.version = version | |||
|
166 | post.save() | |||
|
167 | ||||
|
168 | post.clear_cache() | |||
|
169 | ||||
|
170 | post.images.clear() | |||
|
171 | post.attachments.clear() | |||
|
172 | for file in files: | |||
|
173 | self._add_file_to_post(file, post) | |||
|
174 | ||||
|
175 | thread = post.get_thread() | |||
|
176 | thread.tags.clear() | |||
|
177 | list(map(thread.tags.add, tags)) | |||
|
178 | ||||
156 | def _add_file_to_post(self, file, post): |
|
179 | def _add_file_to_post(self, file, post): | |
157 | file_type = file.name.split('.')[-1].lower() |
|
180 | file_type = file.name.split('.')[-1].lower() | |
158 | if file_type in IMAGE_TYPES: |
|
181 | if file_type in IMAGE_TYPES: |
@@ -172,8 +172,10 b' class SyncManager:' | |||||
172 | global_id, exists = GlobalId.from_xml_element(tag_id) |
|
172 | global_id, exists = GlobalId.from_xml_element(tag_id) | |
173 | signatures = SyncManager._verify_model(global_id, content_str, tag_model) |
|
173 | signatures = SyncManager._verify_model(global_id, content_str, tag_model) | |
174 |
|
174 | |||
175 | if exists: |
|
175 | version = int(tag_content.find(TAG_VERSION).text) | |
176 | print('Post with same ID already exists') |
|
176 | is_old = exists and global_id.post.version < version | |
|
177 | if exists and not is_old: | |||
|
178 | print('Post with same ID exists and is up to date.') | |||
177 | else: |
|
179 | else: | |
178 | global_id.content = content_str |
|
180 | global_id.content = content_str | |
179 | global_id.save() |
|
181 | global_id.save() | |
@@ -227,11 +229,20 b' class SyncManager:' | |||||
227 |
|
229 | |||
228 | files.append(attached_file) |
|
230 | files.append(attached_file) | |
229 |
|
231 | |||
230 | Post.objects.import_post( |
|
232 | if is_old: | |
231 | title=title, text=text, pub_time=pub_time, |
|
233 | post = global_id.post | |
232 | opening_post=opening_post, tags=tags, |
|
234 | Post.objects.update_post( | |
233 | global_id=global_id, files=files, tripcode=tripcode) |
|
235 | post, title=title, text=text, pub_time=pub_time, | |
234 | print('Parsed post {}'.format(global_id)) |
|
236 | tags=tags, files=files, tripcode=tripcode, | |
|
237 | version=version) | |||
|
238 | print('Parsed updated post {}'.format(global_id)) | |||
|
239 | else: | |||
|
240 | Post.objects.import_post( | |||
|
241 | title=title, text=text, pub_time=pub_time, | |||
|
242 | opening_post=opening_post, tags=tags, | |||
|
243 | global_id=global_id, files=files, tripcode=tripcode, | |||
|
244 | version=version) | |||
|
245 | print('Parsed new post {}'.format(global_id)) | |||
235 | else: |
|
246 | else: | |
236 | raise SyncException(EXCEPTION_NODE.format(tag_status.text)) |
|
247 | raise SyncException(EXCEPTION_NODE.format(tag_status.text)) | |
237 |
|
248 |
@@ -130,6 +130,14 b' class GlobalId(models.Model):' | |||||
130 | return KeyPair.objects.filter( |
|
130 | return KeyPair.objects.filter( | |
131 | key_type=self.key_type, public_key=self.key).exists() |
|
131 | key_type=self.key_type, public_key=self.key).exists() | |
132 |
|
132 | |||
|
133 | def clear_cache(self): | |||
|
134 | """ | |||
|
135 | Removes content cache and signatures. | |||
|
136 | """ | |||
|
137 | self.content = None | |||
|
138 | self.save() | |||
|
139 | self.signature_set.all().delete() | |||
|
140 | ||||
133 |
|
141 | |||
134 | class Signature(models.Model): |
|
142 | class Signature(models.Model): | |
135 | class Meta: |
|
143 | class Meta: |
@@ -1,3 +1,5 b'' | |||||
|
1 | from django.test import TestCase | |||
|
2 | ||||
1 | from boards.models import KeyPair, Post, Tag |
|
3 | from boards.models import KeyPair, Post, Tag | |
2 | from boards.models.post.sync import SyncManager |
|
4 | from boards.models.post.sync import SyncManager | |
3 | from boards.tests.mocks import MockRequest |
|
5 | from boards.tests.mocks import MockRequest | |
@@ -6,9 +8,6 b' from boards.views.sync import response_g' | |||||
6 | __author__ = 'neko259' |
|
8 | __author__ = 'neko259' | |
7 |
|
9 | |||
8 |
|
10 | |||
9 | from django.test import TestCase |
|
|||
10 |
|
||||
11 |
|
||||
12 | class SyncTest(TestCase): |
|
11 | class SyncTest(TestCase): | |
13 | def test_get(self): |
|
12 | def test_get(self): | |
14 | """ |
|
13 | """ |
General Comments 0
You need to be logged in to leave comments.
Login now