OpenSlides/openslides/mediafiles/models.py

84 lines
2.5 KiB
Python
Raw Normal View History

from django.conf import settings
from django.db import models
from django.utils.translation import ugettext as _
2016-01-03 15:33:51 +01:00
from openslides.utils.search import user_name_helper
from ..utils.models import RESTModelMixin
from .access_permissions import MediafileAccessPermissions
2015-06-29 13:31:07 +02:00
class Mediafile(RESTModelMixin, models.Model):
2013-03-19 00:51:52 +01:00
"""
Class for uploaded files which can be delivered under a certain url.
"""
access_permissions = MediafileAccessPermissions()
mediafile = models.FileField(upload_to='file')
2013-03-19 00:51:52 +01:00
"""
See https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
for more information.
"""
2013-03-19 00:51:52 +01:00
2016-01-10 00:20:32 +01:00
title = models.CharField(max_length=255, unique=True)
"""A string representing the title of the file."""
uploader = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
null=True,
blank=True)
"""A user the uploader of a file."""
private = models.BooleanField(default=False)
"""Whether or not this mediafile should be listed as private"""
timestamp = models.DateTimeField(auto_now_add=True)
"""A DateTimeField to save the upload date and time."""
class Meta:
2013-03-19 00:51:52 +01:00
"""
Meta class for the mediafile model.
"""
ordering = ['title']
2015-12-10 00:20:59 +01:00
default_permissions = ()
permissions = (
('can_see', 'Can see the list of files'),
('can_see_private', 'Can see private files'),
('can_upload', 'Can upload files'),
('can_manage', 'Can manage files'))
def __str__(self):
2013-03-19 00:51:52 +01:00
"""
Method for representation.
"""
return self.title
def get_filesize(self):
2013-03-19 00:51:52 +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.
try:
size = self.mediafile.size
except OSError:
size_string = _('unknown')
else:
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
2016-01-03 15:33:51 +01:00
def get_search_index_string(self):
"""
Returns a string that can be indexed for the search.
"""
return " ".join((
self.title,
user_name_helper(self.uploader)))