Skip to content

Commit

Permalink
✨ Delete Multiple Elections (#362)
Browse files Browse the repository at this point in the history
Parent issue: sequentech/meta#229
  • Loading branch information
Findeton authored Dec 3, 2024
1 parent b151b9a commit 3a9ec7b
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions iam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3383,50 +3383,57 @@ def post(request, pk=None):
error_codename=ErrorCodes.BAD_REQUEST)

for election_id in election_ids:
permission_required(request.user, 'AuthEvent', ['edit', 'delete'], election_id)

election_obj = AuthEvent.objects.get(pk=election_id)
children_pks = [child.id for child in election_obj.children.all()]
children_pks.append(election_obj.id)
# delete event and children in ballot box:
for pk in children_pks:
pk_obj = AuthEvent.objects.get(pk=pk)
ballot_box_base = settings.SEQUENT_ELECTIONS_BASE[0]
ballot_box_url = "%s/api/election/%s/delete" % (
ballot_box_base,
pk
)
ballot_box_request = requests.post(
ballot_box_url,
json=[],
headers={
'Authorization': generate_access_token_hmac(
settings.SHARED_SECRET,
"1:AuthEvent:%s:edit|delete" % pk,
pk_obj.get_refresh_token_duration_secs()
),
'Content-type': 'application/json'
}
)

LOGGER.info(
"DeleteElections.post\n" +
"delete pk (Ballot Box) '%d'\n" +
"ballot_box_url '%r'\n" +
"ballot_box_request.status_code '%r'\n" +
"ballot_box_request.text '%r'\n",
pk,
ballot_box_url,
ballot_box_request.status_code,
ballot_box_request.text
)
if ballot_box_request.status_code != 200:
return json_response(
status=500,
error_codename=ErrorCodes.INTERNAL_SERVER_ERROR
)
try:
permission_required(request.user, 'AuthEvent', ['edit', 'delete'], election_id)

election_obj = AuthEvent.objects.get(pk=election_id)
children_pks = [child.id for child in election_obj.children.all()]
children_pks.append(election_obj.id)
# delete event and children in ballot box:
for pk in children_pks:
try:
pk_obj = AuthEvent.objects.get(pk=pk)
ballot_box_base = settings.SEQUENT_ELECTIONS_BASE[0]
ballot_box_url = "%s/api/election/%s/delete" % (
ballot_box_base,
pk
)
ballot_box_request = requests.post(
ballot_box_url,
json=[],
headers={
'Authorization': generate_access_token_hmac(
settings.SHARED_SECRET,
"1:AuthEvent:%s:edit|delete" % pk,
pk_obj.get_refresh_token_duration_secs()
),
'Content-type': 'application/json'
}
)

LOGGER.info(
"DeleteElections.post\n" +
"delete pk (Ballot Box) '%d'\n" +
"ballot_box_url '%r'\n" +
"ballot_box_request.status_code '%r'\n" +
"ballot_box_request.text '%r'\n",
pk,
ballot_box_url,
ballot_box_request.status_code,
ballot_box_request.text
)
if ballot_box_request.status_code != 200:
return json_response(
status=500,
error_codename=ErrorCodes.INTERNAL_SERVER_ERROR
)
except Exception as err:
print(f"Exception deleting child {pk} for election {election_id}: {err}")

election_obj.delete()
except Exception as err:
print(f"Exception deleting election {election_id}: {err}")

election_obj.delete()

data = {'status': 'ok'}
return json_response(data)
Expand Down

0 comments on commit 3a9ec7b

Please sign in to comment.