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