2013-02-16 16:19:20 +01:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.mediafile.models
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Models for the mediafile app.
|
|
|
|
|
|
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import mimetypes
|
|
|
|
|
|
|
|
|
|
from django.db import models
|
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
|
|
|
|
|
|
|
|
|
from openslides.utils.person.models import PersonField
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Mediafile(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-04-22 19:59:05 +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
|
|
|
|
|
2013-04-22 19:59:05 +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."""
|
|
|
|
|
|
2013-04-22 19:59:05 +02:00
|
|
|
|
uploader = PersonField(blank=True, verbose_name=ugettext_lazy("Uploaded by"))
|
2013-02-16 16:19:20 +01:00
|
|
|
|
"""A person – the uploader of a file."""
|
|
|
|
|
|
|
|
|
|
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."""
|
|
|
|
|
|
|
|
|
|
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')),)
|
|
|
|
|
|
|
|
|
|
def __unicode__(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)
|
|
|
|
|
|
|
|
|
|
@models.permalink
|
|
|
|
|
def get_absolute_url(self, link='update'):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
Returns the URL to a mediafile. The link can be 'update' or 'delete'.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
if link == 'update' or link == 'edit': # 'edit' ist only used until utils/views.py is fixed
|
|
|
|
|
return ('mediafile_update', [str(self.id)])
|
|
|
|
|
if link == 'delete':
|
|
|
|
|
return ('mediafile_delete', [str(self.id)])
|
|
|
|
|
|
|
|
|
|
def get_filesize(self):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
Transforms Bytes to Kilobytes or Megabytes. Returns the size as string.
|
|
|
|
|
"""
|
|
|
|
|
# TODO: Read http://stackoverflow.com/a/1094933 and think about it.
|
2013-02-16 16:19:20 +01:00
|
|
|
|
size = self.mediafile.size
|
|
|
|
|
if size < 1024:
|
|
|
|
|
return '< 1 kB'
|
|
|
|
|
if size >= 1024 * 1024:
|
|
|
|
|
mB = size / 1024 / 1024
|
|
|
|
|
return '%d MB' % mB
|
|
|
|
|
else:
|
|
|
|
|
kB = size / 1024
|
|
|
|
|
return '%d kB' % kB
|