From d47ea765e609fbcdc7dc3ac07f375f927698b2e0 Mon Sep 17 00:00:00 2001 From: joseluisps21 Date: Thu, 24 Nov 2022 15:14:28 +0100 Subject: [PATCH 1/2] feat: Added Groups (Maritial Status and Nationality) functionality With this functionality you can add groups of voters to an specific voting. You can access by census/add/by_group The functionality is not working properly until decide-part-rota-main Rota1-003 issue is completed. --- decide/census/templates/census_by_group.html | 58 ++++++++++ .../templates/census_maritialStatus.html | 74 +++++++++++++ .../census/templates/census_nationality.html | 73 +++++++++++++ decide/census/urls.py | 5 +- decide/census/views.py | 102 ++++++++++++++++++ 5 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 decide/census/templates/census_by_group.html create mode 100644 decide/census/templates/census_maritialStatus.html create mode 100644 decide/census/templates/census_nationality.html diff --git a/decide/census/templates/census_by_group.html b/decide/census/templates/census_by_group.html new file mode 100644 index 0000000000..f828759a60 --- /dev/null +++ b/decide/census/templates/census_by_group.html @@ -0,0 +1,58 @@ +{% extends "base.html" %} +{% load i18n static %} + +{% block extrahead %} + + + + +{% endblock %} + + +{% block content %} +
+
+

Census Groups

+
+
+
+
+

Select which type of grouping you want to apply

+
+
+ +
+ +
+ +
+ + +
+ +
+
+ +
+
+ +{% if messages %} + + +{% endif %} + + +{% endblock %} \ No newline at end of file diff --git a/decide/census/templates/census_maritialStatus.html b/decide/census/templates/census_maritialStatus.html new file mode 100644 index 0000000000..d60446b309 --- /dev/null +++ b/decide/census/templates/census_maritialStatus.html @@ -0,0 +1,74 @@ +{% extends "base.html" %} +{% load i18n static %} + +{% block extrahead %} + + + + +{% endblock %} + + +{% block content %} +
+
+

Census Groups

+
+
+
+
+

Now you can add groups of voters to an specific voting

+
for example, if you select Single, every user who satisfies that requirement will be added to the chosen voting
+
+
+ +
+ +
+ +
+ {% csrf_token %} +
+
+ + + +
+
+
+
+ + +
+
+
+
+ +
+
+
+ +{% if messages %} + + +{% endif %} + + +{% endblock %} \ No newline at end of file diff --git a/decide/census/templates/census_nationality.html b/decide/census/templates/census_nationality.html new file mode 100644 index 0000000000..bf1fd3e8f4 --- /dev/null +++ b/decide/census/templates/census_nationality.html @@ -0,0 +1,73 @@ +{% extends "base.html" %} +{% load i18n static %} + +{% block extrahead %} + + + + +{% endblock %} + + +{% block content %} +
+
+

Census Groups

+
+
+
+
+

Now you can add groups of voters to an specific voting

