diff --git a/sigi/apps/servicos/api_urls.py b/sigi/apps/servicos/api_urls.py new file mode 100644 index 0000000..2991c40 --- /dev/null +++ b/sigi/apps/servicos/api_urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from sigi.apps.servicos import views + +urlpatterns = [ + path("resumoprodutos/", views.ResumoProdutosApiView.as_view()), +] diff --git a/sigi/apps/servicos/serializers.py b/sigi/apps/servicos/serializers.py new file mode 100644 index 0000000..c01f733 --- /dev/null +++ b/sigi/apps/servicos/serializers.py @@ -0,0 +1,6 @@ +from rest_framework import serializers + + +class ProdutosSerializer(serializers.Serializer): + produto = serializers.CharField(max_length=40) + quantidade = serializers.IntegerField() diff --git a/sigi/apps/servicos/views.py b/sigi/apps/servicos/views.py index 4562a4f..8ab2820 100644 --- a/sigi/apps/servicos/views.py +++ b/sigi/apps/servicos/views.py @@ -1,5 +1,5 @@ import csv -from django.db.models import Q, Prefetch, Count +from django.db.models import Q, Prefetch, Count, F, Value, Case, When from django.http import HttpResponse from django.shortcuts import render from django.utils.translation import gettext as _ @@ -7,11 +7,13 @@ from django.views.decorators.clickjacking import xframe_options_exempt from django.views.generic import ListView from import_export import resources from import_export.fields import Field +from rest_framework import generics from sigi.apps.casas.models import Orgao from sigi.apps.contatos.models import UnidadeFederativa from sigi.apps.convenios.models import Convenio from sigi.apps.eventos.models import Evento, TipoEvento from sigi.apps.servicos.models import Servico +from sigi.apps.servicos.serializers import ProdutosSerializer from sigi.apps.utils import to_ascii @@ -143,3 +145,30 @@ class CasasAtendidasListView(ListView): @xframe_options_exempt def dispatch(self, *args, **kwargs): return super().dispatch(*args, **kwargs) + + +class ResumoProdutosApiView(generics.ListAPIView): + """ + Lista os produtos Interlegis com a quantidade atual de instâncias + """ + + serializer_class = ProdutosSerializer + + def get_queryset(self): + query_case = Case( + *[ + When(tipo_evento__categoria=c[0], then=Value(c[1])) + for c in TipoEvento.CATEGORIA_CHOICES + ] + ) + produtos = ( + Servico.objects.filter(data_desativacao=None) + .values(produto=F("tipo_servico__nome")) + .annotate(quantidade=Count("id")) + ).union( + Evento.objects.order_by() + .filter(status=Evento.STATUS_REALIZADO) + .values(produto=query_case) + .annotate(quantidade=Count("id")) + ) + return produtos diff --git a/sigi/urls.py b/sigi/urls.py index a4038bf..4b5a359 100644 --- a/sigi/urls.py +++ b/sigi/urls.py @@ -68,6 +68,7 @@ urlpatterns = [ ), path("api/casas/", include("sigi.apps.casas.api_urls")), path("api/eventos/", include("sigi.apps.eventos.api_urls")), + path("api/servicos/", include("sigi.apps.servicos.api_urls")), path("tinymce/", include("tinymce.urls")), path("accounts/", include("sigi.apps.home.accounts_urls")), path("", include("sigi.apps.home.urls")),