2015-09-17 22:58:24 +02:00
|
|
|
|
from django.conf import settings
|
2013-02-16 16:19:20 +01:00
|
|
|
|
from django.db import models
|
|
|
|
|
|
2017-09-26 09:00:34 +02:00
|
|
|
|
from ..core.config import config
|
2017-04-28 22:10:18 +02:00
|
|
|
|
from ..utils.autoupdate import inform_changed_data
|
2015-09-17 22:58:24 +02:00
|
|
|
|
from ..utils.models import RESTModelMixin
|
2016-02-11 22:58:32 +01:00
|
|
|
|
from .access_permissions import MediafileAccessPermissions
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
|
2015-06-29 13:31:07 +02:00
|
|
|
|
class Mediafile(RESTModelMixin, 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.
|
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
|
access_permissions = MediafileAccessPermissions()
|
2019-01-06 16:22:33 +01:00
|
|
|
|
can_see_permission = "mediafiles.can_see"
|
2016-02-11 22:58:32 +01:00
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
|
mediafile = models.FileField(upload_to="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
|
|
|
|
|
2016-01-10 00:20:32 +01:00
|
|
|
|
title = models.CharField(max_length=255, unique=True)
|
2013-02-16 16:19:20 +01:00
|
|
|
|
"""A string representing the title of the file."""
|
|
|
|
|
|
2016-01-09 09:58:22 +01:00
|
|
|
|
uploader = models.ForeignKey(
|
2019-01-06 16:22:33 +01:00
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True
|
|
|
|
|
)
|
2014-10-11 14:34:49 +02:00
|
|
|
|
"""A user – the uploader of a file."""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
2016-12-12 16:15:04 +01:00
|
|
|
|
hidden = models.BooleanField(default=False)
|
|
|
|
|
"""Whether or not this mediafile should be marked as hidden"""
|
2016-05-14 14:49:24 +02:00
|
|
|
|
|
2013-02-16 16:19:20 +01:00
|
|
|
|
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.
|
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
|
|
ordering = ["title"]
|
2015-12-10 00:20:59 +01:00
|
|
|
|
default_permissions = ()
|
2013-02-16 16:19:20 +01:00
|
|
|
|
permissions = (
|
2019-01-06 16:22:33 +01:00
|
|
|
|
("can_see", "Can see the list of files"),
|
|
|
|
|
("can_see_hidden", "Can see hidden files"),
|
|
|
|
|
("can_upload", "Can upload files"),
|
|
|
|
|
("can_manage", "Can manage files"),
|
|
|
|
|
)
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
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
|
|
|
|
|
|
2017-04-28 22:10:18 +02:00
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Saves mediafile (mainly on create and update requests).
|
|
|
|
|
"""
|
|
|
|
|
result = super().save(*args, **kwargs)
|
|
|
|
|
# Send uploader via autoupdate because users without permission
|
|
|
|
|
# to see users may not have it but can get it now.
|
|
|
|
|
inform_changed_data(self.uploader)
|
|
|
|
|
return result
|
|
|
|
|
|
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:
|
2019-01-12 23:01:42 +01:00
|
|
|
|
size_string = "unknown"
|
2013-02-16 16:19:20 +01:00
|
|
|
|
else:
|
2013-12-23 18:46:04 +01:00
|
|
|
|
if size < 1024:
|
2019-01-06 16:22:33 +01:00
|
|
|
|
size_string = "< 1 kB"
|
2013-12-23 18:46:04 +01:00
|
|
|
|
elif size >= 1024 * 1024:
|
|
|
|
|
mB = size / 1024 / 1024
|
2019-01-06 16:22:33 +01:00
|
|
|
|
size_string = "%d MB" % mB
|
2013-12-23 18:46:04 +01:00
|
|
|
|
else:
|
|
|
|
|
kB = size / 1024
|
2019-01-06 16:22:33 +01:00
|
|
|
|
size_string = "%d kB" % kB
|
2013-12-23 18:46:04 +01:00
|
|
|
|
return size_string
|
2017-09-26 09:00:34 +02:00
|
|
|
|
|
|
|
|
|
def is_logo(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
|
for key in config["logos_available"]:
|
|
|
|
|
if config[key]["path"] == self.mediafile.url:
|
2017-09-26 09:00:34 +02:00
|
|
|
|
return True
|
|
|
|
|
return False
|
2018-04-04 11:25:45 +02:00
|
|
|
|
|
|
|
|
|
def is_font(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
|
for key in config["fonts_available"]:
|
|
|
|
|
if config[key]["path"] == self.mediafile.url:
|
2018-04-04 11:25:45 +02:00
|
|
|
|
return True
|
|
|
|
|
return False
|