Show More
@@ -0,0 +1,55 b'' | |||||
|
1 | from boards import utils | |||
|
2 | ||||
|
3 | ||||
|
4 | PARAMETER_TRUNCATED = 'truncated' | |||
|
5 | ||||
|
6 | DIFF_TYPE_HTML = 'html' | |||
|
7 | DIFF_TYPE_JSON = 'json' | |||
|
8 | ||||
|
9 | ||||
|
10 | class Exporter(): | |||
|
11 | @staticmethod | |||
|
12 | def export(post, request, include_last_update) -> str: | |||
|
13 | pass | |||
|
14 | ||||
|
15 | ||||
|
16 | class HtmlExporter(Exporter): | |||
|
17 | @staticmethod | |||
|
18 | def export(post, request, include_last_update): | |||
|
19 | if request is not None and PARAMETER_TRUNCATED in request.GET: | |||
|
20 | truncated = True | |||
|
21 | reply_link = False | |||
|
22 | else: | |||
|
23 | truncated = False | |||
|
24 | reply_link = True | |||
|
25 | ||||
|
26 | return post.get_view(truncated=truncated, reply_link=reply_link, | |||
|
27 | moderator=utils.is_moderator(request)) | |||
|
28 | ||||
|
29 | ||||
|
30 | class JsonExporter(Exporter): | |||
|
31 | @staticmethod | |||
|
32 | def export(post, request, include_last_update): | |||
|
33 | post_json = { | |||
|
34 | 'id': post.id, | |||
|
35 | 'title': post.title, | |||
|
36 | 'text': post.get_raw_text(), | |||
|
37 | } | |||
|
38 | if post.images.exists(): | |||
|
39 | post_image = post.get_first_image() | |||
|
40 | post_json['image'] = post_image.image.url | |||
|
41 | post_json['image_preview'] = post_image.image.url_200x150 | |||
|
42 | if include_last_update: | |||
|
43 | post_json['bump_time'] = utils.datetime_to_epoch( | |||
|
44 | post.get_thread().bump_time) | |||
|
45 | return post_json | |||
|
46 | ||||
|
47 | ||||
|
48 | EXPORTERS = { | |||
|
49 | DIFF_TYPE_HTML: HtmlExporter, | |||
|
50 | DIFF_TYPE_JSON: JsonExporter, | |||
|
51 | } | |||
|
52 | ||||
|
53 | ||||
|
54 | def get_exporter(export_type: str) -> Exporter: | |||
|
55 | return EXPORTERS[export_type]() |
@@ -16,6 +16,7 b' from boards.mdx_neboard import Parser' | |||||
16 | from boards.models import PostImage |
|
16 | from boards.models import PostImage | |
17 | from boards.models.base import Viewable |
|
17 | from boards.models.base import Viewable | |
18 | from boards import utils |
|
18 | from boards import utils | |
|
19 | from boards.models.post.export import get_exporter, DIFF_TYPE_JSON | |||
19 | from boards.models.user import Notification, Ban |
|
20 | from boards.models.user import Notification, Ban | |
20 | import boards.models.thread |
|
21 | import boards.models.thread | |
21 |
|
22 | |||
@@ -49,9 +50,6 b" PARAMETER_OP_ID = 'opening_post_id'" | |||||
49 | PARAMETER_NEED_OPEN_LINK = 'need_open_link' |
|
50 | PARAMETER_NEED_OPEN_LINK = 'need_open_link' | |
50 | PARAMETER_REPLY_LINK = 'reply_link' |
|
51 | PARAMETER_REPLY_LINK = 'reply_link' | |
51 |
|
52 | |||
52 | DIFF_TYPE_HTML = 'html' |
|
|||
53 | DIFF_TYPE_JSON = 'json' |
|
|||
54 |
|
||||
55 | REFMAP_STR = '<a href="{}">>>{}</a>' |
|
53 | REFMAP_STR = '<a href="{}">>>{}</a>' | |
56 |
|
54 | |||
57 |
|
55 | |||
@@ -292,7 +290,6 b' class Post(models.Model, Viewable):' | |||||
292 | logging.getLogger('boards.post.delete').info( |
|
290 | logging.getLogger('boards.post.delete').info( | |
293 | 'Deleted post {}'.format(self)) |
|
291 | 'Deleted post {}'.format(self)) | |
294 |
|
292 | |||
295 | # TODO Implement this with OOP, e.g. use the factory and HtmlPostData class |
|
|||
296 | def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None, |
|
293 | def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None, | |
297 | include_last_update=False) -> str: |
|
294 | include_last_update=False) -> str: | |
298 | """ |
|
295 | """ | |
@@ -300,30 +297,8 b' class Post(models.Model, Viewable):' | |||||
300 | API. |
|
297 | API. | |
301 | """ |
|
298 | """ | |
302 |
|
299 | |||
303 | if format_type == DIFF_TYPE_HTML: |
|
300 | return get_exporter(format_type).export(self, request, | |
304 | if request is not None and PARAMETER_TRUNCATED in request.GET: |
|
301 | include_last_update) | |
305 | truncated = True |
|
|||
306 | reply_link = False |
|
|||
307 | else: |
|
|||
308 | truncated = False |
|
|||
309 | reply_link = True |
|
|||
310 |
|
||||
311 | return self.get_view(truncated=truncated, reply_link=reply_link, |
|
|||
312 | moderator=utils.is_moderator(request)) |
|
|||
313 | elif format_type == DIFF_TYPE_JSON: |
|
|||
314 | post_json = { |
|
|||
315 | 'id': self.id, |
|
|||
316 | 'title': self.title, |
|
|||
317 | 'text': self._text_rendered, |
|
|||
318 | } |
|
|||
319 | if self.images.exists(): |
|
|||
320 | post_image = self.get_first_image() |
|
|||
321 | post_json['image'] = post_image.image.url |
|
|||
322 | post_json['image_preview'] = post_image.image.url_200x150 |
|
|||
323 | if include_last_update: |
|
|||
324 | post_json['bump_time'] = utils.datetime_to_epoch( |
|
|||
325 | self.get_thread().bump_time) |
|
|||
326 | return post_json |
|
|||
327 |
|
302 | |||
328 | def notify_clients(self, recursive=True): |
|
303 | def notify_clients(self, recursive=True): | |
329 | """ |
|
304 | """ |
General Comments 0
You need to be logged in to leave comments.
Login now