Thursday 30 September 2021

How to queue requests in Django?

I manage a physical locker with Django (DRF). Users fill out a form, authenticate via link sent to their e-mail, authorize via a pin displayed on the locker.

My view should handle three cases:

  • If user authenticates and authorizes successfully, pin displayed on the locker is replaced with a generic message and the locker opens. (Already implemented)

  • If the user fails to authorize within 3 minutes, locker pin is replaced with a generic message.

  • If a new authorization request is made by user Foo, while authorization for user Bar is still incomplete, stack the request in a queue and wait for case 1. or case 2. to complete.

How can I:

  • Implement a request queue, such that the pin displayed on the locker does not get overridden/replaced when a new request comes in?
  • How can I wait 3 minutes for authorization to be completed before processing the next request?

View as is, in case it is useful:

   if request.method == 'POST':
        form = ConfirmationForm(request.POST)
        if form.is_valid():
            if pin == form.cleaned_data['pin']:
                open_bay(jwt_token=jwt[1], pin=pin)
                display_generic_message(jwt_token=jwt[1])
                lock_bay(jwt_token=jwt[1], pin=pin)
                return render(request, 'static/pages/request-success.html')
            else:
                pass
    else:
        form = ConfirmationForm()
    return render(request, 'static/pages/confirmation.html', {'form': form})


from How to queue requests in Django?

No comments:

Post a Comment