##// END OF EJS Templates
Image deduplication (BB-53). When an image with the same hash is uploaded, it...
neko259 -
r944:6ed17cb6 default
parent child Browse files
Show More
@@ -22,8 +22,6 b" LAST_LOGIN_TIME = 'last_login_time'"
22 TEXT_PLACEHOLDER = _('Type message here. Use formatting panel for more advanced usage.')
22 TEXT_PLACEHOLDER = _('Type message here. Use formatting panel for more advanced usage.')
23 TAGS_PLACEHOLDER = _('tag1 several_words_tag')
23 TAGS_PLACEHOLDER = _('tag1 several_words_tag')
24
24
25 ERROR_IMAGE_DUPLICATE = _('Such image was already posted')
26
27 LABEL_TITLE = _('Title')
25 LABEL_TITLE = _('Title')
28 LABEL_TEXT = _('Text')
26 LABEL_TEXT = _('Text')
29 LABEL_TAG = _('Tag')
27 LABEL_TAG = _('Tag')
@@ -149,10 +147,6 b' class PostForm(NeboardForm):'
149 _('Image must be less than %s bytes')
147 _('Image must be less than %s bytes')
150 % str(board_settings.MAX_IMAGE_SIZE))
148 % str(board_settings.MAX_IMAGE_SIZE))
151
149
152 image_hash = PostImage.get_hash(image)
153 if PostImage.objects.filter(hash=image_hash).exists():
154 raise forms.ValidationError(ERROR_IMAGE_DUPLICATE)
155
156 return image
150 return image
157
151
158 def clean(self):
152 def clean(self):
@@ -103,10 +103,17 b' class PostManager(models.Manager):'
103 post, post.poster_ip))
103 post, post.poster_ip))
104
104
105 if image:
105 if image:
106 post_image = PostImage.objects.create(image=image)
106 # Try to find existing image. If it exists, assign it to the post
107 # instead of createing the new one
108 image_hash = PostImage.get_hash(image)
109 existing = PostImage.objects.filter(hash=image_hash)
110 if len(existing) > 0:
111 post_image = existing[0]
112 else:
113 post_image = PostImage.objects.create(image=image)
114 logger.info('Created new image #{} for post #{}'.format(
115 post_image.id, post.id))
107 post.images.add(post_image)
116 post.images.add(post_image)
108 logger.info('Created image #{} for post #{}'.format(
109 post_image.id, post.id))
110
117
111 thread.replies.add(post)
118 thread.replies.add(post)
112 list(map(thread.add_tag, tags))
119 list(map(thread.add_tag, tags))
@@ -352,7 +359,10 b' class Post(models.Model, Viewable):'
352 thread with all posts is deleted.
359 thread with all posts is deleted.
353 """
360 """
354
361
355 self.images.all().delete()
362 for image in self.images.all():
363 image_refs_count = Post.objects.filter(images__in=[image]).count()
364 if image_refs_count == 1:
365 image.delete()
356
366
357 if self.is_opening():
367 if self.is_opening():
358 self.get_thread().delete()
368 self.get_thread().delete()
General Comments 0
You need to be logged in to leave comments. Login now