diff --git a/core/templates/core/user_preferences.jinja b/core/templates/core/user_preferences.jinja index d70371a2..722e7c44 100644 --- a/core/templates/core/user_preferences.jinja +++ b/core/templates/core/user_preferences.jinja @@ -36,19 +36,11 @@ {% if student_card %} - {% with - form=student_card.form, - action=student_card.context.action, - customer=student_card.context.customer, - student_cards=student_card.context.student_cards - %} - {% include student_card.template %} - {% endwith %} - -
- {% trans %}You can add a card by asking at a counter or add it yourself here. If you want to manually - add a student card yourself, you'll need a NFC reader. We store the UID of the card which is 14 characters long.{% endtrans %} -
-{% endif %} - + {{ student_card }} ++ {% trans %}You can add a card by asking at a counter or add it yourself here. If you want to manually + add a student card yourself, you'll need a NFC reader. We store the UID of the card which is 14 characters long.{% endtrans %} +
+ {% endif %} + {% endblock %} \ No newline at end of file diff --git a/core/utils.py b/core/utils.py index 5b6191f6..cdd72fa6 100644 --- a/core/utils.py +++ b/core/utils.py @@ -13,22 +13,41 @@ # # +from dataclasses import dataclass from datetime import date # Image utils from io import BytesIO -from typing import Optional +from typing import Any import PIL from django.conf import settings from django.core.files.base import ContentFile +from django.forms import BaseForm from django.http import HttpRequest +from django.template.loader import render_to_string +from django.utils.html import SafeString from django.utils.timezone import localdate from PIL import ExifTags from PIL.Image import Image, Resampling -def get_start_of_semester(today: Optional[date] = None) -> date: +@dataclass +class FormFragmentTemplateData[T: BaseForm]: + """Dataclass used to pre-render form fragments""" + + form: T + template: str + context: dict[str, Any] + + def render(self, request: HttpRequest) -> SafeString: + # Request is needed for csrf_tokens + return render_to_string( + self.template, context={"form": self.form, **self.context}, request=request + ) + + +def get_start_of_semester(today: date | None = None) -> date: """Return the date of the start of the semester of the given date. If no date is given, return the start date of the current semester. @@ -58,7 +77,7 @@ def get_start_of_semester(today: Optional[date] = None) -> date: return autumn.replace(year=autumn.year - 1) -def get_semester_code(d: Optional[date] = None) -> str: +def get_semester_code(d: date | None = None) -> str: """Return the semester code of the given date. If no date is given, return the semester code of the current semester. diff --git a/core/views/user.py b/core/views/user.py index 83916fb1..2c6b01fc 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -578,8 +578,8 @@ class UserPreferencesView(UserTabsMixin, CanEditMixin, UpdateView): kwargs["trombi_form"] = UserTrombiForm() if hasattr(self.object, "customer"): kwargs["student_card"] = StudentCardFormView.get_template_data( - self.request, self.object.customer - ).as_dict() + self.object.customer + ).render(self.request) return kwargs diff --git a/counter/templates/counter/counter_click.jinja b/counter/templates/counter/counter_click.jinja index ed89b100..7c36b01b 100644 --- a/counter/templates/counter/counter_click.jinja +++ b/counter/templates/counter/counter_click.jinja @@ -31,131 +31,124 @@{% trans %}Amount: {% endtrans %}{{ customer.amount }} €
{% if counter.type == 'BAR' %} - {% with - form=student_card.form, - action=student_card.context.action, - customer=student_card.context.customer, - student_cards=student_card.context.student_cards - %} - {% include student_card.template %} - {% endwith %} -{% endif %} - + {{ student_card }} + {% endif %} + -{% trans %}Basket: {% endtrans %}
- -- Total: - - € -
- - - -{% trans %}Basket: {% endtrans %}
+ ++ Total: + + € +
+ + + +