##// END OF EJS Templates
Load URL if the file could not be loaded
neko259 -
r1660:80bdd1ce default
parent child Browse files
Show More
@@ -69,6 +69,9 b' MIMETYPE_EXTENSIONS = {'
69 }
69 }
70
70
71
71
72 logger = logging.getLogger('boards.forms')
73
74
72 def get_timezones():
75 def get_timezones():
73 timezones = []
76 timezones = []
74 for tz in pytz.common_timezones:
77 for tz in pytz.common_timezones:
@@ -235,6 +238,7 b' class PostForm(NeboardForm):'
235 file = None
238 file = None
236
239
237 if url:
240 if url:
241 try:
238 file = get_image_by_alias(url, self.session)
242 file = get_image_by_alias(url, self.session)
239 self.image = file
243 self.image = file
240
244
@@ -248,6 +252,10 b' class PostForm(NeboardForm):'
248 else:
252 else:
249 validate_file_size(file.size)
253 validate_file_size(file.size)
250 self._update_file_extension(file)
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 return file
260 return file
253
261
@@ -298,7 +306,6 b' class PostForm(NeboardForm):'
298 self._validate_posting_speed()
306 self._validate_posting_speed()
299 settings_manager.set_setting('confirmed_user', True)
307 settings_manager.set_setting('confirmed_user', True)
300
308
301
302 return cleaned_data
309 return cleaned_data
303
310
304 def get_file(self):
311 def get_file(self):
@@ -307,7 +314,15 b' class PostForm(NeboardForm):'
307 """
314 """
308
315
309 file = self.cleaned_data['file']
316 file = self.cleaned_data['file']
310 return file or self.cleaned_data['file_url']
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 def get_tripcode(self):
327 def get_tripcode(self):
313 title = self.cleaned_data['title']
328 title = self.cleaned_data['title']
@@ -337,9 +352,10 b' class PostForm(NeboardForm):'
337 def _clean_text_file(self):
352 def _clean_text_file(self):
338 text = self.cleaned_data.get('text')
353 text = self.cleaned_data.get('text')
339 file = self.get_file()
354 file = self.get_file()
355 file_url = self.get_file_url()
340 images = self.get_images()
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 error_message = _('Either text or file must be entered.')
359 error_message = _('Either text or file must be entered.')
344 self._errors['text'] = self.error_class([error_message])
360 self._errors['text'] = self.error_class([error_message])
345
361
@@ -23,6 +23,14 b' class AttachmentManager(models.Manager):'
23
23
24 return attachment
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 def get_random_images(self, count, tags=None):
34 def get_random_images(self, count, tags=None):
27 images = self.filter(mimetype__in=FILE_TYPES_IMAGE).exclude(
35 images = self.filter(mimetype__in=FILE_TYPES_IMAGE).exclude(
28 attachment_posts__thread__status=STATUS_ARCHIVE)
36 attachment_posts__thread__status=STATUS_ARCHIVE)
@@ -34,10 +42,11 b' class AttachmentManager(models.Manager):'
34 class Attachment(models.Model):
42 class Attachment(models.Model):
35 objects = AttachmentManager()
43 objects = AttachmentManager()
36
44
37 file = models.FileField(upload_to=get_upload_filename)
45 file = models.FileField(upload_to=get_upload_filename, null=True)
38 mimetype = models.CharField(max_length=50)
46 mimetype = models.CharField(max_length=50, null=True)
39 hash = models.CharField(max_length=36)
47 hash = models.CharField(max_length=36, null=True)
40 alias = models.TextField(unique=True, null=True, blank=True)
48 alias = models.TextField(unique=True, null=True, blank=True)
49 url = models.TextField(null=True)
41
50
42 def get_view(self):
51 def get_view(self):
43 file_viewer = None
52 file_viewer = None
@@ -48,10 +57,10 b' class Attachment(models.Model):'
48 if file_viewer is None:
57 if file_viewer is None:
49 file_viewer = AbstractViewer
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 def __str__(self):
62 def __str__(self):
54 return self.file.url
63 return self.url or self.file.url
55
64
56 def get_random_associated_post(self):
65 def get_random_associated_post(self):
57 posts = boards.models.Post.objects.filter(attachments__in=[self])
66 posts = boards.models.Post.objects.filter(attachments__in=[self])
@@ -59,6 +68,7 b' class Attachment(models.Model):'
59
68
60 @cached_result()
69 @cached_result()
61 def get_size(self):
70 def get_size(self):
71 if self.file:
62 if self.mimetype in FILE_TYPES_IMAGE:
72 if self.mimetype in FILE_TYPES_IMAGE:
63 return get_image_dimensions(self.file)
73 return get_image_dimensions(self.file)
64 else:
74 else:
@@ -3,6 +3,7 b' from django.template.defaultfilters impo'
3 from django.contrib.staticfiles.templatetags.staticfiles import static
3 from django.contrib.staticfiles.templatetags.staticfiles import static
4
4
5 FILE_STUB_IMAGE = 'images/file.png'
5 FILE_STUB_IMAGE = 'images/file.png'
6 FILE_STUB_URL = 'images/url.png'
6
7
7 FILE_TYPES_VIDEO = (
8 FILE_TYPES_VIDEO = (
8 'webm',
9 'webm',
@@ -39,10 +40,11 b' def get_viewers():'
39
40
40
41
41 class AbstractViewer:
42 class AbstractViewer:
42 def __init__(self, file, file_type, hash):
43 def __init__(self, file, file_type, hash, url):
43 self.file = file
44 self.file = file
44 self.file_type = file_type
45 self.file_type = file_type
45 self.hash = hash
46 self.hash = hash
47 self.url = url
46
48
47 @staticmethod
49 @staticmethod
48 def supports(file_type):
50 def supports(file_type):
@@ -129,3 +131,17 b' class ImageViewer(AbstractViewer):'
129 str(pre_height), str(width), str(height),
131 str(pre_height), str(width), str(height),
130 full=self.file.url, image_meta=metadata)
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 @transaction.atomic
28 @transaction.atomic
29 def create_post(self, title: str, text: str, file=None, thread=None,
29 def create_post(self, title: str, text: str, file=None, thread=None,
30 ip=NO_IP, tags: list=None, opening_posts: list=None,
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 Creates new post
34 Creates new post
34 """
35 """
@@ -79,6 +80,8 b' class PostManager(models.Manager):'
79 self._add_file_to_post(file, post)
80 self._add_file_to_post(file, post)
80 for image in images:
81 for image in images:
81 post.attachments.add(image)
82 post.attachments.add(image)
83 if file_url:
84 post.attachments.add(Attachment.objects.create_from_url(file_url))
82
85
83 post.connect_threads(opening_posts)
86 post.connect_threads(opening_posts)
84 post.set_global_id()
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
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 title = form.get_title()
134 title = form.get_title()
135 text = data[FORM_TEXT]
135 text = data[FORM_TEXT]
136 file = form.get_file()
136 file = form.get_file()
137 file_url = form.get_file_url()
137 threads = data[FORM_THREADS]
138 threads = data[FORM_THREADS]
138 images = form.get_images()
139 images = form.get_images()
139
140
@@ -145,7 +146,8 b' class AllThreadsView(PostMixin, FileUplo'
145 post = Post.objects.create_post(title=title, text=text, file=file,
146 post = Post.objects.create_post(title=title, text=text, file=file,
146 ip=ip, tags=tags, opening_posts=threads,
147 ip=ip, tags=tags, opening_posts=threads,
147 tripcode=form.get_tripcode(),
148 tripcode=form.get_tripcode(),
148 monochrome=monochrome, images=images)
149 monochrome=monochrome, images=images,
150 file_url = file_url)
149
151
150 # This is required to update the threads to which posts we have replied
152 # This is required to update the threads to which posts we have replied
151 # when creating this one
153 # when creating this one
@@ -128,6 +128,7 b' class ThreadView(BaseBoardView, PostMixi'
128 title = form.get_title()
128 title = form.get_title()
129 text = data[FORM_TEXT]
129 text = data[FORM_TEXT]
130 file = form.get_file()
130 file = form.get_file()
131 file_url = form.get_file_url()
131 threads = data[FORM_THREADS]
132 threads = data[FORM_THREADS]
132 images = form.get_images()
133 images = form.get_images()
133
134
@@ -139,7 +140,7 b' class ThreadView(BaseBoardView, PostMixi'
139 thread=post_thread, ip=ip,
140 thread=post_thread, ip=ip,
140 opening_posts=threads,
141 opening_posts=threads,
141 tripcode=form.get_tripcode(),
142 tripcode=form.get_tripcode(),
142 images=images)
143 images=images, file_url=file_url)
143 post.notify_clients()
144 post.notify_clients()
144
145
145 if form.is_subscribe():
146 if form.is_subscribe():
General Comments 0
You need to be logged in to leave comments. Login now