##// END OF EJS Templates
Fixed issue with newly added posts appearance
neko259 -
r1650:8a0acd9f default
parent child Browse files
Show More
@@ -1,410 +1,410 b''
1 import uuid
1 import uuid
2 import hashlib
2 import hashlib
3 import re
3 import re
4
4
5 from boards import settings
5 from boards import settings
6 from boards.abstracts.tripcode import Tripcode
6 from boards.abstracts.tripcode import Tripcode
7 from boards.models import Attachment, KeyPair, GlobalId
7 from boards.models import Attachment, KeyPair, GlobalId
8 from boards.models.attachment import FILE_TYPES_IMAGE
8 from boards.models.attachment import FILE_TYPES_IMAGE
9 from boards.models.base import Viewable
9 from boards.models.base import Viewable
10 from boards.models.post.export import get_exporter, DIFF_TYPE_JSON
10 from boards.models.post.export import get_exporter, DIFF_TYPE_JSON
11 from boards.models.post.manager import PostManager
11 from boards.models.post.manager import PostManager
12 from boards.utils import datetime_to_epoch
12 from boards.utils import datetime_to_epoch
13 from django.core.exceptions import ObjectDoesNotExist
13 from django.core.exceptions import ObjectDoesNotExist
14 from django.core.urlresolvers import reverse
14 from django.core.urlresolvers import reverse
15 from django.db import models
15 from django.db import models
16 from django.db.models import TextField, QuerySet, F
16 from django.db.models import TextField, QuerySet, F
17 from django.template.defaultfilters import truncatewords, striptags
17 from django.template.defaultfilters import truncatewords, striptags
18 from django.template.loader import render_to_string
18 from django.template.loader import render_to_string
19
19
20 CSS_CLS_HIDDEN_POST = 'hidden_post'
20 CSS_CLS_HIDDEN_POST = 'hidden_post'
21 CSS_CLS_DEAD_POST = 'dead_post'
21 CSS_CLS_DEAD_POST = 'dead_post'
22 CSS_CLS_ARCHIVE_POST = 'archive_post'
22 CSS_CLS_ARCHIVE_POST = 'archive_post'
23 CSS_CLS_POST = 'post'
23 CSS_CLS_POST = 'post'
24 CSS_CLS_MONOCHROME = 'monochrome'
24 CSS_CLS_MONOCHROME = 'monochrome'
25
25
26 TITLE_MAX_WORDS = 10
26 TITLE_MAX_WORDS = 10
27
27
28 APP_LABEL_BOARDS = 'boards'
28 APP_LABEL_BOARDS = 'boards'
29
29
30 BAN_REASON_AUTO = 'Auto'
30 BAN_REASON_AUTO = 'Auto'
31
31
32 TITLE_MAX_LENGTH = 200
32 TITLE_MAX_LENGTH = 200
33
33
34 REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]')
34 REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]')
35 REGEX_GLOBAL_REPLY = re.compile(r'\[post\](\w+)::([^:]+)::(\d+)\[/post\]')
35 REGEX_GLOBAL_REPLY = re.compile(r'\[post\](\w+)::([^:]+)::(\d+)\[/post\]')
36 REGEX_URL = re.compile(r'https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?')
36 REGEX_URL = re.compile(r'https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?')
37 REGEX_NOTIFICATION = re.compile(r'\[user\](\w+)\[/user\]')
37 REGEX_NOTIFICATION = re.compile(r'\[user\](\w+)\[/user\]')
38
38
39 PARAMETER_TRUNCATED = 'truncated'
39 PARAMETER_TRUNCATED = 'truncated'
40 PARAMETER_TAG = 'tag'
40 PARAMETER_TAG = 'tag'
41 PARAMETER_OFFSET = 'offset'
41 PARAMETER_OFFSET = 'offset'
42 PARAMETER_DIFF_TYPE = 'type'
42 PARAMETER_DIFF_TYPE = 'type'
43 PARAMETER_CSS_CLASS = 'css_class'
43 PARAMETER_CSS_CLASS = 'css_class'
44 PARAMETER_THREAD = 'thread'
44 PARAMETER_THREAD = 'thread'
45 PARAMETER_IS_OPENING = 'is_opening'
45 PARAMETER_IS_OPENING = 'is_opening'
46 PARAMETER_POST = 'post'
46 PARAMETER_POST = 'post'
47 PARAMETER_OP_ID = 'opening_post_id'
47 PARAMETER_OP_ID = 'opening_post_id'
48 PARAMETER_NEED_OPEN_LINK = 'need_open_link'
48 PARAMETER_NEED_OPEN_LINK = 'need_open_link'
49 PARAMETER_REPLY_LINK = 'reply_link'
49 PARAMETER_REPLY_LINK = 'reply_link'
50 PARAMETER_NEED_OP_DATA = 'need_op_data'
50 PARAMETER_NEED_OP_DATA = 'need_op_data'
51
51
52 POST_VIEW_PARAMS = (
52 POST_VIEW_PARAMS = (
53 'need_op_data',
53 'need_op_data',
54 'reply_link',
54 'reply_link',
55 'need_open_link',
55 'need_open_link',
56 'truncated',
56 'truncated',
57 'mode_tree',
57 'mode_tree',
58 'perms',
58 'perms',
59 'tree_depth',
59 'tree_depth',
60 )
60 )
61
61
62
62
63 class Post(models.Model, Viewable):
63 class Post(models.Model, Viewable):
64 """A post is a message."""
64 """A post is a message."""
65
65
66 objects = PostManager()
66 objects = PostManager()
67
67
68 class Meta:
68 class Meta:
69 app_label = APP_LABEL_BOARDS
69 app_label = APP_LABEL_BOARDS
70 ordering = ('id',)
70 ordering = ('id',)
71
71
72 title = models.CharField(max_length=TITLE_MAX_LENGTH, null=True, blank=True)
72 title = models.CharField(max_length=TITLE_MAX_LENGTH, null=True, blank=True)
73 pub_time = models.DateTimeField()
73 pub_time = models.DateTimeField()
74 text = TextField(blank=True, null=True)
74 text = TextField(blank=True, null=True)
75 _text_rendered = TextField(blank=True, null=True, editable=False)
75 _text_rendered = TextField(blank=True, null=True, editable=False)
76
76
77 attachments = models.ManyToManyField(Attachment, null=True, blank=True,
77 attachments = models.ManyToManyField(Attachment, null=True, blank=True,
78 related_name='attachment_posts')
78 related_name='attachment_posts')
79
79
80 poster_ip = models.GenericIPAddressField()
80 poster_ip = models.GenericIPAddressField()
81
81
82 # Used for cache and threads updating
82 # Used for cache and threads updating
83 last_edit_time = models.DateTimeField()
83 last_edit_time = models.DateTimeField()
84
84
85 referenced_posts = models.ManyToManyField('Post', symmetrical=False,
85 referenced_posts = models.ManyToManyField('Post', symmetrical=False,
86 null=True,
86 null=True,
87 blank=True, related_name='refposts',
87 blank=True, related_name='refposts',
88 db_index=True)
88 db_index=True)
89 refmap = models.TextField(null=True, blank=True)
89 refmap = models.TextField(null=True, blank=True)
90 threads = models.ManyToManyField('Thread', db_index=True,
90 threads = models.ManyToManyField('Thread', db_index=True,
91 related_name='multi_replies')
91 related_name='multi_replies')
92 thread = models.ForeignKey('Thread', db_index=True, related_name='pt+')
92 thread = models.ForeignKey('Thread', db_index=True, related_name='pt+')
93
93
94 url = models.TextField()
94 url = models.TextField()
95 uid = models.TextField(db_index=True)
95 uid = models.TextField(db_index=True)
96
96
97 # Global ID with author key. If the message was downloaded from another
97 # Global ID with author key. If the message was downloaded from another
98 # server, this indicates the server.
98 # server, this indicates the server.
99 global_id = models.OneToOneField(GlobalId, null=True, blank=True,
99 global_id = models.OneToOneField(GlobalId, null=True, blank=True,
100 on_delete=models.CASCADE)
100 on_delete=models.CASCADE)
101
101
102 tripcode = models.CharField(max_length=50, blank=True, default='')
102 tripcode = models.CharField(max_length=50, blank=True, default='')
103 opening = models.BooleanField(db_index=True)
103 opening = models.BooleanField(db_index=True)
104 hidden = models.BooleanField(default=False)
104 hidden = models.BooleanField(default=False)
105 version = models.IntegerField(default=1)
105 version = models.IntegerField(default=1)
106
106
107 def __str__(self):
107 def __str__(self):
108 return 'P#{}/{}'.format(self.id, self.get_title())
108 return 'P#{}/{}'.format(self.id, self.get_title())
109
109
110 def get_title(self) -> str:
110 def get_title(self) -> str:
111 return self.title
111 return self.title
112
112
113 def get_title_or_text(self):
113 def get_title_or_text(self):
114 title = self.get_title()
114 title = self.get_title()
115 if not title:
115 if not title:
116 title = truncatewords(striptags(self.get_text()), TITLE_MAX_WORDS)
116 title = truncatewords(striptags(self.get_text()), TITLE_MAX_WORDS)
117
117
118 return title
118 return title
119
119
120 def build_refmap(self, excluded_ids=None) -> None:
120 def build_refmap(self, excluded_ids=None) -> None:
121 """
121 """
122 Builds a replies map string from replies list. This is a cache to stop
122 Builds a replies map string from replies list. This is a cache to stop
123 the server from recalculating the map on every post show.
123 the server from recalculating the map on every post show.
124 """
124 """
125
125
126 replies = self.referenced_posts
126 replies = self.referenced_posts
127 if excluded_ids is not None:
127 if excluded_ids is not None:
128 replies = replies.exclude(id__in=excluded_ids)
128 replies = replies.exclude(id__in=excluded_ids)
129 else:
129 else:
130 replies = replies.all()
130 replies = replies.all()
131
131
132 post_urls = [refpost.get_link_view() for refpost in replies]
132 post_urls = [refpost.get_link_view() for refpost in replies]
133
133
134 self.refmap = ', '.join(post_urls)
134 self.refmap = ', '.join(post_urls)
135
135
136 def is_referenced(self) -> bool:
136 def is_referenced(self) -> bool:
137 return self.refmap and len(self.refmap) > 0
137 return self.refmap and len(self.refmap) > 0
138
138
139 def is_opening(self) -> bool:
139 def is_opening(self) -> bool:
140 """
140 """
141 Checks if this is an opening post or just a reply.
141 Checks if this is an opening post or just a reply.
142 """
142 """
143
143
144 return self.opening
144 return self.opening
145
145
146 def get_absolute_url(self, thread=None):
146 def get_absolute_url(self, thread=None):
147 url = None
147 url = None
148
148
149 if thread is None:
149 if thread is None:
150 thread = self.get_thread()
150 thread = self.get_thread()
151
151
152 # Url is cached only for the "main" thread. When getting url
152 # Url is cached only for the "main" thread. When getting url
153 # for other threads, do it manually.
153 # for other threads, do it manually.
154 if self.url:
154 if self.url:
155 url = self.url
155 url = self.url
156
156
157 if url is None:
157 if url is None:
158 opening = self.is_opening()
158 opening = self.is_opening()
159 opening_id = self.id if opening else thread.get_opening_post_id()
159 opening_id = self.id if opening else thread.get_opening_post_id()
160 url = reverse('thread', kwargs={'post_id': opening_id})
160 url = reverse('thread', kwargs={'post_id': opening_id})
161 if not opening:
161 if not opening:
162 url += '#' + str(self.id)
162 url += '#' + str(self.id)
163
163
164 return url
164 return url
165
165
166 def get_thread(self):
166 def get_thread(self):
167 return self.thread
167 return self.thread
168
168
169 def get_thread_id(self):
169 def get_thread_id(self):
170 return self.thread_id
170 return self.thread_id
171
171
172 def get_threads(self) -> QuerySet:
172 def get_threads(self) -> QuerySet:
173 """
173 """
174 Gets post's thread.
174 Gets post's thread.
175 """
175 """
176
176
177 return self.threads
177 return self.threads
178
178
179 def _get_cache_key(self):
179 def _get_cache_key(self):
180 return [datetime_to_epoch(self.last_edit_time)]
180 return [datetime_to_epoch(self.last_edit_time)]
181
181
182 def get_view_params(self, *args, **kwargs):
182 def get_view_params(self, *args, **kwargs):
183 thread = self.get_thread()
183 thread = self.get_thread()
184
184
185 css_classes = [CSS_CLS_POST]
185 css_classes = [CSS_CLS_POST]
186 if thread.is_archived():
186 if thread.is_archived():
187 css_classes.append(CSS_CLS_ARCHIVE_POST)
187 css_classes.append(CSS_CLS_ARCHIVE_POST)
188 elif not thread.can_bump():
188 elif not thread.can_bump():
189 css_classes.append(CSS_CLS_DEAD_POST)
189 css_classes.append(CSS_CLS_DEAD_POST)
190 if self.is_hidden():
190 if self.is_hidden():
191 css_classes.append(CSS_CLS_HIDDEN_POST)
191 css_classes.append(CSS_CLS_HIDDEN_POST)
192 if thread.is_monochrome():
192 if thread.is_monochrome():
193 css_classes.append(CSS_CLS_MONOCHROME)
193 css_classes.append(CSS_CLS_MONOCHROME)
194
194
195 params = dict()
195 params = dict()
196 for param in POST_VIEW_PARAMS:
196 for param in POST_VIEW_PARAMS:
197 if param in kwargs:
197 if param in kwargs:
198 params[param] = kwargs[param]
198 params[param] = kwargs[param]
199
199
200 params.update({
200 params.update({
201 PARAMETER_POST: self,
201 PARAMETER_POST: self,
202 PARAMETER_IS_OPENING: self.is_opening(),
202 PARAMETER_IS_OPENING: self.is_opening(),
203 PARAMETER_THREAD: thread,
203 PARAMETER_THREAD: thread,
204 PARAMETER_CSS_CLASS: ' '.join(css_classes),
204 PARAMETER_CSS_CLASS: ' '.join(css_classes),
205 })
205 })
206
206
207 return params
207 return params
208
208
209 def get_view(self, *args, **kwargs) -> str:
209 def get_view(self, *args, **kwargs) -> str:
210 """
210 """
211 Renders post's HTML view. Some of the post params can be passed over
211 Renders post's HTML view. Some of the post params can be passed over
212 kwargs for the means of caching (if we view the thread, some params
212 kwargs for the means of caching (if we view the thread, some params
213 are same for every post and don't need to be computed over and over.
213 are same for every post and don't need to be computed over and over.
214 """
214 """
215 params = self.get_view_params(args, kwargs)
215 params = self.get_view_params(*args, **kwargs)
216
216
217 return render_to_string('boards/post.html', params)
217 return render_to_string('boards/post.html', params)
218
218
219 def get_search_view(self, *args, **kwargs):
219 def get_search_view(self, *args, **kwargs):
220 return self.get_view(need_op_data=True, *args, **kwargs)
220 return self.get_view(need_op_data=True, *args, **kwargs)
221
221
222 def get_first_image(self) -> Attachment:
222 def get_first_image(self) -> Attachment:
223 return self.attachments.filter(mimetype__in=FILE_TYPES_IMAGE).earliest('id')
223 return self.attachments.filter(mimetype__in=FILE_TYPES_IMAGE).earliest('id')
224
224
225 def set_global_id(self, key_pair=None):
225 def set_global_id(self, key_pair=None):
226 """
226 """
227 Sets global id based on the given key pair. If no key pair is given,
227 Sets global id based on the given key pair. If no key pair is given,
228 default one is used.
228 default one is used.
229 """
229 """
230
230
231 if key_pair:
231 if key_pair:
232 key = key_pair
232 key = key_pair
233 else:
233 else:
234 try:
234 try:
235 key = KeyPair.objects.get(primary=True)
235 key = KeyPair.objects.get(primary=True)
236 except KeyPair.DoesNotExist:
236 except KeyPair.DoesNotExist:
237 # Do not update the global id because there is no key defined
237 # Do not update the global id because there is no key defined
238 return
238 return
239 global_id = GlobalId(key_type=key.key_type,
239 global_id = GlobalId(key_type=key.key_type,
240 key=key.public_key,
240 key=key.public_key,
241 local_id=self.id)
241 local_id=self.id)
242 global_id.save()
242 global_id.save()
243
243
244 self.global_id = global_id
244 self.global_id = global_id
245
245
246 self.save(update_fields=['global_id'])
246 self.save(update_fields=['global_id'])
247
247
248 def get_pub_time_str(self):
248 def get_pub_time_str(self):
249 return str(self.pub_time)
249 return str(self.pub_time)
250
250
251 def get_replied_ids(self):
251 def get_replied_ids(self):
252 """
252 """
253 Gets ID list of the posts that this post replies.
253 Gets ID list of the posts that this post replies.
254 """
254 """
255
255
256 raw_text = self.get_raw_text()
256 raw_text = self.get_raw_text()
257
257
258 local_replied = REGEX_REPLY.findall(raw_text)
258 local_replied = REGEX_REPLY.findall(raw_text)
259 global_replied = []
259 global_replied = []
260 for match in REGEX_GLOBAL_REPLY.findall(raw_text):
260 for match in REGEX_GLOBAL_REPLY.findall(raw_text):
261 key_type = match[0]
261 key_type = match[0]
262 key = match[1]
262 key = match[1]
263 local_id = match[2]
263 local_id = match[2]
264
264
265 try:
265 try:
266 global_id = GlobalId.objects.get(key_type=key_type,
266 global_id = GlobalId.objects.get(key_type=key_type,
267 key=key, local_id=local_id)
267 key=key, local_id=local_id)
268 for post in Post.objects.filter(global_id=global_id).only('id'):
268 for post in Post.objects.filter(global_id=global_id).only('id'):
269 global_replied.append(post.id)
269 global_replied.append(post.id)
270 except GlobalId.DoesNotExist:
270 except GlobalId.DoesNotExist:
271 pass
271 pass
272 return local_replied + global_replied
272 return local_replied + global_replied
273
273
274 def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None,
274 def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None,
275 include_last_update=False) -> str:
275 include_last_update=False) -> str:
276 """
276 """
277 Gets post HTML or JSON data that can be rendered on a page or used by
277 Gets post HTML or JSON data that can be rendered on a page or used by
278 API.
278 API.
279 """
279 """
280
280
281 return get_exporter(format_type).export(self, request,
281 return get_exporter(format_type).export(self, request,
282 include_last_update)
282 include_last_update)
283
283
284 def notify_clients(self, recursive=True):
284 def notify_clients(self, recursive=True):
285 """
285 """
286 Sends post HTML data to the thread web socket.
286 Sends post HTML data to the thread web socket.
287 """
287 """
288
288
289 if not settings.get_bool('External', 'WebsocketsEnabled'):
289 if not settings.get_bool('External', 'WebsocketsEnabled'):
290 return
290 return
291
291
292 thread_ids = list()
292 thread_ids = list()
293 for thread in self.get_threads().all():
293 for thread in self.get_threads().all():
294 thread_ids.append(thread.id)
294 thread_ids.append(thread.id)
295
295
296 thread.notify_clients()
296 thread.notify_clients()
297
297
298 if recursive:
298 if recursive:
299 for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()):
299 for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()):
300 post_id = reply_number.group(1)
300 post_id = reply_number.group(1)
301
301
302 try:
302 try:
303 ref_post = Post.objects.get(id=post_id)
303 ref_post = Post.objects.get(id=post_id)
304
304
305 if ref_post.get_threads().exclude(id__in=thread_ids).exists():
305 if ref_post.get_threads().exclude(id__in=thread_ids).exists():
306 # If post is in this thread, its thread was already notified.
306 # If post is in this thread, its thread was already notified.
307 # Otherwise, notify its thread separately.
307 # Otherwise, notify its thread separately.
308 ref_post.notify_clients(recursive=False)
308 ref_post.notify_clients(recursive=False)
309 except ObjectDoesNotExist:
309 except ObjectDoesNotExist:
310 pass
310 pass
311
311
312 def build_url(self):
312 def build_url(self):
313 self.url = self.get_absolute_url()
313 self.url = self.get_absolute_url()
314 self.save(update_fields=['url'])
314 self.save(update_fields=['url'])
315
315
316 def save(self, force_insert=False, force_update=False, using=None,
316 def save(self, force_insert=False, force_update=False, using=None,
317 update_fields=None):
317 update_fields=None):
318 new_post = self.id is None
318 new_post = self.id is None
319
319
320 self.uid = str(uuid.uuid4())
320 self.uid = str(uuid.uuid4())
321 if update_fields is not None and 'uid' not in update_fields:
321 if update_fields is not None and 'uid' not in update_fields:
322 update_fields += ['uid']
322 update_fields += ['uid']
323
323
324 if not new_post:
324 if not new_post:
325 for thread in self.get_threads().all():
325 for thread in self.get_threads().all():
326 thread.last_edit_time = self.last_edit_time
326 thread.last_edit_time = self.last_edit_time
327
327
328 thread.save(update_fields=['last_edit_time', 'status'])
328 thread.save(update_fields=['last_edit_time', 'status'])
329
329
330 super().save(force_insert, force_update, using, update_fields)
330 super().save(force_insert, force_update, using, update_fields)
331
331
332 if self.url is None:
332 if self.url is None:
333 self.build_url()
333 self.build_url()
334
334
335 def get_text(self) -> str:
335 def get_text(self) -> str:
336 return self._text_rendered
336 return self._text_rendered
337
337
338 def get_raw_text(self) -> str:
338 def get_raw_text(self) -> str:
339 return self.text
339 return self.text
340
340
341 def get_sync_text(self) -> str:
341 def get_sync_text(self) -> str:
342 """
342 """
343 Returns text applicable for sync. It has absolute post reflinks.
343 Returns text applicable for sync. It has absolute post reflinks.
344 """
344 """
345
345
346 replacements = dict()
346 replacements = dict()
347 for post_id in REGEX_REPLY.findall(self.get_raw_text()):
347 for post_id in REGEX_REPLY.findall(self.get_raw_text()):
348 try:
348 try:
349 absolute_post_id = str(Post.objects.get(id=post_id).global_id)
349 absolute_post_id = str(Post.objects.get(id=post_id).global_id)
350 replacements[post_id] = absolute_post_id
350 replacements[post_id] = absolute_post_id
351 except Post.DoesNotExist:
351 except Post.DoesNotExist:
352 pass
352 pass
353
353
354 text = self.get_raw_text() or ''
354 text = self.get_raw_text() or ''
355 for key in replacements:
355 for key in replacements:
356 text = text.replace('[post]{}[/post]'.format(key),
356 text = text.replace('[post]{}[/post]'.format(key),
357 '[post]{}[/post]'.format(replacements[key]))
357 '[post]{}[/post]'.format(replacements[key]))
358 text = text.replace('\r\n', '\n').replace('\r', '\n')
358 text = text.replace('\r\n', '\n').replace('\r', '\n')
359
359
360 return text
360 return text
361
361
362 def connect_threads(self, opening_posts):
362 def connect_threads(self, opening_posts):
363 for opening_post in opening_posts:
363 for opening_post in opening_posts:
364 threads = opening_post.get_threads().all()
364 threads = opening_post.get_threads().all()
365 for thread in threads:
365 for thread in threads:
366 if thread.can_bump():
366 if thread.can_bump():
367 thread.update_bump_status()
367 thread.update_bump_status()
368
368
369 thread.last_edit_time = self.last_edit_time
369 thread.last_edit_time = self.last_edit_time
370 thread.save(update_fields=['last_edit_time', 'status'])
370 thread.save(update_fields=['last_edit_time', 'status'])
371 self.threads.add(opening_post.get_thread())
371 self.threads.add(opening_post.get_thread())
372
372
373 def get_tripcode(self):
373 def get_tripcode(self):
374 if self.tripcode:
374 if self.tripcode:
375 return Tripcode(self.tripcode)
375 return Tripcode(self.tripcode)
376
376
377 def get_link_view(self):
377 def get_link_view(self):
378 """
378 """
379 Gets view of a reflink to the post.
379 Gets view of a reflink to the post.
380 """
380 """
381 result = '<a href="{}">&gt;&gt;{}</a>'.format(self.get_absolute_url(),
381 result = '<a href="{}">&gt;&gt;{}</a>'.format(self.get_absolute_url(),
382 self.id)
382 self.id)
383 if self.is_opening():
383 if self.is_opening():
384 result = '<b>{}</b>'.format(result)
384 result = '<b>{}</b>'.format(result)
385
385
386 return result
386 return result
387
387
388 def is_hidden(self) -> bool:
388 def is_hidden(self) -> bool:
389 return self.hidden
389 return self.hidden
390
390
391 def set_hidden(self, hidden):
391 def set_hidden(self, hidden):
392 self.hidden = hidden
392 self.hidden = hidden
393
393
394 def increment_version(self):
394 def increment_version(self):
395 self.version = F('version') + 1
395 self.version = F('version') + 1
396
396
397 def clear_cache(self):
397 def clear_cache(self):
398 """
398 """
399 Clears sync data (content cache, signatures etc).
399 Clears sync data (content cache, signatures etc).
400 """
400 """
401 global_id = self.global_id
401 global_id = self.global_id
402 if global_id is not None and global_id.is_local()\
402 if global_id is not None and global_id.is_local()\
403 and global_id.content is not None:
403 and global_id.content is not None:
404 global_id.clear_cache()
404 global_id.clear_cache()
405
405
406 def get_tags(self):
406 def get_tags(self):
407 return self.get_thread().get_tags()
407 return self.get_thread().get_tags()
408
408
409 def get_ip_color(self):
409 def get_ip_color(self):
410 return hashlib.md5(self.poster_ip.encode()).hexdigest()[:6]
410 return hashlib.md5(self.poster_ip.encode()).hexdigest()[:6]
@@ -1,50 +1,51 b''
1 import re
1 import re
2 from django.shortcuts import get_object_or_404
2 from django.shortcuts import get_object_or_404
3 from django import template
3 from django import template
4
4
5
5
6 IMG_ACTION_URL = '[<a href="{}">{}</a>]'
6 IMG_ACTION_URL = '[<a href="{}">{}</a>]'
7
7
8
8
9 register = template.Library()
9 register = template.Library()
10
10
11 actions = [
11 actions = [
12 {
12 {
13 'name': 'google',
13 'name': 'google',
14 'link': 'http://google.com/searchbyimage?image_url=%s',
14 'link': 'http://google.com/searchbyimage?image_url=%s',
15 },
15 },
16 {
16 {
17 'name': 'iqdb',
17 'name': 'iqdb',
18 'link': 'http://iqdb.org/?url=%s',
18 'link': 'http://iqdb.org/?url=%s',
19 },
19 },
20 ]
20 ]
21
21
22
22
23 @register.simple_tag(name='post_url')
23 @register.simple_tag(name='post_url')
24 def post_url(*args, **kwargs):
24 def post_url(*args, **kwargs):
25 post_id = args[0]
25 post_id = args[0]
26
26
27 post = get_object_or_404('Post', id=post_id)
27 post = get_object_or_404('Post', id=post_id)
28
28
29 return post.get_absolute_url()
29 return post.get_absolute_url()
30
30
31
31
32 @register.simple_tag(name='image_actions')
32 @register.simple_tag(name='image_actions')
33 def image_actions(*args, **kwargs):
33 def image_actions(*args, **kwargs):
34 image_link = args[0]
34 image_link = args[0]
35 if len(args) > 1:
35 if len(args) > 1:
36 image_link = 'http://' + args[1] + image_link # TODO https?
36 image_link = 'http://' + args[1] + image_link # TODO https?
37
37
38 return ', '.join([IMG_ACTION_URL.format(
38 return ', '.join([IMG_ACTION_URL.format(
39 action['link'] % image_link, action['name']) for action in actions])
39 action['link'] % image_link, action['name']) for action in actions])
40
40
41
41
42 @register.inclusion_tag('boards/post.html', name='post_view', takes_context=True)
42 @register.inclusion_tag('boards/post.html', name='post_view', takes_context=True)
43 def post_view(context, post, *args, **kwargs):
43 def post_view(context, post, *args, **kwargs):
44 kwargs['perms'] = context['perms']
44 kwargs['perms'] = context['perms']
45 return post.get_view_params(*args, **kwargs)
45 return post.get_view_params(*args, **kwargs)
46
46
47
47 @register.simple_tag(name='page_url')
48 @register.simple_tag(name='page_url')
48 def page_url(paginator, page_number, *args, **kwargs):
49 def page_url(paginator, page_number, *args, **kwargs):
49 if paginator.supports_urls():
50 if paginator.supports_urls():
50 return paginator.get_page_url(page_number)
51 return paginator.get_page_url(page_number)
General Comments 0
You need to be logged in to leave comments. Login now