Show More
@@ -69,6 +69,9 b' MIMETYPE_EXTENSIONS = {' | |||
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | |
|
72 | logger = logging.getLogger('boards.forms') | |
|
73 | ||
|
74 | ||
|
72 | 75 | def get_timezones(): |
|
73 | 76 | timezones = [] |
|
74 | 77 | for tz in pytz.common_timezones: |
@@ -235,19 +238,24 b' class PostForm(NeboardForm):' | |||
|
235 | 238 | file = None |
|
236 | 239 | |
|
237 | 240 | if url: |
|
238 | file = get_image_by_alias(url, self.session) | |
|
239 | self.image = file | |
|
241 | try: | |
|
242 | file = get_image_by_alias(url, self.session) | |
|
243 | self.image = file | |
|
240 | 244 | |
|
241 | if file is not None: | |
|
242 | return | |
|
245 | if file is not None: | |
|
246 | return | |
|
243 | 247 | |
|
244 | if file is None: | |
|
245 | file = self._get_file_from_url(url) | |
|
246 | if not file: | |
|
247 | raise forms.ValidationError(_('Invalid URL')) | |
|
248 | else: | |
|
249 | validate_file_size(file.size) | |
|
250 | self._update_file_extension(file) | |
|
248 | if file is None: | |
|
249 | file = self._get_file_from_url(url) | |
|
250 | if not file: | |
|
251 | raise forms.ValidationError(_('Invalid URL')) | |
|
252 | else: | |
|
253 | validate_file_size(file.size) | |
|
254 | self._update_file_extension(file) | |
|
255 | except forms.ValidationError as e: | |
|
256 | # Assume we will get the plain URL instead of a file and save it | |
|
257 | logger.info('Error in forms: {}'.format(e)) | |
|
258 | return url | |
|
251 | 259 | |
|
252 | 260 | return file |
|
253 | 261 | |
@@ -298,7 +306,6 b' class PostForm(NeboardForm):' | |||
|
298 | 306 | self._validate_posting_speed() |
|
299 | 307 | settings_manager.set_setting('confirmed_user', True) |
|
300 | 308 | |
|
301 | ||
|
302 | 309 | return cleaned_data |
|
303 | 310 | |
|
304 | 311 | def get_file(self): |
@@ -307,7 +314,15 b' class PostForm(NeboardForm):' | |||
|
307 | 314 | """ |
|
308 | 315 | |
|
309 | 316 | file = self.cleaned_data['file'] |
|
310 |
|
|
|
317 | if type(self.cleaned_data['file_url']) is not str: | |
|
318 | file_url = self.cleaned_data['file_url'] | |
|
319 | else: | |
|
320 | file_url = None | |
|
321 | return file or file_url | |
|
322 | ||
|
323 | def get_file_url(self): | |
|
324 | if not self.get_file(): | |
|
325 | return self.cleaned_data['file_url'] | |
|
311 | 326 | |
|
312 | 327 | def get_tripcode(self): |
|
313 | 328 | title = self.cleaned_data['title'] |
@@ -337,9 +352,10 b' class PostForm(NeboardForm):' | |||
|
337 | 352 | def _clean_text_file(self): |
|
338 | 353 | text = self.cleaned_data.get('text') |
|
339 | 354 | file = self.get_file() |
|
355 | file_url = self.get_file_url() | |
|
340 | 356 | images = self.get_images() |
|
341 | 357 | |
|
342 | if (not text) and (not file) and len(images) == 0: | |
|
358 | if (not text) and (not file) and (not file_url) and len(images) == 0: | |
|
343 | 359 | error_message = _('Either text or file must be entered.') |
|
344 | 360 | self._errors['text'] = self.error_class([error_message]) |
|
345 | 361 |
@@ -23,6 +23,14 b' class AttachmentManager(models.Manager):' | |||
|
23 | 23 | |
|
24 | 24 | return attachment |
|
25 | 25 | |
|
26 | def create_from_url(self, url): | |
|
27 | existing = self.filter(url=url) | |
|
28 | if len(existing) > 0: | |
|
29 | attachment = existing[0] | |
|
30 | else: | |
|
31 | attachment = self.create(url=url) | |
|
32 | return attachment | |
|
33 | ||
|
26 | 34 | def get_random_images(self, count, tags=None): |
|
27 | 35 | images = self.filter(mimetype__in=FILE_TYPES_IMAGE).exclude( |
|
28 | 36 | attachment_posts__thread__status=STATUS_ARCHIVE) |
@@ -34,10 +42,11 b' class AttachmentManager(models.Manager):' | |||
|
34 | 42 | class Attachment(models.Model): |
|
35 | 43 | objects = AttachmentManager() |
|
36 | 44 | |
|
37 | file = models.FileField(upload_to=get_upload_filename) | |
|
38 | mimetype = models.CharField(max_length=50) | |
|
39 | hash = models.CharField(max_length=36) | |
|
45 | file = models.FileField(upload_to=get_upload_filename, null=True) | |
|
46 | mimetype = models.CharField(max_length=50, null=True) | |
|
47 | hash = models.CharField(max_length=36, null=True) | |
|
40 | 48 | alias = models.TextField(unique=True, null=True, blank=True) |
|
49 | url = models.TextField(null=True) | |
|
41 | 50 | |
|
42 | 51 | def get_view(self): |
|
43 | 52 | file_viewer = None |
@@ -48,10 +57,10 b' class Attachment(models.Model):' | |||
|
48 | 57 | if file_viewer is None: |
|
49 | 58 | file_viewer = AbstractViewer |
|
50 | 59 | |
|
51 | return file_viewer(self.file, self.mimetype, self.hash).get_view() | |
|
60 | return file_viewer(self.file, self.mimetype, self.hash, self.url).get_view() | |
|
52 | 61 | |
|
53 | 62 | def __str__(self): |
|
54 | return self.file.url | |
|
63 | return self.url or self.file.url | |
|
55 | 64 | |
|
56 | 65 | def get_random_associated_post(self): |
|
57 | 66 | posts = boards.models.Post.objects.filter(attachments__in=[self]) |
@@ -59,10 +68,11 b' class Attachment(models.Model):' | |||
|
59 | 68 | |
|
60 | 69 | @cached_result() |
|
61 | 70 | def get_size(self): |
|
62 | if self.mimetype in FILE_TYPES_IMAGE: | |
|
63 | return get_image_dimensions(self.file) | |
|
64 | else: | |
|
65 | return 200, 150 | |
|
71 | if self.file: | |
|
72 | if self.mimetype in FILE_TYPES_IMAGE: | |
|
73 | return get_image_dimensions(self.file) | |
|
74 | else: | |
|
75 | return 200, 150 | |
|
66 | 76 | |
|
67 | 77 | def get_thumb_url(self): |
|
68 | 78 | split = self.file.url.rsplit('.', 1) |
@@ -3,6 +3,7 b' from django.template.defaultfilters impo' | |||
|
3 | 3 | from django.contrib.staticfiles.templatetags.staticfiles import static |
|
4 | 4 | |
|
5 | 5 | FILE_STUB_IMAGE = 'images/file.png' |
|
6 | FILE_STUB_URL = 'images/url.png' | |
|
6 | 7 | |
|
7 | 8 | FILE_TYPES_VIDEO = ( |
|
8 | 9 | 'webm', |
@@ -39,10 +40,11 b' def get_viewers():' | |||
|
39 | 40 | |
|
40 | 41 | |
|
41 | 42 | class AbstractViewer: |
|
42 | def __init__(self, file, file_type, hash): | |
|
43 | def __init__(self, file, file_type, hash, url): | |
|
43 | 44 | self.file = file |
|
44 | 45 | self.file_type = file_type |
|
45 | 46 | self.hash = hash |
|
47 | self.url = url | |
|
46 | 48 | |
|
47 | 49 | @staticmethod |
|
48 | 50 | def supports(file_type): |
@@ -129,3 +131,17 b' class ImageViewer(AbstractViewer):' | |||
|
129 | 131 | str(pre_height), str(width), str(height), |
|
130 | 132 | full=self.file.url, image_meta=metadata) |
|
131 | 133 | |
|
134 | ||
|
135 | class UrlViewer(AbstractViewer): | |
|
136 | @staticmethod | |
|
137 | def supports(file_type): | |
|
138 | return file_type is None | |
|
139 | ||
|
140 | def get_view(self): | |
|
141 | return '<div class="image">' \ | |
|
142 | '{}' \ | |
|
143 | '</div>'.format(self.get_format_view()) | |
|
144 | def get_format_view(self): | |
|
145 | return '<a href="{}">' \ | |
|
146 | '<img src="{}" width="200" height="150"/>' \ | |
|
147 | '</a>'.format(self.url, static(FILE_STUB_URL)) |
@@ -28,7 +28,8 b' class PostManager(models.Manager):' | |||
|
28 | 28 | @transaction.atomic |
|
29 | 29 | def create_post(self, title: str, text: str, file=None, thread=None, |
|
30 | 30 | ip=NO_IP, tags: list=None, opening_posts: list=None, |
|
31 |
tripcode='', monochrome=False, images=[] |
|
|
31 | tripcode='', monochrome=False, images=[], | |
|
32 | file_url=None): | |
|
32 | 33 | """ |
|
33 | 34 | Creates new post |
|
34 | 35 | """ |
@@ -79,6 +80,8 b' class PostManager(models.Manager):' | |||
|
79 | 80 | self._add_file_to_post(file, post) |
|
80 | 81 | for image in images: |
|
81 | 82 | post.attachments.add(image) |
|
83 | if file_url: | |
|
84 | post.attachments.add(Attachment.objects.create_from_url(file_url)) | |
|
82 | 85 | |
|
83 | 86 | post.connect_threads(opening_posts) |
|
84 | 87 | post.set_global_id() |
|
1 | NO CONTENT: file copied from boards/static/images/file.png to boards/static/images/url.png, binary diff hidden |
@@ -134,6 +134,7 b' class AllThreadsView(PostMixin, FileUplo' | |||
|
134 | 134 | title = form.get_title() |
|
135 | 135 | text = data[FORM_TEXT] |
|
136 | 136 | file = form.get_file() |
|
137 | file_url = form.get_file_url() | |
|
137 | 138 | threads = data[FORM_THREADS] |
|
138 | 139 | images = form.get_images() |
|
139 | 140 | |
@@ -145,7 +146,8 b' class AllThreadsView(PostMixin, FileUplo' | |||
|
145 | 146 | post = Post.objects.create_post(title=title, text=text, file=file, |
|
146 | 147 | ip=ip, tags=tags, opening_posts=threads, |
|
147 | 148 | tripcode=form.get_tripcode(), |
|
148 |
monochrome=monochrome, images=images |
|
|
149 | monochrome=monochrome, images=images, | |
|
150 | file_url = file_url) | |
|
149 | 151 | |
|
150 | 152 | # This is required to update the threads to which posts we have replied |
|
151 | 153 | # when creating this one |
@@ -128,6 +128,7 b' class ThreadView(BaseBoardView, PostMixi' | |||
|
128 | 128 | title = form.get_title() |
|
129 | 129 | text = data[FORM_TEXT] |
|
130 | 130 | file = form.get_file() |
|
131 | file_url = form.get_file_url() | |
|
131 | 132 | threads = data[FORM_THREADS] |
|
132 | 133 | images = form.get_images() |
|
133 | 134 | |
@@ -139,7 +140,7 b' class ThreadView(BaseBoardView, PostMixi' | |||
|
139 | 140 | thread=post_thread, ip=ip, |
|
140 | 141 | opening_posts=threads, |
|
141 | 142 | tripcode=form.get_tripcode(), |
|
142 | images=images) | |
|
143 | images=images, file_url=file_url) | |
|
143 | 144 | post.notify_clients() |
|
144 | 145 | |
|
145 | 146 | if form.is_subscribe(): |
General Comments 0
You need to be logged in to leave comments.
Login now