Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casser counter click étape 1 : introduire des fragments #921

Open
wants to merge 2 commits into
base: taiste
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion core/static/webpack/utils/web-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
**/
export function registerComponent(name: string, options?: ElementDefinitionOptions) {
return (component: CustomElementConstructor) => {
window.customElements.define(name, component, options);
try {
window.customElements.define(name, component, options);
} catch (e) {
if (e instanceof DOMException) {
// biome-ignore lint/suspicious/noConsole: it's handy to troobleshot
console.warn(e.message);
return;
}
throw e;
}
};
}

Expand Down
12 changes: 1 addition & 11 deletions counter/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ class Meta:


class StudentCardForm(forms.ModelForm):
"""Form for adding student cards
Only used for user profile since CounterClick is to complicated.
"""
"""Form for adding student cards"""

class Meta:
model = StudentCard
Expand Down Expand Up @@ -114,14 +112,6 @@ def clean(self):
return cleaned_data


class NFCCardForm(forms.Form):
student_card_uid = forms.CharField(
max_length=StudentCard.UID_SIZE,
required=False,
widget=NFCTextInput,
)


class RefillForm(forms.ModelForm):
error_css_class = "error"
required_css_class = "required"
Expand Down
25 changes: 25 additions & 0 deletions counter/templates/counter/add_student_card_fragment.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div id="student_card_form">
<h3>{% trans %}Add a student card{% endtrans %}</h3>
<form
hx-trigger="submit"
hx-post="{{ action }}"
hx-swap="outerHTML"
hx-target="#student_card_form"
>
{% csrf_token %}
{{ form.as_p() }}
<button>{% trans %}Go{% endtrans %}</button>

</form>
<h6>{% trans %}Registered cards{% endtrans %}</h6>
{% if student_cards %}

<ul>
{% for card in student_cards %}
<li>{{ card.uid }}</li>
{% endfor %}
</ul>
{% else %}
{% trans %}No card registered{% endtrans %}
{% endif %}
</div>
25 changes: 6 additions & 19 deletions counter/templates/counter/counter_click.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,13 @@
{{ user_mini_profile(customer.user) }}
{{ user_subscription(customer.user) }}
<p>{% trans %}Amount: {% endtrans %}{{ customer.amount }} €</p>
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
<input type="hidden" name="action" value="add_student_card">
{% trans %}Add a student card{% endtrans %}
{{ student_card_input.student_card_uid }}
{% if request.session['not_valid_student_card_uid'] %}
<p><strong>{% trans %}This is not a valid student card UID{% endtrans %}</strong></p>
{% endif %}
<input type="submit" value="{% trans %}Go{% endtrans %}"/>
</form>
<h6>{% trans %}Registered cards{% endtrans %}</h6>
{% if student_cards %}

<ul>
{% for card in student_cards %}
<li>{{ card.uid }}</li>
{% endfor %}
</ul>
{% else %}
{% trans %}No card registered{% endtrans %}
{% if counter.type == 'BAR' %}
<div
hx-get="{{ url('counter:add_student_card_fragment', counter_id=counter.id, customer_id=customer.pk) }}"
hx-trigger="load"
hx-swap="outerHTML"
></div>
{% endif %}
</div>

Expand Down
127 changes: 89 additions & 38 deletions counter/tests/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def setUpTestData(cls):
cls.root = User.objects.get(username="root")

cls.counter = Counter.objects.get(id=2)
cls.ae_counter = Counter.objects.get(name="AE")

def setUp(self):
# Auto login on counter
Expand All @@ -191,93 +192,143 @@ def test_add_student_card_from_counter(self):
# Test card with mixed letters and numbers
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": "8B90734A802A8F", "action": "add_student_card"},
{"uid": "8B90734A802A8F"},
)
self.assertContains(response, text="8B90734A802A8F")
assert response.status_code == 302
self.assertContains(self.client.get(response.url), text="8B90734A802A8F")

