forked from benadida/helios-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
136 lines (104 loc) · 4.14 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Celery queued tasks for Helios
2010-08-01
"""
from celery.decorators import task
from models import *
from view_utils import render_template_raw
import signals
import copy
@task()
def cast_vote_verify_and_store(cast_vote_id, status_update_message=None, **kwargs):
cast_vote = CastVote.objects.get(id = cast_vote_id)
result = cast_vote.verify_and_store()
voter = cast_vote.voter
election = voter.election
user = voter.user
if result:
# send the signal
signals.vote_cast.send(sender=election, election=election, user=user, voter=voter, cast_vote=cast_vote)
if status_update_message and user.can_update_status():
from views import get_election_url
user.update_status(status_update_message)
else:
logger = cast_vote_verify_and_store.get_logger(**kwargs)
logger.error("Failed to verify and store %d" % cast_vote_id)
@task()
def voters_email(election_id, subject_template, body_template, extra_vars={},
voter_constraints_include=None, voter_constraints_exclude=None):
"""
voter_constraints_include are conditions on including voters
voter_constraints_exclude are conditions on excluding voters
"""
election = Election.objects.get(id = election_id)
# select the right list of voters
voters = election.voter_set.all()
if voter_constraints_include:
voters = voters.filter(**voter_constraints_include)
if voter_constraints_exclude:
voters = voters.exclude(**voter_constraints_exclude)
for voter in voters:
single_voter_email.delay(voter.uuid, subject_template, body_template, extra_vars)
@task()
def voters_notify(election_id, notification_template, extra_vars={}):
election = Election.objects.get(id = election_id)
for voter in election.voter_set.all():
single_voter_notify.delay(voter.uuid, notification_template, extra_vars)
@task()
def single_voter_email(voter_uuid, subject_template, body_template, extra_vars={}):
voter = Voter.objects.get(uuid = voter_uuid)
the_vars = copy.copy(extra_vars)
the_vars.update({'voter' : voter})
subject = render_template_raw(None, subject_template, the_vars)
body = render_template_raw(None, body_template, the_vars)
voter.user.send_message(subject, body)
@task()
def single_voter_notify(voter_uuid, notification_template, extra_vars={}):
voter = Voter.objects.get(uuid = voter_uuid)
the_vars = copy.copy(extra_vars)
the_vars.update({'voter' : voter})
notification = render_template_raw(None, notification_template, the_vars)
voter.user.send_notification(notification)
@task()
def election_compute_tally(election_id):
election = Election.objects.get(id = election_id)
election.compute_tally()
election_notify_admin.delay(election_id = election_id,
subject = "encrypted tally computed",
body = """
The encrypted tally for election %s has been computed.
--
Helios
""" % election.name)
if election.has_helios_trustee():
tally_helios_decrypt.delay(election_id = election.id)
@task()
def tally_helios_decrypt(election_id):
election = Election.objects.get(id = election_id)
election.helios_trustee_decrypt()
election_notify_admin.delay(election_id = election_id,
subject = 'Helios Decrypt',
body = """
Helios has decrypted its portion of the tally
for election %s.
--
Helios
""" % election.name)
@task()
def voter_file_process(voter_file_id):
voter_file = VoterFile.objects.get(id = voter_file_id)
voter_file.process()
election_notify_admin.delay(election_id = voter_file.election.id,
subject = 'voter file processed',
body = """
Your voter file upload for election %s
has been processed.
%s voters have been created.
--
Helios
""" % (voter_file.election.name, voter_file.num_voters))
@task()
def election_notify_admin(election_id, subject, body):
election = Election.objects.get(id = election_id)
election.admin.send_message(subject, body)