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 a383f3e717
commit a548f4744e
2 changed files with 31 additions and 9 deletions

View File

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

View File

@ -59,5 +59,26 @@
{% endif %}
{% 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 %}