from django.db.models import Count
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from debtors.api.filters import DebtorFilter
from debtors.api.serializers import DebtorSerializer, InvoiceSerializer
from debtors.models.debtors import Debtor
from debtors.models.invoices import Invoice


class BaseGenericViewSet(GenericViewSet):
    permission_classes = (IsAdminUser,)


class DebtorViewSet(BaseGenericViewSet):
    """
    A viewset for listing Debtors
    """

    queryset = Debtor.objects.present()
    serializer_class = DebtorSerializer
    filterset_fields = ('total_invoices', 'invoices_status',)
    filterset_class = DebtorFilter
    filter_backends = (DjangoFilterBackend,)

    def list(self, request):
        self.queryset = self.queryset.annotate(
            total_invoices=Count('invoices'),
        )
        # apply the filters
        qs = self.filter_queryset(self.queryset)
        data = self.serializer_class(qs, many=True,
                                     context={'request': request}).data
        return Response(data=data)


class InvoiceViewSet(BaseGenericViewSet):
    """
    A ViewSet for listing invoices
    """
    queryset = Invoice.objects.present()
    serializer_class = InvoiceSerializer
    filterset_fields = ('debtor__email', 'status', 'amount', 'due_date',)
    ordering_fields = filterset_fields
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter,)

    def list(self, request):
        # apply the filters
        qs = self.filter_queryset(self.queryset)
        data = self.serializer_class(qs, many=True,
                                     context={'request': request}).data
        return Response(data=data)

from django.db.models import Count
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from debtors.api.filters import DebtorFilter
from debtors.api.serializers import DebtorSerializer, InvoiceSerializer
from debtors.models.debtors import Debtor
from debtors.models.invoices import Invoice


class BaseGenericViewSet(GenericViewSet):
    permission_classes = (IsAdminUser,)


class DebtorViewSet(BaseGenericViewSet):
    """
    A viewset for listing Debtors
    """

    queryset = Debtor.objects.present()
    serializer_class = DebtorSerializer
    filterset_fields = ('total_invoices', 'invoices_status',)
    filterset_class = DebtorFilter
    filter_backends = (DjangoFilterBackend,)

    def list(self, request):
        self.queryset = self.queryset.annotate(
            total_invoices=Count('invoices'),
        )
        # apply the filters
        qs = self.filter_queryset(self.queryset)
        data = self.serializer_class(qs, many=True,
                                     context={'request': request}).data
        return Response(data=data)


class InvoiceViewSet(BaseGenericViewSet):
    """
    A ViewSet for listing invoices
    """
    queryset = Invoice.objects.present()
    serializer_class = InvoiceSerializer
    filterset_fields = ('debtor__email', 'status', 'amount', 'due_date',)
    ordering_fields = filterset_fields
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter,)

    def list(self, request):
        # apply the filters
        qs = self.filter_queryset(self.queryset)
        data = self.serializer_class(qs, many=True,
                                     context={'request': request}).data
        return Response(data=data)

A viewset for listing Debtors # apply the filters A ViewSet for listing invoices # apply the filters