2018-05-28 22:07:20 +00:00
|
|
|
from django.db import transaction
|
2018-10-01 16:40:28 +00:00
|
|
|
|
2018-05-28 22:07:20 +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
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
from . import filters
|
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
class ManageUploadArtistSerializer(serializers.ModelSerializer):
|
2018-05-28 22:07:20 +00:00
|
|
|
class Meta:
|
|
|
|
model = music_models.Artist
|
2018-06-09 13:36:16 +00:00
|
|
|
fields = ["id", "mbid", "creation_date", "name"]
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
class ManageUploadAlbumSerializer(serializers.ModelSerializer):
|
|
|
|
artist = ManageUploadArtistSerializer()
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
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-05-28 22:07:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
class ManageUploadTrackSerializer(serializers.ModelSerializer):
|
|
|
|
artist = ManageUploadArtistSerializer()
|
|
|
|
album = ManageUploadAlbumSerializer()
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Track
|
2018-06-09 13:36:16 +00:00
|
|
|
fields = ("id", "mbid", "title", "album", "artist", "creation_date", "position")
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
class ManageUploadSerializer(serializers.ModelSerializer):
|
|
|
|
track = ManageUploadTrackSerializer()
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
class Meta:
|
2018-09-22 12:29:30 +00:00
|
|
|
model = music_models.Upload
|
2018-05-28 22:07:20 +00:00
|
|
|
fields = (
|
2018-06-09 13:36:16 +00:00
|
|
|
"id",
|
|
|
|
"path",
|
|
|
|
"source",
|
|
|
|
"filename",
|
|
|
|
"mimetype",
|
|
|
|
"track",
|
|
|
|
"duration",
|
|
|
|
"mimetype",
|
2018-06-24 12:03:37 +00:00
|
|
|
"creation_date",
|
2018-06-09 13:36:16 +00:00
|
|
|
"bitrate",
|
|
|
|
"size",
|
|
|
|
"path",
|
2018-05-28 22:07:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
class ManageUploadActionSerializer(common_serializers.ActionSerializer):
|
2018-06-21 17:21:51 +00:00
|
|
|
actions = [common_serializers.Action("delete", allow_all=False)]
|
2018-09-22 12:29:30 +00:00
|
|
|
filterset_class = filters.ManageUploadFilterSet
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
@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}
|
|
|
|
|
|
|
|
|
2018-06-19 21:27:21 +00:00
|
|
|
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 21:27:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
2018-06-19 21:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2018-06-21 17:41:40 +00:00
|
|
|
if users_models.Invitation.objects.filter(code__iexact=value).exists():
|
2018-06-19 21:27:21 +00:00
|
|
|
raise serializers.ValidationError(
|
|
|
|
"An invitation with this code already exists"
|
|
|
|
)
|
|
|
|
return value
|
2018-06-21 17:22:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageInvitationActionSerializer(common_serializers.ActionSerializer):
|
2018-06-21 21:31:12 +00:00
|
|
|
actions = [
|
|
|
|
common_serializers.Action(
|
|
|
|
"delete", allow_all=False, qs_filter=lambda qs: qs.open()
|
|
|
|
)
|
|
|
|
]
|
2018-06-21 17:22:51 +00:00
|
|
|
filterset_class = filters.ManageInvitationFilterSet
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_delete(self, objects):
|
|
|
|
return objects.delete()
|