2016-01-10 10:52:44 +01:00
|
|
|
from django.conf import settings
|
2015-09-06 14:12:34 +02:00
|
|
|
|
2019-06-28 07:24:28 +02:00
|
|
|
from ..utils.auth import get_group_model
|
|
|
|
from ..utils.rest_api import (
|
2019-12-04 14:24:30 +01:00
|
|
|
CharField,
|
2019-06-28 07:24:28 +02:00
|
|
|
IdPrimaryKeyRelatedField,
|
2019-12-04 14:24:30 +01:00
|
|
|
JSONField,
|
2019-06-28 07:24:28 +02:00
|
|
|
ModelSerializer,
|
|
|
|
SerializerMethodField,
|
|
|
|
ValidationError,
|
|
|
|
)
|
2019-03-06 14:53:24 +01:00
|
|
|
from .models import Mediafile
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MediafileSerializer(ModelSerializer):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
Serializer for mediafile.models.Mediafile objects.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-01-10 10:52:44 +01:00
|
|
|
media_url_prefix = SerializerMethodField()
|
2019-12-04 14:24:30 +01:00
|
|
|
pdf_information = JSONField(required=False)
|
2019-06-28 07:24:28 +02:00
|
|
|
access_groups = IdPrimaryKeyRelatedField(
|
|
|
|
many=True, required=False, queryset=get_group_model().objects.all()
|
|
|
|
)
|
2019-12-04 14:24:30 +01:00
|
|
|
original_filename = CharField(write_only=True, required=False, allow_null=True)
|
2015-09-06 14:12:34 +02:00
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
class Meta:
|
|
|
|
model = Mediafile
|
2015-02-04 00:08:38 +01:00
|
|
|
fields = (
|
2019-01-06 16:22:33 +01:00
|
|
|
"id",
|
|
|
|
"title",
|
2019-12-04 14:24:30 +01:00
|
|
|
"original_filename",
|
2019-01-06 16:22:33 +01:00
|
|
|
"media_url_prefix",
|
|
|
|
"filesize",
|
2019-12-04 14:24:30 +01:00
|
|
|
"mimetype",
|
|
|
|
"pdf_information",
|
2019-06-28 07:24:28 +02:00
|
|
|
"access_groups",
|
|
|
|
"create_timestamp",
|
|
|
|
"is_directory",
|
|
|
|
"path",
|
|
|
|
"parent",
|
2019-04-23 16:57:35 +02:00
|
|
|
"list_of_speakers_id",
|
2019-06-28 07:24:28 +02:00
|
|
|
"inherited_access_groups_id",
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2019-12-04 14:24:30 +01:00
|
|
|
read_only_fields = ("path", "filesize", "mimetype", "pdf_information")
|
2019-06-28 07:24:28 +02:00
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
title = data.get("title")
|
|
|
|
if title is not None and not title:
|
|
|
|
raise ValidationError({"detail": "The title must not be empty"})
|
|
|
|
|
|
|
|
parent = data.get("parent")
|
|
|
|
if parent and not parent.is_directory:
|
|
|
|
raise ValidationError({"detail": "parent must be a directory."})
|
|
|
|
|
|
|
|
if data.get("is_directory") and "/" in data.get("title", ""):
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": 'The name contains invalid characters: "/"'}
|
|
|
|
)
|
|
|
|
|
|
|
|
return super().validate(data)
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
access_groups = validated_data.pop("access_groups", [])
|
|
|
|
mediafile = super().create(validated_data)
|
|
|
|
mediafile.access_groups.set(access_groups)
|
|
|
|
mediafile.save()
|
|
|
|
return mediafile
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
# remove is_directory, create_timestamp and parent from validated_data
|
|
|
|
# to prevent updating them (mediafile is ensured in the constructor)
|
|
|
|
validated_data.pop("is_directory", None)
|
|
|
|
validated_data.pop("create_timestamp", None)
|
|
|
|
validated_data.pop("parent", None)
|
|
|
|
return super().update(instance, validated_data)
|
|
|
|
|
2016-01-10 10:52:44 +01:00
|
|
|
def get_media_url_prefix(self, mediafile):
|
|
|
|
return settings.MEDIA_URL
|