Skip to content

Commit

Permalink
🐞 Sending census auth codes is not done in async manner (#323) (#324)
Browse files Browse the repository at this point in the history
Parent issue: sequentech/meta#284
  • Loading branch information
edulix committed Oct 12, 2023
1 parent 7491364 commit 338a85f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
10 changes: 10 additions & 0 deletions iam/api/management/commands/bulk_delete_voters.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ def handle(self, *args, **options):
DELETE FROM api_userdata
USING users_to_delete
WHERE api_userdata.id = users_to_delete.userdata_id
),
delete_authmethods_code AS (
DELETE FROM authmethods_code
USING users_to_delete
WHERE authmethods_code.user_id=users_to_delete.user_id
),
delete_authmethods_onetimelink AS (
DELETE FROM authmethods_onetimelink
USING users_to_delete
WHERE authmethods_onetimelink.user_id=users_to_delete.user_id
)
DELETE FROM auth_user
USING users_to_delete
Expand Down
17 changes: 15 additions & 2 deletions iam/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def parse_json_request(request):
'''
return json.loads(request.content.decode('utf-8'))

@shared_task(name='api.tasks.census_send_auth_task')
def census_send_auth_task(
pk,
ip,
Expand Down Expand Up @@ -84,6 +85,7 @@ def census_send_auth_task(
userdata = UserData.objects.filter(user__in=users)
new_census = ACL.objects.filter(perm="vote", object_type="AuthEvent", object_id=str(pk), user__in=userdata)

logger.info("census_send_auth_task(pk = %r): new_census.count() = %r" % (pk, new_census.count()))
census = []
if e.auth_method == auth_method:
census = [i.user.user.id for i in new_census]
Expand All @@ -93,6 +95,7 @@ def census_send_auth_task(
census.append(item.user.user.id)
elif "email" == auth_method and item.user.user.email:
census.append(item.user.user.id)
logger.info("census_send_auth_task(pk = %r): len(final_census) = %r" % (pk, len(census)))

extend_errors = plugins.call("extend_send_message", e, len(census), kwargs)
if extend_errors:
Expand All @@ -107,9 +110,19 @@ def census_send_auth_task(
isinstance(config['force_create_otl'], bool) and
config.get('force_create_otl', False)
)
logger.info("census_send_auth_task(pk = %r): send_codes.apply_async" % pk)

logger.info("census_send_auth_task(pk = %r): send_codes.apply_async with census_size = %r" % (pk, len(census)))
# splitting in multiple jobs
send_codes.apply_async(
args=[census, ip, auth_method, config, sender_uid, pk, force_create_otl]
args=[
census,
ip,
auth_method,
config,
sender_uid,
pk,
force_create_otl
]
)

def launch_tally(auth_event):
Expand Down
18 changes: 10 additions & 8 deletions iam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2203,16 +2203,17 @@ def post(self, request, pk):
if req.get('filter', None):
config['filter'] = req.get('filter', None)
else:
send_error = census_send_auth_task(
census_send_auth_task.apply_async(
args=[
pk,
get_client_ip(request),
None,
userids,
auth_method,
request.user.id,
**extra_req)
if send_error:
return json_response(**send_error)
],
kwargs=extra_req
)
return json_response(data)

if config.get('msg', None) is not None:
Expand All @@ -2233,15 +2234,16 @@ def post(self, request, pk):
status=400,
error_codename=ErrorCodes.BAD_REQUEST)

send_error = census_send_auth_task(
census_send_auth_task.apply_async(
args=[
pk,
get_client_ip(request),
config, userids,
auth_method,
request.user.id,
**extra_req)
if send_error:
return json_response(**send_error)
],
kwargs=extra_req
)
return json_response(data)
census_send_auth = login_required(CensusSendAuth.as_view())

Expand Down
12 changes: 9 additions & 3 deletions iam/captcha/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@ def get(self, request):


def getsize(font, text):
if hasattr(font, 'getoffset'):
if hasattr(font, 'getbbox'):
left, top, right, bottom = font.getbbox(text)
width = abs(right - left)
height = abs(top - bottom)
return [width, height]
elif hasattr(font, 'getoffset'):
return [x + y for x, y in zip(font.getsize(text), font.getoffset(text))]
else:
elif hasattr(font, 'getsize'):
return font.getsize(text)

else:
raise Exception('Font has not known properties to get its size')

def noise_arcs(draw, image):
fg_color = '#001100'
Expand Down
1 change: 1 addition & 0 deletions iam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ def send_codes(
eid=None,
force_create_otl=False
):
LOGGER.info("send_codes(): called with eid=%r, len(users) %r" % (eid, len(users)))
from api.models import Action, AuthEvent
from authmethods.models import OneTimeLink

Expand Down

0 comments on commit 338a85f

Please sign in to comment.