diff --git a/actas/libs.py b/actas/libs.py index 17b65b9..65983cf 100644 --- a/actas/libs.py +++ b/actas/libs.py @@ -3,12 +3,12 @@ from itertools import cycle import re +from django.conf import settings from django.contrib.auth.models import User from django.utils import timezone from pyquery import PyQuery as pq import requests -from .apps import ActasConfig from .models import Comuna, Acta, Item, ActaRespuestaItem @@ -109,13 +109,13 @@ def validar_datos_geograficos(acta): region_seleccionada = acta.get('geo', {}).get('region') if type(region_seleccionada) != int: - return ['Región inválida'] + return ['Región inválida.'] if type(provincia_seleccionada) != int: - return ['Provincia inválida'] + return ['Provincia inválida.'] if type(comuna_seleccionada) != int: - return ['Comuna inválida'] + return ['Comuna inválida.'] comunas = Comuna.objects.filter(pk=comuna_seleccionada) @@ -131,31 +131,15 @@ def validar_datos_geograficos(acta): return errores -def _validar_participante(participante, pos): - errores = [] - - if not verificar_rut(participante.get('rut')): - errores.append('RUT del participante {0:d} es inválido.'.format(pos)) - - nombre = participante.get('nombre') - apellido = participante.get('apellido') - - if type(nombre) not in [str, unicode] or len(nombre) < 2: - errores.append('Nombre del participante {0:d} es inválido.'.format(pos)) - - if type(apellido) not in [str, unicode] or len(apellido) < 2: - errores.append('Apellido del participante {0:d} es inválido.'.format(pos)) - - return errores - - def validar_participantes(acta): errores = [] participantes = acta.get('participantes', []) + config = obtener_config() + if type(participantes) != list \ - or not (ActasConfig.participantes_min <= len(participantes) <= ActasConfig.participantes_max): + or not (config['participantes_min'] <= len(participantes) <= config['participantes_max']): errores.append('Error en el formato de los participantes.') return errores @@ -169,14 +153,14 @@ def validar_participantes(acta): # Ruts diferentes ruts = set(ruts_participantes) - if not (ActasConfig.participantes_min <= len(ruts) <= ActasConfig.participantes_max): + if not (config['participantes_min'] <= len(ruts) <= config['participantes_max']): return ['Existen RUTs repetidos'] # Nombres diferentes nombres = set( (p['nombre'].lower(), p['apellido'].lower(), ) for p in participantes ) - if not (ActasConfig.participantes_min <= len(nombres) <= ActasConfig.participantes_max): + if not (config['participantes_min'] <= len(nombres) <= config['participantes_max']): return ['Existen nombres repetidos'] # Verificar que los participantes no hayan enviado una acta antes @@ -189,6 +173,24 @@ def validar_participantes(acta): return errores +def _validar_participante(participante, pos): + errores = [] + + if not verificar_rut(participante.get('rut')): + errores.append('RUT del participante {0:d} es inválido.'.format(pos)) + + nombre = participante.get('nombre') + apellido = participante.get('apellido') + + if type(nombre) not in [str, unicode] or len(nombre) < 2: + errores.append('Nombre del participante {0:d} es inválido.'.format(pos)) + + if type(apellido) not in [str, unicode] or len(apellido) < 2: + errores.append('Apellido del participante {0:d} es inválido.'.format(pos)) + + return errores + + def validar_cedulas_participantes(acta): errores = [] @@ -228,14 +230,6 @@ def validar_items(acta): return errores -def _crear_usuario(datos_usuario): - usuario = User(username=datos_usuario['rut']) - usuario.first_name = datos_usuario['nombre'] - usuario.last_name = datos_usuario['apellido'] - usuario.save() - return usuario - - def guardar_acta(datos_acta): acta = Acta( comuna=Comuna.objects.get(pk=datos_acta['geo']['comuna']), @@ -279,3 +273,28 @@ def validar_acta_json(request): return (acta, errores,) return (acta, [],) + + +def obtener_config(): + config = { + 'participantes_min': 4, + 'participantes_max': 10, + } + + if hasattr(settings, 'DISCUSION_ABIERTA') and type(settings.DISCUSION_ABIERTA) == dict: + config['participantes_min'] = int( + settings.DISCUSION_ABIERTA.get('PARTICIPANTES_MIN', config['participantes_min']) + ) + config['participantes_max'] = int( + settings.DISCUSION_ABIERTA.get('PARTICIPANTES_MAX', config['participantes_max']) + ) + + return config + + +def _crear_usuario(datos_usuario): + usuario = User(username=datos_usuario['rut']) + usuario.first_name = datos_usuario['nombre'] + usuario.last_name = datos_usuario['apellido'] + usuario.save() + return usuario diff --git a/actas/tests/test_libs_validar_datos_geograficos.py b/actas/tests/test_libs_validar_datos_geograficos.py new file mode 100644 index 0000000..167c1ea --- /dev/null +++ b/actas/tests/test_libs_validar_datos_geograficos.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from django.test.testcases import TransactionTestCase + +from ..libs import validar_datos_geograficos + + +class LibsValidarDatosGeograficosTestCase(TransactionTestCase): + + fixtures = ['regiones.json', 'provincias.json', 'comunas.json'] + + def test_datos_vacios(self): + result = validar_datos_geograficos({}) + self.assertEquals(['Región inválida.'], result) + + result = validar_datos_geograficos({'geo': {'region': 13}}) + self.assertEquals(['Provincia inválida.'], result) + + result = validar_datos_geograficos({'geo': {'region': 13, 'provincia': 131}}) + self.assertEquals(['Comuna inválida.'], result) + + def test_comuna_invalida(self): + result = validar_datos_geograficos({'geo': {'region': 13, 'provincia': 131, 'comuna': 13199}}) + self.assertEquals(['Comuna inválida.'], result) + + def test_provincia_invalida(self): + result = validar_datos_geograficos({'geo': {'region': 13, 'provincia': 132, 'comuna': 13101}}) + self.assertEquals(['Provincia no corresponde a la comuna.'], result) + + def test_region_invalida(self): + result = validar_datos_geograficos({'geo': {'region': 99, 'provincia': 131, 'comuna': 13101}}) + self.assertEquals(['Región no corresponde a la provincia.'], result) diff --git a/actas/views.py b/actas/views.py index e609c82..49aa78b 100644 --- a/actas/views.py +++ b/actas/views.py @@ -1,11 +1,10 @@ # -*- coding: utf-8 -*- -from django.conf import settings from django.db import transaction from django.http import JsonResponse from django.shortcuts import render from django.views.decorators.csrf import ensure_csrf_cookie -from .libs import validar_acta_json, validar_cedulas_participantes, guardar_acta +from .libs import validar_acta_json, validar_cedulas_participantes, guardar_acta, obtener_config from .models import GrupoItems @@ -25,18 +24,13 @@ def subir(request): def acta_base(request): - participantes_min = 4 - participantes_max = 10 - - if hasattr(settings, 'DISCUSION_ABIERTA') and type(settings.DISCUSION_ABIERTA) == dict: - participantes_min = int(settings.DISCUSION_ABIERTA.get('PARTICIPANTES_MIN', participantes_min)) - participantes_max = int(settings.DISCUSION_ABIERTA.get('PARTICIPANTES_MAX', participantes_max)) + config = obtener_config() acta = { - 'min_participantes': participantes_min, - 'max_participantes': participantes_max, + 'min_participantes': config['participantes_min'], + 'max_participantes': config['participantes_max'], 'geo': {}, - 'participantes': [{} for _ in range(participantes_min)] + 'participantes': [{} for _ in range(config['participantes_min'])] } acta['itemsGroups'] = [g.to_dict() for g in GrupoItems.objects.all().order_by('orden')]