Show More
@@ -1,410 +1,405 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(db_index=True) |
|
73 | pub_time = models.DateTimeField(db_index=True) | |
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 is cached only for the "main" thread. When getting url |
|
147 | # Url is cached only for the "main" thread. When getting url | |
148 | # for other threads, do it manually. |
|
148 | # for other threads, do it manually. | |
149 |
|
|
149 | return self.url | |
150 |
|
||||
151 | if url is None: |
|
|||
152 | if thread is None: |
|
|||
153 | thread = self.get_thread() |
|
|||
154 | opening = self.is_opening() |
|
|||
155 | opening_id = self.id if opening else thread.get_opening_post_id() |
|
|||
156 | url = reverse('thread', kwargs={'post_id': opening_id}) |
|
|||
157 | if not opening: |
|
|||
158 | url += '#' + str(self.id) |
|
|||
159 |
|
||||
160 | return url |
|
|||
161 |
|
150 | |||
162 | def get_thread(self): |
|
151 | def get_thread(self): | |
163 | return self.thread |
|
152 | return self.thread | |
164 |
|
153 | |||
165 | def get_thread_id(self): |
|
154 | def get_thread_id(self): | |
166 | return self.thread_id |
|
155 | return self.thread_id | |
167 |
|
156 | |||
168 | def get_threads(self) -> QuerySet: |
|
157 | def get_threads(self) -> QuerySet: | |
169 | """ |
|
158 | """ | |
170 | Gets post's thread. |
|
159 | Gets post's thread. | |
171 | """ |
|
160 | """ | |
172 |
|
161 | |||
173 | return self.threads |
|
162 | return self.threads | |
174 |
|
163 | |||
175 | def _get_cache_key(self): |
|
164 | def _get_cache_key(self): | |
176 | return [datetime_to_epoch(self.last_edit_time)] |
|
165 | return [datetime_to_epoch(self.last_edit_time)] | |
177 |
|
166 | |||
178 | def get_view_params(self, *args, **kwargs): |
|
167 | def get_view_params(self, *args, **kwargs): | |
179 | """ |
|
168 | """ | |
180 | Gets the parameters required for viewing the post based on the arguments |
|
169 | Gets the parameters required for viewing the post based on the arguments | |
181 | given and the post itself. |
|
170 | given and the post itself. | |
182 | """ |
|
171 | """ | |
183 | thread = self.get_thread() |
|
172 | thread = self.get_thread() | |
184 |
|
173 | |||
185 | css_classes = [CSS_CLS_POST] |
|
174 | css_classes = [CSS_CLS_POST] | |
186 | if thread.is_archived(): |
|
175 | if thread.is_archived(): | |
187 | css_classes.append(CSS_CLS_ARCHIVE_POST) |
|
176 | css_classes.append(CSS_CLS_ARCHIVE_POST) | |
188 | elif not thread.can_bump(): |
|
177 | elif not thread.can_bump(): | |
189 | css_classes.append(CSS_CLS_DEAD_POST) |
|
178 | css_classes.append(CSS_CLS_DEAD_POST) | |
190 | if self.is_hidden(): |
|
179 | if self.is_hidden(): | |
191 | css_classes.append(CSS_CLS_HIDDEN_POST) |
|
180 | css_classes.append(CSS_CLS_HIDDEN_POST) | |
192 | if thread.is_monochrome(): |
|
181 | if thread.is_monochrome(): | |
193 | css_classes.append(CSS_CLS_MONOCHROME) |
|
182 | css_classes.append(CSS_CLS_MONOCHROME) | |
194 |
|
183 | |||
195 | params = dict() |
|
184 | params = dict() | |
196 | for param in POST_VIEW_PARAMS: |
|
185 | for param in POST_VIEW_PARAMS: | |
197 | if param in kwargs: |
|
186 | if param in kwargs: | |
198 | params[param] = kwargs[param] |
|
187 | params[param] = kwargs[param] | |
199 |
|
188 | |||
200 | params.update({ |
|
189 | params.update({ | |
201 | PARAMETER_POST: self, |
|
190 | PARAMETER_POST: self, | |
202 | PARAMETER_IS_OPENING: self.is_opening(), |
|
191 | PARAMETER_IS_OPENING: self.is_opening(), | |
203 | PARAMETER_THREAD: thread, |
|
192 | PARAMETER_THREAD: thread, | |
204 | PARAMETER_CSS_CLASS: ' '.join(css_classes), |
|
193 | PARAMETER_CSS_CLASS: ' '.join(css_classes), | |
205 | }) |
|
194 | }) | |
206 |
|
195 | |||
207 | return params |
|
196 | return params | |
208 |
|
197 | |||
209 | def get_view(self, *args, **kwargs) -> str: |
|
198 | def get_view(self, *args, **kwargs) -> str: | |
210 | """ |
|
199 | """ | |
211 | Renders post's HTML view. Some of the post params can be passed over |
|
200 | 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 |
|
201 | 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. |
|
202 | are same for every post and don't need to be computed over and over. | |
214 | """ |
|
203 | """ | |
215 | params = self.get_view_params(*args, **kwargs) |
|
204 | params = self.get_view_params(*args, **kwargs) | |
216 |
|
205 | |||
217 | return render_to_string('boards/post.html', params) |
|
206 | return render_to_string('boards/post.html', params) | |
218 |
|
207 | |||
219 | def get_search_view(self, *args, **kwargs): |
|
208 | def get_search_view(self, *args, **kwargs): | |
220 | return self.get_view(need_op_data=True, *args, **kwargs) |
|
209 | return self.get_view(need_op_data=True, *args, **kwargs) | |
221 |
|
210 | |||
222 | def get_first_image(self) -> Attachment: |
|
211 | def get_first_image(self) -> Attachment: | |
223 | return self.attachments.filter(mimetype__in=FILE_TYPES_IMAGE).earliest('id') |
|
212 | return self.attachments.filter(mimetype__in=FILE_TYPES_IMAGE).earliest('id') | |
224 |
|
213 | |||
225 | def set_global_id(self, key_pair=None): |
|
214 | def set_global_id(self, key_pair=None): | |
226 | """ |
|
215 | """ | |
227 | Sets global id based on the given key pair. If no key pair is given, |
|
216 | Sets global id based on the given key pair. If no key pair is given, | |
228 | default one is used. |
|
217 | default one is used. | |
229 | """ |
|
218 | """ | |
230 |
|
219 | |||
231 | if key_pair: |
|
220 | if key_pair: | |
232 | key = key_pair |
|
221 | key = key_pair | |
233 | else: |
|
222 | else: | |
234 | try: |
|
223 | try: | |
235 | key = KeyPair.objects.get(primary=True) |
|
224 | key = KeyPair.objects.get(primary=True) | |
236 | except KeyPair.DoesNotExist: |
|
225 | except KeyPair.DoesNotExist: | |
237 | # Do not update the global id because there is no key defined |
|
226 | # Do not update the global id because there is no key defined | |
238 | return |
|
227 | return | |
239 | global_id = GlobalId(key_type=key.key_type, |
|
228 | global_id = GlobalId(key_type=key.key_type, | |
240 | key=key.public_key, |
|
229 | key=key.public_key, | |
241 | local_id=self.id) |
|
230 | local_id=self.id) | |
242 | global_id.save() |
|
231 | global_id.save() | |
243 |
|
232 | |||
244 | self.global_id = global_id |
|
233 | self.global_id = global_id | |
245 |
|
234 | |||
246 | self.save(update_fields=['global_id']) |
|
235 | self.save(update_fields=['global_id']) | |
247 |
|
236 | |||
248 | def get_pub_time_str(self): |
|
237 | def get_pub_time_str(self): | |
249 | return str(self.pub_time) |
|
238 | return str(self.pub_time) | |
250 |
|
239 | |||
251 | def get_replied_ids(self): |
|
240 | def get_replied_ids(self): | |
252 | """ |
|
241 | """ | |
253 | Gets ID list of the posts that this post replies. |
|
242 | Gets ID list of the posts that this post replies. | |
254 | """ |
|
243 | """ | |
255 |
|
244 | |||
256 | raw_text = self.get_raw_text() |
|
245 | raw_text = self.get_raw_text() | |
257 |
|
246 | |||
258 | local_replied = REGEX_REPLY.findall(raw_text) |
|
247 | local_replied = REGEX_REPLY.findall(raw_text) | |
259 | global_replied = [] |
|
248 | global_replied = [] | |
260 | for match in REGEX_GLOBAL_REPLY.findall(raw_text): |
|
249 | for match in REGEX_GLOBAL_REPLY.findall(raw_text): | |
261 | key_type = match[0] |
|
250 | key_type = match[0] | |
262 | key = match[1] |
|
251 | key = match[1] | |
263 | local_id = match[2] |
|
252 | local_id = match[2] | |
264 |
|
253 | |||
265 | try: |
|
254 | try: | |
266 | global_id = GlobalId.objects.get(key_type=key_type, |
|
255 | global_id = GlobalId.objects.get(key_type=key_type, | |
267 | key=key, local_id=local_id) |
|
256 | key=key, local_id=local_id) | |
268 | for post in Post.objects.filter(global_id=global_id).only('id'): |
|
257 | for post in Post.objects.filter(global_id=global_id).only('id'): | |
269 | global_replied.append(post.id) |
|
258 | global_replied.append(post.id) | |
270 | except GlobalId.DoesNotExist: |
|
259 | except GlobalId.DoesNotExist: | |
271 | pass |
|
260 | pass | |
272 | return local_replied + global_replied |
|
261 | return local_replied + global_replied | |
273 |
|
262 | |||
274 | def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None, |
|
263 | def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None, | |
275 | include_last_update=False) -> str: |
|
264 | include_last_update=False) -> str: | |
276 | """ |
|
265 | """ | |
277 | Gets post HTML or JSON data that can be rendered on a page or used by |
|
266 | Gets post HTML or JSON data that can be rendered on a page or used by | |
278 | API. |
|
267 | API. | |
279 | """ |
|
268 | """ | |
280 |
|
269 | |||
281 | return get_exporter(format_type).export(self, request, |
|
270 | return get_exporter(format_type).export(self, request, | |
282 | include_last_update) |
|
271 | include_last_update) | |
283 |
|
272 | |||
284 | def notify_clients(self, recursive=True): |
|
273 | def notify_clients(self, recursive=True): | |
285 | """ |
|
274 | """ | |
286 | Sends post HTML data to the thread web socket. |
|
275 | Sends post HTML data to the thread web socket. | |
287 | """ |
|
276 | """ | |
288 |
|
277 | |||
289 | if not settings.get_bool('External', 'WebsocketsEnabled'): |
|
278 | if not settings.get_bool('External', 'WebsocketsEnabled'): | |
290 | return |
|
279 | return | |
291 |
|
280 | |||
292 | thread_ids = list() |
|
281 | thread_ids = list() | |
293 | for thread in self.get_threads().all(): |
|
282 | for thread in self.get_threads().all(): | |
294 | thread_ids.append(thread.id) |
|
283 | thread_ids.append(thread.id) | |
295 |
|
284 | |||
296 | thread.notify_clients() |
|
285 | thread.notify_clients() | |
297 |
|
286 | |||
298 | if recursive: |
|
287 | if recursive: | |
299 | for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): |
|
288 | for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): | |
300 | post_id = reply_number.group(1) |
|
289 | post_id = reply_number.group(1) | |
301 |
|
290 | |||
302 | try: |
|
291 | try: | |
303 | ref_post = Post.objects.get(id=post_id) |
|
292 | ref_post = Post.objects.get(id=post_id) | |
304 |
|
293 | |||
305 | if ref_post.get_threads().exclude(id__in=thread_ids).exists(): |
|
294 | if ref_post.get_threads().exclude(id__in=thread_ids).exists(): | |
306 | # If post is in this thread, its thread was already notified. |
|
295 | # If post is in this thread, its thread was already notified. | |
307 | # Otherwise, notify its thread separately. |
|
296 | # Otherwise, notify its thread separately. | |
308 | ref_post.notify_clients(recursive=False) |
|
297 | ref_post.notify_clients(recursive=False) | |
309 | except ObjectDoesNotExist: |
|
298 | except ObjectDoesNotExist: | |
310 | pass |
|
299 | pass | |
311 |
|
300 | |||
312 | def build_url(self): |
|
301 | def _build_url(self): | |
313 | self.url = self.get_absolute_url() |
|
302 | opening = self.is_opening() | |
314 | self.save(update_fields=['url']) |
|
303 | opening_id = self.id if opening else self.get_thread().get_opening_post_id() | |
|
304 | url = reverse('thread', kwargs={'post_id': opening_id}) | |||
|
305 | if not opening: | |||
|
306 | url += '#' + str(self.id) | |||
|
307 | ||||
|
308 | return url | |||
315 |
|
309 | |||
316 | def save(self, force_insert=False, force_update=False, using=None, |
|
310 | def save(self, force_insert=False, force_update=False, using=None, | |
317 | update_fields=None): |
|
311 | update_fields=None): | |
318 | new_post = self.id is None |
|
312 | new_post = self.id is None | |
319 |
|
313 | |||
320 | self.uid = str(uuid.uuid4()) |
|
314 | self.uid = str(uuid.uuid4()) | |
321 | if update_fields is not None and 'uid' not in update_fields: |
|
315 | if update_fields is not None and 'uid' not in update_fields: | |
322 | update_fields += ['uid'] |
|
316 | update_fields += ['uid'] | |
323 |
|
317 | |||
324 | if not new_post: |
|
318 | if not new_post: | |
325 | for thread in self.get_threads().all(): |
|
319 | for thread in self.get_threads().all(): | |
326 | thread.last_edit_time = self.last_edit_time |
|
320 | thread.last_edit_time = self.last_edit_time | |
327 |
|
321 | |||
328 | thread.save(update_fields=['last_edit_time', 'status']) |
|
322 | thread.save(update_fields=['last_edit_time', 'status']) | |
329 |
|
323 | |||
330 | super().save(force_insert, force_update, using, update_fields) |
|
324 | super().save(force_insert, force_update, using, update_fields) | |
331 |
|
325 | |||
332 | if self.url is None: |
|
326 | if new_post: | |
333 | self.build_url() |
|
327 | self.url = self._build_url() | |
|
328 | super().save(update_fields=['url']) | |||
334 |
|
329 | |||
335 | def get_text(self) -> str: |
|
330 | def get_text(self) -> str: | |
336 | return self._text_rendered |
|
331 | return self._text_rendered | |
337 |
|
332 | |||
338 | def get_raw_text(self) -> str: |
|
333 | def get_raw_text(self) -> str: | |
339 | return self.text |
|
334 | return self.text | |
340 |
|
335 | |||
341 | def get_sync_text(self) -> str: |
|
336 | def get_sync_text(self) -> str: | |
342 | """ |
|
337 | """ | |
343 | Returns text applicable for sync. It has absolute post reflinks. |
|
338 | Returns text applicable for sync. It has absolute post reflinks. | |
344 | """ |
|
339 | """ | |
345 |
|
340 | |||
346 | replacements = dict() |
|
341 | replacements = dict() | |
347 | for post_id in REGEX_REPLY.findall(self.get_raw_text()): |
|
342 | for post_id in REGEX_REPLY.findall(self.get_raw_text()): | |
348 | try: |
|
343 | try: | |
349 | absolute_post_id = str(Post.objects.get(id=post_id).global_id) |
|
344 | absolute_post_id = str(Post.objects.get(id=post_id).global_id) | |
350 | replacements[post_id] = absolute_post_id |
|
345 | replacements[post_id] = absolute_post_id | |
351 | except Post.DoesNotExist: |
|
346 | except Post.DoesNotExist: | |
352 | pass |
|
347 | pass | |
353 |
|
348 | |||
354 | text = self.get_raw_text() or '' |
|
349 | text = self.get_raw_text() or '' | |
355 | for key in replacements: |
|
350 | for key in replacements: | |
356 | text = text.replace('[post]{}[/post]'.format(key), |
|
351 | text = text.replace('[post]{}[/post]'.format(key), | |
357 | '[post]{}[/post]'.format(replacements[key])) |
|
352 | '[post]{}[/post]'.format(replacements[key])) | |
358 | text = text.replace('\r\n', '\n').replace('\r', '\n') |
|
353 | text = text.replace('\r\n', '\n').replace('\r', '\n') | |
359 |
|
354 | |||
360 | return text |
|
355 | return text | |
361 |
|
356 | |||
362 | def connect_threads(self, opening_posts): |
|
357 | def connect_threads(self, opening_posts): | |
363 | for opening_post in opening_posts: |
|
358 | for opening_post in opening_posts: | |
364 | threads = opening_post.get_threads().all() |
|
359 | threads = opening_post.get_threads().all() | |
365 | for thread in threads: |
|
360 | for thread in threads: | |
366 | if thread.can_bump(): |
|
361 | if thread.can_bump(): | |
367 | thread.update_bump_status() |
|
362 | thread.update_bump_status() | |
368 |
|
363 | |||
369 | thread.last_edit_time = self.last_edit_time |
|
364 | thread.last_edit_time = self.last_edit_time | |
370 | thread.save(update_fields=['last_edit_time', 'status']) |
|
365 | thread.save(update_fields=['last_edit_time', 'status']) | |
371 | self.threads.add(opening_post.get_thread()) |
|
366 | self.threads.add(opening_post.get_thread()) | |
372 |
|
367 | |||
373 | def get_tripcode(self): |
|
368 | def get_tripcode(self): | |
374 | if self.tripcode: |
|
369 | if self.tripcode: | |
375 | return Tripcode(self.tripcode) |
|
370 | return Tripcode(self.tripcode) | |
376 |
|
371 | |||
377 | def get_link_view(self): |
|
372 | def get_link_view(self): | |
378 | """ |
|
373 | """ | |
379 | Gets view of a reflink to the post. |
|
374 | Gets view of a reflink to the post. | |
380 | """ |
|
375 | """ | |
381 | result = '<a href="{}">>>{}</a>'.format(self.get_absolute_url(), |
|
376 | result = '<a href="{}">>>{}</a>'.format(self.get_absolute_url(), | |
382 | self.id) |
|
377 | self.id) | |
383 | if self.is_opening(): |
|
378 | if self.is_opening(): | |
384 | result = '<b>{}</b>'.format(result) |
|
379 | result = '<b>{}</b>'.format(result) | |
385 |
|
380 | |||
386 | return result |
|
381 | return result | |
387 |
|
382 | |||
388 | def is_hidden(self) -> bool: |
|
383 | def is_hidden(self) -> bool: | |
389 | return self.hidden |
|
384 | return self.hidden | |
390 |
|
385 | |||
391 | def set_hidden(self, hidden): |
|
386 | def set_hidden(self, hidden): | |
392 | self.hidden = hidden |
|
387 | self.hidden = hidden | |
393 |
|
388 | |||
394 | def increment_version(self): |
|
389 | def increment_version(self): | |
395 | self.version = F('version') + 1 |
|
390 | self.version = F('version') + 1 | |
396 |
|
391 | |||
397 | def clear_cache(self): |
|
392 | def clear_cache(self): | |
398 | """ |
|
393 | """ | |
399 | Clears sync data (content cache, signatures etc). |
|
394 | Clears sync data (content cache, signatures etc). | |
400 | """ |
|
395 | """ | |
401 | global_id = self.global_id |
|
396 | global_id = self.global_id | |
402 | if global_id is not None and global_id.is_local()\ |
|
397 | if global_id is not None and global_id.is_local()\ | |
403 | and global_id.content is not None: |
|
398 | and global_id.content is not None: | |
404 | global_id.clear_cache() |
|
399 | global_id.clear_cache() | |
405 |
|
400 | |||
406 | def get_tags(self): |
|
401 | def get_tags(self): | |
407 | return self.get_thread().get_tags() |
|
402 | return self.get_thread().get_tags() | |
408 |
|
403 | |||
409 | def get_ip_color(self): |
|
404 | def get_ip_color(self): | |
410 | return hashlib.md5(self.poster_ip.encode()).hexdigest()[:6] |
|
405 | return hashlib.md5(self.poster_ip.encode()).hexdigest()[:6] |
General Comments 0
You need to be logged in to leave comments.
Login now