mirror of
https://github.com/ae-utbm/sith.git
synced 2026-05-13 20:48:06 +00:00
refactor: use FragmentMixin for subscription fragments
This commit is contained in:
@@ -79,7 +79,6 @@ class SubscriptionNewUserForm(SubscriptionForm):
|
||||
"""
|
||||
|
||||
allowed_payment_methods = ["CARD", "CASH"]
|
||||
template_name = "subscription/forms/create_new_user.jinja"
|
||||
|
||||
__user_fields = forms.fields_for_model(
|
||||
User,
|
||||
@@ -153,7 +152,6 @@ class SubscriptionNewUserForm(SubscriptionForm):
|
||||
class SubscriptionExistingUserForm(SubscriptionForm):
|
||||
"""Form to add a subscription to an existing user."""
|
||||
|
||||
template_name = "subscription/forms/create_existing_user.jinja"
|
||||
required_css_class = "required"
|
||||
|
||||
birthdate = forms.fields_for_model(
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
{% load static %}
|
||||
{% load i18n %}
|
||||
|
||||
|
||||
<div x-data="existing_user_subscription_form" class="form-content existing-user">
|
||||
<fieldset>
|
||||
{{ errors }}
|
||||
{% for field, errors in fields %}
|
||||
<p{% with classes=field.css_classes %}{% if classes %} class="{{ classes }}"{% endif %}{% endwith %}>
|
||||
{{ field.label_tag }}
|
||||
{{ field }}
|
||||
{% if field.help_text %}
|
||||
<span class="helptext">{{ field.help_text }}</span>
|
||||
{% endif %}
|
||||
</p>
|
||||
{% if field.name == "payment_method" %}
|
||||
<i>
|
||||
{% blocktranslate %}If the subscription is done using the AE account, you must also click it on the AE counter.{% endblocktranslate %}
|
||||
</i>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
<div
|
||||
id="subscription-form-user-mini-profile"
|
||||
x-html="profileFragment"
|
||||
:aria-busy="loading"
|
||||
></div>
|
||||
</div>
|
||||
@@ -1 +0,0 @@
|
||||
{{ form.as_p }}
|
||||
@@ -0,0 +1,38 @@
|
||||
<form
|
||||
hx-post="{{ url("subscription:fragment-existing-user") }}"
|
||||
hx-target="this"
|
||||
hx-disabled-elt="find input[type='submit']"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
{% csrf_token %}
|
||||
|
||||
<div x-data="existing_user_subscription_form" class="form-content existing-user">
|
||||
<fieldset>
|
||||
{{ form.errors }}
|
||||
{% for field in form %}
|
||||
<div class="form-group">
|
||||
{{ field.label_tag() }}
|
||||
{{ field }}
|
||||
{% if field.help_text %}
|
||||
<span class="helptext">{{ field.help_text }}</span>
|
||||
{% endif %}
|
||||
{% if field.name == "payment_method" %}
|
||||
<i>
|
||||
{% trans trimmed %}
|
||||
If the subscription is done using the AE account,
|
||||
you must also click it on the AE counter.
|
||||
{% endtrans %}
|
||||
</i>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
<div
|
||||
id="subscription-form-user-mini-profile"
|
||||
x-html="profileFragment"
|
||||
:aria-busy="loading"
|
||||
></div>
|
||||
</div>
|
||||
|
||||
<input type="submit" value="{% trans %}Save{% endtrans %}">
|
||||
</form>
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
<form
|
||||
hx-post="{{ post_url }}"
|
||||
hx-post="{{ url("subscription:fragment-existing-user") }}"
|
||||
hx-target="this"
|
||||
hx-disabled-elt="find input[type='submit']"
|
||||
hx-swap="outerHTML"
|
||||
@@ -18,22 +18,14 @@
|
||||
<link rel="stylesheet" href="{{ static("subscription/css/subscription.scss") }}">
|
||||
{% endblock %}
|
||||
|
||||
{% macro form_fragment(form_object, post_url) %}
|
||||
{# Include the form fragment inside a with block,
|
||||
in order to inject the right form in the right place #}
|
||||
{% with form=form_object, post_url=post_url %}
|
||||
{% include "subscription/fragments/creation_form.jinja" %}
|
||||
{% endwith %}
|
||||
{% endmacro %}
|
||||
|
||||
{% block content %}
|
||||
<h3>{% trans %}New subscription{% endtrans %}</h3>
|
||||
<ui-tab-group id="subscription-form">
|
||||
<ui-tab title="{% trans %}Existing member{% endtrans %}" active>
|
||||
{{ form_fragment(existing_user_form, existing_user_post_url) }}
|
||||
{{ existing_user_fragment }}
|
||||
</ui-tab>
|
||||
<ui-tab title="{% trans %}New member{% endtrans %}">
|
||||
{{ form_fragment(new_user_form, new_user_post_url) }}
|
||||
{{ new_user_fragment }}
|
||||
</ui-tab>
|
||||
</ui-tab-group>
|
||||
{% endblock %}
|
||||
|
||||
+14
-19
@@ -23,6 +23,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
from django.views.generic import CreateView, DetailView, TemplateView
|
||||
from django.views.generic.edit import FormView
|
||||
|
||||
from core.views import FragmentMixin, UseFragmentsMixin
|
||||
from core.views.group import PermissionGroupsUpdateView
|
||||
from subscription.forms import (
|
||||
SelectionDateForm,
|
||||
@@ -32,24 +33,9 @@ from subscription.forms import (
|
||||
from subscription.models import Subscription
|
||||
|
||||
|
||||
class NewSubscription(PermissionRequiredMixin, TemplateView):
|
||||
template_name = "subscription/subscription.jinja"
|
||||
permission_required = "subscription.add_subscription"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(**kwargs) | {
|
||||
"existing_user_form": SubscriptionExistingUserForm(
|
||||
initial={"member": self.request.GET.get("member")}
|
||||
),
|
||||
"new_user_form": SubscriptionNewUserForm(),
|
||||
"existing_user_post_url": reverse("subscription:fragment-existing-user"),
|
||||
"new_user_post_url": reverse("subscription:fragment-new-user"),
|
||||
}
|
||||
|
||||
|
||||
class CreateSubscriptionFragment(PermissionRequiredMixin, CreateView):
|
||||
template_name = "subscription/fragments/creation_form.jinja"
|
||||
class CreateSubscriptionFragment(PermissionRequiredMixin, FragmentMixin, CreateView):
|
||||
permission_required = "subscription.add_subscription"
|
||||
object = None
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse(
|
||||
@@ -61,14 +47,14 @@ class CreateSubscriptionExistingUserFragment(CreateSubscriptionFragment):
|
||||
"""Create a subscription for a user who already exists."""
|
||||
|
||||
form_class = SubscriptionExistingUserForm
|
||||
extra_context = {"post_url": reverse_lazy("subscription:fragment-existing-user")}
|
||||
template_name = "subscription/fragments/creation_form_existing_user.jinja"
|
||||
|
||||
|
||||
class CreateSubscriptionNewUserFragment(CreateSubscriptionFragment):
|
||||
"""Create a subscription for a user who doesn't exist yet."""
|
||||
|
||||
form_class = SubscriptionNewUserForm
|
||||
extra_context = {"post_url": reverse_lazy("subscription:fragment-new-user")}
|
||||
template_name = "subscription/fragments/creation_form_new_user.jinja"
|
||||
|
||||
def form_valid(self, form):
|
||||
res = super().form_valid(form)
|
||||
@@ -83,6 +69,15 @@ class CreateSubscriptionNewUserFragment(CreateSubscriptionFragment):
|
||||
return res
|
||||
|
||||
|
||||
class NewSubscription(PermissionRequiredMixin, UseFragmentsMixin, TemplateView):
|
||||
template_name = "subscription/subscription.jinja"
|
||||
permission_required = "subscription.add_subscription"
|
||||
fragments = {
|
||||
"new_user_fragment": CreateSubscriptionNewUserFragment,
|
||||
"existing_user_fragment": CreateSubscriptionExistingUserFragment,
|
||||
}
|
||||
|
||||
|
||||
class SubscriptionCreatedFragment(PermissionRequiredMixin, DetailView):
|
||||
template_name = "subscription/fragments/creation_success.jinja"
|
||||
permission_required = "subscription.add_subscription"
|
||||
|
||||
Reference in New Issue
Block a user