Martín Miranda

Merge branch 'validacion' into 'develop'

Validacion



See merge request !19
from rest_framework import viewsets, mixins, status
from rest_framework import viewsets, mixins, status, serializers
from rest_framework.response import Response
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action
from datetime import datetime
from .models import Edicto
from .serializer import EdictoSerializer
from .models import Edicto, Precio
from .serializer import EdictoSerializer, PrecioSerializer
from .utils import contador
... ... @@ -38,3 +38,22 @@ class EdictoViewSet(mixins.CreateModelMixin,
edicto = self.request.data.get('cuerpo_edicto')
cantidad_palabras = contador(edicto)
serializer.save(cantidad_palabras=cantidad_palabras)
class PrecioViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
queryset = Precio.objects.all().order_by('id')
serializer_class = PrecioSerializer
authentication_classes = [SessionAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated]
def create(self, request, *args, **kwargs):
user = request.user
if not user.is_staff:
raise serializers.ValidationError("El usuario no es parte del personal designado.")
return super().create(request, *args, **kwargs)
... ...
... ... @@ -17,3 +17,15 @@ STATUS_CHOICE = [
EXTENSIONES_VALIDAS = ["pdf", "docx", "jpg", "jpeg", "png"]
PESOS = 'peso_argentino'
DOLAR = 'dolar'
EURO = 'euro'
MONEDA = [
(PESOS, _('peso_argentino')),
(DOLAR, _('dolar')),
(EURO, _('euro')),
]
... ...
# Generated by Django 4.1.9 on 2023-07-03 22:26
# Generated by Django 4.1.9 on 2023-07-30 21:21
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import edicto.utils
class Migration(migrations.Migration):
... ... @@ -11,7 +10,6 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('organismo', '0002_organismo_es_publico'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
... ... @@ -37,14 +35,15 @@ class Migration(migrations.Migration):
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('cuerpo_edicto', models.TextField()),
('archivo', models.FileField(upload_to='uploads/%Y/%m/%d/', validators=[edicto.utils.valid_extension])),
('dias_plubicar', models.FloatField(default=0.0)),
('cantidad_sellos', models.FloatField(default=0.0)),
('estado', models.CharField(choices=[('iniciado', 'iniciado'), ('pendiente_de_pago', 'pendiente_de_pago'), ('publicado', 'publicado'), ('rechazado', 'rechazado')], default='inciado', max_length=150)),
('archivo', models.FileField(upload_to='uploads/%Y/%m/%d/')),
('dias_publicar', models.PositiveIntegerField()),
('cantidad_sellos', models.PositiveIntegerField()),
('estado', models.CharField(choices=[('iniciado', 'iniciado'), ('pendiente_de_pago', 'pendiente_de_pago'), ('publicado', 'publicado'), ('aprobado', 'aprobado'), ('rechazado', 'rechazado')], default='inciado', max_length=150)),
('cantidad_palabras', models.IntegerField()),
('cantidad_copias', models.IntegerField()),
('fecha_publicacion', models.DateTimeField()),
('organismo', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='organismo.organismo', verbose_name='Organismo')),
('cantidad_copias', models.PositiveIntegerField()),
('fecha_publicacion', models.DateField()),
('fecha_creacion', models.DateTimeField(auto_now_add=True)),
('fecha_modificacion', models.DateTimeField(auto_now_add=True)),
('usuario', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Usuario')),
],
options={
... ... @@ -57,8 +56,8 @@ class Migration(migrations.Migration):
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('resultado', models.FloatField()),
('numero_comprobante', models.CharField(max_length=300)),
('fecha_pago', models.DateTimeField()),
('numero_comprobante', models.CharField(max_length=300, null=True)),
('fecha_pago', models.DateTimeField(null=True)),
('edicto', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='edicto.edicto')),
],
options={
... ...
# Generated by Django 4.1.9 on 2023-07-12 12:00
# Generated by Django 4.1.9 on 2023-08-02 11:36
from django.db import migrations, models
... ... @@ -11,13 +11,13 @@ class Migration(migrations.Migration):
operations = [
migrations.AlterField(
model_name='comprobantepago',
name='fecha_pago',
field=models.DateTimeField(null=True),
model_name='precio',
name='vigencia_desde',
field=models.DateField(),
),
migrations.AlterField(
model_name='comprobantepago',
name='numero_comprobante',
field=models.CharField(max_length=300, null=True),
model_name='precio',
name='vigencia_hasta',
field=models.DateField(),
),
]
... ...
# Generated by Django 4.1.9 on 2023-07-24 14:46
# Generated by Django 4.1.9 on 2023-08-02 12:21
from django.db import migrations, models
... ... @@ -6,13 +6,13 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edicto', '0007_alter_edicto_cantidad_palabras_alter_edicto_usuario'),
('edicto', '0002_alter_precio_vigencia_desde_and_more'),
]
operations = [
migrations.AlterField(
model_name='edicto',
name='cantidad_palabras',
field=models.IntegerField(),
model_name='precio',
name='moneda',
field=models.CharField(choices=[('peso_argentino', 'peso_argentino'), ('dolar', 'dolar'), ('euro', 'euro')], default='peso_argentino', max_length=50),
),
]
... ...
# Generated by Django 4.1.9 on 2023-07-13 11:49
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('edicto', '0002_alter_comprobantepago_fecha_pago_and_more'),
]
operations = [
migrations.AddField(
model_name='edicto',
name='fecha_creacion',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='edicto',
name='fecha_modificacion',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AlterField(
model_name='edicto',
name='estado',
field=models.CharField(choices=[('iniciado', 'iniciado'), ('pendiente_de_pago', 'pendiente_de_pago'), ('publicado', 'publicado'), ('aprobado', 'aprobado'), ('rechazado', 'rechazado')], default='inciado', max_length=150),
),
]
# Generated by Django 4.1.9 on 2023-07-17 12:04
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('edicto', '0003_edicto_fecha_creacion_edicto_fecha_modificacion_and_more'),
]
operations = [
migrations.RemoveField(
model_name='edicto',
name='organismo',
),
]
# Generated by Django 4.1.9 on 2023-07-20 15:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edicto', '0004_remove_edicto_organismo'),
]
operations = [
migrations.RemoveField(
model_name='edicto',
name='dias_plubicar',
),
migrations.AddField(
model_name='edicto',
name='dias_publicar',
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name='edicto',
name='cantidad_palabras',
field=models.IntegerField(null=True),
),
migrations.AlterField(
model_name='edicto',
name='cantidad_sellos',
field=models.IntegerField(default=0),
),
]
# Generated by Django 4.1.9 on 2023-07-22 16:07
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('edicto', '0005_remove_edicto_dias_plubicar_edicto_dias_publicar_and_more'),
]
operations = [
migrations.AlterField(
model_name='edicto',
name='usuario',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Usuario'),
),
]
# Generated by Django 4.1.9 on 2023-07-24 14:46
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('edicto', '0006_alter_edicto_usuario'),
]
operations = [
migrations.AlterField(
model_name='edicto',
name='cantidad_palabras',
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name='edicto',
name='usuario',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Usuario'),
),
]
# Generated by Django 4.1.9 on 2023-07-25 13:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edicto', '0008_alter_edicto_cantidad_palabras'),
]
operations = [
migrations.AlterField(
model_name='edicto',
name='cantidad_sellos',
field=models.IntegerField(),
),
migrations.AlterField(
model_name='edicto',
name='dias_publicar',
field=models.IntegerField(),
),
migrations.AlterField(
model_name='edicto',
name='fecha_publicacion',
field=models.DateField(),
),
]
# Generated by Django 4.1.9 on 2023-07-25 13:32
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('edicto', '0009_alter_edicto_cantidad_sellos_and_more'),
]
operations = [
migrations.AlterField(
model_name='edicto',
name='cantidad_copias',
field=models.PositiveIntegerField(),
),
migrations.AlterField(
model_name='edicto',
name='cantidad_palabras',
field=models.PositiveIntegerField(),
),
migrations.AlterField(
model_name='edicto',
name='cantidad_sellos',
field=models.PositiveIntegerField(),
),
migrations.AlterField(
model_name='edicto',
name='dias_publicar',
field=models.PositiveIntegerField(),
),
migrations.AlterField(
model_name='edicto',
name='usuario',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Usuario'),
),
]
# Generated by Django 4.1.9 on 2023-07-25 13:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edicto', '0010_alter_edicto_cantidad_copias_and_more'),
]
operations = [
migrations.AlterField(
model_name='edicto',
name='cantidad_palabras',
field=models.IntegerField(),
),
]
from django.db import models
from .constants import STATUS_CHOICE
from .constants import STATUS_CHOICE, MONEDA
from usuario.models import Usuario
... ... @@ -9,10 +9,10 @@ from usuario.models import Usuario
class Precio (models.Model):
precio = models.FloatField(max_length=50, blank=False, null=False)
vigencia_desde = models.DateTimeField()
vigencia_hasta = models.DateTimeField()
vigencia_desde = models.DateField()
vigencia_hasta = models.DateField()
usuario = models.ForeignKey(Usuario, on_delete=models.CASCADE)
moneda = models.CharField(max_length=50, blank=False, null=False)
moneda = models.CharField(max_length=50, blank=False, null=False, choices=MONEDA, default='peso_argentino')
precio_ejemplar = models.FloatField(max_length=50, blank=False, null=False)
class Meta:
... ... @@ -24,7 +24,7 @@ class Precio (models.Model):
class Edicto(models.Model):
usuario = models.ForeignKey(Usuario, on_delete=models.CASCADE, verbose_name='Usuario', null=True)
usuario = models.ForeignKey(Usuario, on_delete=models.CASCADE, verbose_name='Usuario')
cuerpo_edicto = models.TextField(blank=False, null=False)
archivo = models.FileField(upload_to="uploads/%Y/%m/%d/", null=False, blank=False)
dias_publicar = models.PositiveIntegerField(blank=False, null=False)
... ...
from rest_framework import serializers
from .constants import EXTENSIONES_VALIDAS
from .models import Edicto
from .models import Edicto, Precio, ComprobantePago
class EdictoSerializer(serializers.ModelSerializer):
... ... @@ -26,3 +27,25 @@ class EdictoSerializer(serializers.ModelSerializer):
if extension.lower() not in EXTENSIONES_VALIDAS:
raise serializers.ValidationError("Archivos permitidos: .pdf, .docx, .jpg, .jpeg, .png")
return value
class PrecioSerializer(serializers.ModelSerializer):
class Meta:
model = Precio
fields = ('usuario',
'moneda',
'precio',
'precio_ejemplar',
'vigencia_desde',
'vigencia_hasta',
)
def validate(self, data):
request = self.context.get('request')
user = request.user if request else None
if not user or user.is_anonymous:
raise serializers.ValidationError("El usuario debe estar autenticado.")
if not user.is_staff:
raise serializers.ValidationError("El usuario no es parte del personal designado.")
return data
... ...
from django.core.exceptions import ValidationError
import re
def valid_extension(value):
if (not value.name.endswith('.pdf') and
not value.name.endswith('.docx') and
not value.name.endswith('.jpeg') and
not value.name.endswith('.png') and
not value.name.endswith('.jpg')):
raise ValidationError("Archivos permitidos: .pdf, .docx, .jpg, .jpeg, .png")
def contador(edicto):
numeros = re.findall(r'\b\d+(?:\.\d+)?\b', edicto)
expresiones = re.findall(r'[(\[{](.*?)[)\]}]', edicto)
expresiones = re.findall(r'\(\.\.\.', edicto)
texto = edicto.split()
cantidad_palabras = len(texto)
cantidad_numeros = len(numeros)
cantidad_numeros = len(numeros) - 1
cantidad_expresiones = len(expresiones)
total_edicto = cantidad_palabras + cantidad_numeros + cantidad_expresiones
return total_edicto
... ...
... ... @@ -9,3 +9,4 @@ router = routers.DefaultRouter()
router.register(prefix='usuario', viewset=usuario_api.UsuarioViewSet)
router.register(prefix='organismo', viewset=organismo_api.OrganismoViewSet)
router.register(prefix='edicto', viewset=edicto_api.EdictoViewSet)
router.register(prefix='precio', viewset=edicto_api.PrecioViewSet)
... ...