Skip to content

Commit

Permalink
Merge pull request EGCETSII#27 from Decide-Part-Rota/Rota1-013
Browse files Browse the repository at this point in the history
Rota1-013
  • Loading branch information
fraorober authored Nov 24, 2022
2 parents 35d8517 + 8de255c commit 9c21e2a
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 7 deletions.
4 changes: 4 additions & 0 deletions decide/census/export/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Export Folder
================================

Este directorio incluirá todos los censos que exportes.
62 changes: 62 additions & 0 deletions decide/census/templates/census_export.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends "base.html" %}
{% load i18n static %}

{% block extrahead %}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>

<link type="text/css" rel="stylesheet" href="{% static 'style.css' %}" />
{% endblock %}


{% block content %}
<div class="title">
<h2>Export Census</h2>
</div>

<div class="row justify-content-md-center">
<div class="col-sm-8">
<h4>In this view you can manage the census by exporting the users participating in a voting</h4>
</div>
</div>

<div class="row">

</div>

<form class="row gx-3 gy-2 align-items-center" action="exporting_census/" method="POST">
{% csrf_token %}
<div class="row justify-content-md-center">
<div class="col-sm-8">
<label for="specificSizeSelect">Choose a voting</label>
<select class="form-select" id="specificSizeSelect" name="voting-select">
{% for voting in votings %}
<option value="{{voting.id}}">{{voting}}</option>
{%endfor%}
</select>

</div>
</div>
<div class="row justify-content-center submit">
<div class="col col-lg-1">
<button type="submit" class="btn btn-primary">Export</button>
</div>
</div>
</form>

{% if messages %}
<ul class="messages" style="margin-top:2%">
{% for message in messages %}
{% if message.tags == 'error' %}
<li class="{{ message.tags }} alert alert-danger" role="alert">{{ message }}</li>
{% else %}
<li class="{{ message.tags }} alert alert-{{message.tags}}" role="alert">{{ message }}</li>
{% endif %}

{% endfor %}
</ul>

{% endif %}


{% endblock %}
69 changes: 69 additions & 0 deletions decide/census/templates/census_import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{% extends "base.html" %}
{% load i18n static %}

{% block extrahead %}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>

<link type="text/css" rel="stylesheet" href="{% static 'style.css' %}" />
{% endblock %}


{% block content %}
<div class="title">
<h2>Import Census</h2>
</div>

<div class="row justify-content-md-center">
<div class="col-sm-8">
<h4>In this view you can manage the census by importing users participating in a voting</h4>
</div>
</div>

<div class="row">

</div>

<form class="row gx-3 gy-2 align-items-center" action="importing_census/" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="row justify-content-md-center">
<div class="col-sm-8">
<label for="specificSizeSelect">Choose a voting</label>
<select class="form-select" id="specificSizeSelect" name="voting-select">
{% for voting in votings %}
<option value="{{voting.id}}">{{voting}}</option>
{%endfor%}
</select>

</div>
</div>
<div class="row justify-content-md-center">
<div class="col-sm-8">
<label for="file" class="form-label">Upload a csv file</label>
<input class="form-control" type="file" id="formFile" name="csv-file" required>

</div>
</div>
<div class="row justify-content-center submit">
<div class="col col-lg-1">
<button type="submit" class="btn btn-primary">Import</button>
</div>
</div>
</form>

{% if messages %}
<ul class="messages" style="margin-top:2%">
{% for message in messages %}
{% if message.tags == 'error' %}
<li class="{{ message.tags }} alert alert-danger" role="alert">{{ message }}</li>
{% else %}
<li class="{{ message.tags }} alert alert-{{message.tags}}" role="alert">{{ message }}</li>
{% endif %}

{% endfor %}
</ul>

{% endif %}


{% endblock %}
4 changes: 4 additions & 0 deletions decide/census/templates/result_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<div class="col col-lg-2">
{% if remove %}
<a href="/census/remove"><button type="submit" class="btn btn-primary">Return</button></a>
{% elif export %}
<a href="/census/export"><button type="submit" class="btn btn-primary">Return</button></a>
{% elif import %}
<a href="/census/import"><button type="submit" class="btn btn-primary">Return</button></a>
{% else %}
<a href="/census/add"><button type="submit" class="btn btn-primary">Return</button></a>
{% endif %}
Expand Down
133 changes: 129 additions & 4 deletions decide/census/tests.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import random

#Added AnonymousUser
from django.contrib.auth.models import User, AnonymousUser

