Skip to content

Commit

Permalink
EGCETSII#13-fix: Create of public key when a voting start
Browse files Browse the repository at this point in the history
  • Loading branch information
pedalopon committed Jan 2, 2022
1 parent ed30f11 commit 16531fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion decide/administration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ class AdminVotingSerializer(serializers.Serializer):
auth = serializers.URLField()
name = serializers.CharField(max_length=200)
desc = serializers.CharField(max_length=1000, allow_blank=True, allow_null=True)
census = serializers.ListField()
census = serializers.ListField(allow_null=True)
32 changes: 20 additions & 12 deletions decide/administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from authentication.serializers import UserSerializer
from administration.serializers import *
from base.serializers import AuthSerializer, KeySerializer
from decide import settings
from decide.settings import BASEURL
from voting.serializers import VotingSerializer
from .serializers import CensusSerializer
Expand Down Expand Up @@ -39,11 +40,8 @@ def post(self, request):
else:
auth_url = request.data.get("auth")
id_users = request.data.get("census")
try:
auth_object = Auth.objects.filter(url=auth_url).get()
except ObjectDoesNotExist:
auth_object = Auth(name="Auth", url=auth_url, me=True)
auth_object.save()
auth, _ = Auth.objects.get_or_create(url=auth_url,
defaults={'me': True, 'name': 'test auth'})
question = Question(desc=request.data.get('question').get("desc"))
question.save()
options = request.data.get('question').get("options")
Expand All @@ -53,11 +51,15 @@ def post(self, request):
voting = Voting(name=request.data.get("name"), desc=request.data.get("desc"),
question=question)
voting.save()
voting.auths.add(auth_object)
voting.auths.add(auth)
voting_id = voting.id
for voter_id in id_users:
census = Census(voting_id=voting_id, voter_id=voter_id)
census.save()
if id_users is None:
users = User.objects.all()
id_users = [user.id for user in users]
if len(id_users) > 0:
for voter_id in id_users:
census = Census(voting_id=voting_id, voter_id=voter_id)
census.save()
return Response({"id": voting_id, "name": voting.name}, status=HTTP_200_OK)

def put(self, request):
Expand All @@ -70,8 +72,11 @@ def put(self, request):
st = status.HTTP_200_OK
if action == 'start':
votings = Voting.objects.filter(id__in=votings_id, start_date__isnull=True)
if len(votings)> 0:
votings.update(start_date=timezone.now())
if len(votings) > 0:
for voting in votings:
voting.create_pubkey()
voting.start_date = timezone.now()
voting.save()
msg = 'Votings started'
else:
msg = 'All votings all already started'
Expand All @@ -80,7 +85,9 @@ def put(self, request):
elif action == 'stop':
votings = Voting.objects.filter(id__in=votings_id, start_date__isnull=False, end_date__isnull=True)
if len(votings) > 0:
votings.update(end_date=timezone.now())
for voting in votings:
voting.end_date = timezone.now()
voting.save()
msg = 'Votings stopped'
else:
msg = 'All votings all already stopped or not started'
Expand All @@ -91,6 +98,7 @@ def put(self, request):
for voting in votings:
key = request.COOKIES.get('token', "")
voting.tally_votes(key)
msg = 'Votings tallied'
else:
msg = 'All votings all already tallied, not stopped or not started'
st = status.HTTP_400_BAD_REQUEST
Expand Down

0 comments on commit 16531fb

Please sign in to comment.