api.py 1.96 KB
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets, filters, mixins
from rest_framework.permissions import IsAuthenticated

from core.mixins import AuditoriaMixin
from rest_framework.decorators import action
from core.serializers import ActionSerializer
from rest_framework.response import Response

from .filters import EdictoFilter, PrecioFilter
from .models import Edicto, Precio
from .permissions import IsAdminOrAuthorized
from .serializers import EdictoSerializer, PrecioSerializer


class EdictoViewSet(AuditoriaMixin, mixins.CreateModelMixin,
                    mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.ListModelMixin,
                    viewsets.GenericViewSet):

    serializer_class = EdictoSerializer
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter)
    filterset_class = EdictoFilter
    queryset = Edicto.objects.all()
    ordering_fields = ('fecha_creacion', 'id', 'estado')
    ordering = ('fecha_publicacion',)
    lookup_field = 'uuid'

    @action(
        methods=['GET'],
        detail=True,
        url_path='obtener-historial',
        serializer_class=ActionSerializer
    )
    def obtener_historial(self, request, uuid):
        action_objects_id = list(Edicto.objects.filter(uuid=uuid).values_list('id', flat=True))
        serializer = self.obtener_historial_acciones(
            content_type="edicto",
            action_objects_id=action_objects_id,
            request=request
        )
        return Response(serializer.data)


class PrecioViewSet(AuditoriaMixin, viewsets.ReadOnlyModelViewSet):
    serializer_class = PrecioSerializer
    permission_classes = [IsAuthenticated, IsAdminOrAuthorized]
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter)
    filterset_class = PrecioFilter
    ordering_fields = ('usuario',)
    ordering = 'usuario'
    queryset = Precio.objects.all()