Skip to content

Commit

Permalink
Merge pull request #29 from egc-sierrezuela-3/feature/censo-13
Browse files Browse the repository at this point in the history
FUNCIONALIDAD DE EXPORTAR CENSO
  • Loading branch information
margarcac1 authored Dec 21, 2021
2 parents 3896bbd + ac5c6c9 commit f85c53f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions decide/census/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django import forms

class votacionForm(forms.Form):
votacion = forms.IntegerField(label='Id de la votación', widget=forms.TextInput, required=True)
19 changes: 19 additions & 0 deletions decide/census/templates/exportarcenso.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'base.html' %}

{% block extrahead %}{% endblock %}

{% block content %}

<h2> Funcionalidad de censos</h2>

<b> Se han encontrado los siguientes censos: </b>

{% for c in censos %}
<b> Id del votante:</b> {{c.voter_id}}
{% endfor %}

<button type="button" onclick="location.href='exportCSV/'">Exportar censo</button>
{% endblock %}


{% block extrabody %}{% endblock %}
29 changes: 29 additions & 0 deletions decide/census/templates/inicio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends 'base.html' %}

{% block extrahead %}{% endblock %}

{% block content %}

<h2> Módulo de censos</h2>

<h3> Exportación de censo</h3>

<form id="formulario" method="post" action='''>{% csrf_token %}
{{formulario}}
<input type='submit' value='Buscar censos de esta votación'/>
</form>

{% if censos %}
{% for c in censos %}
Id del votante: {{c.voter_id}}
<br>
{% endfor %}

<br>
<button type="button" onclick="location.href='exportCSV/{{idVotacion}}'">Exportar censo</button>

{% endif %}

{% endblock %}

{% block extrabody %}{% endblock %}
4 changes: 3 additions & 1 deletion decide/census/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@


urlpatterns = [
path('', views.CensusCreate.as_view(), name='census_create'),
path('', views.elegirVotacion),
path('exportCSV/<int:idVotacion>', views.exportarCenso),
path('create/', views.CensusCreate.as_view(), name='census_create'),
path('<int:voting_id>/', views.CensusDetail.as_view(), name='census_detail'),
]
33 changes: 33 additions & 0 deletions decide/census/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db.utils import IntegrityError
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.status import (
Expand All @@ -12,6 +13,9 @@

from base.perms import UserIsStaff
from .models import Census
import csv
from django.shortcuts import render
from census.forms import votacionForm


class CensusCreate(generics.ListCreateAPIView):
Expand All @@ -34,6 +38,7 @@ def list(self, request, *args, **kwargs):
return Response({'voters': voters})



class CensusDetail(generics.RetrieveDestroyAPIView):

def destroy(self, request, voting_id, *args, **kwargs):
Expand All @@ -49,3 +54,31 @@ def retrieve(self, request, voting_id, *args, **kwargs):
except ObjectDoesNotExist:
return Response('Invalid voter', status=ST_401)
return Response('Valid voter')



def elegirVotacion(request):
formulario = votacionForm()
censos = None
idVotacion = None
if request.method == 'POST':
formulario = votacionForm(request.POST)
if formulario.is_valid(): #Corre la validación correspondiente
idVotacion = formulario.cleaned_data['votacion']
censos = Census.objects.filter(voting_id = idVotacion)
return render(request, 'inicio.html', {'formulario':formulario, 'censos':censos, 'idVotacion':idVotacion})





def exportarCenso(request, idVotacion):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="censo.csv"'
file = csv.writer(response)
censos = Census.objects.filter(voting_id = idVotacion)
file.writerow(['Id votante'])
for c in censos:
file.writerow([c.voter_id])

return response
1 change: 1 addition & 0 deletions decide/decide/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
from census import views


schema_view = get_swagger_view(title='Decide API')
Expand Down

0 comments on commit f85c53f

Please sign in to comment.