2019-10-18 14:18:49 +02:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2015-09-07 17:09:29 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
2015-06-14 23:26:06 +02:00
|
|
|
from django.db import transaction
|
2012-07-06 18:00:43 +02:00
|
|
|
|
2020-04-22 16:54:50 +02:00
|
|
|
from openslides.core.config import config
|
2019-11-12 18:30:26 +01:00
|
|
|
from openslides.poll.views import BaseOptionViewSet, BasePollViewSet, BaseVoteViewSet
|
2019-10-18 14:18:49 +02:00
|
|
|
from openslides.utils.auth import has_perm
|
2016-12-06 12:21:29 +01:00
|
|
|
from openslides.utils.autoupdate import inform_changed_data
|
2021-04-12 08:20:06 +02:00
|
|
|
from openslides.utils.rest_api import ModelViewSet, Response, ValidationError, action
|
2019-10-18 14:18:49 +02:00
|
|
|
from openslides.utils.utils import is_int
|
2013-09-25 10:01:01 +02:00
|
|
|
|
2019-11-12 18:30:26 +01:00
|
|
|
from .models import (
|
|
|
|
Assignment,
|
|
|
|
AssignmentOption,
|
|
|
|
AssignmentPoll,
|
|
|
|
AssignmentRelatedUser,
|
|
|
|
AssignmentVote,
|
|
|
|
)
|
2012-07-06 18:00:43 +02:00
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
# Viewsets for the REST API
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class AssignmentViewSet(ModelViewSet):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
API endpoint for assignments.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
queryset = Assignment.objects.all()
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def check_view_permissions(self):
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
Returns True if the user has required permissions.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2021-03-04 16:15:57 +01:00
|
|
|
if self.action in (
|
2019-01-06 16:22:33 +01:00
|
|
|
"create",
|
|
|
|
"partial_update",
|
|
|
|
"update",
|
|
|
|
"destroy",
|
|
|
|
"sort_related_users",
|
|
|
|
):
|
|
|
|
result = has_perm(self.request.user, "assignments.can_see") and has_perm(
|
|
|
|
self.request.user, "assignments.can_manage"
|
|
|
|
)
|
|
|
|
elif self.action == "candidature_self":
|
|
|
|
result = has_perm(self.request.user, "assignments.can_see") and has_perm(
|
|
|
|
self.request.user, "assignments.can_nominate_self"
|
|
|
|
)
|
|
|
|
elif self.action == "candidature_other":
|
|
|
|
result = has_perm(self.request.user, "assignments.can_see") and has_perm(
|
|
|
|
self.request.user, "assignments.can_nominate_other"
|
|
|
|
)
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
2015-01-17 14:25:05 +01:00
|
|
|
|
2019-07-10 08:31:58 +02:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.save(request_user=self.request.user)
|
|
|
|
|
2021-04-12 08:20:06 +02:00
|
|
|
@action(detail=True, methods=["post", "delete"])
|
2015-03-29 15:49:37 +02:00
|
|
|
def candidature_self(self, request, pk=None):
|
|
|
|
"""
|
2015-06-14 23:26:06 +02:00
|
|
|
View to nominate self as candidate (POST) or withdraw own
|
|
|
|
candidature (DELETE).
|
2015-03-29 15:49:37 +02:00
|
|
|
"""
|
|
|
|
assignment = self.get_object()
|
2019-01-06 16:22:33 +01:00
|
|
|
if request.method == "POST":
|
2015-03-29 15:49:37 +02:00
|
|
|
message = self.nominate_self(request, assignment)
|
|
|
|
else:
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
message = self.withdraw_self(request, assignment)
|
2019-01-06 16:22:33 +01:00
|
|
|
return Response({"detail": message})
|
2015-03-29 15:49:37 +02:00
|
|
|
|
|
|
|
def nominate_self(self, request, assignment):
|
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
2019-01-12 23:01:42 +01:00
|
|
|
"detail": "You can not candidate to this election because it is finished."
|
2019-01-06 16:22:33 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if assignment.phase == assignment.PHASE_VOTING and not has_perm(
|
|
|
|
request.user, "assignments.can_manage"
|
|
|
|
):
|
2015-03-29 15:49:37 +02:00
|
|
|
# To nominate self during voting you have to be a manager.
|
|
|
|
self.permission_denied(request)
|
|
|
|
# If the request.user is already a candidate he can nominate himself nevertheless.
|
2019-10-18 14:18:49 +02:00
|
|
|
assignment.add_candidate(request.user)
|
2017-04-28 22:10:18 +02:00
|
|
|
# Send new candidate via autoupdate because users without permission
|
|
|
|
# to see users may not have it but can get it now.
|
|
|
|
inform_changed_data([request.user])
|
2019-01-12 23:01:42 +01:00
|
|
|
return "You were nominated successfully."
|
2015-03-29 15:49:37 +02:00
|
|
|
|
|
|
|
def withdraw_self(self, request, assignment):
|
2016-01-09 16:26:00 +01:00
|
|
|
# Withdraw candidature.
|
2015-03-29 15:49:37 +02:00
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
2019-01-12 23:01:42 +01:00
|
|
|
"detail": "You can not withdraw your candidature to this election because it is finished."
|
2019-01-06 16:22:33 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if assignment.phase == assignment.PHASE_VOTING and not has_perm(
|
|
|
|
request.user, "assignments.can_manage"
|
|
|
|
):
|
2015-03-29 15:49:37 +02:00
|
|
|
# To withdraw self during voting you have to be a manager.
|
|
|
|
self.permission_denied(request)
|
|
|
|
if not assignment.is_candidate(request.user):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
2019-01-12 23:01:42 +01:00
|
|
|
{"detail": "You are not a candidate of this election."}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
assignment.remove_candidate(request.user)
|
2019-01-12 23:01:42 +01:00
|
|
|
return "You have withdrawn your candidature successfully."
|
2015-03-29 15:49:37 +02:00
|
|
|
|
2021-04-12 08:20:06 +02:00
|
|
|
@action(detail=True, methods=["post", "delete"])
|
2020-11-09 08:54:41 +01:00
|
|
|
def candidature_other(self, request, pk=None):
|
2015-03-29 15:49:37 +02:00
|
|
|
"""
|
2020-11-09 08:54:41 +01:00
|
|
|
View to nominate other users (POST) or delete their candidature
|
|
|
|
status (DELETE). The client has to send {'user': <id>}.
|
2015-03-29 15:49:37 +02:00
|
|
|
"""
|
2020-11-09 08:54:41 +01:00
|
|
|
user_id = request.data.get("user")
|
|
|
|
if not isinstance(user_id, int):
|
|
|
|
raise ValidationError({"detail": "user_id must be an int."})
|
|
|
|
|
2015-03-29 15:49:37 +02:00
|
|
|
try:
|
2020-11-09 08:54:41 +01:00
|
|
|
user = get_user_model().objects.get(pk=user_id)
|
2015-09-07 17:09:29 +02:00
|
|
|
except get_user_model().DoesNotExist:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
2020-11-09 08:54:41 +01:00
|
|
|
{"detail": "Invalid data. User {0} does not exist.", "args": [user_id]}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2015-03-29 15:49:37 +02:00
|
|
|
|
|
|
|
assignment = self.get_object()
|
2019-01-06 16:22:33 +01:00
|
|
|
if request.method == "POST":
|
2019-09-02 11:09:03 +02:00
|
|
|
return self.nominate_other(request, user, assignment)
|
2015-03-29 15:49:37 +02:00
|
|
|
else:
|
|
|
|
# request.method == 'DELETE'
|
2020-11-09 08:54:41 +01:00
|
|
|
return self.withdraw_other(request, user, assignment)
|
2015-03-29 15:49:37 +02:00
|
|
|
|
|
|
|
def nominate_other(self, request, user, assignment):
|
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
2019-09-02 11:09:03 +02:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"detail": "You can not nominate someone to this election because it is finished."
|
|
|
|
}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
|
|
|
if assignment.phase == assignment.PHASE_VOTING and not has_perm(
|
|
|
|
request.user, "assignments.can_manage"
|
|
|
|
):
|
2015-11-28 21:23:21 +01:00
|
|
|
# To nominate another user during voting you have to be a manager.
|
2015-03-29 15:49:37 +02:00
|
|
|
self.permission_denied(request)
|
2015-11-28 21:23:21 +01:00
|
|
|
if assignment.is_candidate(user):
|
2019-09-02 11:09:03 +02:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "User {0} is already nominated.", "args": [str(user)]}
|
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
assignment.add_candidate(user)
|
2017-04-28 22:10:18 +02:00
|
|
|
# Send new candidate via autoupdate because users without permission
|
|
|
|
# to see users may not have it but can get it now.
|
|
|
|
inform_changed_data(user)
|
2019-09-02 11:09:03 +02:00
|
|
|
return Response(
|
|
|
|
{"detail": "User {0} was nominated successfully.", "args": [str(user)]}
|
|
|
|
)
|
2015-03-29 15:49:37 +02:00
|
|
|
|
2020-11-09 08:54:41 +01:00
|
|
|
def withdraw_other(self, request, user, assignment):
|
2015-03-29 15:49:37 +02:00
|
|
|
# To delete candidature status you have to be a manager.
|
2019-01-06 16:22:33 +01:00
|
|
|
if not has_perm(request.user, "assignments.can_manage"):
|
2015-03-29 15:49:37 +02:00
|
|
|
self.permission_denied(request)
|
|
|
|
if assignment.phase == assignment.PHASE_FINISHED:
|
2019-09-02 11:09:03 +02:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"detail": "You can not delete someone's candidature to this election because it is finished."
|
|
|
|
}
|
|
|
|
)
|
2020-02-24 16:55:07 +01:00
|
|
|
if not assignment.is_candidate(user):
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
2019-09-02 11:09:03 +02:00
|
|
|
{
|
|
|
|
"detail": "User {0} has no status in this election.",
|
|
|
|
"args": [str(user)],
|
|
|
|
}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
assignment.remove_candidate(user)
|
2019-09-02 11:09:03 +02:00
|
|
|
return Response(
|
|
|
|
{"detail": "Candidate {0} was withdrawn successfully.", "args": [str(user)]}
|
|
|
|
)
|
2015-03-29 15:49:37 +02:00
|
|
|
|
2021-04-12 08:20:06 +02:00
|
|
|
@action(detail=True, methods=["post"])
|
2016-12-06 12:21:29 +01:00
|
|
|
def sort_related_users(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to sort the assignment related users.
|
|
|
|
|
|
|
|
Expects a list of IDs of the related users (pk of AssignmentRelatedUser model).
|
|
|
|
"""
|
|
|
|
assignment = self.get_object()
|
|
|
|
|
|
|
|
# Check data
|
2019-01-06 16:22:33 +01:00
|
|
|
related_user_ids = request.data.get("related_users")
|
2016-12-06 12:21:29 +01:00
|
|
|
if not isinstance(related_user_ids, list):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "users has to be a list of IDs."})
|
2016-12-06 12:21:29 +01:00
|
|
|
|
|
|
|
# Get all related users from AssignmentRelatedUser.
|
|
|
|
related_users = {}
|
2019-01-06 16:22:33 +01:00
|
|
|
for related_user in AssignmentRelatedUser.objects.filter(
|
|
|
|
assignment__id=assignment.id
|
|
|
|
):
|
2016-12-06 12:21:29 +01:00
|
|
|
related_users[related_user.pk] = related_user
|
|
|
|
|
|
|
|
# Check all given candidates from the request
|
|
|
|
valid_related_users = []
|
|
|
|
for related_user_id in related_user_ids:
|
2019-01-06 16:22:33 +01:00
|
|
|
if (
|
|
|
|
not isinstance(related_user_id, int)
|
|
|
|
or related_users.get(related_user_id) is None
|
|
|
|
):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "Invalid data."})
|
2016-12-06 12:21:29 +01:00
|
|
|
valid_related_users.append(related_users[related_user_id])
|
|
|
|
|
|
|
|
# Sort the related users
|
|
|
|
weight = 1
|
|
|
|
with transaction.atomic():
|
|
|
|
for valid_related_user in valid_related_users:
|
|
|
|
valid_related_user.weight = weight
|
|
|
|
valid_related_user.save(skip_autoupdate=True)
|
|
|
|
weight += 1
|
|
|
|
|
|
|
|
# send autoupdate
|
|
|
|
inform_changed_data(assignment)
|
|
|
|
|
|
|
|
# Initiate response.
|
2019-01-12 23:01:42 +01:00
|
|
|
return Response({"detail": "Assignment related users successfully sorted."})
|
2016-12-06 12:21:29 +01:00
|
|
|
|
2015-06-14 23:26:06 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class AssignmentPollViewSet(BasePollViewSet):
|
2015-06-14 23:26:06 +02:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
API endpoint for assignment polls.
|
|
|
|
|
2017-02-27 15:37:01 +01:00
|
|
|
There are the following views: update, partial_update and destroy.
|
2015-06-14 23:26:06 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-06-14 23:26:06 +02:00
|
|
|
queryset = AssignmentPoll.objects.all()
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def has_manage_permissions(self):
|
2015-06-14 23:26:06 +02:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
Returns True if the user has required permissions.
|
2015-06-14 23:26:06 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
return has_perm(self.request.user, "assignments.can_see") and has_perm(
|
|
|
|
self.request.user, "assignments.can_manage"
|
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
assignment = serializer.validated_data["assignment"]
|
|
|
|
if not assignment.candidates.exists():
|
|
|
|
raise ValidationError(
|
2019-11-12 18:30:26 +01:00
|
|
|
{"detail": "Cannot create poll because there are no candidates."}
|
2019-10-18 14:18:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
super().perform_create(serializer)
|
2020-03-11 10:22:03 +01:00
|
|
|
poll = AssignmentPoll.objects.get(pk=serializer.data["id"])
|
2020-11-23 18:37:00 +01:00
|
|
|
poll.db_amount_global_yes = Decimal(0)
|
2020-03-11 10:22:03 +01:00
|
|
|
poll.db_amount_global_no = Decimal(0)
|
2020-11-23 18:37:00 +01:00
|
|
|
poll.db_amount_global_abstain = Decimal(0)
|
2020-03-11 10:22:03 +01:00
|
|
|
poll.save()
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2020-09-10 12:09:05 +02:00
|
|
|
def handle_analog_vote(self, data, poll):
|
2019-11-12 18:30:26 +01:00
|
|
|
for field in ["votesvalid", "votesinvalid", "votescast"]:
|
|
|
|
setattr(poll, field, data[field])
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2020-11-23 18:37:00 +01:00
|
|
|
global_yes_enabled = poll.global_yes and poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
|
|
|
)
|
|
|
|
if global_yes_enabled:
|
|
|
|
poll.amount_global_yes = data.get("amount_global_yes", Decimal(0))
|
|
|
|
|
|
|
|
global_no_enabled = poll.global_no and poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
2019-10-18 14:18:49 +02:00
|
|
|
)
|
2020-03-11 10:22:03 +01:00
|
|
|
if global_no_enabled:
|
|
|
|
poll.amount_global_no = data.get("amount_global_no", Decimal(0))
|
2020-11-23 18:37:00 +01:00
|
|
|
|
|
|
|
global_abstain_enabled = poll.global_abstain and poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
2019-10-18 14:18:49 +02:00
|
|
|
)
|
2020-03-11 10:22:03 +01:00
|
|
|
if global_abstain_enabled:
|
|
|
|
poll.amount_global_abstain = data.get("amount_global_abstain", Decimal(0))
|
2019-10-18 14:18:49 +02:00
|
|
|
|
|
|
|
options = poll.get_options()
|
2019-11-12 18:30:26 +01:00
|
|
|
options_data = data.get("options")
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2019-11-12 18:30:26 +01:00
|
|
|
with transaction.atomic():
|
|
|
|
for option_id, vote in options_data.items():
|
|
|
|
option = options.get(pk=int(option_id))
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2020-11-23 18:37:00 +01:00
|
|
|
if poll.pollmethod == AssignmentPoll.POLLMETHOD_N:
|
2019-11-12 18:30:26 +01:00
|
|
|
vote_obj, _ = AssignmentVote.objects.get_or_create(
|
|
|
|
option=option, value="N"
|
|
|
|
)
|
|
|
|
vote_obj.weight = vote["N"]
|
|
|
|
vote_obj.save()
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2020-11-23 18:37:00 +01:00
|
|
|
elif poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_YN,
|
|
|
|
AssignmentPoll.POLLMETHOD_YNA,
|
|
|
|
):
|
|
|
|
# All three methods have a Y
|
2019-11-12 18:30:26 +01:00
|
|
|
vote_obj, _ = AssignmentVote.objects.get_or_create(
|
2020-11-23 18:37:00 +01:00
|
|
|
option=option, value="Y"
|
2019-11-12 18:30:26 +01:00
|
|
|
)
|
2020-11-23 18:37:00 +01:00
|
|
|
vote_obj.weight = vote["Y"]
|
2019-11-12 18:30:26 +01:00
|
|
|
vote_obj.save()
|
2020-11-23 18:37:00 +01:00
|
|
|
|
|
|
|
if poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_YN,
|
|
|
|
AssignmentPoll.POLLMETHOD_YNA,
|
|
|
|
):
|
|
|
|
vote_obj, _ = AssignmentVote.objects.get_or_create(
|
|
|
|
option=option, value="N"
|
|
|
|
)
|
|
|
|
vote_obj.weight = vote["N"]
|
|
|
|
vote_obj.save()
|
|
|
|
|
|
|
|
if poll.pollmethod == AssignmentPoll.POLLMETHOD_YNA:
|
|
|
|
vote_obj, _ = AssignmentVote.objects.get_or_create(
|
|
|
|
option=option, value="A"
|
|
|
|
)
|
|
|
|
vote_obj.weight = vote["A"]
|
|
|
|
vote_obj.save()
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(
|
|
|
|
f"handle_analog_vote not implemented for {poll.pollmethod}"
|
|
|
|
)
|
2020-03-11 10:22:03 +01:00
|
|
|
inform_changed_data(option)
|
2019-11-12 18:30:26 +01:00
|
|
|
|
|
|
|
poll.save()
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2020-09-10 12:09:05 +02:00
|
|
|
def validate_vote_data(self, data, poll):
|
2019-11-12 18:30:26 +01:00
|
|
|
"""
|
|
|
|
Request data:
|
|
|
|
analog:
|
|
|
|
{
|
|
|
|
"options": {<option_id>: {"Y": <amount>, ["N": <amount>], ["A": <amount>] }},
|
|
|
|
["votesvalid": <amount>], ["votesinvalid": <amount>], ["votescast": <amount>],
|
2020-11-23 18:37:00 +01:00
|
|
|
["amount_global_yes": <amount>],
|
|
|
|
["amount_global_no": <amount>],
|
|
|
|
["amount_global_abstain": <amount>]
|
2019-11-12 18:30:26 +01:00
|
|
|
}
|
|
|
|
All amounts are decimals as strings
|
|
|
|
required fields per pollmethod:
|
|
|
|
- votes: Y
|
|
|
|
- YN: YN
|
|
|
|
- YNA: YNA
|
2020-11-23 18:37:00 +01:00
|
|
|
- N: N
|
2019-11-12 18:30:26 +01:00
|
|
|
named|pseudoanonymous:
|
|
|
|
votes:
|
2020-11-23 18:37:00 +01:00
|
|
|
{<option_id>: <amount>} | 'Y' | 'N' | 'A'
|
2019-11-12 18:30:26 +01:00
|
|
|
- Exactly one of the three options must be given
|
2020-11-23 18:37:00 +01:00
|
|
|
- 'Y' is only valid if poll.global_yes==True
|
2019-11-12 18:30:26 +01:00
|
|
|
- 'N' is only valid if poll.global_no==True
|
|
|
|
- 'A' is only valid if poll.global_abstain==True
|
|
|
|
- amounts must be integer numbers >= 0.
|
|
|
|
- ids should be integers of valid option ids for this poll
|
|
|
|
- amounts must be 0 or 1, if poll.allow_multiple_votes_per_candidate is False
|
2020-11-23 18:37:00 +01:00
|
|
|
- if an option is not given, 0 is assumed
|
2020-11-24 09:56:20 +01:00
|
|
|
- The sum of all amounts must be >= poll.min_votes_amount and <= poll.max_votes_amount
|
2019-11-12 18:30:26 +01:00
|
|
|
|
|
|
|
YN/YNA:
|
|
|
|
{<option_id>: 'Y' | 'N' [|'A']}
|
|
|
|
- 'A' is only allowed in YNA pollmethod
|
|
|
|
"""
|
|
|
|
if poll.type == AssignmentPoll.TYPE_ANALOG:
|
2019-10-18 14:18:49 +02:00
|
|
|
if not isinstance(data, dict):
|
2019-11-12 18:30:26 +01:00
|
|
|
raise ValidationError({"detail": "Data must be a dict"})
|
|
|
|
|
|
|
|
options_data = data.get("options")
|
|
|
|
if not isinstance(options_data, dict):
|
|
|
|
raise ValidationError({"detail": "You must provide options"})
|
|
|
|
|
|
|
|
for key, value in options_data.items():
|
|
|
|
if not is_int(key):
|
2019-10-18 14:18:49 +02:00
|
|
|
raise ValidationError({"detail": "Keys must be int"})
|
2019-11-12 18:30:26 +01:00
|
|
|
if not isinstance(value, dict):
|
|
|
|
raise ValidationError({"detail": "A dict per option is required"})
|
2020-11-23 18:37:00 +01:00
|
|
|
if poll.pollmethod == AssignmentPoll.POLLMETHOD_N:
|
2019-11-12 18:30:26 +01:00
|
|
|
value["N"] = self.parse_vote_value(value, "N")
|
2020-11-23 18:37:00 +01:00
|
|
|
else:
|
|
|
|
value["Y"] = self.parse_vote_value(value, "Y")
|
|
|
|
if poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_YN,
|
|
|
|
AssignmentPoll.POLLMETHOD_YNA,
|
|
|
|
):
|
|
|
|
value["N"] = self.parse_vote_value(value, "N")
|
|
|
|
if poll.pollmethod == AssignmentPoll.POLLMETHOD_YNA:
|
|
|
|
value["A"] = self.parse_vote_value(value, "A")
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2019-11-12 18:30:26 +01:00
|
|
|
for field in ["votesvalid", "votesinvalid", "votescast"]:
|
|
|
|
data[field] = self.parse_vote_value(data, field)
|
|
|
|
|
2020-11-23 18:37:00 +01:00
|
|
|
global_yes_enabled = poll.global_yes and poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
2019-11-12 18:30:26 +01:00
|
|
|
)
|
2020-11-23 18:37:00 +01:00
|
|
|
if "amount_global_yes" in data and global_yes_enabled:
|
|
|
|
data["amount_global_yes"] = self.parse_vote_value(
|
|
|
|
data, "amount_global_yes"
|
2020-03-11 10:22:03 +01:00
|
|
|
)
|
2020-11-23 18:37:00 +01:00
|
|
|
|
|
|
|
global_no_enabled = poll.global_no and poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
|
|
|
)
|
2020-03-11 10:22:03 +01:00
|
|
|
if "amount_global_no" in data and global_no_enabled:
|
|
|
|
data["amount_global_no"] = self.parse_vote_value(
|
|
|
|
data, "amount_global_no"
|
|
|
|
)
|
2019-11-12 18:30:26 +01:00
|
|
|
|
2020-11-23 18:37:00 +01:00
|
|
|
global_abstain_enabled = poll.global_abstain and poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
|
|
|
)
|
|
|
|
if "amount_global_abstain" in data and global_abstain_enabled:
|
|
|
|
data["amount_global_abstain"] = self.parse_vote_value(
|
|
|
|
data, "amount_global_abstain"
|
|
|
|
)
|
|
|
|
|
|
|
|
else: # non-analog polls
|
2020-11-05 16:34:18 +01:00
|
|
|
if isinstance(data, dict) and len(data) == 0:
|
|
|
|
raise ValidationError({"details": "Empty ballots are not allowed"})
|
2020-04-24 07:21:27 +02:00
|
|
|
available_options = poll.get_options()
|
2020-11-23 18:37:00 +01:00
|
|
|
if poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
|
|
|
):
|
2019-11-12 18:30:26 +01:00
|
|
|
if isinstance(data, dict):
|
|
|
|
amount_sum = 0
|
|
|
|
for option_id, amount in data.items():
|
|
|
|
if not is_int(option_id):
|
|
|
|
raise ValidationError({"detail": "Each id must be an int."})
|
2020-04-24 07:21:27 +02:00
|
|
|
if not available_options.filter(id=option_id).exists():
|
2020-03-11 10:22:03 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": f"Option {option_id} does not exist."}
|
|
|
|
)
|
2019-11-12 18:30:26 +01:00
|
|
|
if not is_int(amount):
|
2020-11-05 16:34:18 +01:00
|
|
|
raise ValidationError({"detail": "Each amount must be int"})
|
2019-11-12 18:30:26 +01:00
|
|
|
amount = int(amount)
|
|
|
|
if amount < 0:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Negative votes are not allowed"}
|
|
|
|
)
|
|
|
|
# skip empty votes
|
|
|
|
if amount == 0:
|
|
|
|
continue
|
|
|
|
if not poll.allow_multiple_votes_per_candidate and amount != 1:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Multiple votes are not allowed"}
|
|
|
|
)
|
|
|
|
amount_sum += amount
|
|
|
|
|
2020-11-24 09:56:20 +01:00
|
|
|
if amount_sum > poll.max_votes_amount:
|
2019-11-12 18:30:26 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
2020-01-22 17:31:10 +01:00
|
|
|
"detail": "You can give a maximum of {0} votes",
|
2020-11-24 09:56:20 +01:00
|
|
|
"args": [poll.max_votes_amount],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if amount_sum < poll.min_votes_amount:
|
|
|
|
raise ValidationError(
|
|
|
|
{
|
|
|
|
"detail": "You must give a minimum of {0} votes",
|
|
|
|
"args": [poll.min_votes_amount],
|
2019-11-12 18:30:26 +01:00
|
|
|
}
|
|
|
|
)
|
2020-11-23 18:37:00 +01:00
|
|
|
# return, if there is a global vote, because we dont have to check option presence
|
|
|
|
elif data == "Y" and poll.global_yes:
|
|
|
|
return
|
2019-11-12 18:30:26 +01:00
|
|
|
elif data == "N" and poll.global_no:
|
2020-11-23 18:37:00 +01:00
|
|
|
return
|
2019-11-12 18:30:26 +01:00
|
|
|
elif data == "A" and poll.global_abstain:
|
2020-11-23 18:37:00 +01:00
|
|
|
return
|
2019-11-12 18:30:26 +01:00
|
|
|
else:
|
|
|
|
raise ValidationError({"detail": "invalid data."})
|
|
|
|
|
|
|
|
elif poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_YN,
|
|
|
|
AssignmentPoll.POLLMETHOD_YNA,
|
|
|
|
):
|
|
|
|
if not isinstance(data, dict):
|
|
|
|
raise ValidationError({"detail": "Data must be a dict."})
|
|
|
|
for option_id, value in data.items():
|
|
|
|
if not is_int(option_id):
|
|
|
|
raise ValidationError({"detail": "Keys must be int"})
|
2020-04-24 07:21:27 +02:00
|
|
|
if not available_options.filter(id=option_id).exists():
|
2020-03-11 10:22:03 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": f"Option {option_id} does not exist."}
|
|
|
|
)
|
2019-11-12 18:30:26 +01:00
|
|
|
if (
|
|
|
|
poll.pollmethod == AssignmentPoll.POLLMETHOD_YNA
|
2020-08-31 13:24:16 +02:00
|
|
|
and value not in ("Y", "N", "A")
|
2019-11-12 18:30:26 +01:00
|
|
|
):
|
2020-02-12 17:18:01 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{"detail": "Every value must be Y, N or A"}
|
|
|
|
)
|
2019-11-12 18:30:26 +01:00
|
|
|
elif (
|
|
|
|
poll.pollmethod == AssignmentPoll.POLLMETHOD_YN
|
2020-08-31 13:24:16 +02:00
|
|
|
and value not in ("Y", "N")
|
2019-11-12 18:30:26 +01:00
|
|
|
):
|
2020-02-12 17:18:01 +01:00
|
|
|
raise ValidationError({"detail": "Every value must be Y or N"})
|
2019-11-12 18:30:26 +01:00
|
|
|
|
|
|
|
options_data = data
|
|
|
|
|
2020-09-10 12:09:05 +02:00
|
|
|
def create_votes_type_votes(self, data, poll, vote_weight, vote_user, request_user):
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
|
|
|
Helper function for handle_(named|pseudoanonymous)_vote
|
|
|
|
Assumes data is already validated
|
2020-09-10 12:09:05 +02:00
|
|
|
vote_user is the user whose vote is given
|
|
|
|
request_user is the user who gives the vote, may be a delegate
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
2019-10-18 14:18:49 +02:00
|
|
|
options = poll.get_options()
|
2020-02-12 17:18:01 +01:00
|
|
|
if isinstance(data, dict):
|
2021-03-25 13:13:49 +01:00
|
|
|
user_token = AssignmentVote.objects.generate_user_token()
|
2020-02-12 17:18:01 +01:00
|
|
|
for option_id, amount in data.items():
|
|
|
|
# Add user to the option's voted array
|
|
|
|
option = options.get(pk=option_id)
|
|
|
|
# skip creating votes with empty weights
|
|
|
|
if amount == 0:
|
|
|
|
continue
|
2020-04-22 16:54:50 +02:00
|
|
|
weight = Decimal(amount)
|
|
|
|
if config["users_activate_vote_weight"]:
|
2020-09-10 12:09:05 +02:00
|
|
|
weight *= vote_weight
|
2020-11-23 18:37:00 +01:00
|
|
|
value = "Y" # POLLMETHOD_Y
|
|
|
|
if poll.pollmethod == AssignmentPoll.POLLMETHOD_N:
|
|
|
|
value = "N"
|
2019-10-18 14:18:49 +02:00
|
|
|
vote = AssignmentVote.objects.create(
|
2020-09-10 12:09:05 +02:00
|
|
|
option=option,
|
|
|
|
user=vote_user,
|
|
|
|
delegated_user=request_user,
|
|
|
|
weight=weight,
|
2020-11-23 18:37:00 +01:00
|
|
|
value=value,
|
2021-03-25 13:13:49 +01:00
|
|
|
user_token=user_token,
|
2019-10-18 14:18:49 +02:00
|
|
|
)
|
2021-03-04 16:15:57 +01:00
|
|
|
inform_changed_data(vote)
|
2020-02-12 17:18:01 +01:00
|
|
|
else: # global_no or global_abstain
|
2020-03-12 15:42:41 +01:00
|
|
|
option = options[0]
|
2020-09-10 12:09:05 +02:00
|
|
|
weight = vote_weight if config["users_activate_vote_weight"] else Decimal(1)
|
2020-03-12 15:42:41 +01:00
|
|
|
vote = AssignmentVote.objects.create(
|
2020-09-10 12:09:05 +02:00
|
|
|
option=option,
|
|
|
|
user=vote_user,
|
|
|
|
delegated_user=request_user,
|
|
|
|
weight=weight,
|
|
|
|
value=data,
|
2020-03-12 15:42:41 +01:00
|
|
|
)
|
2021-03-04 16:15:57 +01:00
|
|
|
inform_changed_data(vote)
|
2020-03-12 15:42:41 +01:00
|
|
|
inform_changed_data(option)
|
|
|
|
inform_changed_data(poll)
|
2020-03-11 10:22:03 +01:00
|
|
|
|
2020-09-10 12:09:05 +02:00
|
|
|
def create_votes_types_yn_yna(
|
|
|
|
self, data, poll, vote_weight, vote_user, request_user
|
|
|
|
):
|
2020-04-06 14:14:00 +02:00
|
|
|
"""
|
2020-09-10 12:09:05 +02:00
|
|
|
Helper function for handle_(named|pseudoanonymous)_vote
|
|
|
|
Assumes data is already validated
|
2020-09-10 12:09:05 +02:00
|
|
|
vote_user is the user whose vote is given
|
|
|
|
request_user is the user who gives the vote, may be a delegate
|
2020-04-06 14:14:00 +02:00
|
|
|
"""
|
2020-02-12 17:18:01 +01:00
|
|
|
options = poll.get_options()
|
2021-03-25 13:13:49 +01:00
|
|
|
user_token = AssignmentVote.objects.generate_user_token()
|
2020-09-10 12:09:05 +02:00
|
|
|
weight = vote_weight if config["users_activate_vote_weight"] else Decimal(1)
|
2020-02-12 17:18:01 +01:00
|
|
|
for option_id, result in data.items():
|
|
|
|
option = options.get(pk=option_id)
|
|
|
|
vote = AssignmentVote.objects.create(
|
2020-09-10 12:09:05 +02:00
|
|
|
option=option,
|
|
|
|
user=vote_user,
|
|
|
|
delegated_user=request_user,
|
|
|
|
value=result,
|
|
|
|
weight=weight,
|
2021-03-25 13:13:49 +01:00
|
|
|
user_token=user_token,
|
2020-02-12 17:18:01 +01:00
|
|
|
)
|
2021-03-04 16:15:57 +01:00
|
|
|
inform_changed_data(vote)
|
|
|
|
inform_changed_data(option)
|
2020-03-11 10:22:03 +01:00
|
|
|
|
2020-04-06 16:38:07 +02:00
|
|
|
def add_user_to_voted_array(self, user, poll):
|
|
|
|
VotedModel = AssignmentPoll.voted.through
|
|
|
|
VotedModel.objects.create(assignmentpoll=poll, user=user)
|
2020-03-11 10:22:03 +01:00
|
|
|
|
2020-09-10 12:09:05 +02:00
|
|
|
def handle_named_vote(self, data, poll, vote_user, request_user):
|
2020-11-23 18:37:00 +01:00
|
|
|
if poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
|
|
|
):
|
2020-09-10 12:09:05 +02:00
|
|
|
self.create_votes_type_votes(
|
|
|
|
data, poll, vote_user.vote_weight, vote_user, request_user
|
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
elif poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_YN,
|
|
|
|
AssignmentPoll.POLLMETHOD_YNA,
|
|
|
|
):
|
2020-09-10 12:09:05 +02:00
|
|
|
self.create_votes_types_yn_yna(
|
|
|
|
data, poll, vote_user.vote_weight, vote_user, request_user
|
|
|
|
)
|
2020-11-23 18:37:00 +01:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(f"The method {poll.pollmethod} is not supported!")
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2020-02-12 17:18:01 +01:00
|
|
|
def handle_pseudoanonymous_vote(self, data, poll, user):
|
2020-11-23 18:37:00 +01:00
|
|
|
if poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_Y,
|
|
|
|
AssignmentPoll.POLLMETHOD_N,
|
|
|
|
):
|
2020-09-10 12:09:05 +02:00
|
|
|
self.create_votes_type_votes(data, poll, user.vote_weight, None, None)
|
2020-02-12 17:18:01 +01:00
|
|
|
elif poll.pollmethod in (
|
|
|
|
AssignmentPoll.POLLMETHOD_YN,
|
|
|
|
AssignmentPoll.POLLMETHOD_YNA,
|
|
|
|
):
|
2020-09-10 12:09:05 +02:00
|
|
|
self.create_votes_types_yn_yna(data, poll, user.vote_weight, None, None)
|
2020-11-23 18:37:00 +01:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(f"The method {poll.pollmethod} is not supported!")
|
2019-10-18 14:18:49 +02:00
|
|
|
|
2019-11-12 18:30:26 +01:00
|
|
|
def convert_option_data(self, poll, data):
|
|
|
|
poll_options = poll.get_options()
|
|
|
|
new_option_data = {}
|
|
|
|
option_data = data.get("options")
|
|
|
|
if option_data is None:
|
|
|
|
raise ValidationError({"detail": "You must provide options"})
|
|
|
|
for id, val in option_data.items():
|
|
|
|
option = poll_options.filter(user_id=id).first()
|
|
|
|
if option is None:
|
|
|
|
raise ValidationError(
|
|
|
|
{"detail": f"Assignment related user with id {id} not found"}
|
|
|
|
)
|
|
|
|
new_option_data[option.id] = val
|
|
|
|
data["options"] = new_option_data
|
|
|
|
|
|
|
|
|
|
|
|
class AssignmentOptionViewSet(BaseOptionViewSet):
|
|
|
|
queryset = AssignmentOption.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
return has_perm(self.request.user, "assignments.can_see")
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
|
|
|
|
class AssignmentVoteViewSet(BaseVoteViewSet):
|
|
|
|
queryset = AssignmentVote.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
return has_perm(self.request.user, "assignments.can_see")
|