+
for example, if you select Spain, every spanish user will be added to the chosen voting
+
+
+ +
+ +
+ +
+ {% csrf_token %} +
+
+ + +
+
+
+
+ + +
+
+
+
+ +
+
+
+ +{% if messages %} + + +{% endif %} + + +{% endblock %} \ No newline at end of file diff --git a/decide/census/urls.py b/decide/census/urls.py index 41cc26e1d7..ac62378b9d 100644 --- a/decide/census/urls.py +++ b/decide/census/urls.py @@ -8,5 +8,8 @@ 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('add/by_group/', views.census_group, name='census_by_group'), + path('add/by_group/maritialStatus', views.census_maritialStatus), + path('add/by_group/nationality', views.census_nationality), ] diff --git a/decide/census/views.py b/decide/census/views.py index ee09f11c20..7113b3592b 100644 --- a/decide/census/views.py +++ b/decide/census/views.py @@ -145,4 +145,106 @@ def remove_from_census(request): messages.error(request, "You must be a staff member to access this page") return HttpResponse(template.render({'remove': True}, request), status=ST_401) +def census_group(request): + if request.user.is_staff: + template = loader.get_template("census_by_group.html") + users = User.objects.all() + votings = Voting.objects.all() + context = { + 'votings': votings, + 'users': users + } + return HttpResponse(template.render(context, request)) + else: + template = loader.get_template("result_page.html") + messages.error(request, "You must be a staff member to access this page") + return HttpResponse(template.render({}, request), status=ST_401) + +def census_maritialStatus(request): + if request.user.is_staff: + template = loader.get_template("census_maritialStatus.html") + votings = Voting.objects.all() + users = User.objects.all() + context = { + 'votings': votings, + 'users': users, + } + return HttpResponse(template.render(context, request)) + else: + template = loader.get_template("result_page.html") + messages.error(request, "You must be a staff member to access this page") + return HttpResponse(template.render({}, request), status=ST_401) + +def add_by_maritialStatus_to_census(request): + template = loader.get_template("result_page.html") + if request.user.is_staff: + voting_id = request.POST['voting-select'] + maritialStatus = request.POST['maritialStatus-select'] + users = User.objects.filter(maritial_status = maritialStatus) + for user in users: + try: + census_by_voting = Census.objects.get(voting_id=voting_id,voter_id=user.id) + except Census.DoesNotExist: + census_by_voting = None + status_code=404 + if census_by_voting == None: + census = Census(voting_id=voting_id, voter_id=user.id) + census.save() + messages.success(request, "User added to the voting correctly") + status_code=ST_201 + + else: + messages.info(request, "The user was already assigned to the voting") + status_code = 200 + + + return HttpResponse(template.render({}, request), status=status_code) + + else: + messages.error(request, "You must be a staff member to access this page") + return HttpResponse(template.render({}, request), status=ST_401) + + +def census_nationality(request): + if request.user.is_staff: + template = loader.get_template("census_nationality.html") + votings = Voting.objects.all() + users = User.objects.all() + context = { + 'votings': votings, + 'users': users, + } + return HttpResponse(template.render(context, request)) + else: + template = loader.get_template("result_page.html") + messages.error(request, "You must be a staff member to access this page") + return HttpResponse(template.render({}, request), status=ST_401) + +def add_by_nationality_to_census(request): + template = loader.get_template("result_page.html") + if request.user.is_staff: + voting_id = request.POST['voting-select'] + nation = request.POST['nationality-select'] + users = User.objects.filter(nationality = nation) + for user in users: + try: + census_by_voting = Census.objects.get(voting_id=voting_id,voter_id=user.id) + except Census.DoesNotExist: + census_by_voting = None + status_code=404 + if census_by_voting == None: + census = Census(voting_id=voting_id, voter_id=user.id) + census.save() + messages.success(request, "User added to the voting correctly") + status_code=ST_201 + else: + messages.info(request, "The user was already assigned to the voting") + status_code = 200 + + + return HttpResponse(template.render({}, request), status=status_code) + + else: + messages.error(request, "You must be a staff member to access this page") + return HttpResponse(template.render({}, request), status=ST_401) From 9feeae25e5db153ccf9a1109332caa939600b6dd Mon Sep 17 00:00:00 2001 From: joseluisps21 Date: Thu, 24 Nov 2022 18:26:51 +0100 Subject: [PATCH 2/2] fix: Fix for codacy & some code corrections Fixes the error detected by codacy scan Correct the functionality of groups, which now satisfies the requirement better. (Now You can select multiple choices in the same attribute) --- .../templates/census_maritialStatus.html | 10 +++--- .../census/templates/census_nationality.html | 13 ++++---- decide/census/urls.py | 3 ++ decide/census/views.py | 32 +++++++++++-------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/decide/census/templates/census_maritialStatus.html b/decide/census/templates/census_maritialStatus.html index d60446b309..deca586cfb 100644 --- a/decide/census/templates/census_maritialStatus.html +++ b/decide/census/templates/census_maritialStatus.html @@ -26,7 +26,7 @@
for example, if you select Single, every user who satisfies that requirement -
+ {% csrf_token %}
@@ -42,10 +42,10 @@
for example, if you select Single, every user who satisfies that requirement
- +
diff --git a/decide/census/templates/census_nationality.html b/decide/census/templates/census_nationality.html index bf1fd3e8f4..5acfafb56f 100644 --- a/decide/census/templates/census_nationality.html +++ b/decide/census/templates/census_nationality.html @@ -26,7 +26,7 @@
for example, if you select Spain, every spanish user will be added to the ch
- + {% csrf_token %}
@@ -41,11 +41,12 @@
for example, if you select Spain, every spanish user will be added to the ch
- + + +
diff --git a/decide/census/urls.py b/decide/census/urls.py index ac62378b9d..f275e5adef 100644 --- a/decide/census/urls.py +++ b/decide/census/urls.py @@ -12,4 +12,7 @@ path('add/by_group/', views.census_group, name='census_by_group'), path('add/by_group/maritialStatus', views.census_maritialStatus), path('add/by_group/nationality', views.census_nationality), + path('add/by_group/maritialStatus/add_by_maritialStatus_to_census', views.add_by_maritialStatus_to_census), + path('add/by_group/nationality/add_by_nationality_to_census', views.add_by_nationality_to_census), + ] diff --git a/decide/census/views.py b/decide/census/views.py index 7113b3592b..caf054ed86 100644 --- a/decide/census/views.py +++ b/decide/census/views.py @@ -164,10 +164,13 @@ def census_maritialStatus(request): if request.user.is_staff: template = loader.get_template("census_maritialStatus.html") votings = Voting.objects.all() - users = User.objects.all() + try: + maritialStatus = set(u.maritialStatus for u in User.objects.all()) + except BaseException: + maritialStatus = set() context = { 'votings': votings, - 'users': users, + 'maritialStatus': maritialStatus, } return HttpResponse(template.render(context, request)) else: @@ -180,7 +183,7 @@ def add_by_maritialStatus_to_census(request): if request.user.is_staff: voting_id = request.POST['voting-select'] maritialStatus = request.POST['maritialStatus-select'] - users = User.objects.filter(maritial_status = maritialStatus) + users = User.objects.filter(maritial_status in maritialStatus) for user in users: try: census_by_voting = Census.objects.get(voting_id=voting_id,voter_id=user.id) @@ -200,19 +203,22 @@ def add_by_maritialStatus_to_census(request): return HttpResponse(template.render({}, request), status=status_code) - else: - messages.error(request, "You must be a staff member to access this page") - return HttpResponse(template.render({}, request), status=ST_401) + else: + messages.error(request, "You must be a staff member to access this page") + return HttpResponse(template.render({}, request), status=ST_401) def census_nationality(request): if request.user.is_staff: template = loader.get_template("census_nationality.html") votings = Voting.objects.all() - users = User.objects.all() + try: + nationality = set(u.nationality for u in User.objects.all()) + except BaseException: + nationality = set() context = { 'votings': votings, - 'users': users, + 'nationality': nationality, } return HttpResponse(template.render(context, request)) else: @@ -225,7 +231,7 @@ def add_by_nationality_to_census(request): if request.user.is_staff: voting_id = request.POST['voting-select'] nation = request.POST['nationality-select'] - users = User.objects.filter(nationality = nation) + users = User.objects.filter(nationality in nation) for user in users: try: census_by_voting = Census.objects.get(voting_id=voting_id,voter_id=user.id) @@ -244,7 +250,7 @@ def add_by_nationality_to_census(request): return HttpResponse(template.render({}, request), status=status_code) - - else: - messages.error(request, "You must be a staff member to access this page") - return HttpResponse(template.render({}, request), status=ST_401) + + else: + messages.error(request, "You must be a staff member to access this page") + return HttpResponse(template.render({}, request), status=ST_401)