##// END OF EJS Templates
Subscribe to a multiple of users for notifications
Subscribe to a multiple of users for notifications

File last commit:

r1389:c9a1cd47 default
r1429:5967a527 default
Show More
__init__.py
42 lines | 1.3 KiB | text/x-python | PythonLexer
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
Autodetect attachment viewers by getting all abstract viewer subclasses
r1286 from boards.models.attachment.viewers import get_viewers, AbstractViewer
neko259
Recognize flash mimetype properly. Store extension as mimetype in attachment...
r1382 from boards.utils import get_upload_filename, get_file_mimetype, get_extension
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
class Attachment(models.Model):
objects = AttachmentManager()
neko259
Deduplicated upload_to method for images and file attachments
r1368 file = models.FileField(upload_to=get_upload_filename)
neko259
Added support for different attachment types
r1273 mimetype = models.CharField(max_length=50)
hash = models.CharField(max_length=36)
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
Refactored attachment viewer getting method
r1306 return file_viewer(self.file, self.mimetype).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):
return self.file.url