Showing
4 changed files
with
32 additions
and
15 deletions
1 | -from rest_framework import viewsets | ||
2 | -from rest_framework import status | 1 | +from rest_framework import viewsets, mixins, status |
3 | from rest_framework.response import Response | 2 | from rest_framework.response import Response |
4 | -# from rest_framework.decorators import action | 3 | +from rest_framework.authentication import SessionAuthentication, BasicAuthentication |
4 | +from rest_framework.permissions import IsAuthenticated | ||
5 | +from rest_framework.decorators import action | ||
6 | +from datetime import datetime | ||
5 | 7 | ||
6 | from .models import Edicto | 8 | from .models import Edicto |
7 | from .serializer import EdictoSerializer | 9 | from .serializer import EdictoSerializer |
8 | from .utils import contador | 10 | from .utils import contador |
9 | 11 | ||
10 | 12 | ||
11 | -class EdictoViewSet(viewsets.ModelViewSet): | 13 | +class EdictoViewSet(mixins.CreateModelMixin, |
14 | + mixins.RetrieveModelMixin, | ||
15 | + mixins.UpdateModelMixin, | ||
16 | + mixins.DestroyModelMixin, | ||
17 | + mixins.ListModelMixin, | ||
18 | + viewsets.GenericViewSet): | ||
19 | + | ||
12 | queryset = Edicto.objects.all().order_by('id') | 20 | queryset = Edicto.objects.all().order_by('id') |
13 | serializer_class = EdictoSerializer | 21 | serializer_class = EdictoSerializer |
22 | + authentication_classes = [SessionAuthentication, BasicAuthentication] | ||
23 | + permission_classes = [IsAuthenticated] | ||
14 | 24 | ||
15 | - def create(self, request, *args, **kwargs): | 25 | + @action(detail=True, methods=['put', 'patch']) |
26 | + def custom_update(self, request): | ||
16 | edicto = request.data.get('cuerpo_edicto') | 27 | edicto = request.data.get('cuerpo_edicto') |
17 | cantidad_palabras = contador(edicto) | 28 | cantidad_palabras = contador(edicto) |
18 | 29 | ||
19 | - serializer = self.get_serializer(data=request.data) | 30 | + instance = self.get_object() |
31 | + serializer = self.get_serializer(instance, data=request.data) | ||
20 | serializer.is_valid(raise_exception=True) | 32 | serializer.is_valid(raise_exception=True) |
21 | - serializer.save(cantidad_palabras=cantidad_palabras) | 33 | + serializer.save(cantidad_palabras=cantidad_palabras, fecha_actualizacion=datetime.now()) |
34 | + | ||
35 | + return Response(serializer.data, status=status.HTTP_200_OK) | ||
22 | 36 | ||
23 | - return Response(serializer.data, status=status.HTTP_201_CREATED) | 37 | + def perform_create(self, serializer): |
38 | + edicto = self.request.data.get('cuerpo_edicto') | ||
39 | + cantidad_palabras = contador(edicto) | ||
40 | + serializer.save(cantidad_palabras=cantidad_palabras) |
@@ -11,8 +11,9 @@ STATUS_CHOICE = [ | @@ -11,8 +11,9 @@ STATUS_CHOICE = [ | ||
11 | (INICIADO, _('iniciado')), | 11 | (INICIADO, _('iniciado')), |
12 | (PENDIENTE_DE_PAGO, _('pendiente_de_pago')), | 12 | (PENDIENTE_DE_PAGO, _('pendiente_de_pago')), |
13 | (PUBLICADO, _('publicado')), | 13 | (PUBLICADO, _('publicado')), |
14 | - (APROBADO,_('aprobado')), | 14 | + (APROBADO, _('aprobado')), |
15 | (RECHAZADO, _('rechazado')), | 15 | (RECHAZADO, _('rechazado')), |
16 | ] | 16 | ] |
17 | 17 | ||
18 | -EXTENSIONES_VALIDAS = [".pdf", ".docx", ".jpg", ".jpeg", ".png"] | 18 | + |
19 | +EXTENSIONES_VALIDAS = ["pdf", "docx", "jpg", "jpeg", "png"] |
@@ -5,9 +5,6 @@ from .models import Edicto | @@ -5,9 +5,6 @@ from .models import Edicto | ||
5 | 5 | ||
6 | 6 | ||
7 | class EdictoSerializer(serializers.ModelSerializer): | 7 | class EdictoSerializer(serializers.ModelSerializer): |
8 | - cantidad_palabras = serializers.IntegerField(read_only=True) | ||
9 | - fecha_creacion = serializers.DateTimeField(read_only=True) | ||
10 | - | ||
11 | class Meta: | 8 | class Meta: |
12 | model = Edicto | 9 | model = Edicto |
13 | fields = ('usuario', | 10 | fields = ('usuario', |
@@ -19,7 +16,9 @@ class EdictoSerializer(serializers.ModelSerializer): | @@ -19,7 +16,9 @@ class EdictoSerializer(serializers.ModelSerializer): | ||
19 | 'cantidad_copias', | 16 | 'cantidad_copias', |
20 | 'fecha_publicacion', | 17 | 'fecha_publicacion', |
21 | 'archivo', | 18 | 'archivo', |
22 | - 'fecha_creacion',) | 19 | + 'fecha_creacion', |
20 | + ) | ||
21 | + read_only_fields = ('cantidad_palabras', 'fecha_creacion') | ||
23 | 22 | ||
24 | @staticmethod | 23 | @staticmethod |
25 | def validate_archivo(value): | 24 | def validate_archivo(value): |
@@ -157,7 +157,7 @@ REST_FRAMEWORK = { | @@ -157,7 +157,7 @@ REST_FRAMEWORK = { | ||
157 | 'rest_framework.authentication.BasicAuthentication', | 157 | 'rest_framework.authentication.BasicAuthentication', |
158 | 'rest_framework.authentication.SessionAuthentication', | 158 | 'rest_framework.authentication.SessionAuthentication', |
159 | ), | 159 | ), |
160 | - 'DEFAULT_RENDERER_CLASSES': ('rest_framework_json_api.renderers.JSONRenderer',), | 160 | + # 'DEFAULT_RENDERER_CLASSES': ('rest_framework_json_api.renderers.JSONRenderer',), |
161 | 'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata', | 161 | 'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata', |
162 | 'NON_FIELD_ERRORS_KEY': 'error_messages' | 162 | 'NON_FIELD_ERRORS_KEY': 'error_messages' |
163 | } | 163 | } |
-
Please register or login to post a comment