##// END OF EJS Templates
Merged in Lotgyero/neboard/Lotgyero/apimarkdown-bitbucket-1462952910699 (pull request #20)...
Merged in Lotgyero/neboard/Lotgyero/apimarkdown-bitbucket-1462952910699 (pull request #20) api.markdown отредактирован онлайн на Bitbucket

File last commit:

r1500:9178427e default
r1568:33ff4ffc merge default
Show More
image.py
98 lines | 3.3 KiB | text/x-python | PythonLexer
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693 from django.db import models
neko259
Added image format and size label to images
r1245 from django.template.defaultfilters import filesizeformat
neko259
Don't return posts with the same image in random view
r1249
neko259
Deduplicated file hash calculation method
r1305 from boards import thumbs, utils
neko259
Don't return posts with the same image in random view
r1249 import boards
neko259
Refactored post image
r931 from boards.models.base import Viewable
neko259
Don't show archived random images
r1416 from boards.models import STATUS_ARCHIVE
neko259
Deduplicated upload_to method for images and file attachments
r1368 from boards.utils import get_upload_filename
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693
neko259
Don't show archived random images
r1416
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693 __author__ = 'neko259'
IMAGE_THUMB_SIZE = (200, 150)
neko259
Refactored post image
r931 HASH_LENGTH = 36
CSS_CLASS_IMAGE = 'image'
CSS_CLASS_THUMB = 'thumb'
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693
neko259
Refactored post image code. Added a method to create an image or get an...
r1025 class PostImageManager(models.Manager):
def create_with_hash(self, image):
neko259
Deduplicated file hash calculation method
r1305 image_hash = utils.get_file_hash(image)
neko259
Refactored post image code. Added a method to create an image or get an...
r1025 existing = self.filter(hash=image_hash)
if len(existing) > 0:
post_image = existing[0]
else:
post_image = PostImage.objects.create(image=image)
return post_image
neko259
Thread status field instead of bumpable and archived fields (per BB-73)
r1414 def get_random_images(self, count, tags=None):
neko259
Don't show archived random images
r1416 images = self.exclude(post_images__thread__status=STATUS_ARCHIVE)
neko259
Added random images, associated with the tag, to the tag page
r1252 if tags is not None:
neko259
Show link to posts in this tag for the tag image even if the image is used in...
r1263 images = images.filter(post_images__threads__tags__in=tags)
neko259
Added random images, associated with the tag, to the tag page
r1252 return images.order_by('?')[:count]
neko259
Don't return posts with the same image in random view
r1249
neko259
Refactored post image code. Added a method to create an image or get an...
r1025
neko259
Refactored post image
r931 class PostImage(models.Model, Viewable):
neko259
Refactored post image code. Added a method to create an image or get an...
r1025 objects = PostImageManager()
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693 class Meta:
app_label = 'boards'
ordering = ('id',)
width = models.IntegerField(default=0)
height = models.IntegerField(default=0)
pre_width = models.IntegerField(default=0)
pre_height = models.IntegerField(default=0)
neko259
Deduplicated upload_to method for images and file attachments
r1368 image = thumbs.ImageWithThumbsField(upload_to=get_upload_filename,
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693 blank=True, sizes=(IMAGE_THUMB_SIZE,),
width_field='width',
height_field='height',
preview_width_field='pre_width',
preview_height_field='pre_height')
neko259
Refactored post image
r931 hash = models.CharField(max_length=HASH_LENGTH)
neko259
Added image aliases to upload the same images (like "fake" or "gtfo")
r1500 alias = models.TextField(unique=True, null=True, blank=True)
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693
def save(self, *args, **kwargs):
"""
Saves the model and computes the image hash for deduplication purposes.
"""
if not self.pk and self.image:
neko259
Deduplicated file hash calculation method
r1305 self.hash = utils.get_file_hash(self.image)
neko259
Moved post image to a separate model. Each post (as of model) can contain multiple images now. The image shown to the user is got with get_first_image method
r693 super(PostImage, self).save(*args, **kwargs)
neko259
Made post image use url as a string in admin site
r812 def __str__(self):
return self.image.url
neko259
Refactored post image
r931 def get_view(self):
neko259
Added image format and size label to images
r1245 metadata = '{}, {}'.format(self.image.name.split('.')[-1],
filesizeformat(self.image.size))
neko259
Refactored post image
r931 return '<div class="{}">' \
neko259
Setting for image view mode: in post (simple) or in popup
r1122 '<a class="{}" href="{full}">' \
'<img class="post-image-preview"' \
neko259
Refactored post image
r931 ' src="{}"' \
' alt="{}"' \
' width="{}"' \
' height="{}"' \
' data-width="{}"' \
' data-height="{}" />' \
'</a>' \
neko259
Removed obsolete code. Added download link to files
r1308 '<div class="image-metadata">'\
'<a href="{full}" download>{image_meta}</a>'\
'</div>' \
neko259
Refactored post image
r931 '</div>'\
neko259
Setting for image view mode: in post (simple) or in popup
r1122 .format(CSS_CLASS_IMAGE, CSS_CLASS_THUMB,
neko259
Refactored post image
r931 self.image.url_200x150,
str(self.hash), str(self.pre_width),
neko259
Added image format and size label to images
r1245 str(self.pre_height), str(self.width), str(self.height),
full=self.image.url, image_meta=metadata)
neko259
Don't return posts with the same image in random view
r1249
neko259
Speed up getting tag's random image
r1264 def get_random_associated_post(self):
neko259
Show link to posts in this tag for the tag image even if the image is used in...
r1263 posts = boards.models.Post.objects.filter(images__in=[self])
return posts.order_by('?').first()