Show More
@@ -1,57 +1,57 b'' | |||
|
1 | 1 | from django.contrib.auth.context_processors import PermWrapper |
|
2 | 2 | |
|
3 | 3 | from boards import utils |
|
4 | 4 | |
|
5 | 5 | |
|
6 | 6 | PARAMETER_TRUNCATED = 'truncated' |
|
7 | 7 | |
|
8 | 8 | DIFF_TYPE_HTML = 'html' |
|
9 | 9 | DIFF_TYPE_JSON = 'json' |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | class Exporter(): |
|
13 | 13 | @staticmethod |
|
14 | 14 | def export(post, request, include_last_update) -> str: |
|
15 | 15 | pass |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | class HtmlExporter(Exporter): |
|
19 | 19 | @staticmethod |
|
20 | 20 | def export(post, request, include_last_update): |
|
21 | 21 | if request is not None and PARAMETER_TRUNCATED in request.GET: |
|
22 | 22 | truncated = True |
|
23 | 23 | reply_link = False |
|
24 | 24 | else: |
|
25 | 25 | truncated = False |
|
26 | 26 | reply_link = True |
|
27 | 27 | |
|
28 | 28 | return post.get_view(truncated=truncated, reply_link=reply_link, |
|
29 | 29 | perms=PermWrapper(request.user)) |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | class JsonExporter(Exporter): |
|
33 | 33 | @staticmethod |
|
34 | 34 | def export(post, request, include_last_update): |
|
35 | 35 | post_json = { |
|
36 | 36 | 'id': post.id, |
|
37 | 37 | 'title': post.title, |
|
38 | 38 | 'text': post.get_raw_text(), |
|
39 | 39 | } |
|
40 |
if post. |
|
|
40 | if post.attachments.exists(): | |
|
41 | 41 | post_image = post.get_first_image() |
|
42 |
post_json['image'] = post_image. |
|
|
43 |
post_json['image_preview'] = post_image. |
|
|
42 | post_json['image'] = post_image.file.url | |
|
43 | post_json['image_preview'] = post_image.get_preview_url() | |
|
44 | 44 | if include_last_update: |
|
45 | 45 | post_json['bump_time'] = utils.datetime_to_epoch( |
|
46 | 46 | post.get_thread().bump_time) |
|
47 | 47 | return post_json |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | EXPORTERS = { |
|
51 | 51 | DIFF_TYPE_HTML: HtmlExporter, |
|
52 | 52 | DIFF_TYPE_JSON: JsonExporter, |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | def get_exporter(export_type: str) -> Exporter: |
|
57 | 57 | return EXPORTERS[export_type]() |
@@ -1,116 +1,108 b'' | |||
|
1 | 1 | import re |
|
2 | 2 | from boards import thumbs |
|
3 | 3 | from boards.mdx_neboard import get_parser |
|
4 | 4 | |
|
5 | 5 | from boards.models import Post, GlobalId, Attachment |
|
6 | 6 | from boards.models.attachment.viewers import FILE_TYPES_IMAGE |
|
7 | 7 | from boards.models.post import REGEX_NOTIFICATION, REGEX_REPLY,\ |
|
8 | 8 | REGEX_GLOBAL_REPLY |
|
9 | 9 | from boards.models.post.manager import post_import_deps |
|
10 | 10 | from boards.models.user import Notification |
|
11 | 11 | from django.db.models.signals import post_save, pre_save, pre_delete, \ |
|
12 | 12 | post_delete |
|
13 | 13 | from django.dispatch import receiver |
|
14 | 14 | from django.utils import timezone |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | THUMB_SIZES = ((200, 150),) |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | @receiver(post_save, sender=Post) |
|
21 | 21 | def connect_replies(instance, **kwargs): |
|
22 | 22 | for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()): |
|
23 | 23 | post_id = reply_number.group(1) |
|
24 | 24 | |
|
25 | 25 | try: |
|
26 | 26 | referenced_post = Post.objects.get(id=post_id) |
|
27 | 27 | |
|
28 | 28 | if not referenced_post.referenced_posts.filter( |
|
29 | 29 | id=instance.id).exists(): |
|
30 | 30 | referenced_post.referenced_posts.add(instance) |
|
31 | 31 | referenced_post.last_edit_time = instance.pub_time |
|
32 | 32 | referenced_post.build_refmap() |
|
33 | 33 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
34 | 34 | except Post.DoesNotExist: |
|
35 | 35 | pass |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | @receiver(post_save, sender=Post) |
|
39 | 39 | @receiver(post_import_deps, sender=Post) |
|
40 | 40 | def connect_global_replies(instance, **kwargs): |
|
41 | 41 | for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()): |
|
42 | 42 | key_type = reply_number.group(1) |
|
43 | 43 | key = reply_number.group(2) |
|
44 | 44 | local_id = reply_number.group(3) |
|
45 | 45 | |
|
46 | 46 | try: |
|
47 | 47 | global_id = GlobalId.objects.get(key_type=key_type, key=key, |
|
48 | 48 | local_id=local_id) |
|
49 | 49 | referenced_post = Post.objects.get(global_id=global_id) |
|
50 | 50 | referenced_post.referenced_posts.add(instance) |
|
51 | 51 | referenced_post.last_edit_time = instance.pub_time |
|
52 | 52 | referenced_post.build_refmap() |
|
53 | 53 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
54 | 54 | except (GlobalId.DoesNotExist, Post.DoesNotExist): |
|
55 | 55 | pass |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | @receiver(post_save, sender=Post) |
|
59 | 59 | def connect_notifications(instance, **kwargs): |
|
60 | 60 | for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()): |
|
61 | 61 | user_name = reply_number.group(1).lower() |
|
62 | 62 | Notification.objects.get_or_create(name=user_name, post=instance) |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | @receiver(pre_save, sender=Post) |
|
66 | 66 | @receiver(post_import_deps, sender=Post) |
|
67 | 67 | def parse_text(instance, **kwargs): |
|
68 | 68 | instance._text_rendered = get_parser().parse(instance.get_raw_text()) |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | @receiver(pre_delete, sender=Post) |
|
72 | def delete_images(instance, **kwargs): | |
|
73 | for image in instance.images.all(): | |
|
74 | image_refs_count = image.post_images.count() | |
|
75 | if image_refs_count == 1: | |
|
76 | image.delete() | |
|
77 | ||
|
78 | ||
|
79 | @receiver(pre_delete, sender=Post) | |
|
80 | 72 | def delete_attachments(instance, **kwargs): |
|
81 | 73 | for attachment in instance.attachments.all(): |
|
82 | 74 | attachment_refs_count = attachment.attachment_posts.count() |
|
83 | 75 | if attachment_refs_count == 1: |
|
84 | 76 | attachment.delete() |
|
85 | 77 | |
|
86 | 78 | |
|
87 | 79 | @receiver(post_delete, sender=Post) |
|
88 | 80 | def update_thread_on_delete(instance, **kwargs): |
|
89 | 81 | thread = instance.get_thread() |
|
90 | 82 | thread.last_edit_time = timezone.now() |
|
91 | 83 | thread.save() |
|
92 | 84 | |
|
93 | 85 | |
|
94 | 86 | @receiver(post_delete, sender=Post) |
|
95 | 87 | def delete_global_id(instance, **kwargs): |
|
96 | 88 | if instance.global_id and instance.global_id.id: |
|
97 | 89 | instance.global_id.delete() |
|
98 | 90 | |
|
99 | 91 | |
|
100 | 92 | @receiver(post_save, sender=Attachment) |
|
101 | 93 | def generate_thumb(instance, **kwargs): |
|
102 | 94 | if instance.mimetype in FILE_TYPES_IMAGE: |
|
103 | 95 | for size in THUMB_SIZES: |
|
104 | 96 | (w, h) = size |
|
105 | 97 | split = instance.file.name.rsplit('.', 1) |
|
106 | 98 | thumb_name = '%s.%sx%s.%s' % (split[0], w, h, split[1]) |
|
107 | 99 | |
|
108 | 100 | if not instance.file.storage.exists(thumb_name): |
|
109 | 101 | # you can use another thumbnailing function if you like |
|
110 | 102 | thumb_content = thumbs.generate_thumb(instance.file, size, split[1]) |
|
111 | 103 | |
|
112 | 104 | thumb_name_ = instance.file.storage.save(thumb_name, thumb_content) |
|
113 | 105 | |
|
114 | 106 | if not thumb_name == thumb_name_: |
|
115 | 107 | raise ValueError( |
|
116 | 108 | 'There is already a file named %s' % thumb_name_) |
General Comments 0
You need to be logged in to leave comments.
Login now