Fix counter main

* Fix crash when submitting nothing
* Fix code field not being autofocus
This commit is contained in:
Antoine Bartuccio 2024-12-20 19:06:01 +01:00
parent eaf72ad0ed
commit 652ceb27c2
2 changed files with 31 additions and 9 deletions

View File

@ -87,7 +87,7 @@ class GetUserForm(forms.Form):
def clean(self): def clean(self):
cleaned_data = super().clean() cleaned_data = super().clean()
cus = None customer = None
if cleaned_data["code"] != "": if cleaned_data["code"] != "":
if len(cleaned_data["code"]) == StudentCard.UID_SIZE: if len(cleaned_data["code"]) == StudentCard.UID_SIZE:
card = ( card = (
@ -96,17 +96,18 @@ class GetUserForm(forms.Form):
.first() .first()
) )
if card is not None: if card is not None:
cus = card.customer customer = card.customer
if cus is None: if customer is None:
cus = Customer.objects.filter( customer = Customer.objects.filter(
account_id__iexact=cleaned_data["code"] account_id__iexact=cleaned_data["code"]
).first() ).first()
elif cleaned_data["id"] is not None: elif cleaned_data["id"]:
cus = Customer.objects.filter(user=cleaned_data["id"]).first() customer = Customer.objects.filter(user=cleaned_data["id"]).first()
if cus is None or not cus.can_buy:
if customer is None or not customer.can_buy:
raise forms.ValidationError(_("User not found")) raise forms.ValidationError(_("User not found"))
cleaned_data["user_id"] = cus.user.id cleaned_data["user_id"] = customer.user.id
cleaned_data["user"] = cus.user cleaned_data["user"] = customer.user
return cleaned_data return cleaned_data

View File

@ -59,5 +59,26 @@
{% endif %} {% endif %}
{% endblock %} {% endblock %}
{% block script %}
{{ super() }}
<script type="text/javascript">
window.addEventListener("DOMContentLoaded", () => {
// The login form annoyingly takes priority over the code form
// This is due to the loading time of the web component
// We can't rely on DOMContentLoaded to know if the component is there so we
// periodically run a script until the field is there
const autofocus = () => {
const field = document.querySelector("input[id='id_code']");
if (field === null){
setTimeout(autofocus, 0.5);
return;
}
field.focus();
}
autofocus()
})
</script>
{% endblock %}