se agrego visibilidad de serializer e included, ademas se corrigio la funcion co…
…ntador para que no cuente los guiones libres
Showing
8 changed files
with
89 additions
and
44 deletions
@@ -7,9 +7,12 @@ from datetime import datetime | @@ -7,9 +7,12 @@ from datetime import datetime | ||
7 | 7 | ||
8 | from .models import Edicto, Precio | 8 | from .models import Edicto, Precio |
9 | from .serializer import EdictoSerializer, PrecioSerializer | 9 | from .serializer import EdictoSerializer, PrecioSerializer |
10 | -from .filters import EdictoFilter | 10 | +from usuario.serializers import UsuarioListaSerializer |
11 | +from .filters import EdictoFilter, PrecioFilter | ||
12 | +from .pagination import CustomPagination | ||
13 | +from .permissions import IsAdminOrAuthorized | ||
11 | from .utils import contador | 14 | from .utils import contador |
12 | -# from .precio import contador | 15 | +# from .precio import calculadora |
13 | 16 | ||
14 | 17 | ||
15 | class EdictoViewSet(mixins.CreateModelMixin, | 18 | class EdictoViewSet(mixins.CreateModelMixin, |
@@ -24,13 +27,15 @@ class EdictoViewSet(mixins.CreateModelMixin, | @@ -24,13 +27,15 @@ class EdictoViewSet(mixins.CreateModelMixin, | ||
24 | filter_backends = (DjangoFilterBackend, filters.OrderingFilter) | 27 | filter_backends = (DjangoFilterBackend, filters.OrderingFilter) |
25 | filterset_class = EdictoFilter | 28 | filterset_class = EdictoFilter |
26 | queryset = Edicto.objects.all() | 29 | queryset = Edicto.objects.all() |
27 | - ordering_fields = ('usuario__usuario', 'estado', 'fecha_publicacion', 'id',) | ||
28 | - ordering = 'id' | 30 | + ordering_fields = ('usuario__usuario',) |
31 | + ordering = 'usuario' | ||
32 | + pagination_class = CustomPagination | ||
29 | 33 | ||
30 | def perform_create(self, serializer): | 34 | def perform_create(self, serializer): |
31 | edicto = self.request.data.get('cuerpo_edicto') | 35 | edicto = self.request.data.get('cuerpo_edicto') |
32 | cantidad_palabras = contador(edicto) | 36 | cantidad_palabras = contador(edicto) |
33 | - serializer.save(cantidad_palabras=cantidad_palabras, usuario=self.request.user) | 37 | + usuario = self.request.user |
38 | + serializer.save(cantidad_palabras=cantidad_palabras, usuario=usuario) | ||
34 | 39 | ||
35 | @action(detail=True, methods=['put', 'patch']) | 40 | @action(detail=True, methods=['put', 'patch']) |
36 | def custom_update(self, request): | 41 | def custom_update(self, request): |
@@ -45,26 +50,12 @@ class EdictoViewSet(mixins.CreateModelMixin, | @@ -45,26 +50,12 @@ class EdictoViewSet(mixins.CreateModelMixin, | ||
45 | return Response(serializer.data, status=status.HTTP_200_OK) | 50 | return Response(serializer.data, status=status.HTTP_200_OK) |
46 | 51 | ||
47 | 52 | ||
48 | -class PrecioViewSet(mixins.CreateModelMixin, | ||
49 | - mixins.RetrieveModelMixin, | ||
50 | - mixins.ListModelMixin, | ||
51 | - viewsets.GenericViewSet): | ||
52 | - | ||
53 | - queryset = Precio.objects.all().order_by('id') | 53 | +class PrecioViewSet(viewsets.ReadOnlyModelViewSet): |
54 | serializer_class = PrecioSerializer | 54 | serializer_class = PrecioSerializer |
55 | - permission_classes = [IsAuthenticated, ] | ||
56 | - | ||
57 | - def create(self, request, *args, **kwargs): | ||
58 | - user = request.user | ||
59 | - | ||
60 | - if not user.is_staff: | ||
61 | - raise serializers.ValidationError("El usuario no es parte del personal designado.") | ||
62 | - | ||
63 | - return super().create(request, *args, **kwargs) | ||
64 | - | ||
65 | - def retrieve(self, request, *args, **kwargs): | ||
66 | - instance = self.get_object() | ||
67 | - serializer = self.get_serializer(instance) | ||
68 | - data = serializer.data | ||
69 | - data['id'] = instance.id | ||
70 | - return Response(data) | 55 | + permission_classes = [IsAuthenticated, IsAdminOrAuthorized] |
56 | + filter_backends = (DjangoFilterBackend, filters.OrderingFilter) | ||
57 | + filterset_class = PrecioFilter | ||
58 | + ordering_fields = ('usuario__usuario', ) | ||
59 | + ordering = 'usuario' | ||
60 | + queryset = Precio.objects.all() | ||
61 | + pagination_class = CustomPagination |
1 | from django_filters import rest_framework as filters | 1 | from django_filters import rest_framework as filters |
2 | 2 | ||
3 | -from edicto.models import Edicto | 3 | +from edicto.models import Edicto, Precio |
4 | 4 | ||
5 | 5 | ||
6 | class EdictoFilter(filters.FilterSet): | 6 | class EdictoFilter(filters.FilterSet): |
7 | + fecha_creacion__gte = filters.DateFilter(field_name="fecha_creacion", lookup_expr='gte', label='Desde') | ||
8 | + fecha_creacion__lte = filters.DateFilter(field_name="fecha_creacion", lookup_expr='lte', label='Hasta') | ||
9 | + | ||
7 | class Meta: | 10 | class Meta: |
8 | model = Edicto | 11 | model = Edicto |
9 | - fields = ('usuario', 'estado', 'fecha_publicacion', 'id',) | 12 | + fields = ('usuario', |
13 | + 'estado', | ||
14 | + 'fecha_creacion__gte', | ||
15 | + 'fecha_creacion__lte', | ||
16 | + 'id', | ||
17 | + ) | ||
18 | + | ||
19 | + | ||
20 | +class PrecioFilter(filters.FilterSet): | ||
21 | + class Meta: | ||
22 | + model = Precio | ||
23 | + fields = { | ||
24 | + 'usuario': ['exact'], | ||
25 | + 'precio': ['exact'], | ||
26 | + 'vigencia_desde': ['exact'], | ||
27 | + 'vigencia_hasta': ['exact'], | ||
28 | + 'moneda': ['exact'], | ||
29 | + } | ||
10 | 30 | ||
11 | 31 | ||
12 | 32 |
project/apps/edicto/pagination.py
0 → 100644
project/apps/edicto/permissions.py
0 → 100644
1 | from rest_framework import serializers | 1 | from rest_framework import serializers |
2 | 2 | ||
3 | + | ||
3 | from .constants import EXTENSIONES_VALIDAS | 4 | from .constants import EXTENSIONES_VALIDAS |
4 | 5 | ||
5 | -from .models import Edicto, Precio, ComprobantePago | ||
6 | -from usuario.serializers import UsuarioSerializer | 6 | +from .models import Edicto, Precio |
7 | +from usuario.serializers import UsuarioListaSerializer | ||
7 | 8 | ||
8 | 9 | ||
9 | class EdictoSerializer(serializers.ModelSerializer): | 10 | class EdictoSerializer(serializers.ModelSerializer): |
11 | + included_serializers = { | ||
12 | + 'usuario': UsuarioListaSerializer, | ||
13 | + } | ||
10 | 14 | ||
11 | class Meta: | 15 | class Meta: |
12 | model = Edicto | 16 | model = Edicto |
@@ -21,11 +25,8 @@ class EdictoSerializer(serializers.ModelSerializer): | @@ -21,11 +25,8 @@ class EdictoSerializer(serializers.ModelSerializer): | ||
21 | 'fecha_creacion', | 25 | 'fecha_creacion', |
22 | 'observaciones', | 26 | 'observaciones', |
23 | 'fecha_publicacion', | 27 | 'fecha_publicacion', |
24 | - ) | ||
25 | 28 | ||
26 | - included_serializers = { | ||
27 | - 'usuario': 'username.serializers.UsuarioListaSerializer', | ||
28 | - } | 29 | + ) |
29 | 30 | ||
30 | read_only_fields = ('usuario', 'fecha_publicacion', 'cantidad_palabras', 'fecha_creacion') | 31 | read_only_fields = ('usuario', 'fecha_publicacion', 'cantidad_palabras', 'fecha_creacion') |
31 | 32 | ||
@@ -45,7 +46,6 @@ class EdictoSerializer(serializers.ModelSerializer): | @@ -45,7 +46,6 @@ class EdictoSerializer(serializers.ModelSerializer): | ||
45 | 46 | ||
46 | 47 | ||
47 | class PrecioSerializer(serializers.ModelSerializer): | 48 | class PrecioSerializer(serializers.ModelSerializer): |
48 | - usuario = UsuarioSerializer() | ||
49 | 49 | ||
50 | class Meta: | 50 | class Meta: |
51 | model = Precio | 51 | model = Precio |
@@ -66,3 +66,7 @@ class PrecioSerializer(serializers.ModelSerializer): | @@ -66,3 +66,7 @@ class PrecioSerializer(serializers.ModelSerializer): | ||
66 | raise serializers.ValidationError("El usuario no es parte del personal designado.") | 66 | raise serializers.ValidationError("El usuario no es parte del personal designado.") |
67 | 67 | ||
68 | return data | 68 | return data |
69 | + | ||
70 | + included_serializers = { | ||
71 | + 'usuario': UsuarioListaSerializer | ||
72 | + } |
@@ -2,9 +2,8 @@ import re | @@ -2,9 +2,8 @@ import re | ||
2 | 2 | ||
3 | 3 | ||
4 | def contador(edicto): | 4 | def contador(edicto): |
5 | - expresiones = re.findall(r'\(\.\.\.', edicto) | ||
6 | - texto = edicto.split() | ||
7 | - cantidad_palabras = len(texto) | ||
8 | - cantidad_expresiones = len(expresiones) | ||
9 | - total_edicto = cantidad_palabras + cantidad_expresiones | ||
10 | - return total_edicto | 5 | + edicto_sin_guiones = re.sub(r'(?<!\w)-|-(?!\w)', ' ', edicto) |
6 | + palabras = re.split(r'\s+', edicto_sin_guiones) | ||
7 | + palabras_filtradas = [palabra for palabra in palabras if palabra.strip()] | ||
8 | + total_palabras = len(palabras_filtradas) | ||
9 | + return total_palabras |
@@ -48,6 +48,7 @@ DJANGO_APPS = ( | @@ -48,6 +48,7 @@ DJANGO_APPS = ( | ||
48 | 48 | ||
49 | THIRD_PARTY_APPS = ( | 49 | THIRD_PARTY_APPS = ( |
50 | 'rest_framework', | 50 | 'rest_framework', |
51 | + 'rest_framework_json_api', | ||
51 | 'django_filters', | 52 | 'django_filters', |
52 | 'corsheaders', | 53 | 'corsheaders', |
53 | 'oauth2_provider', | 54 | 'oauth2_provider', |
@@ -157,9 +158,24 @@ REST_FRAMEWORK = { | @@ -157,9 +158,24 @@ REST_FRAMEWORK = { | ||
157 | 'rest_framework.authentication.BasicAuthentication', | 158 | 'rest_framework.authentication.BasicAuthentication', |
158 | 'rest_framework.authentication.SessionAuthentication', | 159 | 'rest_framework.authentication.SessionAuthentication', |
159 | ), | 160 | ), |
160 | - 'DEFAULT_RENDERER_CLASSES': ('rest_framework_json_api.renderers.JSONRenderer',), | 161 | + 'DEFAULT_RENDERER_CLASSES': ( |
162 | + 'rest_framework_json_api.renderers.JSONRenderer', | ||
163 | + 'rest_framework_json_api.renderers.BrowsableAPIRenderer' | ||
164 | + ), | ||
161 | 'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata', | 165 | 'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata', |
162 | - 'NON_FIELD_ERRORS_KEY': 'error_messages' | 166 | + 'DEFAULT_FILTER_BACKENDS': ( |
167 | + 'rest_framework_json_api.filters.QueryParameterValidationFilter', | ||
168 | + 'rest_framework_json_api.filters.OrderingFilter', | ||
169 | + 'rest_framework_json_api.django_filters.DjangoFilterBackend', | ||
170 | + 'rest_framework.filters.SearchFilter', | ||
171 | + ), | ||
172 | + 'SEARCH_PARAM': 'filter[search]', | ||
173 | + 'TEST_REQUEST_RENDERER_CLASSES': ( | ||
174 | + 'rest_framework_json_api.renderers.JSONRenderer', | ||
175 | + ), | ||
176 | + 'TEST_REQUEST_DEFAULT_FORMAT': 'vnd.api+json', | ||
177 | + | ||
178 | + | ||
163 | } | 179 | } |
164 | 180 | ||
165 | ACTIVAR_HERRAMIENTAS_DEBUGGING = env.bool('ACTIVAR_HERRAMIENTAS_DEBUGGING', default=True) | 181 | ACTIVAR_HERRAMIENTAS_DEBUGGING = env.bool('ACTIVAR_HERRAMIENTAS_DEBUGGING', default=True) |
-
Please register or login to post a comment