Add tooltip on current registered card, allow barmen to delete cards and make card deletion a fragment

This commit is contained in:
2024-12-10 23:48:46 +01:00
parent 466fe58763
commit 4975475e85
6 changed files with 107 additions and 32 deletions

View File

@ -13,10 +13,13 @@
<em class="no-cards">{% trans %}No student card registered.{% endtrans %}</em>
{% else %}
<p>
{% trans %}Registered{% endtrans %} <i class="fa fa-check"></i> &nbsp; - &nbsp;
<a href="{{ url('counter:delete_student_card', customer_id=customer.pk) }}">
{% trans %}Delete{% endtrans %}
</a>
{% trans %}Card registered{% endtrans %}</span>
<span tooltip="{% trans uid=customer.student_card.uid %}uid: {{ uid }} {% endtrans %}"><i class="fa fa-check" style="color: green"></i></span> &nbsp; - &nbsp;
<button
hx-get="{{ url('counter:delete_student_card', customer_id=customer.pk) }}"
hx-swap="outerHTML"
hx-target="#student_card_form"
>{% trans %}Delete{% endtrans %}</button>
</p>
{% endif %}
</div>

View File

@ -0,0 +1,15 @@
<div id="student_card_form">
<form hx-post="{{ action }}" hx-swap="outerHTML" hx-target="#student_card_form">
{% csrf_token %}
<p>{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}</p>
<input type="submit" value="{% trans %}Confirm{% endtrans %}" />
<input
hx-get="{{ action_cancel }}"
hx-swap="outerHTML"
hx-target="#student_card_form"
type="submit"
name="cancel"
value="{% trans %}Cancel{% endtrans %}"
/>
</form>
</div>

View File

@ -198,8 +198,7 @@ class TestStudentCard(TestCase):
StudentCard, customer=cls.customer.customer, uid="8A89B82018B0A0"
)
def setUp(self):
# Auto login on counter
def login_in_counter(self):
self.client.post(
reverse("counter:login", args=[self.counter.id]),
{"username": self.barmen.username, "password": "plop"},
@ -222,6 +221,7 @@ class TestStudentCard(TestCase):
]
def test_search_user_with_student_card(self):
self.login_in_counter()
response = self.client.post(
reverse("counter:details", args=[self.counter.id]),
{"code": self.valid_card.uid},
@ -233,6 +233,7 @@ class TestStudentCard(TestCase):
)
def test_add_student_card_from_counter(self):
self.login_in_counter()
for uid in ["8B90734A802A8F", "ABCAAAFAAFAAAB", "15248196326518"]:
customer = subscriber_user.make().customer
response = self.client.post(
@ -251,6 +252,7 @@ class TestStudentCard(TestCase):
assert customer.student_card.uid == uid
def test_add_student_card_from_counter_fail(self):
self.login_in_counter()
customer = subscriber_user.make().customer
for uid, error_msg in self.invalid_uids():
response = self.client.post(
@ -269,25 +271,15 @@ class TestStudentCard(TestCase):
assert not hasattr(customer, "student_card")
def test_add_student_card_from_counter_unauthorized(self):
barman = subscriber_user.make()
self.counter.sellers.add(barman)
customer = self.customer.customer
# There is someone logged to a counter
# with the client of this TestCase instance,
# so we create a new client, in order to check
# that using a client not logged to a counter
# where another client is logged still isn't authorized.
client = Client()
def send_valid_request(counter_id):
return client.post(
return self.client.post(
reverse(
"counter:add_student_card", kwargs={"customer_id": customer.pk}
"counter:add_student_card", kwargs={"customer_id": self.customer.pk}
),
{"uid": "8B90734A802A8F"},
HTTP_REFERER=reverse(
"counter:click",
kwargs={"counter_id": counter_id, "user_id": customer.pk},
kwargs={"counter_id": counter_id, "user_id": self.customer.pk},
),
)
@ -295,7 +287,7 @@ class TestStudentCard(TestCase):
assert send_valid_request(self.counter.id).status_code == 403
# Send to a non bar counter
client.force_login(self.club_admin)
self.client.force_login(self.club_admin)
assert send_valid_request(self.club_counter.id).status_code == 403
def test_delete_student_card_with_owner(self):
@ -322,6 +314,24 @@ class TestStudentCard(TestCase):
self.customer.customer.refresh_from_db()
assert not hasattr(self.customer.customer, "student_card")
def test_delete_student_card_from_counter(self):
self.login_in_counter()
self.client.post(
reverse(
"counter:delete_student_card",
kwargs={"customer_id": self.customer.customer.pk},
),
http_referer=reverse(
"counter:click",
kwargs={
"counter_id": self.counter.id,
"user_id": self.customer.customer.pk,
},
),
)
self.customer.customer.refresh_from_db()
assert not hasattr(self.customer.customer, "student_card")
def test_delete_student_card_fail(self):
"""Test that non-admin users cannot delete student cards"""
self.client.force_login(self.subscriber)
@ -336,7 +346,7 @@ class TestStudentCard(TestCase):
assert not hasattr(self.subscriber.customer, "student_card")
def test_add_student_card_from_user_preferences(self):
users = [self.subscriber, self.board_admin, self.root]
users = [self.customer, self.board_admin, self.root]
uids = ["8B90734A802A8F", "ABCAAAFAAFAAAB", "15248196326518"]
for user, uid in itertools.product(users, uids):
self.customer.customer.student_card.delete()
@ -353,7 +363,7 @@ class TestStudentCard(TestCase):
self.customer.customer.refresh_from_db()
assert self.customer.customer.student_card.uid == uid
self.assertContains(response, text="Enregistré")
self.assertContains(response, text="Carte enregistrée")
def test_add_student_card_from_user_preferences_fail(self):
customer = subscriber_user.make()

View File

@ -22,22 +22,32 @@ from django.utils.translation import gettext as _
from django.views.generic.edit import DeleteView, FormView
from core.utils import FormFragmentTemplateData
from core.views import CanEditMixin
from core.views import can_edit
from counter.forms import StudentCardForm
from counter.models import Customer, StudentCard
from counter.utils import is_logged_in_counter
class StudentCardDeleteView(DeleteView, CanEditMixin):
"""View used to delete a card from a user."""
class StudentCardDeleteView(DeleteView):
"""View used to delete a card from a user. This is a fragment view !"""
model = StudentCard
template_name = "core/delete_confirm.jinja"
template_name = "counter/fragments/delete_student_card.jinja"
def dispatch(self, request, *args, **kwargs):
def dispatch(self, request: HttpRequest, *args, **kwargs):
self.customer = get_object_or_404(Customer, pk=kwargs["customer_id"])
if not is_logged_in_counter(request) and not can_edit(
self.get_object(), request.user
):
raise PermissionDenied()
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["action"] = self.request.path
context["action_cancel"] = self.get_success_url()
return context
def get_object(self, queryset=None):
if not hasattr(self.customer, "student_card"):
raise Http404(
@ -47,7 +57,9 @@ class StudentCardDeleteView(DeleteView, CanEditMixin):
return self.customer.student_card
def get_success_url(self, **kwargs):
return reverse("core:user_prefs", kwargs={"user_id": self.customer.user_id})
return reverse(
"counter:add_student_card", kwargs={"customer_id": self.customer.pk}
)
class StudentCardFormView(FormView):