Browse Source

Webservice contador de produtos. Gertiq #180550

pull/173/head 3.0.80
Sesóstris Vieira 9 months ago
parent
commit
cbd7e7d24a
  1. 6
      sigi/apps/servicos/api_urls.py
  2. 6
      sigi/apps/servicos/serializers.py
  3. 31
      sigi/apps/servicos/views.py
  4. 1
      sigi/urls.py

6
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()),
]

6
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()

31
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

1
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")),

Loading…
Cancel
Save