forked from hiperderecho/proyectos_de_ley
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
315 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.http import HttpResponse | ||
from rest_framework.renderers import JSONRenderer | ||
from rest_framework_csv import renderers | ||
|
||
|
||
class JSONResponse(HttpResponse): | ||
""" | ||
An HttpResponse that renders its content into JSON. | ||
""" | ||
def __init__(self, data, **kwargs): | ||
content = JSONRenderer().render(data) | ||
kwargs['content_type'] = 'application/json' | ||
super(JSONResponse, self).__init__(content, **kwargs) | ||
|
||
|
||
class CSVResponse(HttpResponse): | ||
""" | ||
An HttpResponse that renders its content into CSV. | ||
""" | ||
def __init__(self, data, **kwargs): | ||
content = CSVRenderer().render(data) | ||
kwargs['content_type'] = 'text/csv' | ||
super(CSVResponse, self).__init__(content, **kwargs) | ||
|
||
|
||
class CSVRenderer(renderers.CSVRenderer): | ||
media_type = 'text/csv' | ||
format = 'csv' | ||
|
||
def render(self, data, media_type=None, renderer_context=None): | ||
return super(CSVRenderer, self).render(data, media_type, renderer_context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import datetime | ||
|
||
from django.test import TestCase | ||
|
||
from pdl.models import Proyecto, Seguimientos, Expedientes | ||
from api import utils | ||
|
||
|
||
class Object(object): | ||
"""Dummy class for testing.""" | ||
pass | ||
|
||
|
||
class TestSeguimientos(TestCase): | ||
def setUp(self): | ||
proyecto = Proyecto(**{ | ||
"numero_proyecto": "02764/2013-CR", | ||
"codigo": "02764", | ||
"short_url": "4zhube", | ||
"titulo": "Propone Ley Universitaria", | ||
"iniciativas_agrupadas": ['01790', '01800'], | ||
"fecha_presentacion": "2010-10-10", | ||
"id": 1, | ||
}) | ||
proyecto.save() | ||
|
||
seguimiento1 = { | ||
'fecha': '2013-10-14', | ||
'evento': 'Decretado a... Educación, Juventud y Deporte', | ||
'proyecto': proyecto, | ||
} | ||
seguimiento2 = { | ||
'fecha': '2013-10-15', | ||
'evento': 'En comisión Educación, Juventud y Deporte', | ||
'proyecto': proyecto, | ||
} | ||
b = Seguimientos(**seguimiento1) | ||
b.save() | ||
b = Seguimientos(**seguimiento2) | ||
b.save() | ||
|
||
expediente1 = seguimiento1 # Expediente y Seguimiento con casi lo mismo | ||
expediente2 = seguimiento2 # Expediente y Seguimiento con casi lo mismo | ||
b = Expedientes(**expediente1) | ||
b.save() | ||
b = Expedientes(**expediente2) | ||
b.save() | ||
|
||
def test_prepare_json_for_d3(self): | ||
item = Object() | ||
item.numero_proyecto = "02764/2013-CR" | ||
item.codigo = "02764" | ||
item.titulo = "Propone Ley Universitaria" | ||
item.iniciativas_agrupadas = '{02764}' | ||
|
||
expected = {'iniciativas': [{'codigo': '02764', 'url': '/p/4zhube'}]} | ||
result = utils.prepare_json_for_d3(item) | ||
self.assertEqual(expected, result) | ||
|
||
def test_prepare_json_for_d3_no_project(self): | ||
item = Object() | ||
item.numero_proyecto = "02764/2013-CR" | ||
item.codigo = "02764" | ||
item.titulo = "Propone Ley Universitaria" | ||
item.iniciativas_agrupadas = '{027640000}' | ||
|
||
expected = {'iniciativas': []} | ||
result = utils.prepare_json_for_d3(item) | ||
self.assertEqual(expected, result) | ||
|
||
def test_convert_date_to_string(self): | ||
fecha = datetime.datetime(2010, 10, 10) | ||
expected = '2010-10-10' | ||
result = utils.convert_date_to_string(fecha) | ||
self.assertEqual(expected, result) | ||
|
||
def test_convert_date_to_string_with_exception(self): | ||
fecha = '2010-10-10' | ||
expected = '2010-10-10' | ||
result = utils.convert_date_to_string(fecha) | ||
self.assertEqual(expected, result) | ||
|
||
def test_get_seguimientos_from_proyecto_id(self): | ||
result = utils.get_seguimientos_from_proyecto_id('1') | ||
expected = 'Decretado a... Educación, Juventud y Deporte' | ||
self.assertEqual(expected, result[0]['headline']) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import datetime | ||
import re | ||
import unicodedata | ||
|
||
from django.db.models import Q | ||
|
||
from pdl.models import Proyecto | ||
from pdl.models import Seguimientos | ||
from pdl.models import Slug | ||
|
||
|
||
def get_projects_for_person(names): | ||
projects_and_person = [] | ||
for name in names: | ||
queryset = Proyecto.objects.filter( | ||
congresistas__icontains=name).order_by('-codigo') | ||
projects_list = [str(i.codigo) + '-2011' for i in queryset] | ||
obj = {'nombre': name, 'proyectos': projects_list} | ||
projects_and_person.append(obj) | ||
return projects_and_person | ||
|
||
|
||
def get_projects_by_comission_for_person(comision, names): | ||
projects_and_person = [] | ||
for name in names: | ||
queryset = Proyecto.objects.filter( | ||
congresistas__icontains=name).order_by('-codigo') | ||
if comision != '': | ||
queryset = queryset.filter(nombre_comision__icontains=comision) | ||
projects_list = [str(i.codigo) + '-2011' for i in queryset] | ||
obj = {'nombre': name, 'proyectos': projects_list} | ||
projects_and_person.append(obj) | ||
return projects_and_person | ||
|
||
|
||
def find_name_from_short_name(nombre_corto): | ||
nombre_corto = unicodedata.normalize('NFKD', nombre_corto).encode('ascii', 'ignore') | ||
nombre_corto = re.sub('\s+', ' ', nombre_corto.decode('utf-8')) | ||
nombre_corto = nombre_corto.split(' ') | ||
if len(nombre_corto) < 2: | ||
return ['---error---', 'ingrese un nombre y un apellido'] | ||
|
||
nombre_corto = nombre_corto[:2] | ||
res = Slug.objects.filter(Q(ascii__icontains=nombre_corto[0]) & Q(ascii__icontains=nombre_corto[1])) | ||
|
||
if len(res) > 0: | ||
return [i.nombre for i in res] | ||
else: | ||
return ['---error---', 'no se pudo encontrar congresista'] | ||
|
||
|
||
def prepare_json_for_d3(item): | ||
nodes = [] | ||
append = nodes.append | ||
|
||
iniciativas_agrupadas = item.iniciativas_agrupadas.replace('{', '').replace('}', '').split(',') | ||
|
||
for i in iniciativas_agrupadas: | ||
try: | ||
queryset = Proyecto.objects.get(codigo=i) | ||
except Proyecto.DoesNotExist: | ||
continue | ||
node = {"codigo": i, "url": "/p/" + queryset.short_url} | ||
append(node) | ||
|
||
sorted_nodes_by_code = sorted(nodes, key=lambda k: k['codigo']) | ||
return {'iniciativas': sorted_nodes_by_code} | ||
|
||
|
||
def get_seguimientos_from_proyecto_id(id): | ||
res = Seguimientos.objects.all().filter(proyecto_id=id) | ||
seguimientos = [] | ||
append = seguimientos.append | ||
for i in res: | ||
obj = {} | ||
obj['startDate'] = str(i.fecha).replace("-", ",") | ||
obj['headline'] = i.evento | ||
append(obj) | ||
return seguimientos | ||
|
||
|
||
def convert_date_to_string(dateobj): | ||
try: | ||
fecha = datetime.datetime.strftime(dateobj, '%Y-%m-%d') | ||
except TypeError: | ||
return dateobj | ||
return fecha | ||
|
||
|
||
class MyObj(object): | ||
pass |
Oops, something went wrong.