2013-02-16 16:19:20 +01:00
|
|
|
|
import mimetypes
|
|
|
|
|
|
2013-10-04 11:38:39 +02:00
|
|
|
|
from django.core.urlresolvers import reverse
|
2013-02-16 16:19:20 +01:00
|
|
|
|
from django.db import models
|
2013-12-23 18:46:04 +01:00
|
|
|
|
from django.utils.translation import ugettext as _
|
2013-04-22 19:59:05 +02:00
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext_noop
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
2013-10-04 11:38:39 +02:00
|
|
|
|
from openslides.projector.models import SlideMixin
|
2014-01-11 17:57:24 +01:00
|
|
|
|
from openslides.utils.models import AbsoluteUrlMixin
|
2014-10-11 14:34:49 +02:00
|
|
|
|
from openslides.users.models import User
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
|
2014-01-11 17:57:24 +01:00
|
|
|
|
class Mediafile(SlideMixin, AbsoluteUrlMixin, models.Model):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
Class for uploaded files which can be delivered under a certain url.
|
|
|
|
|
"""
|
2013-10-04 11:38:39 +02:00
|
|
|
|
slide_callback_name = 'mediafile'
|
|
|
|
|
PRESENTABLE_FILE_TYPES = ['application/pdf']
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
|
mediafile = models.FileField(upload_to='file', verbose_name=ugettext_lazy('File'))
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
See https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
|
|
|
|
|
for more information.
|
|
|
|
|
"""
|
2013-03-19 00:51:52 +01:00
|
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
|
title = models.CharField(max_length=255, unique=True, verbose_name=ugettext_lazy('Title'))
|
2013-02-16 16:19:20 +01:00
|
|
|
|
"""A string representing the title of the file."""
|
|
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
|
uploader = models.ForeignKey(User, null=True, blank=True, verbose_name=ugettext_lazy('Uploaded by'))
|
|
|
|
|
"""A user – the uploader of a file."""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
timestamp = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
"""A DateTimeField to save the upload date and time."""
|
|
|
|
|
|
|
|
|
|
filetype = models.CharField(max_length=255, editable=False)
|
|
|
|
|
"""A string used to show the type of the file."""
|
|
|
|
|
|
2013-10-04 11:38:39 +02:00
|
|
|
|
is_presentable = models.BooleanField(
|
|
|
|
|
default=False,
|
|
|
|
|
verbose_name=ugettext_lazy("Is Presentable"),
|
|
|
|
|
help_text=ugettext_lazy("If checked, this file can be presented on the projector. "
|
|
|
|
|
"Currently, this is only possible for PDFs."))
|
|
|
|
|
|
2013-02-16 16:19:20 +01:00
|
|
|
|
class Meta:
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
Meta class for the mediafile model.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
ordering = ['title']
|
|
|
|
|
permissions = (
|
|
|
|
|
('can_see', ugettext_noop('Can see the list of files')),
|
|
|
|
|
('can_upload', ugettext_noop('Can upload files')),
|
|
|
|
|
('can_manage', ugettext_noop('Can manage files')),)
|
|
|
|
|
|
2014-08-16 09:25:18 +02:00
|
|
|
|
def __str__(self):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
Method for representation.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
Method to read filetype and then save to the database.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
if self.mediafile:
|
|
|
|
|
self.filetype = mimetypes.guess_type(self.mediafile.path)[0] or ugettext_noop('unknown')
|
|
|
|
|
else:
|
|
|
|
|
self.filetype = ugettext_noop('unknown')
|
|
|
|
|
return super(Mediafile, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def get_absolute_url(self, link='update'):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
2013-10-04 11:38:39 +02:00
|
|
|
|
Returns the URL to a mediafile. The link can be 'projector',
|
|
|
|
|
'update' or 'delete'.
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
2014-01-11 17:37:13 +01:00
|
|
|
|
if link == 'update':
|
2014-01-11 17:57:24 +01:00
|
|
|
|
url = reverse('mediafile_update', kwargs={'pk': str(self.pk)})
|
|
|
|
|
elif link == 'delete':
|
|
|
|
|
url = reverse('mediafile_delete', kwargs={'pk': str(self.pk)})
|
|
|
|
|
else:
|
|
|
|
|
url = super(Mediafile, self).get_absolute_url(link)
|
|
|
|
|
return url
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
def get_filesize(self):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
2013-12-23 18:46:04 +01:00
|
|
|
|
Transforms bytes to kilobytes or megabytes. Returns the size as string.
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
# TODO: Read http://stackoverflow.com/a/1094933 and think about it.
|
2013-12-23 18:46:04 +01:00
|
|
|
|
try:
|
|
|
|
|
size = self.mediafile.size
|
|
|
|
|
except OSError:
|
|
|
|
|
size_string = _('unknown')
|
2013-02-16 16:19:20 +01:00
|
|
|
|
else:
|
2013-12-23 18:46:04 +01:00
|
|
|
|
if size < 1024:
|
|
|
|
|
size_string = '< 1 kB'
|
|
|
|
|
elif size >= 1024 * 1024:
|
|
|
|
|
mB = size / 1024 / 1024
|
|
|
|
|
size_string = '%d MB' % mB
|
|
|
|
|
else:
|
|
|
|
|
kB = size / 1024
|
|
|
|
|
size_string = '%d kB' % kB
|
|
|
|
|
return size_string
|