funkwhale/api/funkwhale_api/manage/serializers.py

171 wiersze
4.7 KiB
Python
Czysty Zwykły widok Historia

from django.db import transaction
2018-10-01 16:40:28 +00:00
from rest_framework import serializers
from funkwhale_api.common import serializers as common_serializers
from funkwhale_api.music import models as music_models
2018-06-19 16:48:43 +00:00
from funkwhale_api.users import models as users_models
from . import filters
2018-09-22 12:29:30 +00:00
class ManageUploadArtistSerializer(serializers.ModelSerializer):
class Meta:
model = music_models.Artist
2018-06-09 13:36:16 +00:00
fields = ["id", "mbid", "creation_date", "name"]
2018-09-22 12:29:30 +00:00
class ManageUploadAlbumSerializer(serializers.ModelSerializer):
artist = ManageUploadArtistSerializer()
class Meta:
model = music_models.Album
fields = (
2018-06-09 13:36:16 +00:00
"id",
"mbid",
"title",
"artist",
"release_date",
"cover",
"creation_date",
)
2018-09-22 12:29:30 +00:00
class ManageUploadTrackSerializer(serializers.ModelSerializer):
artist = ManageUploadArtistSerializer()
album = ManageUploadAlbumSerializer()
class Meta:
model = music_models.Track
2018-06-09 13:36:16 +00:00
fields = ("id", "mbid", "title", "album", "artist", "creation_date", "position")
2018-09-22 12:29:30 +00:00
class ManageUploadSerializer(serializers.ModelSerializer):
track = ManageUploadTrackSerializer()
class Meta:
2018-09-22 12:29:30 +00:00
model = music_models.Upload
fields = (
2018-06-09 13:36:16 +00:00
"id",
"path",
"source",
"filename",
"mimetype",
"track",
"duration",
"mimetype",
"creation_date",
2018-06-09 13:36:16 +00:00
"bitrate",
"size",
"path",
)
2018-09-22 12:29:30 +00:00
class ManageUploadActionSerializer(common_serializers.ActionSerializer):
actions = [common_serializers.Action("delete", allow_all=False)]
2018-09-22 12:29:30 +00:00
filterset_class = filters.ManageUploadFilterSet
@transaction.atomic
def handle_delete(self, objects):
return objects.delete()
2018-06-19 16:48:43 +00:00
2018-06-19 18:11:40 +00:00
class PermissionsSerializer(serializers.Serializer):
def to_representation(self, o):
return o.get_permissions(defaults=self.context.get("default_permissions"))
def to_internal_value(self, o):
return {"permissions": o}
class ManageUserSimpleSerializer(serializers.ModelSerializer):
class Meta:
model = users_models.User
fields = (
"id",
"username",
"email",
"name",
"is_active",
"is_staff",
"is_superuser",
"date_joined",
"last_activity",
"privacy_level",
2018-09-28 18:45:31 +00:00
"upload_quota",
)
2018-06-19 16:48:43 +00:00
class ManageUserSerializer(serializers.ModelSerializer):
2018-06-19 18:11:40 +00:00
permissions = PermissionsSerializer(source="*")
2018-09-28 18:45:31 +00:00
upload_quota = serializers.IntegerField(allow_null=True)
2018-06-19 16:48:43 +00:00
class Meta:
model = users_models.User
fields = (
"id",
"username",
"email",
"name",
"is_active",
"is_staff",
"is_superuser",
"date_joined",
"last_activity",
"permissions",
"privacy_level",
2018-09-28 18:45:31 +00:00
"upload_quota",
2018-06-19 16:48:43 +00:00
)
read_only_fields = [
"id",
"email",
"privacy_level",
"username",
"date_joined",
"last_activity",
]
2018-06-19 18:11:40 +00:00
def update(self, instance, validated_data):
instance = super().update(instance, validated_data)
permissions = validated_data.pop("permissions", {})
if permissions:
for p, value in permissions.items():
setattr(instance, "permission_{}".format(p), value)
instance.save(
update_fields=["permission_{}".format(p) for p in permissions.keys()]
)
return instance
class ManageInvitationSerializer(serializers.ModelSerializer):
users = ManageUserSimpleSerializer(many=True, required=False)
owner = ManageUserSimpleSerializer(required=False)
code = serializers.CharField(required=False, allow_null=True)
class Meta:
model = users_models.Invitation
fields = ("id", "owner", "code", "expiration_date", "creation_date", "users")
read_only_fields = ["id", "expiration_date", "owner", "creation_date", "users"]
def validate_code(self, value):
if not value:
return value
if users_models.Invitation.objects.filter(code__iexact=value).exists():
raise serializers.ValidationError(
"An invitation with this code already exists"
)
return value
class ManageInvitationActionSerializer(common_serializers.ActionSerializer):
actions = [
common_serializers.Action(
"delete", allow_all=False, qs_filter=lambda qs: qs.open()
)
]
filterset_class = filters.ManageInvitationFilterSet
@transaction.atomic
def handle_delete(self, objects):
return objects.delete()