Showing
4 changed files
with
77 additions
and
51 deletions
@@ -6,6 +6,7 @@ from django.conf import settings | @@ -6,6 +6,7 @@ from django.conf import settings | ||
6 | 6 | ||
7 | import datetime | 7 | import datetime |
8 | 8 | ||
9 | +from actstream.models import actor_stream | ||
9 | from actstream.models import Action | 10 | from actstream.models import Action |
10 | from django.http import Http404 | 11 | from django.http import Http404 |
11 | from django_filters.rest_framework import DjangoFilterBackend | 12 | from django_filters.rest_framework import DjangoFilterBackend |
@@ -38,31 +39,31 @@ class AuditoriaViewSet(ReadOnlyModelViewSet): | @@ -38,31 +39,31 @@ class AuditoriaViewSet(ReadOnlyModelViewSet): | ||
38 | serializer_class = ActionSerializer | 39 | serializer_class = ActionSerializer |
39 | filter_backends = (DjangoFilterBackend, filters.OrderingFilter) | 40 | filter_backends = (DjangoFilterBackend, filters.OrderingFilter) |
40 | ordering = '-timestamp' | 41 | ordering = '-timestamp' |
41 | - # | ||
42 | - # def get_queryset(self): | ||
43 | - # queryset = super().get_queryset() | ||
44 | - # | ||
45 | - # if not self.action == 'list': | ||
46 | - # return queryset | ||
47 | - # | ||
48 | - # # en el caso de que la accion sea listar, controlar que se filtre por fecha obligatoriamente | ||
49 | - # usuario_id = self.request.GET.get('u_id', None) | ||
50 | - # fecha_desde = self.request.GET.get('fecha_desde', None) | ||
51 | - # fecha_hasta = self.request.GET.get('fecha_hasta', None) | ||
52 | - # | ||
53 | - # if not fecha_desde: | ||
54 | - # return queryset.none() | ||
55 | - # | ||
56 | - # if not fecha_hasta or fecha_hasta < fecha_desde: | ||
57 | - # fecha_hasta = datetime.datetime.now() | ||
58 | - # | ||
59 | - # if usuario_id: | ||
60 | - # try: | ||
61 | - # usuario = get_object_or_404(Usuario, id=usuario_id) | ||
62 | - # queryset = usuario.actor_actions.public(timestamp__date__range=(fecha_desde, fecha_hasta)) | ||
63 | - # except Http404: | ||
64 | - # return queryset.none() | ||
65 | - # else: | ||
66 | - # queryset = Action.objects.public(timestamp__date__range=(fecha_desde, fecha_hasta)) | ||
67 | - # | ||
68 | - # return queryset | 42 | + |
43 | + def get_queryset(self): | ||
44 | + queryset = super().get_queryset() | ||
45 | + | ||
46 | + if not self.action == 'list': | ||
47 | + return queryset | ||
48 | + | ||
49 | + # en el caso de que la accion sea listar, controlar que se filtre por fecha obligatoriamente | ||
50 | + usuario_id = self.request.GET.get('usuario_id', None) | ||
51 | + fecha_desde = self.request.GET.get('fecha_desde', None) | ||
52 | + fecha_hasta = self.request.GET.get('fecha_hasta', None) | ||
53 | + | ||
54 | + if not fecha_desde: | ||
55 | + return queryset.none() | ||
56 | + | ||
57 | + if not fecha_hasta or fecha_hasta < fecha_desde: | ||
58 | + fecha_hasta = datetime.datetime.now() | ||
59 | + | ||
60 | + if usuario_id: | ||
61 | + try: | ||
62 | + usuario = get_object_or_404(Usuario, id=usuario_id) | ||
63 | + queryset = usuario.actor_actions.public(timestamp__date__range=(fecha_desde, fecha_hasta)) | ||
64 | + except Http404: | ||
65 | + return queryset.none() | ||
66 | + else: | ||
67 | + queryset = Action.objects.public(timestamp__date__range=(fecha_desde, fecha_hasta)) | ||
68 | + | ||
69 | + return queryset |
@@ -9,29 +9,29 @@ from core.constants import iniciado | @@ -9,29 +9,29 @@ from core.constants import iniciado | ||
9 | from core.serializers import ActionSerializer | 9 | from core.serializers import ActionSerializer |
10 | 10 | ||
11 | 11 | ||
12 | -# class FiltroObligatorioMixin(object): | ||
13 | -# nombre_filtro_obligatorio: Optional[str] = None | ||
14 | -# | ||
15 | -# def get_queryset(self): | ||
16 | -# queryset = super().get_queryset() | ||
17 | -# | ||
18 | -# if not self.action == 'list': | ||
19 | -# return queryset | ||
20 | -# | ||
21 | -# # en el caso de que la accion sea listar, controlar que vengan los datos del filtro indicado | ||
22 | -# filtro = self.request.GET.get(self.get_nombre_filtro_obligatorio(), None) | ||
23 | -# if filtro: | ||
24 | -# return queryset | ||
25 | -# | ||
26 | -# return queryset.none() | ||
27 | -# | ||
28 | -# def get_nombre_filtro_obligatorio(self): | ||
29 | -# assert self.nombre_filtro_obligatorio is not None, ( | ||
30 | -# "Debe definir el atributo nombre_filtro_obligatorio a la clase '%s'" | ||
31 | -# % self.__class__.__name__ | ||
32 | -# ) | ||
33 | -# | ||
34 | -# return self.nombre_filtro_obligatorio | 12 | +class FiltroObligatorioMixin(object): |
13 | + nombre_filtro_obligatorio: Optional[str] = None | ||
14 | + | ||
15 | + def get_queryset(self): | ||
16 | + queryset = super().get_queryset() | ||
17 | + | ||
18 | + if not self.action == 'list': | ||
19 | + return queryset | ||
20 | + | ||
21 | + # en el caso de que la accion sea listar, controlar que vengan los datos del filtro indicado | ||
22 | + filtro = self.request.GET.get(self.get_nombre_filtro_obligatorio(), None) | ||
23 | + if filtro: | ||
24 | + return queryset | ||
25 | + | ||
26 | + return queryset.none() | ||
27 | + | ||
28 | + def get_nombre_filtro_obligatorio(self): | ||
29 | + assert self.nombre_filtro_obligatorio is not None, ( | ||
30 | + "Debe definir el atributo nombre_filtro_obligatorio a la clase '%s'" | ||
31 | + % self.__class__.__name__ | ||
32 | + ) | ||
33 | + | ||
34 | + return self.nombre_filtro_obligatorio | ||
35 | 35 | ||
36 | 36 | ||
37 | class AuditoriaMixin: | 37 | class AuditoriaMixin: |
@@ -3,6 +3,9 @@ from rest_framework import viewsets, filters, mixins | @@ -3,6 +3,9 @@ from rest_framework import viewsets, filters, mixins | ||
3 | from rest_framework.permissions import IsAuthenticated | 3 | from rest_framework.permissions import IsAuthenticated |
4 | 4 | ||
5 | from core.mixins import AuditoriaMixin | 5 | from core.mixins import AuditoriaMixin |
6 | +from rest_framework.decorators import action | ||
7 | +from core.serializers import ActionSerializer | ||
8 | +from rest_framework.response import Response | ||
6 | 9 | ||
7 | from .filters import EdictoFilter, PrecioFilter | 10 | from .filters import EdictoFilter, PrecioFilter |
8 | from .models import Edicto, Precio | 11 | from .models import Edicto, Precio |
@@ -25,6 +28,21 @@ class EdictoViewSet(AuditoriaMixin, mixins.CreateModelMixin, | @@ -25,6 +28,21 @@ class EdictoViewSet(AuditoriaMixin, mixins.CreateModelMixin, | ||
25 | ordering = ('fecha_publicacion',) | 28 | ordering = ('fecha_publicacion',) |
26 | lookup_field = 'uuid' | 29 | lookup_field = 'uuid' |
27 | 30 | ||
31 | + @action( | ||
32 | + methods=['GET'], | ||
33 | + detail=True, | ||
34 | + url_path='obtener-historial', | ||
35 | + serializer_class=ActionSerializer | ||
36 | + ) | ||
37 | + def obtener_historial(self, request, uuid): | ||
38 | + action_objects_id = list(Edicto.objects.filter(uuid=uuid).values_list('id', flat=True)) | ||
39 | + serializer = self.obtener_historial_acciones( | ||
40 | + content_type="edicto", | ||
41 | + action_objects_id=action_objects_id, | ||
42 | + request=request | ||
43 | + ) | ||
44 | + return Response(serializer.data) | ||
45 | + | ||
28 | 46 | ||
29 | class PrecioViewSet(AuditoriaMixin, viewsets.ReadOnlyModelViewSet): | 47 | class PrecioViewSet(AuditoriaMixin, viewsets.ReadOnlyModelViewSet): |
30 | serializer_class = PrecioSerializer | 48 | serializer_class = PrecioSerializer |
@@ -4,3 +4,10 @@ from django.apps import AppConfig | @@ -4,3 +4,10 @@ from django.apps import AppConfig | ||
4 | class UsuarioConfig(AppConfig): | 4 | class UsuarioConfig(AppConfig): |
5 | default_auto_field = 'django.db.models.BigAutoField' | 5 | default_auto_field = 'django.db.models.BigAutoField' |
6 | name = 'usuario' | 6 | name = 'usuario' |
7 | + | ||
8 | + def ready(self): | ||
9 | + from actstream import registry | ||
10 | + registry.register(self.get_model('Usuario')) | ||
11 | + | ||
12 | + | ||
13 | +default_app_config = 'usuario.apps.UsuarioConfig' |
-
Please register or login to post a comment