cambios en permisos, sobreescritura del metodo get y serializar comprobante de pago
Showing
12 changed files
with
178 additions
and
13 deletions
1 | from rest_framework import viewsets, mixins, status, serializers | 1 | from rest_framework import viewsets, mixins, status, serializers |
2 | from rest_framework.response import Response | 2 | from rest_framework.response import Response |
3 | -from rest_framework.authentication import SessionAuthentication, BasicAuthentication | ||
4 | from rest_framework.permissions import IsAuthenticated | 3 | from rest_framework.permissions import IsAuthenticated |
5 | from rest_framework.decorators import action | 4 | from rest_framework.decorators import action |
6 | from datetime import datetime | 5 | from datetime import datetime |
@@ -8,6 +7,7 @@ from datetime import datetime | @@ -8,6 +7,7 @@ from datetime import datetime | ||
8 | from .models import Edicto, Precio | 7 | from .models import Edicto, Precio |
9 | from .serializer import EdictoSerializer, PrecioSerializer | 8 | from .serializer import EdictoSerializer, PrecioSerializer |
10 | from .utils import contador | 9 | from .utils import contador |
10 | +# from .precio import contador | ||
11 | 11 | ||
12 | 12 | ||
13 | class EdictoViewSet(mixins.CreateModelMixin, | 13 | class EdictoViewSet(mixins.CreateModelMixin, |
@@ -19,8 +19,7 @@ class EdictoViewSet(mixins.CreateModelMixin, | @@ -19,8 +19,7 @@ class EdictoViewSet(mixins.CreateModelMixin, | ||
19 | 19 | ||
20 | queryset = Edicto.objects.all().order_by('id') | 20 | queryset = Edicto.objects.all().order_by('id') |
21 | serializer_class = EdictoSerializer | 21 | serializer_class = EdictoSerializer |
22 | - authentication_classes = [SessionAuthentication, BasicAuthentication] | ||
23 | - permission_classes = [IsAuthenticated] | 22 | + permission_classes = [IsAuthenticated, ] |
24 | 23 | ||
25 | @action(detail=True, methods=['put', 'patch']) | 24 | @action(detail=True, methods=['put', 'patch']) |
26 | def custom_update(self, request): | 25 | def custom_update(self, request): |
@@ -39,6 +38,29 @@ class EdictoViewSet(mixins.CreateModelMixin, | @@ -39,6 +38,29 @@ class EdictoViewSet(mixins.CreateModelMixin, | ||
39 | cantidad_palabras = contador(edicto) | 38 | cantidad_palabras = contador(edicto) |
40 | serializer.save(cantidad_palabras=cantidad_palabras) | 39 | serializer.save(cantidad_palabras=cantidad_palabras) |
41 | 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 | + | ||
42 | 64 | ||
43 | class PrecioViewSet(mixins.CreateModelMixin, | 65 | class PrecioViewSet(mixins.CreateModelMixin, |
44 | mixins.RetrieveModelMixin, | 66 | mixins.RetrieveModelMixin, |
@@ -47,8 +69,7 @@ class PrecioViewSet(mixins.CreateModelMixin, | @@ -47,8 +69,7 @@ class PrecioViewSet(mixins.CreateModelMixin, | ||
47 | 69 | ||
48 | queryset = Precio.objects.all().order_by('id') | 70 | queryset = Precio.objects.all().order_by('id') |
49 | serializer_class = PrecioSerializer | 71 | serializer_class = PrecioSerializer |
50 | - authentication_classes = [SessionAuthentication, BasicAuthentication] | ||
51 | - permission_classes = [IsAuthenticated] | 72 | + permission_classes = [IsAuthenticated, ] |
52 | 73 | ||
53 | def create(self, request, *args, **kwargs): | 74 | def create(self, request, *args, **kwargs): |
54 | user = request.user | 75 | user = request.user |
@@ -57,3 +78,10 @@ class PrecioViewSet(mixins.CreateModelMixin, | @@ -57,3 +78,10 @@ class PrecioViewSet(mixins.CreateModelMixin, | ||
57 | raise serializers.ValidationError("El usuario no es parte del personal designado.") | 78 | raise serializers.ValidationError("El usuario no es parte del personal designado.") |
58 | 79 | ||
59 | return super().create(request, *args, **kwargs) | 80 | return super().create(request, *args, **kwargs) |
81 | + | ||
82 | + def retrieve(self, request, *args, **kwargs): | ||
83 | + instance = self.get_object() | ||
84 | + serializer = self.get_serializer(instance) | ||
85 | + data = serializer.data | ||
86 | + data['id'] = instance.id | ||
87 | + return Response(data) |
1 | +# Generated by Django 4.1.9 on 2023-08-11 12:57 | ||
2 | + | ||
3 | +from django.db import migrations, models | ||
4 | + | ||
5 | + | ||
6 | +class Migration(migrations.Migration): | ||
7 | + | ||
8 | + dependencies = [ | ||
9 | + ('edicto', '0003_alter_precio_moneda'), | ||
10 | + ] | ||
11 | + | ||
12 | + operations = [ | ||
13 | + migrations.AddField( | ||
14 | + model_name='edicto', | ||
15 | + name='comentarios', | ||
16 | + field=models.CharField(blank=True, max_length=500, null=True), | ||
17 | + ), | ||
18 | + ] |
1 | +# Generated by Django 4.1.9 on 2023-08-11 13:14 | ||
2 | + | ||
3 | +from django.db import migrations | ||
4 | + | ||
5 | + | ||
6 | +class Migration(migrations.Migration): | ||
7 | + | ||
8 | + dependencies = [ | ||
9 | + ('edicto', '0004_edicto_comentarios'), | ||
10 | + ] | ||
11 | + | ||
12 | + operations = [ | ||
13 | + migrations.RenameField( | ||
14 | + model_name='edicto', | ||
15 | + old_name='comentarios', | ||
16 | + new_name='observaciones', | ||
17 | + ), | ||
18 | + ] |
1 | +# Generated by Django 4.1.9 on 2023-08-14 14:29 | ||
2 | + | ||
3 | +from django.db import migrations | ||
4 | + | ||
5 | + | ||
6 | +class Migration(migrations.Migration): | ||
7 | + | ||
8 | + dependencies = [ | ||
9 | + ('edicto', '0005_rename_comentarios_edicto_observaciones'), | ||
10 | + ] | ||
11 | + | ||
12 | + operations = [ | ||
13 | + migrations.RemoveField( | ||
14 | + model_name='comprobantepago', | ||
15 | + name='edicto', | ||
16 | + ), | ||
17 | + ] |
1 | +# Generated by Django 4.1.9 on 2023-08-14 22:33 | ||
2 | + | ||
3 | +from django.conf import settings | ||
4 | +from django.db import migrations, models | ||
5 | +import django.db.models.deletion | ||
6 | + | ||
7 | + | ||
8 | +class Migration(migrations.Migration): | ||
9 | + | ||
10 | + dependencies = [ | ||
11 | + migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
12 | + ('edicto', '0006_remove_comprobantepago_edicto'), | ||
13 | + ] | ||
14 | + | ||
15 | + operations = [ | ||
16 | + migrations.AlterField( | ||
17 | + model_name='edicto', | ||
18 | + name='usuario', | ||
19 | + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='Usuario', to=settings.AUTH_USER_MODEL), | ||
20 | + ), | ||
21 | + ] |
project/apps/edicto/migrations/0008_alter_edicto_fecha_publicacion_alter_edicto_usuario.py
0 → 100644
1 | +# Generated by Django 4.1.9 on 2023-08-15 00:08 | ||
2 | + | ||
3 | +from django.conf import settings | ||
4 | +from django.db import migrations, models | ||
5 | +import django.db.models.deletion | ||
6 | + | ||
7 | + | ||
8 | +class Migration(migrations.Migration): | ||
9 | + | ||
10 | + dependencies = [ | ||
11 | + migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
12 | + ('edicto', '0007_alter_edicto_usuario'), | ||
13 | + ] | ||
14 | + | ||
15 | + operations = [ | ||
16 | + migrations.AlterField( | ||
17 | + model_name='edicto', | ||
18 | + name='fecha_publicacion', | ||
19 | + field=models.DateField(blank=True, null=True), | ||
20 | + ), | ||
21 | + migrations.AlterField( | ||
22 | + model_name='edicto', | ||
23 | + name='usuario', | ||
24 | + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Usuario'), | ||
25 | + ), | ||
26 | + ] |
@@ -32,7 +32,8 @@ class Edicto(models.Model): | @@ -32,7 +32,8 @@ class Edicto(models.Model): | ||
32 | estado = models.CharField(max_length=150, choices=STATUS_CHOICE, default='inciado') | 32 | estado = models.CharField(max_length=150, choices=STATUS_CHOICE, default='inciado') |
33 | cantidad_palabras = models.IntegerField(blank=False, null=False) | 33 | cantidad_palabras = models.IntegerField(blank=False, null=False) |
34 | cantidad_copias = models.PositiveIntegerField(blank=False, null=False) | 34 | cantidad_copias = models.PositiveIntegerField(blank=False, null=False) |
35 | - fecha_publicacion = models.DateField(blank=False, null=False) | 35 | + fecha_publicacion = models.DateField(blank=True, null=True) |
36 | + observaciones = models.CharField(max_length=500, blank=True, null=True) | ||
36 | fecha_creacion = models.DateTimeField(auto_now_add=True) | 37 | fecha_creacion = models.DateTimeField(auto_now_add=True) |
37 | fecha_modificacion = models.DateTimeField(auto_now_add=True) | 38 | fecha_modificacion = models.DateTimeField(auto_now_add=True) |
38 | 39 | ||
@@ -45,7 +46,6 @@ class Edicto(models.Model): | @@ -45,7 +46,6 @@ class Edicto(models.Model): | ||
45 | 46 | ||
46 | 47 | ||
47 | class ComprobantePago(models.Model): | 48 | class ComprobantePago(models.Model): |
48 | - edicto = models.ForeignKey(Edicto, on_delete=models.CASCADE) | ||
49 | resultado = models.FloatField(blank=False, null=False) | 49 | resultado = models.FloatField(blank=False, null=False) |
50 | numero_comprobante = models.CharField(max_length=300, blank=False, null=True) | 50 | numero_comprobante = models.CharField(max_length=300, blank=False, null=True) |
51 | fecha_pago = models.DateTimeField(blank=False, null=True) | 51 | fecha_pago = models.DateTimeField(blank=False, null=True) |
@@ -55,4 +55,4 @@ class ComprobantePago(models.Model): | @@ -55,4 +55,4 @@ class ComprobantePago(models.Model): | ||
55 | verbose_name_plural = 'ComprobantesPagos' | 55 | verbose_name_plural = 'ComprobantesPagos' |
56 | 56 | ||
57 | def __str__(self): | 57 | def __str__(self): |
58 | - return self.edicto | 58 | + return self.numero_comprobante |
1 | +from django.utils import timezone | ||
1 | from .models import Edicto, ComprobantePago, Precio | 2 | from .models import Edicto, ComprobantePago, Precio |
2 | 3 | ||
3 | 4 | ||
@@ -10,9 +11,20 @@ def contador(edicto_id, precio_id): | @@ -10,9 +11,20 @@ def contador(edicto_id, precio_id): | ||
10 | copias = edicto.cantidad_copias | 11 | copias = edicto.cantidad_copias |
11 | publicar = edicto.dias_publicar | 12 | publicar = edicto.dias_publicar |
12 | 13 | ||
13 | - precio_valores = Precio.objects.values('precio', 'precio_ejemplar').get(id=precio_id) | 14 | + precio_valores = Precio.objects.values('precio', |
15 | + 'precio_ejemplar', | ||
16 | + 'vigencia_desde', | ||
17 | + 'vigencia_hasta',).get(id=precio_id) | ||
18 | + | ||
14 | precio = precio_valores['precio'] | 19 | precio = precio_valores['precio'] |
15 | precio_ejemplar = precio_valores['precio_ejemplar'] | 20 | precio_ejemplar = precio_valores['precio_ejemplar'] |
21 | + vigencia_desde = precio_valores['vigencia_desde'] | ||
22 | + vigencia_hasta = precio_valores['vigencia_hasta'] | ||
23 | + | ||
24 | + current_datetime = timezone.now() | ||
25 | + | ||
26 | + if not (vigencia_desde <= current_datetime <= vigencia_hasta): | ||
27 | + raise ValueError("El precio seleccionado no se encuentra dentro de las fechas permitidas.") | ||
16 | 28 | ||
17 | result_palabra = (sellos + palabras) * precio | 29 | result_palabra = (sellos + palabras) * precio |
18 | result_ejemplar = (copias * precio_ejemplar) + (publicar * precio_ejemplar) | 30 | result_ejemplar = (copias * precio_ejemplar) + (publicar * precio_ejemplar) |
@@ -3,33 +3,52 @@ from rest_framework import serializers | @@ -3,33 +3,52 @@ from rest_framework import serializers | ||
3 | from .constants import EXTENSIONES_VALIDAS | 3 | from .constants import EXTENSIONES_VALIDAS |
4 | 4 | ||
5 | from .models import Edicto, Precio, ComprobantePago | 5 | from .models import Edicto, Precio, ComprobantePago |
6 | +from usuario.serializers import UsuarioSerializer | ||
6 | 7 | ||
7 | 8 | ||
8 | class EdictoSerializer(serializers.ModelSerializer): | 9 | class EdictoSerializer(serializers.ModelSerializer): |
10 | + | ||
11 | + edicto_id = serializers.ReadOnlyField(source='id') | ||
12 | + | ||
9 | class Meta: | 13 | class Meta: |
10 | model = Edicto | 14 | model = Edicto |
11 | fields = ('usuario', | 15 | fields = ('usuario', |
16 | + 'edicto_id', | ||
12 | 'cuerpo_edicto', | 17 | 'cuerpo_edicto', |
13 | 'estado', | 18 | 'estado', |
14 | 'cantidad_palabras', | 19 | 'cantidad_palabras', |
15 | 'dias_publicar', | 20 | 'dias_publicar', |
16 | 'cantidad_sellos', | 21 | 'cantidad_sellos', |
17 | 'cantidad_copias', | 22 | 'cantidad_copias', |
18 | - 'fecha_publicacion', | ||
19 | 'archivo', | 23 | 'archivo', |
20 | 'fecha_creacion', | 24 | 'fecha_creacion', |
25 | + 'observaciones', | ||
21 | ) | 26 | ) |
22 | read_only_fields = ('cantidad_palabras', 'fecha_creacion') | 27 | read_only_fields = ('cantidad_palabras', 'fecha_creacion') |
28 | + included_serializers = { | ||
29 | + 'usuario': 'usuario.serializers.UsuarioSerializer', | ||
30 | + } | ||
31 | + | ||
32 | + def to_representation(self, instance): | ||
33 | + data = super().to_representation(instance) | ||
34 | + user = self.context['request'].user | ||
35 | + | ||
36 | + if user.is_staff: | ||
37 | + data['fecha_publicacion'] = instance.fecha_publicacion | ||
38 | + | ||
39 | + return data | ||
23 | 40 | ||
24 | @staticmethod | 41 | @staticmethod |
25 | def validate_archivo(value): | 42 | def validate_archivo(value): |
26 | filename, extension = value.name.rsplit(".", 1) | 43 | filename, extension = value.name.rsplit(".", 1) |
27 | if extension.lower() not in EXTENSIONES_VALIDAS: | 44 | if extension.lower() not in EXTENSIONES_VALIDAS: |
28 | - raise serializers.ValidationError("Archivos permitidos: .pdf, .docx, .jpg, .jpeg, .png") | 45 | + raise serializers.ValidationError("Archivos permitidos: .pdf, .docx, .jpg, .png") |
29 | return value | 46 | return value |
30 | 47 | ||
31 | 48 | ||
32 | class PrecioSerializer(serializers.ModelSerializer): | 49 | class PrecioSerializer(serializers.ModelSerializer): |
50 | + usuario = UsuarioSerializer() | ||
51 | + | ||
33 | class Meta: | 52 | class Meta: |
34 | model = Precio | 53 | model = Precio |
35 | fields = ('usuario', | 54 | fields = ('usuario', |
@@ -45,7 +64,7 @@ class PrecioSerializer(serializers.ModelSerializer): | @@ -45,7 +64,7 @@ class PrecioSerializer(serializers.ModelSerializer): | ||
45 | user = request.user if request else None | 64 | user = request.user if request else None |
46 | if not user or user.is_anonymous: | 65 | if not user or user.is_anonymous: |
47 | raise serializers.ValidationError("El usuario debe estar autenticado.") | 66 | raise serializers.ValidationError("El usuario debe estar autenticado.") |
48 | - if not user.is_staff: | 67 | + if not user.es_staff: |
49 | raise serializers.ValidationError("El usuario no es parte del personal designado.") | 68 | raise serializers.ValidationError("El usuario no es parte del personal designado.") |
50 | 69 | ||
51 | return data | 70 | return data |
@@ -10,3 +10,4 @@ router.register(prefix='usuario', viewset=usuario_api.UsuarioViewSet) | @@ -10,3 +10,4 @@ router.register(prefix='usuario', viewset=usuario_api.UsuarioViewSet) | ||
10 | router.register(prefix='organismo', viewset=organismo_api.OrganismoViewSet) | 10 | router.register(prefix='organismo', viewset=organismo_api.OrganismoViewSet) |
11 | router.register(prefix='edicto', viewset=edicto_api.EdictoViewSet) | 11 | router.register(prefix='edicto', viewset=edicto_api.EdictoViewSet) |
12 | router.register(prefix='precio', viewset=edicto_api.PrecioViewSet) | 12 | router.register(prefix='precio', viewset=edicto_api.PrecioViewSet) |
13 | + |
-
Please register or login to post a comment