##// END OF EJS Templates
Allow ftp links to be saved with post
Allow ftp links to be saved with post

File last commit:

r1660:80bdd1ce default
r1674:f45ca462 default
Show More
__init__.py
88 lines | 2.9 KiB | text/x-python | PythonLexer
neko259
Store images as regular attachments instead of separate model
r1590 import boards
from boards.models import STATUS_ARCHIVE
from django.core.files.images import get_image_dimensions
neko259
Deduplicated upload_to method for images and file attachments
r1368 from django.db import models
neko259
Added support for different attachment types
r1273
neko259
Deduplicated file hash calculation method
r1305 from boards import utils
neko259
Store images as regular attachments instead of separate model
r1590 from boards.models.attachment.viewers import get_viewers, AbstractViewer, \
FILE_TYPES_IMAGE
from boards.utils import get_upload_filename, get_extension, cached_result
neko259
Added support for different attachment types
r1273
class AttachmentManager(models.Manager):
def create_with_hash(self, file):
neko259
Deduplicated file hash calculation method
r1305 file_hash = utils.get_file_hash(file)
neko259
Added support for different attachment types
r1273 existing = self.filter(hash=file_hash)
if len(existing) > 0:
attachment = existing[0]
else:
neko259
Autodetect only a white list of mimetypes
r1372 # FIXME Use full mimetype here, need to modify viewers too
neko259
Recognize flash mimetype properly. Store extension as mimetype in attachment...
r1382 file_type = get_extension(file.name)
neko259
Get mimetype for file in the form and use it for both images and attachments
r1371 attachment = self.create(file=file, mimetype=file_type,
hash=file_hash)
neko259
Added support for different attachment types
r1273
return attachment
neko259
Load URL if the file could not be loaded
r1660 def create_from_url(self, url):
existing = self.filter(url=url)
if len(existing) > 0:
attachment = existing[0]
else:
attachment = self.create(url=url)
return attachment
neko259
Store images as regular attachments instead of separate model
r1590 def get_random_images(self, count, tags=None):
images = self.filter(mimetype__in=FILE_TYPES_IMAGE).exclude(
neko259
Fixed issue with random images
r1592 attachment_posts__thread__status=STATUS_ARCHIVE)
neko259
Store images as regular attachments instead of separate model
r1590 if tags is not None:
neko259
Fixed issue with random images
r1592 images = images.filter(attachment_posts__threads__tags__in=tags)
neko259
Store images as regular attachments instead of separate model
r1590 return images.order_by('?')[:count]
neko259
Added support for different attachment types
r1273
class Attachment(models.Model):
objects = AttachmentManager()
neko259
Load URL if the file could not be loaded
r1660 file = models.FileField(upload_to=get_upload_filename, null=True)
mimetype = models.CharField(max_length=50, null=True)
hash = models.CharField(max_length=36, null=True)
neko259
Store images as regular attachments instead of separate model
r1590 alias = models.TextField(unique=True, null=True, blank=True)
neko259
Load URL if the file could not be loaded
r1660 url = models.TextField(null=True)
neko259
Added support for different attachment types
r1273
def get_view(self):
file_viewer = None
neko259
Autodetect attachment viewers by getting all abstract viewer subclasses
r1286 for viewer in get_viewers():
neko259
Added support for different attachment types
r1273 if viewer.supports(self.mimetype):
neko259
Refactored attachment viewer getting method
r1306 file_viewer = viewer
neko259
Added support for different attachment types
r1273 break
if file_viewer is None:
neko259
Refactored attachment viewer getting method
r1306 file_viewer = AbstractViewer
neko259
Added support for different attachment types
r1273
neko259
Load URL if the file could not be loaded
r1660 return file_viewer(self.file, self.mimetype, self.hash, self.url).get_view()
neko259
Added support for different attachment types
r1273
neko259
Made some fields in post admin read-only. Show attachment as its url
r1389 def __str__(self):
neko259
Load URL if the file could not be loaded
r1660 return self.url or self.file.url
neko259
Store images as regular attachments instead of separate model
r1590
def get_random_associated_post(self):
posts = boards.models.Post.objects.filter(attachments__in=[self])
return posts.order_by('?').first()
@cached_result()
def get_size(self):
neko259
Load URL if the file could not be loaded
r1660 if self.file:
if self.mimetype in FILE_TYPES_IMAGE:
return get_image_dimensions(self.file)
else:
return 200, 150
neko259
Store images as regular attachments instead of separate model
r1590
def get_thumb_url(self):
split = self.file.url.rsplit('.', 1)
w, h = 200, 150
return '%s.%sx%s.%s' % (split[0], w, h, split[1])
@cached_result()
def get_preview_size(self):
if self.mimetype in FILE_TYPES_IMAGE:
preview_path = self.file.path.replace('.', '.200x150.')
return get_image_dimensions(preview_path)
else:
return 200, 150