# Test card with only numbers
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": "04786547890123", "action": "add_student_card"},
{"uid": "04786547890123"},
)
self.assertContains(response, text="04786547890123")
assert response.status_code == 302
self.assertContains(self.client.get(response.url), text="04786547890123")

# Test card with only letters
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": "ABCAAAFAAFAAAB", "action": "add_student_card"},
{"uid": "ABCAAAFAAFAAAB"},
)
self.assertContains(response, text="ABCAAAFAAFAAAB")
assert response.status_code == 302
self.assertContains(self.client.get(response.url), text="ABCAAAFAAFAAAB")

def test_add_student_card_from_counter_fail(self):
# UID too short
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": "8B90734A802A8", "action": "add_student_card"},
)
self.assertContains(
response, text="Ce n'est pas un UID de carte étudiante valide"
{"uid": "8B90734A802A8"},
)
self.assertContains(response, text="Cet UID est invalide")

# UID too long
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": "8B90734A802A8FA", "action": "add_student_card"},
{"uid": "8B90734A802A8FA"},
)
self.assertContains(response, text="Cet UID est invalide")
self.assertContains(
response, text="Ce n'est pas un UID de carte étudiante valide"
response,
text="Assurez-vous que cette valeur comporte au plus 14 caractères (actuellement 15).",
)

# Test with already existing card
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": "9A89B82018B0A0", "action": "add_student_card"},
{"uid": "9A89B82018B0A0"},
)
self.assertContains(response, text="Cet UID est invalide")
self.assertContains(
response, text="Ce n'est pas un UID de carte étudiante valide"
response, text="Un objet Student card avec ce champ Uid existe déjà."
)

# Test with lowercase
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": "8b90734a802a9f", "action": "add_student_card"},
)
self.assertContains(
response, text="Ce n'est pas un UID de carte étudiante valide"
{"uid": "8b90734a802a9f"},
)
self.assertContains(response, text="Cet UID est invalide")

# Test with white spaces
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
"counter:add_student_card_fragment",
kwargs={
"counter_id": self.counter.id,
"customer_id": self.sli.customer.pk,
},
),
{"student_card_uid": " ", "action": "add_student_card"},
)
self.assertContains(
response, text="Ce n'est pas un UID de carte étudiante valide"
{"uid": " "},
)
self.assertContains(response, text="Cet UID est invalide")
self.assertContains(response, text="Ce champ est obligatoire.")

def test_add_student_card_from_counter_unauthorized(self):
# Send to a counter where you aren't logged in
self.client.post(
reverse("counter:logout", args=[self.counter.id]),
{"user_id": self.krophil.id},
)

def send_valid_request(client, counter_id):
return client.post(
reverse(
"counter:add_student_card_fragment",
kwargs={
"counter_id": counter_id,
"customer_id": self.sli.customer.pk,
},
),
{"uid": "8B90734A802A8F"},
)

assert send_valid_request(self.client, self.counter.id).status_code == 403

# Send to a non bar counter
self.client.force_login(self.skia)
assert send_valid_request(self.client, self.ae_counter.id)

def test_delete_student_card_with_owner(self):
self.client.force_login(self.sli)
Expand Down
6 changes: 6 additions & 0 deletions counter/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
RefillingDeleteView,
SellingDeleteView,
StudentCardDeleteView,
StudentCardFormFragmentView,
StudentCardFormView,
counter_login,
counter_logout,
Expand Down Expand Up @@ -73,6 +74,11 @@
StudentCardFormView.as_view(),
name="add_student_card",
),
path(
"customer/<int:customer_id>/card/add/counter/<int:counter_id>/",
StudentCardFormFragmentView.as_view(),
name="add_student_card_fragment",
),
path(
"customer/<int:customer_id>/card/delete/<int:card_id>/",
StudentCardDeleteView.as_view(),
Expand Down
Loading
Loading