Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Marta Miranda
/
boletin_api
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Enzo Yair
2023-09-08 07:16:37 -0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
348e9aaba28d9b748722d6df43e4b510ce3d8758
348e9aab
1 parent
87db16a0
crear dos endpoints para comprobantepago
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
212 additions
and
41 deletions
project/apps/edicto/admin.py
project/apps/edicto/api.py
project/apps/edicto/filters.py
project/apps/edicto/migrations/0011_alter_comprobantepago_edicto.py
project/apps/edicto/migrations/0012_rename_precio_precio_precio_palabra.py
project/apps/edicto/migrations/0013_comprobantepago_archivo_alter_edicto_estado.py
project/apps/edicto/migrations/0014_alter_comprobantepago_fecha_pago.py
project/apps/edicto/models.py
project/apps/edicto/precio.py
project/apps/edicto/serializer.py
project/router.py
project/apps/edicto/admin.py
View file @
348e9aa
from
django.contrib
import
admin
from
django.core.exceptions
import
ValidationError
from
django.utils
import
timezone
from
.models
import
Edicto
,
Precio
# Register your models here.
admin
.
site
.
register
(
Precio
)
class
PrecioAdmin
(
admin
.
ModelAdmin
):
def
save_model
(
self
,
request
,
obj
,
form
,
change
):
precios_activos
=
Precio
.
objects
.
filter
(
vigencia_hasta__gte
=
timezone
.
now
()
.
date
())
if
not
change
:
today
=
timezone
.
now
()
.
date
()
if
obj
.
vigencia_desde
<=
today
:
if
not
precios_activos
.
exists
():
obj
.
save
()
else
:
raise
ValidationError
(
"Ya existe un precio activo."
)
else
:
raise
ValidationError
(
"La fecha de inicio no es válida."
)
else
:
obj
.
save
()
admin
.
site
.
register
(
Precio
,
PrecioAdmin
)
admin
.
site
.
register
(
Edicto
)
...
...
project/apps/edicto/api.py
View file @
348e9aa
...
...
@@ -5,11 +5,12 @@ from rest_framework.permissions import IsAuthenticated
from
rest_framework.decorators
import
action
from
datetime
import
datetime
from
.models
import
Edicto
,
Precio
from
.serializer
import
EdictoSerializer
,
PrecioSerializer
from
.models
import
Edicto
,
Precio
,
ComprobantePago
from
.serializer
import
EdictoSerializer
,
PrecioSerializer
,
PagoSerializer
,
ComprobanteSerializer
from
.filters
import
EdictoFilter
,
PrecioFilter
from
.permissions
import
IsAdminOrAuthorized
from
.utils
import
contador
from
.precio
import
calculadora
class
EdictoViewSet
(
mixins
.
CreateModelMixin
,
...
...
@@ -54,3 +55,42 @@ class PrecioViewSet(viewsets.ReadOnlyModelViewSet):
ordering_fields
=
(
'usuario'
,
)
ordering
=
'usuario'
queryset
=
Precio
.
objects
.
all
()
class
PagoViewSets
(
mixins
.
CreateModelMixin
,
mixins
.
RetrieveModelMixin
,
mixins
.
UpdateModelMixin
,
mixins
.
DestroyModelMixin
,
mixins
.
ListModelMixin
,
viewsets
.
GenericViewSet
):
serializer_class
=
PagoSerializer
permission_classes
=
[
IsAuthenticated
,
IsAdminOrAuthorized
]
queryset
=
ComprobantePago
.
objects
.
all
()
def
perform_create
(
self
,
serializer
):
try
:
edicto
=
Edicto
.
objects
.
get
(
estado
=
'aprobado'
)
precio_admi
=
Precio
.
objects
.
latest
(
'id'
)
precio
=
Precio
.
objects
.
get
(
id
=
precio_admi
.
id
)
monto
=
calculadora
(
edicto
,
precio
)
usuario_actual
=
self
.
request
.
user
if
edicto
.
usuario
!=
usuario_actual
:
raise
UsuarioNoAutorizado
serializer
.
validated_data
[
'monto'
]
=
monto
serializer
.
validated_data
[
'edicto'
]
=
edicto
serializer
.
save
()
except
Edicto
.
DoesNotExist
:
raise
EdictoNoEncontrado
except
Precio
.
DoesNotExist
:
raise
PrecioNoEncontrado
class
ComprobanteViewSets
(
mixins
.
CreateModelMixin
,
mixins
.
RetrieveModelMixin
,
mixins
.
UpdateModelMixin
,
mixins
.
DestroyModelMixin
,
mixins
.
ListModelMixin
,
viewsets
.
GenericViewSet
):
serializer_class
=
ComprobanteSerializer
queryset
=
ComprobantePago
.
objects
.
all
()
permission_classes
=
[
IsAuthenticated
,
]
...
...
project/apps/edicto/filters.py
View file @
348e9aa
...
...
@@ -20,12 +20,8 @@ class PrecioFilter(filters.FilterSet):
model
=
Precio
fields
=
{
'usuario'
:
[
'exact'
],
'precio'
:
[
'exact'
],
'precio
_palabra
'
:
[
'exact'
],
'vigencia_desde'
:
[
'exact'
],
'vigencia_hasta'
:
[
'exact'
],
'moneda'
:
[
'exact'
],
}
...
...
project/apps/edicto/migrations/0011_alter_comprobantepago_edicto.py
0 → 100644
View file @
348e9aa
# Generated by Django 4.1.9 on 2023-09-06 14:56
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'edicto'
,
'0010_alter_edicto_fecha_creacion'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'comprobantepago'
,
name
=
'edicto'
,
field
=
models
.
OneToOneField
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
to
=
'edicto.edicto'
),
),
]
...
...
project/apps/edicto/migrations/0012_rename_precio_precio_precio_palabra.py
0 → 100644
View file @
348e9aa
# Generated by Django 4.1.9 on 2023-09-06 15:01
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'edicto'
,
'0011_alter_comprobantepago_edicto'
),
]
operations
=
[
migrations
.
RenameField
(
model_name
=
'precio'
,
old_name
=
'precio'
,
new_name
=
'precio_palabra'
,
),
]
...
...
project/apps/edicto/migrations/0013_comprobantepago_archivo_alter_edicto_estado.py
0 → 100644
View file @
348e9aa
# Generated by Django 4.1.9 on 2023-09-08 10:13
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'edicto'
,
'0012_rename_precio_precio_precio_palabra'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'comprobantepago'
,
name
=
'archivo'
,
field
=
models
.
FileField
(
null
=
True
,
upload_to
=
''
),
),
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
=
'iniciado'
,
max_length
=
150
),
),
]
...
...
project/apps/edicto/migrations/0014_alter_comprobantepago_fecha_pago.py
0 → 100644
View file @
348e9aa
# Generated by Django 4.1.9 on 2023-09-08 10:15
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'edicto'
,
'0013_comprobantepago_archivo_alter_edicto_estado'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'comprobantepago'
,
name
=
'fecha_pago'
,
field
=
models
.
DateField
(
null
=
True
),
),
]
...
...
project/apps/edicto/models.py
View file @
348e9aa
...
...
@@ -8,7 +8,7 @@ from usuario.models import Usuario
class
Precio
(
models
.
Model
):
precio
=
models
.
FloatField
(
max_length
=
50
,
blank
=
False
,
null
=
False
)
precio
_palabra
=
models
.
FloatField
(
max_length
=
50
,
blank
=
False
,
null
=
False
)
vigencia_desde
=
models
.
DateField
()
vigencia_hasta
=
models
.
DateField
()
usuario
=
models
.
ForeignKey
(
Usuario
,
on_delete
=
models
.
CASCADE
)
...
...
@@ -29,7 +29,7 @@ class Edicto(models.Model):
archivo
=
models
.
FileField
(
upload_to
=
"uploads/
%
Y/
%
m/
%
d/"
,
null
=
False
,
blank
=
False
)
dias_publicar
=
models
.
PositiveIntegerField
(
blank
=
False
,
null
=
False
)
cantidad_sellos
=
models
.
PositiveIntegerField
(
blank
=
False
,
null
=
False
)
estado
=
models
.
CharField
(
max_length
=
150
,
choices
=
STATUS_CHOICE
,
default
=
'inciado'
)
estado
=
models
.
CharField
(
max_length
=
150
,
choices
=
STATUS_CHOICE
,
default
=
'in
i
ciado'
)
cantidad_palabras
=
models
.
IntegerField
(
blank
=
False
,
null
=
False
)
cantidad_copias
=
models
.
PositiveIntegerField
(
blank
=
False
,
null
=
False
)
fecha_publicacion
=
models
.
DateField
(
blank
=
True
,
null
=
True
)
...
...
@@ -42,18 +42,19 @@ class Edicto(models.Model):
verbose_name_plural
=
'Edictos'
def
__str__
(
self
):
return
self
.
cuerpo_edicto
return
f
"{self.cuerpo_edicto} Edicto"
class
ComprobantePago
(
models
.
Model
):
monto
=
models
.
FloatField
(
blank
=
False
,
null
=
False
)
numero_comprobante
=
models
.
CharField
(
max_length
=
300
,
blank
=
False
,
null
=
True
)
fecha_pago
=
models
.
DateTimeField
(
blank
=
False
,
null
=
True
)
edicto
=
models
.
ForeignKey
(
Edicto
,
on_delete
=
models
.
CASCADE
)
fecha_pago
=
models
.
DateField
(
blank
=
False
,
null
=
True
)
edicto
=
models
.
OneToOneField
(
Edicto
,
on_delete
=
models
.
CASCADE
)
archivo
=
models
.
FileField
(
blank
=
False
,
null
=
True
)
class
Meta
:
verbose_name
=
'ComprobantePago'
verbose_name_plural
=
'ComprobantesPagos'
def
__str__
(
self
):
return
self
.
numero_comprobante
return
f
"{self.numero_comprobante} comprobante pago de {self.edicto.cuerpo_edicto}"
...
...
project/apps/edicto/precio.py
View file @
348e9aa
from
django.db
import
transaction
from
django.utils
import
timezone
from
.models
import
Edicto
,
ComprobantePago
,
Precio
from
datetime
import
datetime
,
time
def
contador
(
edicto_id
,
precio_id
):
edicto
=
Edicto
.
objects
.
select_related
(
'usuario'
)
.
get
(
id
=
edicto_id
)
organismo
=
edicto
.
usuario
.
organismo
.
es_publico
def
calculadora
(
edicto
,
precio
):
try
:
with
transaction
.
atomic
():
organismo
=
edicto
.
usuario
.
organismo
es_publico
=
organismo
.
es_publico
if
organismo
else
False
sellos
=
edicto
.
cantidad_sellos
palabras
=
edicto
.
cantidad_palabras
copias
=
edicto
.
cantidad_copias
publicar
=
edicto
.
dias_publicar
precio_valores
=
Precio
.
objects
.
values
(
'precio'
,
'precio_ejemplar'
,
'vigencia_desde'
,
'vigencia_hasta'
,)
.
get
(
id
=
precio_id
)
precio
=
precio_valores
[
'precio'
]
precio_ejemplar
=
precio_valores
[
'precio_ejemplar'
]
vigencia_desde
=
precio_valores
[
'vigencia_desde'
]
vigencia_hasta
=
precio_valores
[
'vigencia_hasta'
]
current_datetime
=
timezone
.
now
()
precio_valores
=
{
'precio_palabra'
:
precio
.
precio
,
'precio_ejemplar'
:
precio
.
precio_ejemplar
,
'vigencia_desde'
:
precio
.
vigencia_desde
,
'vigencia_hasta'
:
precio
.
vigencia_hasta
,
}
current_datetime
=
timezone
.
now
()
.
replace
(
tzinfo
=
None
)
vigencia_desde
=
datetime
.
combine
(
precio_valores
[
'vigencia_desde'
],
time
.
min
)
vigencia_hasta
=
datetime
.
combine
(
precio_valores
[
'vigencia_hasta'
],
time
.
max
)
if
not
(
vigencia_desde
<=
current_datetime
<=
vigencia_hasta
):
raise
ValueError
(
"El precio seleccionado no se encuentra dentro de las fechas permitidas."
)
result_palabra
=
(
sellos
+
palabras
)
*
precio
result_ejemplar
=
(
copias
*
precio_ejemplar
)
+
(
publicar
*
precio_ejemplar
)
result_palabra
=
(
sellos
+
palabras
)
*
precio_valores
[
'precio'
]
result_ejemplar
=
\
(
copias
*
precio_valores
[
'precio_ejemplar'
])
+
(
publicar
*
precio_valores
[
'precio_ejemplar'
])
if
organism
o
:
if
es_public
o
:
resultado
=
result_ejemplar
+
result_palabra
/
2
else
:
resultado
=
result_ejemplar
+
result_palabra
precio_resultado
=
ComprobantePago
(
edicto
=
edicto
,
resultado
=
resultado
)
precio_resultado
.
save
()
return
precio_resultado
return
resultado
except
ValueError
as
values
:
raise
ValueError
(
"Los valores proporcionados son incorrectos: "
+
str
(
values
))
\ No newline at end of file
...
...
project/apps/edicto/serializer.py
View file @
348e9aa
...
...
@@ -3,7 +3,7 @@ from rest_framework import serializers
from
.constants
import
EXTENSIONES_VALIDAS
from
.models
import
Edicto
,
Precio
from
.models
import
Edicto
,
Precio
,
ComprobantePago
from
usuario.serializers
import
UsuarioListaSerializer
...
...
@@ -51,7 +51,7 @@ class PrecioSerializer(serializers.ModelSerializer):
model
=
Precio
fields
=
(
'usuario'
,
'moneda'
,
'precio'
,
'precio
_palabra
'
,
'precio_ejemplar'
,
'vigencia_desde'
,
'vigencia_hasta'
,
...
...
@@ -60,3 +60,38 @@ class PrecioSerializer(serializers.ModelSerializer):
included_serializers
=
{
'usuario'
:
UsuarioListaSerializer
}
class
PagoSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
ComprobantePago
fields
=
(
'monto'
,
'edicto'
,
)
read_only_fields
=
(
'monto'
,
)
included_serializers
=
{
'edicto'
:
EdictoSerializer
,
}
def
validate
(
self
,
data
):
edicto
=
data
.
get
(
'edicto'
)
if
edicto
and
edicto
.
estado
!=
'pendiente_de_pago'
:
raise
serializers
.
ValidationError
(
"El edicto debe estar en estado 'pendiente de pago' para visualizar el monto a pagar."
)
return
data
class
ComprobanteSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
ComprobantePago
fields
=
(
'numero_comprobante'
,
'fecha_pago'
,
)
included_serializers
=
{
'edicto'
:
EdictoSerializer
,
'monto'
:
'monto.serializers.PagoSerialzer'
,
}
\ No newline at end of file
...
...
project/router.py
View file @
348e9aa
...
...
@@ -2,7 +2,7 @@ from rest_framework import routers
from
organismo
import
api
as
organismo_api
from
usuario
import
api
as
usuario_api
from
edicto.api
import
EdictoViewSet
from
edicto.api
import
EdictoViewSet
,
ComprobanteViewSets
from
edicto
import
api
as
edicto_api
# Define routes
router
=
routers
.
DefaultRouter
()
...
...
@@ -11,5 +11,7 @@ router.register(prefix='usuario', viewset=usuario_api.UsuarioViewSet)
router
.
register
(
prefix
=
'organismo'
,
viewset
=
organismo_api
.
OrganismoViewSet
)
router
.
register
(
r'edicto'
,
EdictoViewSet
,
basename
=
'edicto'
)
router
.
register
(
prefix
=
'precio'
,
viewset
=
edicto_api
.
PrecioViewSet
)
router
.
register
(
prefix
=
'pago'
,
viewset
=
edicto_api
.
PagoViewSets
)
router
.
register
(
r'comprobante'
,
ComprobanteViewSets
,
basename
=
'comprobante'
)
...
...
Please
register
or
login
to post a comment