Merge branch 'feature/#31_modificar_modelo_edicto' into 'develop'
Feature/#31 modificar modelo edicto See merge request !27
Showing
7 changed files
with
41 additions
and
47 deletions
1 | -from rest_framework import viewsets, mixins, status, serializers | 1 | +from rest_framework import viewsets, filters, mixins, status, serializers |
2 | +from django_filters.rest_framework import DjangoFilterBackend | ||
2 | from rest_framework.response import Response | 3 | from rest_framework.response import Response |
3 | from rest_framework.permissions import IsAuthenticated | 4 | from rest_framework.permissions import IsAuthenticated |
4 | from rest_framework.decorators import action | 5 | from rest_framework.decorators import action |
@@ -6,6 +7,7 @@ from datetime import datetime | @@ -6,6 +7,7 @@ from datetime import datetime | ||
6 | 7 | ||
7 | from .models import Edicto, Precio | 8 | from .models import Edicto, Precio |
8 | from .serializer import EdictoSerializer, PrecioSerializer | 9 | from .serializer import EdictoSerializer, PrecioSerializer |
10 | +from .filters import EdictoFilter | ||
9 | from .utils import contador | 11 | from .utils import contador |
10 | # from .precio import contador | 12 | # from .precio import contador |
11 | 13 | ||
@@ -17,9 +19,18 @@ class EdictoViewSet(mixins.CreateModelMixin, | @@ -17,9 +19,18 @@ class EdictoViewSet(mixins.CreateModelMixin, | ||
17 | mixins.ListModelMixin, | 19 | mixins.ListModelMixin, |
18 | viewsets.GenericViewSet): | 20 | viewsets.GenericViewSet): |
19 | 21 | ||
20 | - queryset = Edicto.objects.all().order_by('id') | ||
21 | serializer_class = EdictoSerializer | 22 | serializer_class = EdictoSerializer |
22 | permission_classes = [IsAuthenticated, ] | 23 | permission_classes = [IsAuthenticated, ] |
24 | + filter_backends = (DjangoFilterBackend, filters.OrderingFilter) | ||
25 | + filterset_class = EdictoFilter | ||
26 | + queryset = Edicto.objects.all() | ||
27 | + ordering_fields = ('usuario__usuario', 'estado', 'fecha_publicacion', 'id',) | ||
28 | + ordering = 'id' | ||
29 | + | ||
30 | + def perform_create(self, serializer): | ||
31 | + edicto = self.request.data.get('cuerpo_edicto') | ||
32 | + cantidad_palabras = contador(edicto) | ||
33 | + serializer.save(cantidad_palabras=cantidad_palabras, usuario=self.request.user) | ||
23 | 34 | ||
24 | @action(detail=True, methods=['put', 'patch']) | 35 | @action(detail=True, methods=['put', 'patch']) |
25 | def custom_update(self, request): | 36 | def custom_update(self, request): |
@@ -33,34 +44,6 @@ class EdictoViewSet(mixins.CreateModelMixin, | @@ -33,34 +44,6 @@ class EdictoViewSet(mixins.CreateModelMixin, | ||
33 | 44 | ||
34 | return Response(serializer.data, status=status.HTTP_200_OK) | 45 | return Response(serializer.data, status=status.HTTP_200_OK) |
35 | 46 | ||
36 | - def perform_create(self, serializer): | ||
37 | - edicto = self.request.data.get('cuerpo_edicto') | ||
38 | - cantidad_palabras = contador(edicto) | ||
39 | - serializer.save(cantidad_palabras=cantidad_palabras) | ||
40 | - | ||
41 | - def include_fecha_publicacion(self): | ||
42 | - return self.request.user.is_staff | ||
43 | - | ||
44 | - def list(self, request, *args, **kwargs): | ||
45 | - queryset = self.filter_queryset(self.get_queryset()) | ||
46 | - serializer = self.get_serializer(queryset, many=True) | ||
47 | - | ||
48 | - for data in serializer.data: | ||
49 | - if self.include_fecha_publicacion(): | ||
50 | - edicto = queryset.get(id=data['edicto_id']) | ||
51 | - data['fecha_publicacion'] = edicto.fecha_publicacion | ||
52 | - | ||
53 | - return Response(serializer.data, status=status.HTTP_200_OK) | ||
54 | - | ||
55 | - def retrieve(self, request, *args, **kwargs): | ||
56 | - instance = self.get_object() | ||
57 | - serializer = self.get_serializer(instance) | ||
58 | - | ||
59 | - if self.include_fecha_publicacion(): | ||
60 | - serializer.data['fecha_publicacion'] = instance.fecha_publicacion | ||
61 | - | ||
62 | - return Response(serializer.data, status=status.HTTP_200_OK) | ||
63 | - | ||
64 | 47 | ||
65 | class PrecioViewSet(mixins.CreateModelMixin, | 48 | class PrecioViewSet(mixins.CreateModelMixin, |
66 | mixins.RetrieveModelMixin, | 49 | mixins.RetrieveModelMixin, |
project/apps/edicto/filters.py
0 → 100644
@@ -8,14 +8,11 @@ from usuario.serializers import UsuarioSerializer | @@ -8,14 +8,11 @@ from usuario.serializers import UsuarioSerializer | ||
8 | 8 | ||
9 | class EdictoSerializer(serializers.ModelSerializer): | 9 | class EdictoSerializer(serializers.ModelSerializer): |
10 | 10 | ||
11 | - edicto_id = serializers.ReadOnlyField(source='id') | ||
12 | - | ||
13 | class Meta: | 11 | class Meta: |
14 | model = Edicto | 12 | model = Edicto |
15 | fields = ('usuario', | 13 | fields = ('usuario', |
16 | - 'edicto_id', | ||
17 | - 'cuerpo_edicto', | ||
18 | 'estado', | 14 | 'estado', |
15 | + 'cuerpo_edicto', | ||
19 | 'cantidad_palabras', | 16 | 'cantidad_palabras', |
20 | 'dias_publicar', | 17 | 'dias_publicar', |
21 | 'cantidad_sellos', | 18 | 'cantidad_sellos', |
@@ -23,26 +20,27 @@ class EdictoSerializer(serializers.ModelSerializer): | @@ -23,26 +20,27 @@ class EdictoSerializer(serializers.ModelSerializer): | ||
23 | 'archivo', | 20 | 'archivo', |
24 | 'fecha_creacion', | 21 | 'fecha_creacion', |
25 | 'observaciones', | 22 | 'observaciones', |
23 | + 'fecha_publicacion', | ||
26 | ) | 24 | ) |
27 | - read_only_fields = ('cantidad_palabras', 'fecha_creacion') | 25 | + |
28 | included_serializers = { | 26 | included_serializers = { |
29 | - 'usuario': 'usuario.serializers.UsuarioSerializer', | 27 | + 'usuario': 'username.serializers.UsuarioListaSerializer', |
30 | } | 28 | } |
31 | 29 | ||
30 | + read_only_fields = ('usuario', 'fecha_publicacion', 'cantidad_palabras', 'fecha_creacion') | ||
31 | + | ||
32 | def to_representation(self, instance): | 32 | def to_representation(self, instance): |
33 | data = super().to_representation(instance) | 33 | data = super().to_representation(instance) |
34 | user = self.context['request'].user | 34 | user = self.context['request'].user |
35 | - | ||
36 | - if user.is_staff: | 35 | + if not user.is_staff: |
37 | data['fecha_publicacion'] = instance.fecha_publicacion | 36 | data['fecha_publicacion'] = instance.fecha_publicacion |
38 | - | ||
39 | return data | 37 | return data |
40 | 38 | ||
41 | @staticmethod | 39 | @staticmethod |
42 | def validate_archivo(value): | 40 | def validate_archivo(value): |
43 | filename, extension = value.name.rsplit(".", 1) | 41 | filename, extension = value.name.rsplit(".", 1) |
44 | if extension.lower() not in EXTENSIONES_VALIDAS: | 42 | if extension.lower() not in EXTENSIONES_VALIDAS: |
45 | - raise serializers.ValidationError("Archivos permitidos: .pdf, .docx, .jpg, .png") | 43 | + raise serializers.ValidationError("Archivos permitidos: .pdf") |
46 | return value | 44 | return value |
47 | 45 | ||
48 | 46 | ||
@@ -64,7 +62,7 @@ class PrecioSerializer(serializers.ModelSerializer): | @@ -64,7 +62,7 @@ class PrecioSerializer(serializers.ModelSerializer): | ||
64 | user = request.user if request else None | 62 | user = request.user if request else None |
65 | if not user or user.is_anonymous: | 63 | if not user or user.is_anonymous: |
66 | raise serializers.ValidationError("El usuario debe estar autenticado.") | 64 | raise serializers.ValidationError("El usuario debe estar autenticado.") |
67 | - if not user.es_staff: | 65 | + if not user.is_staff: |
68 | raise serializers.ValidationError("El usuario no es parte del personal designado.") | 66 | raise serializers.ValidationError("El usuario no es parte del personal designado.") |
69 | 67 | ||
70 | return data | 68 | return data |
@@ -2,11 +2,9 @@ import re | @@ -2,11 +2,9 @@ import re | ||
2 | 2 | ||
3 | 3 | ||
4 | def contador(edicto): | 4 | def contador(edicto): |
5 | - numeros = re.findall(r'\b\d+(?:\.\d+)?\b', edicto) | ||
6 | expresiones = re.findall(r'\(\.\.\.', edicto) | 5 | expresiones = re.findall(r'\(\.\.\.', edicto) |
7 | texto = edicto.split() | 6 | texto = edicto.split() |
8 | cantidad_palabras = len(texto) | 7 | cantidad_palabras = len(texto) |
9 | - cantidad_numeros = len(numeros) - 1 | ||
10 | cantidad_expresiones = len(expresiones) | 8 | cantidad_expresiones = len(expresiones) |
11 | - total_edicto = cantidad_palabras + cantidad_numeros + cantidad_expresiones | 9 | + total_edicto = cantidad_palabras + cantidad_expresiones |
12 | return total_edicto | 10 | return total_edicto |
@@ -2,12 +2,14 @@ from rest_framework import routers | @@ -2,12 +2,14 @@ from rest_framework import routers | ||
2 | 2 | ||
3 | from organismo import api as organismo_api | 3 | from organismo import api as organismo_api |
4 | from usuario import api as usuario_api | 4 | from usuario import api as usuario_api |
5 | +from edicto.api import EdictoViewSet | ||
5 | from edicto import api as edicto_api | 6 | from edicto import api as edicto_api |
6 | # Define routes | 7 | # Define routes |
7 | router = routers.DefaultRouter() | 8 | router = routers.DefaultRouter() |
8 | 9 | ||
9 | router.register(prefix='usuario', viewset=usuario_api.UsuarioViewSet) | 10 | router.register(prefix='usuario', viewset=usuario_api.UsuarioViewSet) |
10 | router.register(prefix='organismo', viewset=organismo_api.OrganismoViewSet) | 11 | router.register(prefix='organismo', viewset=organismo_api.OrganismoViewSet) |
11 | -router.register(prefix='edicto', viewset=edicto_api.EdictoViewSet) | 12 | +router.register(r'edicto', EdictoViewSet, basename='edicto') |
12 | router.register(prefix='precio', viewset=edicto_api.PrecioViewSet) | 13 | router.register(prefix='precio', viewset=edicto_api.PrecioViewSet) |
13 | 14 | ||
15 | + |
@@ -162,7 +162,7 @@ REST_FRAMEWORK = { | @@ -162,7 +162,7 @@ REST_FRAMEWORK = { | ||
162 | 'NON_FIELD_ERRORS_KEY': 'error_messages' | 162 | 'NON_FIELD_ERRORS_KEY': 'error_messages' |
163 | } | 163 | } |
164 | 164 | ||
165 | -ACTIVAR_HERRAMIENTAS_DEBUGGING = env.bool('ACTIVAR_HERRAMIENTAS_DEBUGGING', default=False) | 165 | +ACTIVAR_HERRAMIENTAS_DEBUGGING = env.bool('ACTIVAR_HERRAMIENTAS_DEBUGGING', default=True) |
166 | if ACTIVAR_HERRAMIENTAS_DEBUGGING: | 166 | if ACTIVAR_HERRAMIENTAS_DEBUGGING: |
167 | INTERNAL_IPS = ['127.0.0.1'] | 167 | INTERNAL_IPS = ['127.0.0.1'] |
168 | INSTALLED_APPS += ('debug_toolbar', 'django_extensions') | 168 | INSTALLED_APPS += ('debug_toolbar', 'django_extensions') |
-
Please register or login to post a comment