#Added RequestFactory to handle the request.user.is_staff check in the views
from django.test import RequestFactory
#Added these Middleware to simulate the messages, since RequestFactory doesn't do it
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware

from rest_framework.test import APIClient

from .models import Census
#Added the necessary models
from voting.models import Voting, Question, QuestionOption
from base import mods
from base.tests import BaseTestCase
from .views import add_to_census, remove_from_census
import os

from .views import exporting_census, importing_census, add_to_census, remove_from_census
import csv

from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware


class CensusTestCase(BaseTestCase):
Expand Down Expand Up @@ -187,4 +197,119 @@ def test_delete_census_from_gui(self):
request.user = self.user
response = remove_from_census(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(existing_censuss, Census.objects.count())
self.assertEqual(existing_censuss, Census.objects.count())

class CensusExportImport(BaseTestCase):
def setUp(self):
super().setUp()

self.q = Question(desc='test question')
self.q.save()
for i in range(5):
self.opt = QuestionOption(question=self.q, option='option {}'.format(i+1))
self.opt.save()
self.v = Voting(name='test voting', question=self.q)
self.v.save()

self.voter = User(username='test_user')
self.voter.save()

self.census = Census(voting_id=self.v.id, voter_id=self.voter.id)
self.census.save()

self.factory = RequestFactory()
self.sm = SessionMiddleware()
self.mm = MessageMiddleware()

def tearDown(self):
super().tearDown()
self.census = None

self.q = None
self.opt = None

if os.path.exists('./census/export/export_' + self.v.name + '.csv'):
os.remove('./census/export/export_' + self.v.name + '.csv')

if os.path.exists('./census/export/import_test.csv'):
os.remove('./census/export/import_test.csv')

self.v = None
self.voter = None
self.factory = None
self.sm = None
self.mm = None

self.file = None

def test_export_census(self):
self.user = AnonymousUser()
data = {'voting-select': self.v.id}
request = self.factory.post('/census/export/exporting_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = exporting_census(request)
self.assertEqual(response.status_code, 401)
self.assertFalse(os.path.exists('./census/export/export_' + self.v.name + '.csv'))

user_admin = User.objects.get(username="admin")
self.user = user_admin
data = {'voting-select': self.v.id}
request = self.factory.post('/census/export/exporting_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = exporting_census(request)
self.assertEqual(response.status_code, 302)
self.assertTrue(os.path.exists('./census/export/export_' + self.v.name + '.csv'))
with open('./census/export/export_' + self.v.name + '.csv', 'r') as csvfile:
self.assertEqual(2, len(csvfile.readlines()))


def generate_import_csv(self):
#Creates a csv file with a row containing the user admin
try:
user_admin = User.objects.get(username="admin")
self.file = open('./census/export/import_test.csv', 'w', encoding='UTF8')
wr = csv.writer(self.file)
header = ['username', 'first_name', 'last_name', 'email']
wr.writerow(header)
row = [user_admin.username,'','','']
wr.writerow(row)
finally:
self.file.close()

return self.file

def test_import_census(self):
#Gets the csv file with the user admin to import it into the census created in the set up method
import_csv = self.generate_import_csv()
file_path = import_csv.name

f = open(file_path, "r")

self.user = AnonymousUser()
data = {'voting-select': self.v.id, 'csv-file': f}
request = self.factory.post('/census/import/importing_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = importing_census(request)
self.assertEqual(response.status_code, 401)

f.close()
f = open(file_path, "r")

user_admin = User.objects.get(username="admin")
self.user = user_admin
data = {'voting-select': self.v.id, 'csv-file': f}
request = self.factory.post('/census/import/importing_census/', data, format='json')
self.sm.process_request(request)
self.mm.process_request(request)
request.user = self.user
response = importing_census(request)
self.assertEqual(response.status_code, 302)
self.assertTrue(Census.objects.all().filter(voting_id=self.v.id,voter_id=user_admin.id).exists())

f.close()
6 changes: 5 additions & 1 deletion decide/census/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
path('add/', views.census_add, name='census_add'),
path('add/add_to_census', views.add_to_census),
path('remove/', views.census_remove, name='census_remove'),
path('remove/remove_from_census', views.remove_from_census)
path('remove/remove_from_census', views.remove_from_census),
path('export/', views.export_census),
path('export/exporting_census/', views.exporting_census),
path('import/', views.import_census),
path('import/importing_census/', views.importing_census)
]
Loading

0 comments on commit 9c21e2a

Please sign in to comment.