Views
FragmentMixin
¶
Bases: TemplateResponseMixin, ContextMixin
Make a view buildable as a fragment that can be embedded in a template.
Most fragments are used in two different ways : - in the request/response cycle, like any regular view - in templates, where the rendering is done in another view
This mixin aims to simplify the initial fragment rendering. The rendered fragment will then be able to re-render itself through the request/response cycle if it uses HTMX.
Example
class MyFragment(FragmentMixin, FormView):
template_name = "app/fragment.jinja"
form_class = MyForm
success_url = reverse_lazy("foo:bar")
# in another view :
def some_view(request):
fragment = MyFragment.as_fragment()
return render(
request,
"app/template.jinja",
context={"fragment": fragment(request)
}
# in urls.py
urlpatterns = [
path("foo/view", some_view),
path("foo/fragment", MyFragment.as_view()),
]
reload_on_redirect = False
class-attribute
instance-attribute
¶
If True, this fragment will trigger a full page reload on redirect.
UseFragmentsMixin
¶
Bases: ContextMixin
Mark a view as using fragments.
This mixin is not mandatory
(you may as well render manually your fragments in the get_context_data method).
However, the interface of this class bring some distinction
between fragments and other context data, which may
reduce boilerplate.
Example
class FooFragment(FragmentMixin, FormView): ...
class BarFragment(FragmentMixin, FormView): ...
class AdminFragment(FragmentMixin, FormView): ...
class MyView(UseFragmentsMixin, TemplateView)
template_name = "app/view.jinja"
fragments = {
"foo": FooFragment
"bar": BarFragment(template_name="some_template.jinja")
}
fragments_data = {
"foo": {"some": "data"} # this will be passed to the FooFragment renderer
}
def get_fragments(self):
res = super().get_fragments()
if self.request.user.is_superuser:
res["admin_fragment"] = AdminFragment
return res
get_fragment_data()
¶
get_fragment_context_data()
¶
Return the rendered fragments as context data.
Source code in core/views/mixins.py
PermissionGroupsUpdateView
¶
Bases: PermissionRequiredMixin, SuccessMessageMixin, UpdateView
Manage the groups that have a specific permission.
Notes
This is an UpdateView, but unlike typical UpdateView,
it doesn't accept url arguments to retrieve the object
to update.
As such, a PermissionGroupsUpdateView can only deal with
a single hardcoded permission.
This is not a limitation, but an on-purpose design, mainly for security matters.
Example
SelectionDateForm(*args, **kwargs)
¶
Bases: Form
Source code in subscription/forms.py
SubscriptionExistingUserForm(*args, initial=None, **kwargs)
¶
Bases: SubscriptionForm
Form to add a subscription to an existing user.
Source code in subscription/forms.py
SubscriptionNewUserForm(*args, initial=None, **kwargs)
¶
Bases: SubscriptionForm
Form to create subscriptions with the user they belong to.
Examples:
```py assert not User.objects.filter(email=request.POST.get("email")).exists() form = SubscriptionNewUserForm(request.POST) if form.is_valid(): form.save()
now the user exists and is subscribed¶
user = User.objects.get(email=request.POST.get("email")) assert user.is_subscribed
Source code in subscription/forms.py
clean()
¶
Initialize the User linked to this subscription.
Warning
The User is initialized, but not saved.
Don't use it for operations that expect
a persisted object.
Source code in subscription/forms.py
Subscription
¶
Bases: Model
semester_duration
property
¶
compute_start(d=None, duration=1, user=None)
staticmethod
¶
Computes the start date of the subscription.
The computation is done with respect to the given date (default is today) and the start date given in settings.SITH_SEMESTER_START_AUTUMN. It takes the nearest past start date. Exemples: with SITH_SEMESTER_START_AUTUMN = (8, 15) Today -> Start date 2015-03-17 -> 2015-02-15 2015-01-11 -> 2014-08-15.
Source code in subscription/models.py
compute_end(duration, start=None, user=None)
staticmethod
¶
Compute the end date of the subscription.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration
|
int | float
|
the duration of the subscription, in semester (for example, 2 => 2 semesters => 1 year) |
required |
start
|
date | None
|
The start date of the subscription |
None
|
user
|
User | None
|
the user which is (or will be) subscribed |
None
|
Exemples
Start - Duration -> End date 2015-09-18 - 1 -> 2016-03-18 2015-09-18 - 2 -> 2016-09-18 2015-09-18 - 3 -> 2017-03-18 2015-09-18 - 4 -> 2017-09-18.
Source code in subscription/models.py
CreateSubscriptionFragment
¶
Bases: PermissionRequiredMixin, FragmentMixin, CreateView
CreateSubscriptionExistingUserFragment
¶
CreateSubscriptionNewUserFragment
¶
NewSubscription
¶
Bases: PermissionRequiredMixin, UseFragmentsMixin, TemplateView
SubscriptionCreatedFragment
¶
Bases: PermissionRequiredMixin, DetailView
SubscriptionPermissionView
¶
Bases: PermissionGroupsUpdateView
Manage the groups that have access to the subscription creation page.
SubscriptionsStatsView
¶
Bases: FormView