Skip to content

Commit

Permalink
initial work to support suspend and resume features (#166)
Browse files Browse the repository at this point in the history
* initial work to support suspend and resume features

* fixing typos

* adding enforce_state_controls support to authapi

* adding missing ENFORCE_STATE_CONTROLS in test_settings

* invalid status transition

* allowing setting data in error output

* fixing time_limit

* fixing process_tallies

* stopping tally_status if stopping auth_event

* do not allow to pending from suspended, first election needs to stop

* fixing status name

* fixing status ref

* adding some missing support for AuthEvent.RESUMED as it was AuthEvent.STARTED

* allowing to suspend from resumed
  • Loading branch information
edulix committed Jan 9, 2022
1 parent 43e86fc commit fc4eea9
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 49 deletions.
27 changes: 27 additions & 0 deletions authapi/api/migrations/0048_authevent_allowed_statuses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Edulix on 2021-12-30 11:00

from django.db import migrations, models

class Migration(migrations.Migration):
dependencies = [
('api', '0047_userdata_use_generated_auth_code'),
]

operations = [
migrations.AlterField(
model_name='authevent',
name='status',
field=models.CharField(
default='notstarted',
max_length=15,
choices=[
('notstarted', 'not-started'),
('started', 'started'),
('stopped', 'stopped'),
('resumed', 'resumed'),
('suspended', 'suspended')
]
),
preserve_default=True
)
]
19 changes: 16 additions & 3 deletions authapi/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
('notstarted', 'not-started'),
('started', 'started'),
('stopped', 'stopped'),
('resumed', 'resumed'),
('suspended', 'suspended')
)

AE_TALLY_STATUSES = (
Expand Down Expand Up @@ -262,19 +264,30 @@ class AuthEvent(models.Model):
use case is that each AuthEvent corresponds with an Election, in principle
it could be used for any kind of authentication and authorization event.
'''
NOT_STARTED = "notstarted"
STARTED = "started"
STOPPED = "stopped"
SUSPENDED = "suspended"
RESUMED = "resumed"
PENDING = "pending"
SUCCESS = "success"

auth_method = models.CharField(max_length=255)
census = models.CharField(max_length=5, choices=CENSUS, default="close")
auth_method_config = JSONField()
extra_fields = JSONField(blank=True, null=True)
status = models.CharField(max_length=15, choices=AE_STATUSES, default="notstarted")

status = models.CharField(
max_length=15,
choices=AE_STATUSES,
default=NOT_STARTED
)

# used by authapi_celery to know what tallies to launch, and to serialize
# those launches one by one. set/get with (s|g)et_tally_status api calls
tally_status = models.CharField(
max_length=15,
choices=AE_TALLY_STATUSES,
default="notstarted"
default=NOT_STARTED
)

created = models.DateTimeField(auto_now_add=True)
Expand Down
16 changes: 8 additions & 8 deletions authapi/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def launch_tally(auth_event):
agora_elections_request.status_code,
agora_elections_request.text
)
auth_event.tally_status = 'notstarted'
auth_event.tally_status = AuthEvent.STARTED
auth_event.save()

# log the action
Expand Down Expand Up @@ -165,7 +165,7 @@ def launch_tally(auth_event):
agora_elections_request.status_code,
agora_elections_request.text
)
auth_event.tally_status = 'started'
auth_event.tally_status = AuthEvent.STARTED
auth_event.save()

# log the action
Expand Down Expand Up @@ -218,7 +218,7 @@ def launch_virtual_tally(auth_event):
agora_elections_request.status_code,
agora_elections_request.text
)
auth_event.tally_status = 'notstarted'
auth_event.tally_status = AuthEvent.NOT_STARTED
auth_event.save()

# log the action
Expand Down Expand Up @@ -246,7 +246,7 @@ def launch_virtual_tally(auth_event):
agora_elections_request.status_code,
agora_elections_request.text
)
auth_event.tally_status = 'success'
auth_event.tally_status = AuthEvent.SUCCESS
auth_event.save()

# log the action
Expand Down Expand Up @@ -319,7 +319,7 @@ def update_tally_status(auth_event):
election_state = updated_election['payload']['state']

if election_state in ['tally_error', 'stopped', 'started']:
auth_event.tally_status = 'notstarted'
auth_event.tally_status = AuthEvent.NOT_STARTED
auth_event.save()

# log the action
Expand All @@ -334,7 +334,7 @@ def update_tally_status(auth_event):
)
action.save()
elif election_state in ['tally_ok', 'results_ok', 'results_pub']:
auth_event.tally_status = 'success'
auth_event.tally_status = AuthEvent.SUCCESS
auth_event.save()

# log the action
Expand Down Expand Up @@ -383,15 +383,15 @@ def process_tallies():
'''
logger.info('\n\ntasks.process_tallies')
tallying_events = AuthEvent.objects\
.filter(tally_status='started')\
.filter(tally_status=AuthEvent.STARTED)\
.order_by('id')

# Review which tallies have succeeded and update corresponding AuthEvents
for auth_event in tallying_events:
update_tally_status(auth_event)

pending_events = AuthEvent.objects\
.filter(tally_status='pending')\
.filter(tally_status=AuthEvent.PENDING)\
.order_by('id')

logger.info(
Expand Down
2 changes: 1 addition & 1 deletion authapi/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
url(r'^auth-event/(?P<pk>\d+)/resend_auth_code/$', views.resend_auth_code, name='resend_auth_code'),
url(r'^auth-event/(?P<pk>\d+)/census/send_auth/$', views.census_send_auth, name='census_send_auth'),
url(r'^auth-event/(?P<pk>\d+)/census/reset-voter/$', views.census_reset_voter, name='census_reset_voter'),
url(r'^auth-event/(?P<pk>\d+)/(?P<status>(notstarted|started|stopped))/$', views.ae_status, name='ae_status'),
url(r'^auth-event/(?P<pk>\d+)/(?P<status>(notstarted|started|stopped|suspended|resumed))/$', views.ae_status, name='ae_status'),
url(r'^auth-event/module/$', views.authevent_module, name='authevent_module'),
url(r'^auth-event/module/(?P<name>[-\w]+)/$', views.authevent_module, name='authevent_module'),

Expand Down
Loading

0 comments on commit fc4eea9

Please sign in to comment.