2019-08-27 12:00:33 +00:00
|
|
|
from django.conf import settings
|
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
|
|
|
|
|
2020-03-31 08:45:41 +00:00
|
|
|
from funkwhale_api.audio import models as audio_models
|
2019-08-29 09:45:41 +00:00
|
|
|
from funkwhale_api.common import fields as common_fields
|
2018-05-28 22:07:20 +00:00
|
|
|
from funkwhale_api.common import serializers as common_serializers
|
2019-01-09 13:18:32 +00:00
|
|
|
from funkwhale_api.common import utils as common_utils
|
2018-12-26 22:03:27 +00:00
|
|
|
from funkwhale_api.federation import models as federation_models
|
2019-01-07 08:45:53 +00:00
|
|
|
from funkwhale_api.federation import fields as federation_fields
|
2019-01-09 13:18:32 +00:00
|
|
|
from funkwhale_api.federation import tasks as federation_tasks
|
2019-01-07 08:45:53 +00:00
|
|
|
from funkwhale_api.moderation import models as moderation_models
|
2019-08-26 12:37:48 +00:00
|
|
|
from funkwhale_api.moderation import serializers as moderation_serializers
|
2019-08-29 12:00:10 +00:00
|
|
|
from funkwhale_api.moderation import utils as moderation_utils
|
2018-05-28 22:07:20 +00:00
|
|
|
from funkwhale_api.music import models as music_models
|
2019-04-17 12:17:59 +00:00
|
|
|
from funkwhale_api.music import serializers as music_serializers
|
2019-07-24 08:24:30 +00:00
|
|
|
from funkwhale_api.tags import models as tags_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-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)
|
2019-06-27 12:50:56 +00:00
|
|
|
actor = serializers.SerializerMethodField()
|
2018-06-19 16:48:43 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = users_models.User
|
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"username",
|
2019-06-27 12:50:56 +00:00
|
|
|
"actor",
|
2018-06-19 16:48:43 +00:00
|
|
|
"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",
|
2019-01-03 16:10:02 +00:00
|
|
|
"full_username",
|
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
|
|
|
|
2019-06-27 12:50:56 +00:00
|
|
|
def get_actor(self, obj):
|
|
|
|
if obj.actor:
|
|
|
|
return ManageBaseActorSerializer(obj.actor).data
|
|
|
|
|
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()
|
2018-12-26 22:03:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageDomainSerializer(serializers.ModelSerializer):
|
|
|
|
actors_count = serializers.SerializerMethodField()
|
|
|
|
outbox_activities_count = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = federation_models.Domain
|
|
|
|
fields = [
|
|
|
|
"name",
|
|
|
|
"creation_date",
|
|
|
|
"actors_count",
|
|
|
|
"outbox_activities_count",
|
2018-12-27 16:42:43 +00:00
|
|
|
"nodeinfo",
|
|
|
|
"nodeinfo_fetch_date",
|
2019-01-07 08:45:53 +00:00
|
|
|
"instance_policy",
|
2019-06-17 06:48:05 +00:00
|
|
|
"allowed",
|
2019-01-07 08:45:53 +00:00
|
|
|
]
|
|
|
|
read_only_fields = [
|
|
|
|
"creation_date",
|
|
|
|
"instance_policy",
|
|
|
|
"nodeinfo",
|
|
|
|
"nodeinfo_fetch_date",
|
2018-12-26 22:03:27 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_actors_count(self, o):
|
|
|
|
return getattr(o, "actors_count", 0)
|
|
|
|
|
|
|
|
def get_outbox_activities_count(self, o):
|
|
|
|
return getattr(o, "outbox_activities_count", 0)
|
2019-01-03 10:47:29 +00:00
|
|
|
|
|
|
|
|
2019-06-17 06:48:05 +00:00
|
|
|
class ManageDomainUpdateSerializer(ManageDomainSerializer):
|
|
|
|
class Meta(ManageDomainSerializer.Meta):
|
|
|
|
read_only_fields = ["name"] + ManageDomainSerializer.Meta.read_only_fields
|
|
|
|
|
|
|
|
|
2019-01-09 13:18:32 +00:00
|
|
|
class ManageDomainActionSerializer(common_serializers.ActionSerializer):
|
2019-06-26 08:22:53 +00:00
|
|
|
actions = [
|
|
|
|
common_serializers.Action("purge", allow_all=False),
|
|
|
|
common_serializers.Action("allow_list_add", allow_all=True),
|
|
|
|
common_serializers.Action("allow_list_remove", allow_all=True),
|
|
|
|
]
|
2019-01-09 13:18:32 +00:00
|
|
|
filterset_class = filters.ManageDomainFilterSet
|
|
|
|
pk_field = "name"
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_purge(self, objects):
|
2019-12-04 08:55:07 +00:00
|
|
|
ids = objects.values_list("pk", flat=True).order_by("pk")
|
2019-01-09 13:18:32 +00:00
|
|
|
common_utils.on_commit(federation_tasks.purge_actors.delay, domains=list(ids))
|
|
|
|
|
2019-06-26 08:22:53 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def handle_allow_list_add(self, objects):
|
|
|
|
objects.update(allowed=True)
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_allow_list_remove(self, objects):
|
|
|
|
objects.update(allowed=False)
|
|
|
|
|
2019-01-09 13:18:32 +00:00
|
|
|
|
2019-04-17 12:17:59 +00:00
|
|
|
class ManageBaseActorSerializer(serializers.ModelSerializer):
|
2019-08-27 12:00:33 +00:00
|
|
|
is_local = serializers.SerializerMethodField()
|
|
|
|
|
2019-01-03 10:47:29 +00:00
|
|
|
class Meta:
|
|
|
|
model = federation_models.Actor
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"url",
|
|
|
|
"fid",
|
|
|
|
"preferred_username",
|
2019-01-03 16:10:02 +00:00
|
|
|
"full_username",
|
2019-01-03 10:47:29 +00:00
|
|
|
"domain",
|
|
|
|
"name",
|
|
|
|
"summary",
|
|
|
|
"type",
|
|
|
|
"creation_date",
|
|
|
|
"last_fetch_date",
|
|
|
|
"inbox_url",
|
|
|
|
"outbox_url",
|
|
|
|
"shared_inbox_url",
|
|
|
|
"manually_approves_followers",
|
2019-08-27 12:00:33 +00:00
|
|
|
"is_local",
|
2019-04-17 12:17:59 +00:00
|
|
|
]
|
|
|
|
read_only_fields = ["creation_date", "instance_policy"]
|
|
|
|
|
2019-08-27 12:00:33 +00:00
|
|
|
def get_is_local(self, o):
|
|
|
|
return o.domain_id == settings.FEDERATION_HOSTNAME
|
|
|
|
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class ManageActorSerializer(ManageBaseActorSerializer):
|
|
|
|
uploads_count = serializers.SerializerMethodField()
|
|
|
|
user = ManageUserSerializer()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = federation_models.Actor
|
|
|
|
fields = ManageBaseActorSerializer.Meta.fields + [
|
2019-01-03 10:47:29 +00:00
|
|
|
"uploads_count",
|
2019-01-03 16:10:02 +00:00
|
|
|
"user",
|
2019-01-07 08:45:53 +00:00
|
|
|
"instance_policy",
|
2019-01-03 10:47:29 +00:00
|
|
|
]
|
2019-01-07 08:45:53 +00:00
|
|
|
read_only_fields = ["creation_date", "instance_policy"]
|
2019-01-03 10:47:29 +00:00
|
|
|
|
|
|
|
def get_uploads_count(self, o):
|
|
|
|
return getattr(o, "uploads_count", 0)
|
2019-01-07 08:45:53 +00:00
|
|
|
|
|
|
|
|
2019-01-09 13:18:32 +00:00
|
|
|
class ManageActorActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("purge", allow_all=False)]
|
|
|
|
filterset_class = filters.ManageActorFilterSet
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_purge(self, objects):
|
|
|
|
ids = objects.values_list("id", flat=True)
|
|
|
|
common_utils.on_commit(federation_tasks.purge_actors.delay, ids=list(ids))
|
|
|
|
|
|
|
|
|
2019-01-07 08:45:53 +00:00
|
|
|
class TargetSerializer(serializers.Serializer):
|
|
|
|
type = serializers.ChoiceField(choices=["domain", "actor"])
|
|
|
|
id = serializers.CharField()
|
|
|
|
|
|
|
|
def to_representation(self, value):
|
|
|
|
if value["type"] == "domain":
|
|
|
|
return {"type": "domain", "id": value["obj"].name}
|
|
|
|
if value["type"] == "actor":
|
|
|
|
return {"type": "actor", "id": value["obj"].full_username}
|
|
|
|
|
|
|
|
def to_internal_value(self, value):
|
|
|
|
if value["type"] == "domain":
|
|
|
|
field = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=federation_models.Domain.objects.external()
|
|
|
|
)
|
|
|
|
if value["type"] == "actor":
|
|
|
|
field = federation_fields.ActorRelatedField()
|
|
|
|
value["obj"] = field.to_internal_value(value["id"])
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
class ManageInstancePolicySerializer(serializers.ModelSerializer):
|
|
|
|
target = TargetSerializer()
|
|
|
|
actor = federation_fields.ActorRelatedField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = moderation_models.InstancePolicy
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"target",
|
|
|
|
"creation_date",
|
|
|
|
"actor",
|
|
|
|
"summary",
|
|
|
|
"is_active",
|
|
|
|
"block_all",
|
|
|
|
"silence_activity",
|
|
|
|
"silence_notifications",
|
|
|
|
"reject_media",
|
|
|
|
]
|
|
|
|
|
|
|
|
read_only_fields = ["uuid", "id", "creation_date", "actor", "target"]
|
|
|
|
|
|
|
|
def validate(self, data):
|
2019-01-09 13:18:32 +00:00
|
|
|
try:
|
|
|
|
target = data.pop("target")
|
|
|
|
except KeyError:
|
|
|
|
# partial update
|
|
|
|
return data
|
2019-01-07 08:45:53 +00:00
|
|
|
if target["type"] == "domain":
|
|
|
|
data["target_domain"] = target["obj"]
|
|
|
|
if target["type"] == "actor":
|
|
|
|
data["target_actor"] = target["obj"]
|
|
|
|
|
|
|
|
return data
|
2019-01-09 13:18:32 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
instance = super().save(*args, **kwargs)
|
2019-01-10 10:02:09 +00:00
|
|
|
need_purge = self.instance.is_active and (
|
|
|
|
self.instance.block_all or self.instance.reject_media
|
|
|
|
)
|
2019-01-09 13:18:32 +00:00
|
|
|
if need_purge:
|
2019-01-10 10:02:09 +00:00
|
|
|
only = []
|
|
|
|
if self.instance.reject_media:
|
|
|
|
only.append("media")
|
2019-01-09 13:18:32 +00:00
|
|
|
target = instance.target
|
|
|
|
if target["type"] == "domain":
|
|
|
|
common_utils.on_commit(
|
2019-01-10 10:02:09 +00:00
|
|
|
federation_tasks.purge_actors.delay,
|
|
|
|
domains=[target["obj"].pk],
|
|
|
|
only=only,
|
2019-01-09 13:18:32 +00:00
|
|
|
)
|
|
|
|
if target["type"] == "actor":
|
|
|
|
common_utils.on_commit(
|
2019-01-10 10:02:09 +00:00
|
|
|
federation_tasks.purge_actors.delay,
|
|
|
|
ids=[target["obj"].pk],
|
|
|
|
only=only,
|
2019-01-09 13:18:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return instance
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageBaseArtistSerializer(serializers.ModelSerializer):
|
|
|
|
domain = serializers.CharField(source="domain_name")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Artist
|
|
|
|
fields = ["id", "fid", "mbid", "name", "creation_date", "domain", "is_local"]
|
|
|
|
|
|
|
|
|
|
|
|
class ManageBaseAlbumSerializer(serializers.ModelSerializer):
|
|
|
|
cover = music_serializers.cover_field
|
|
|
|
domain = serializers.CharField(source="domain_name")
|
2020-07-06 08:16:45 +00:00
|
|
|
tracks_count = serializers.SerializerMethodField()
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Album
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"fid",
|
|
|
|
"mbid",
|
|
|
|
"title",
|
|
|
|
"creation_date",
|
|
|
|
"release_date",
|
|
|
|
"cover",
|
|
|
|
"domain",
|
|
|
|
"is_local",
|
2020-07-06 08:16:45 +00:00
|
|
|
"tracks_count",
|
2019-04-17 12:17:59 +00:00
|
|
|
]
|
|
|
|
|
2020-07-06 08:16:45 +00:00
|
|
|
def get_tracks_count(self, o):
|
|
|
|
return getattr(o, "_tracks_count", None)
|
|
|
|
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class ManageNestedTrackSerializer(serializers.ModelSerializer):
|
|
|
|
domain = serializers.CharField(source="domain_name")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Track
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"fid",
|
|
|
|
"mbid",
|
|
|
|
"title",
|
|
|
|
"creation_date",
|
|
|
|
"position",
|
|
|
|
"disc_number",
|
|
|
|
"domain",
|
|
|
|
"is_local",
|
|
|
|
"copyright",
|
|
|
|
"license",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ManageNestedAlbumSerializer(ManageBaseAlbumSerializer):
|
|
|
|
|
|
|
|
tracks_count = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Album
|
|
|
|
fields = ManageBaseAlbumSerializer.Meta.fields + ["tracks_count"]
|
|
|
|
|
|
|
|
def get_tracks_count(self, obj):
|
|
|
|
return getattr(obj, "tracks_count", None)
|
|
|
|
|
|
|
|
|
2020-01-14 13:00:08 +00:00
|
|
|
class ManageArtistSerializer(
|
|
|
|
music_serializers.OptionalDescriptionMixin, ManageBaseArtistSerializer
|
|
|
|
):
|
2019-04-17 12:17:59 +00:00
|
|
|
attributed_to = ManageBaseActorSerializer()
|
2019-07-25 08:50:23 +00:00
|
|
|
tags = serializers.SerializerMethodField()
|
2020-03-31 08:45:41 +00:00
|
|
|
tracks_count = serializers.SerializerMethodField()
|
|
|
|
albums_count = serializers.SerializerMethodField()
|
|
|
|
channel = serializers.SerializerMethodField()
|
2020-01-17 15:27:11 +00:00
|
|
|
cover = music_serializers.cover_field
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Artist
|
|
|
|
fields = ManageBaseArtistSerializer.Meta.fields + [
|
2020-03-31 08:45:41 +00:00
|
|
|
"tracks_count",
|
|
|
|
"albums_count",
|
2019-04-17 12:17:59 +00:00
|
|
|
"attributed_to",
|
2019-07-25 08:50:23 +00:00
|
|
|
"tags",
|
2020-01-17 15:27:11 +00:00
|
|
|
"cover",
|
2020-03-31 08:45:41 +00:00
|
|
|
"channel",
|
|
|
|
"content_category",
|
2019-04-17 12:17:59 +00:00
|
|
|
]
|
|
|
|
|
2020-03-31 08:45:41 +00:00
|
|
|
def get_tracks_count(self, obj):
|
|
|
|
return getattr(obj, "_tracks_count", None)
|
|
|
|
|
|
|
|
def get_albums_count(self, obj):
|
|
|
|
return getattr(obj, "_albums_count", None)
|
|
|
|
|
2019-07-25 08:50:23 +00:00
|
|
|
def get_tags(self, obj):
|
|
|
|
tagged_items = getattr(obj, "_prefetched_tagged_items", [])
|
|
|
|
return [ti.tag.name for ti in tagged_items]
|
|
|
|
|
2020-03-31 08:45:41 +00:00
|
|
|
def get_channel(self, obj):
|
|
|
|
if "channel" in obj._state.fields_cache and obj.get_channel():
|
|
|
|
return str(obj.channel.uuid)
|
|
|
|
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class ManageNestedArtistSerializer(ManageBaseArtistSerializer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-01-14 13:00:08 +00:00
|
|
|
class ManageAlbumSerializer(
|
|
|
|
music_serializers.OptionalDescriptionMixin, ManageBaseAlbumSerializer
|
|
|
|
):
|
2019-04-17 12:17:59 +00:00
|
|
|
attributed_to = ManageBaseActorSerializer()
|
|
|
|
artist = ManageNestedArtistSerializer()
|
2019-07-25 08:50:23 +00:00
|
|
|
tags = serializers.SerializerMethodField()
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Album
|
|
|
|
fields = ManageBaseAlbumSerializer.Meta.fields + [
|
|
|
|
"artist",
|
|
|
|
"attributed_to",
|
2019-07-25 08:50:23 +00:00
|
|
|
"tags",
|
2020-07-09 09:51:58 +00:00
|
|
|
"tracks_count",
|
2019-04-17 12:17:59 +00:00
|
|
|
]
|
|
|
|
|
2020-07-09 09:51:58 +00:00
|
|
|
def get_tracks_count(self, o):
|
|
|
|
return len(o.tracks.all())
|
|
|
|
|
2019-07-25 08:50:23 +00:00
|
|
|
def get_tags(self, obj):
|
|
|
|
tagged_items = getattr(obj, "_prefetched_tagged_items", [])
|
|
|
|
return [ti.tag.name for ti in tagged_items]
|
|
|
|
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class ManageTrackAlbumSerializer(ManageBaseAlbumSerializer):
|
|
|
|
artist = ManageNestedArtistSerializer()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Album
|
|
|
|
fields = ManageBaseAlbumSerializer.Meta.fields + ["artist"]
|
|
|
|
|
|
|
|
|
2020-01-14 13:00:08 +00:00
|
|
|
class ManageTrackSerializer(
|
|
|
|
music_serializers.OptionalDescriptionMixin, ManageNestedTrackSerializer
|
|
|
|
):
|
2019-04-17 12:17:59 +00:00
|
|
|
artist = ManageNestedArtistSerializer()
|
|
|
|
album = ManageTrackAlbumSerializer()
|
|
|
|
attributed_to = ManageBaseActorSerializer()
|
|
|
|
uploads_count = serializers.SerializerMethodField()
|
2019-07-25 08:50:23 +00:00
|
|
|
tags = serializers.SerializerMethodField()
|
2020-01-17 15:27:11 +00:00
|
|
|
cover = music_serializers.cover_field
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Track
|
|
|
|
fields = ManageNestedTrackSerializer.Meta.fields + [
|
|
|
|
"artist",
|
|
|
|
"album",
|
|
|
|
"attributed_to",
|
|
|
|
"uploads_count",
|
2019-07-25 08:50:23 +00:00
|
|
|
"tags",
|
2020-01-17 15:27:11 +00:00
|
|
|
"cover",
|
2019-04-17 12:17:59 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_uploads_count(self, obj):
|
|
|
|
return getattr(obj, "uploads_count", None)
|
|
|
|
|
2019-07-25 08:50:23 +00:00
|
|
|
def get_tags(self, obj):
|
|
|
|
tagged_items = getattr(obj, "_prefetched_tagged_items", [])
|
|
|
|
return [ti.tag.name for ti in tagged_items]
|
|
|
|
|
2019-04-17 12:17:59 +00:00
|
|
|
|
|
|
|
class ManageTrackActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("delete", allow_all=False)]
|
|
|
|
filterset_class = filters.ManageTrackFilterSet
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_delete(self, objects):
|
|
|
|
return objects.delete()
|
|
|
|
|
|
|
|
|
|
|
|
class ManageAlbumActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("delete", allow_all=False)]
|
|
|
|
filterset_class = filters.ManageAlbumFilterSet
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_delete(self, objects):
|
|
|
|
return objects.delete()
|
|
|
|
|
|
|
|
|
|
|
|
class ManageArtistActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("delete", allow_all=False)]
|
|
|
|
filterset_class = filters.ManageArtistFilterSet
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_delete(self, objects):
|
|
|
|
return objects.delete()
|
2019-04-19 10:05:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageLibraryActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("delete", allow_all=False)]
|
|
|
|
filterset_class = filters.ManageLibraryFilterSet
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_delete(self, objects):
|
|
|
|
return objects.delete()
|
|
|
|
|
|
|
|
|
|
|
|
class ManageUploadActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("delete", allow_all=False)]
|
|
|
|
filterset_class = filters.ManageUploadFilterSet
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_delete(self, objects):
|
|
|
|
return objects.delete()
|
|
|
|
|
|
|
|
|
|
|
|
class ManageLibrarySerializer(serializers.ModelSerializer):
|
|
|
|
domain = serializers.CharField(source="domain_name")
|
|
|
|
actor = ManageBaseActorSerializer()
|
|
|
|
uploads_count = serializers.SerializerMethodField()
|
|
|
|
followers_count = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Library
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"fid",
|
|
|
|
"url",
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
"domain",
|
|
|
|
"is_local",
|
|
|
|
"creation_date",
|
|
|
|
"privacy_level",
|
|
|
|
"uploads_count",
|
|
|
|
"followers_count",
|
|
|
|
"followers_url",
|
|
|
|
"actor",
|
|
|
|
]
|
2019-07-25 12:14:44 +00:00
|
|
|
read_only_fields = [
|
|
|
|
"fid",
|
|
|
|
"uuid",
|
|
|
|
"id",
|
|
|
|
"url",
|
|
|
|
"domain",
|
|
|
|
"actor",
|
|
|
|
"creation_date",
|
|
|
|
]
|
2019-04-19 10:05:13 +00:00
|
|
|
|
|
|
|
def get_uploads_count(self, obj):
|
|
|
|
return getattr(obj, "_uploads_count", obj.uploads_count)
|
|
|
|
|
|
|
|
def get_followers_count(self, obj):
|
|
|
|
return getattr(obj, "followers_count", None)
|
|
|
|
|
|
|
|
|
|
|
|
class ManageNestedLibrarySerializer(serializers.ModelSerializer):
|
|
|
|
domain = serializers.CharField(source="domain_name")
|
|
|
|
actor = ManageBaseActorSerializer()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Library
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"fid",
|
|
|
|
"url",
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
"domain",
|
|
|
|
"is_local",
|
|
|
|
"creation_date",
|
|
|
|
"privacy_level",
|
|
|
|
"followers_url",
|
|
|
|
"actor",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ManageUploadSerializer(serializers.ModelSerializer):
|
|
|
|
track = ManageNestedTrackSerializer()
|
|
|
|
library = ManageNestedLibrarySerializer()
|
|
|
|
domain = serializers.CharField(source="domain_name")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Upload
|
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"fid",
|
|
|
|
"domain",
|
|
|
|
"is_local",
|
|
|
|
"audio_file",
|
|
|
|
"listen_url",
|
|
|
|
"source",
|
|
|
|
"filename",
|
|
|
|
"mimetype",
|
|
|
|
"duration",
|
|
|
|
"mimetype",
|
|
|
|
"bitrate",
|
|
|
|
"size",
|
|
|
|
"creation_date",
|
|
|
|
"accessed_date",
|
|
|
|
"modification_date",
|
|
|
|
"metadata",
|
|
|
|
"import_date",
|
|
|
|
"import_details",
|
|
|
|
"import_status",
|
|
|
|
"import_metadata",
|
|
|
|
"import_reference",
|
|
|
|
"track",
|
|
|
|
"library",
|
|
|
|
)
|
2019-07-24 08:24:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageTagSerializer(ManageBaseAlbumSerializer):
|
|
|
|
|
|
|
|
tracks_count = serializers.SerializerMethodField()
|
|
|
|
albums_count = serializers.SerializerMethodField()
|
|
|
|
artists_count = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = tags_models.Tag
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"creation_date",
|
|
|
|
"tracks_count",
|
|
|
|
"albums_count",
|
|
|
|
"artists_count",
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_tracks_count(self, obj):
|
|
|
|
return getattr(obj, "_tracks_count", None)
|
|
|
|
|
|
|
|
def get_albums_count(self, obj):
|
|
|
|
return getattr(obj, "_albums_count", None)
|
|
|
|
|
|
|
|
def get_artists_count(self, obj):
|
|
|
|
return getattr(obj, "_artists_count", None)
|
2019-07-25 08:17:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageTagActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("delete", allow_all=False)]
|
|
|
|
filterset_class = filters.ManageTagFilterSet
|
|
|
|
pk_field = "name"
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def handle_delete(self, objects):
|
|
|
|
return objects.delete()
|
2019-08-26 12:37:48 +00:00
|
|
|
|
|
|
|
|
2019-08-29 12:12:26 +00:00
|
|
|
class ManageBaseNoteSerializer(serializers.ModelSerializer):
|
|
|
|
author = ManageBaseActorSerializer(required=False, read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = moderation_models.Note
|
|
|
|
fields = ["id", "uuid", "creation_date", "summary", "author"]
|
|
|
|
read_only_fields = ["uuid", "creation_date", "author"]
|
|
|
|
|
|
|
|
|
|
|
|
class ManageNoteSerializer(ManageBaseNoteSerializer):
|
2019-08-29 12:53:20 +00:00
|
|
|
target = common_fields.GenericRelation(moderation_utils.NOTE_TARGET_FIELDS)
|
2019-08-29 12:12:26 +00:00
|
|
|
|
|
|
|
class Meta(ManageBaseNoteSerializer.Meta):
|
2019-08-29 12:53:20 +00:00
|
|
|
fields = ManageBaseNoteSerializer.Meta.fields + ["target"]
|
2019-08-29 12:12:26 +00:00
|
|
|
|
|
|
|
|
2019-08-26 12:37:48 +00:00
|
|
|
class ManageReportSerializer(serializers.ModelSerializer):
|
|
|
|
assigned_to = ManageBaseActorSerializer()
|
|
|
|
target_owner = ManageBaseActorSerializer()
|
|
|
|
submitter = ManageBaseActorSerializer()
|
|
|
|
target = moderation_serializers.TARGET_FIELD
|
2019-08-29 12:12:26 +00:00
|
|
|
notes = serializers.SerializerMethodField()
|
2019-08-26 12:37:48 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = moderation_models.Report
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"fid",
|
|
|
|
"creation_date",
|
|
|
|
"handled_date",
|
|
|
|
"summary",
|
|
|
|
"type",
|
|
|
|
"target",
|
|
|
|
"target_state",
|
|
|
|
"is_handled",
|
|
|
|
"assigned_to",
|
|
|
|
"target_owner",
|
|
|
|
"submitter",
|
|
|
|
"submitter_email",
|
2019-08-29 12:12:26 +00:00
|
|
|
"notes",
|
2019-08-26 12:37:48 +00:00
|
|
|
]
|
|
|
|
read_only_fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"fid",
|
|
|
|
"submitter",
|
|
|
|
"submitter_email",
|
|
|
|
"creation_date",
|
|
|
|
"handled_date",
|
|
|
|
"target",
|
|
|
|
"target_state",
|
|
|
|
"target_owner",
|
|
|
|
"summary",
|
|
|
|
]
|
2019-08-29 09:45:41 +00:00
|
|
|
|
2019-08-29 12:12:26 +00:00
|
|
|
def get_notes(self, o):
|
2019-08-29 12:53:20 +00:00
|
|
|
notes = getattr(o, "_prefetched_notes", [])
|
2019-08-29 12:12:26 +00:00
|
|
|
return ManageBaseNoteSerializer(notes, many=True).data
|
2020-03-18 10:57:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageUserRequestSerializer(serializers.ModelSerializer):
|
|
|
|
assigned_to = ManageBaseActorSerializer()
|
|
|
|
submitter = ManageBaseActorSerializer()
|
|
|
|
notes = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = moderation_models.UserRequest
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"creation_date",
|
|
|
|
"handled_date",
|
|
|
|
"type",
|
|
|
|
"status",
|
|
|
|
"assigned_to",
|
|
|
|
"submitter",
|
|
|
|
"notes",
|
|
|
|
"metadata",
|
|
|
|
]
|
|
|
|
read_only_fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"submitter",
|
|
|
|
"creation_date",
|
|
|
|
"handled_date",
|
|
|
|
"metadata",
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_notes(self, o):
|
|
|
|
notes = getattr(o, "_prefetched_notes", [])
|
|
|
|
return ManageBaseNoteSerializer(notes, many=True).data
|
2020-03-31 08:45:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageChannelSerializer(serializers.ModelSerializer):
|
|
|
|
attributed_to = ManageBaseActorSerializer()
|
|
|
|
actor = ManageBaseActorSerializer()
|
|
|
|
artist = ManageArtistSerializer()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = audio_models.Channel
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"uuid",
|
|
|
|
"creation_date",
|
|
|
|
"artist",
|
|
|
|
"attributed_to",
|
|
|
|
"actor",
|
|
|
|
"rss_url",
|
|
|
|
"metadata",
|
|
|
|
]
|
|
|
|
read_only_fields = fields
|