2019-02-14 09:49:06 +00:00
|
|
|
from django.db import IntegrityError
|
|
|
|
|
|
|
|
from rest_framework import mixins
|
|
|
|
from rest_framework import response
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework import viewsets
|
|
|
|
|
|
|
|
from . import models
|
|
|
|
from . import serializers
|
|
|
|
|
|
|
|
|
|
|
|
class UserFilterViewSet(
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
):
|
|
|
|
lookup_field = "uuid"
|
|
|
|
queryset = (
|
|
|
|
models.UserFilter.objects.all()
|
|
|
|
.order_by("-creation_date")
|
|
|
|
.select_related("target_artist")
|
|
|
|
)
|
|
|
|
serializer_class = serializers.UserFilterSerializer
|
2019-03-25 16:02:51 +00:00
|
|
|
required_scope = "filters"
|
2019-02-14 09:49:06 +00:00
|
|
|
ordering_fields = ("creation_date",)
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return super().create(request, *args, **kwargs)
|
|
|
|
except IntegrityError:
|
|
|
|
content = {"detail": "A content filter already exists for this object"}
|
|
|
|
return response.Response(content, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
return qs.filter(user=self.request.user)
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.save(user=self.request.user)
|
2019-08-22 09:30:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReportsViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
|
|
|
|
lookup_field = "uuid"
|
|
|
|
queryset = models.Report.objects.all().order_by("-creation_date")
|
|
|
|
serializer_class = serializers.ReportSerializer
|
|
|
|
required_scope = "reports"
|
|
|
|
ordering_fields = ("creation_date",)
|
|
|
|
anonymous_policy = "setting"
|
|
|
|
anonymous_scopes = {"write:reports"}
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
context = super().get_serializer_context()
|
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
context["submitter"] = self.request.user.actor
|
|
|
|
return context
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
submitter = None
|
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
submitter = self.request.user.actor
|
|
|
|
serializer.save(submitter=submitter)
|