mirror of
https://github.com/ae-utbm/sith.git
synced 2026-09-01 18:19:21 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
582e6af2c9 | ||
|
|
bbdf6a5dd2 |
+2
-2
@@ -46,8 +46,8 @@ class ClubAdmin(admin.ModelAdmin):
|
|||||||
@admin.register(ClubRole)
|
@admin.register(ClubRole)
|
||||||
class ClubRoleAdmin(admin.ModelAdmin):
|
class ClubRoleAdmin(admin.ModelAdmin):
|
||||||
list_display = ("name", "club", "is_board", "is_presidency")
|
list_display = ("name", "club", "is_board", "is_presidency")
|
||||||
search_fields = ("name", "club__name")
|
search_fields = ("name",)
|
||||||
autocomplete_fields = ("club", "linked_groups")
|
autocomplete_fields = ("club",)
|
||||||
list_select_related = ("club",)
|
list_select_related = ("club",)
|
||||||
list_filter = (
|
list_filter = (
|
||||||
"is_board",
|
"is_board",
|
||||||
|
|||||||
@@ -479,13 +479,6 @@ class ClubRoleCreateForm(forms.ModelForm):
|
|||||||
class ClubRoleBaseFormSet(forms.BaseInlineFormSet):
|
class ClubRoleBaseFormSet(forms.BaseInlineFormSet):
|
||||||
ordering_widget = forms.HiddenInput()
|
ordering_widget = forms.HiddenInput()
|
||||||
|
|
||||||
def __init__(self, *args, queryset=None, **kwargs):
|
|
||||||
if queryset is None:
|
|
||||||
queryset = self.model._default_manager
|
|
||||||
super().__init__(
|
|
||||||
*args, queryset=queryset.prefetch_related("linked_groups"), **kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
ClubRoleFormSet = forms.inlineformset_factory(
|
ClubRoleFormSet = forms.inlineformset_factory(
|
||||||
Club,
|
Club,
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
# Generated by Django 5.2.17 on 2026-09-01 14:36
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
dependencies = [
|
|
||||||
("club", "0017_linktype_clublink"),
|
|
||||||
("core", "0050_alter_sithfile_moderator"),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.AddField(
|
|
||||||
model_name="clubrole",
|
|
||||||
name="linked_groups",
|
|
||||||
field=models.ManyToManyField(
|
|
||||||
help_text=(
|
|
||||||
"Groups that are automatically given or removed "
|
|
||||||
"to user receiving or losing this club role"
|
|
||||||
),
|
|
||||||
related_name="club_roles",
|
|
||||||
to="core.group",
|
|
||||||
verbose_name="Linked groups",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
+11
-32
@@ -23,8 +23,6 @@
|
|||||||
#
|
#
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import operator
|
|
||||||
from functools import reduce
|
|
||||||
from typing import Iterable, Self
|
from typing import Iterable, Self
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@@ -284,15 +282,6 @@ class ClubRole(OrderedModel):
|
|||||||
"If the role is inactive, people joining the club won't be able to get it."
|
"If the role is inactive, people joining the club won't be able to get it."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
linked_groups = models.ManyToManyField(
|
|
||||||
Group,
|
|
||||||
verbose_name=_("Linked groups"),
|
|
||||||
help_text=_(
|
|
||||||
"Groups that are automatically given or removed "
|
|
||||||
"to user receiving or losing this club role"
|
|
||||||
),
|
|
||||||
related_name="linked_roles",
|
|
||||||
)
|
|
||||||
|
|
||||||
order_with_respect_to = "club"
|
order_with_respect_to = "club"
|
||||||
|
|
||||||
@@ -545,7 +534,7 @@ class Membership(models.Model):
|
|||||||
def _remove_club_groups(
|
def _remove_club_groups(
|
||||||
memberships: Iterable[Membership],
|
memberships: Iterable[Membership],
|
||||||
) -> tuple[int, dict[str, int]]:
|
) -> tuple[int, dict[str, int]]:
|
||||||
"""Remove users of those memberships from the club and club role groups.
|
"""Remove users of those memberships from the club groups.
|
||||||
|
|
||||||
For example, if a user is in the Troll club board,
|
For example, if a user is in the Troll club board,
|
||||||
he is in the board group and the members group of the Troll.
|
he is in the board group and the members group of the Troll.
|
||||||
@@ -564,19 +553,15 @@ class Membership(models.Model):
|
|||||||
clubs = {m.club_id for m in memberships}
|
clubs = {m.club_id for m in memberships}
|
||||||
users = {m.user_id for m in memberships}
|
users = {m.user_id for m in memberships}
|
||||||
groups = Group.objects.filter(Q(club__in=clubs) | Q(club_board__in=clubs))
|
groups = Group.objects.filter(Q(club__in=clubs) | Q(club_board__in=clubs))
|
||||||
role_groups = [
|
|
||||||
Q(user_id=m.user_id, group__linked_roles=m.role_id) for m in memberships
|
|
||||||
]
|
|
||||||
return User.groups.through.objects.filter(
|
return User.groups.through.objects.filter(
|
||||||
(Q(group__in=groups) & Q(user__in=users))
|
Q(group__in=groups) & Q(user__in=users)
|
||||||
| reduce(operator.or_, role_groups)
|
|
||||||
).delete()
|
).delete()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _add_club_groups(
|
def _add_club_groups(
|
||||||
memberships: Iterable[Membership],
|
memberships: Iterable[Membership],
|
||||||
) -> list[User.groups.through]:
|
) -> list[User.groups.through]:
|
||||||
"""Add users of those memberships to the club and club role groups.
|
"""Add users of those memberships to the club groups.
|
||||||
|
|
||||||
For example, if a user just joined the Troll club board,
|
For example, if a user just joined the Troll club board,
|
||||||
he will be added in both the members group and the board group
|
he will be added in both the members group and the board group
|
||||||
@@ -597,40 +582,34 @@ class Membership(models.Model):
|
|||||||
memberships = [m for m in memberships if m.end_date is None]
|
memberships = [m for m in memberships if m.end_date is None]
|
||||||
if not memberships:
|
if not memberships:
|
||||||
return []
|
return []
|
||||||
nb_prefetched = sum(
|
|
||||||
1 for m in memberships if not hasattr(m, "club") or not hasattr(m, "role")
|
if sum(1 for m in memberships if not hasattr(m, "club")) > 1:
|
||||||
)
|
|
||||||
if nb_prefetched > 1:
|
|
||||||
# if more than one membership hasn't its `club` attribute set
|
# if more than one membership hasn't its `club` attribute set
|
||||||
# it's less expensive to reload the whole query with
|
# it's less expensive to reload the whole query with
|
||||||
# a select_related than perform a distinct query
|
# a select_related than perform a distinct query
|
||||||
# to fetch each club.
|
# to fetch each club.
|
||||||
ids = {m.id for m in memberships}
|
ids = {m.id for m in memberships}
|
||||||
memberships = list(
|
memberships = list(
|
||||||
Membership.objects.filter(id__in=ids)
|
Membership.objects.filter(id__in=ids).select_related("club")
|
||||||
.select_related("club", "role")
|
|
||||||
.prefetch_related("role__linked_groups")
|
|
||||||
)
|
)
|
||||||
groups = []
|
club_groups = []
|
||||||
for membership in memberships:
|
for membership in memberships:
|
||||||
groups.append(
|
club_groups.append(
|
||||||
User.groups.through(
|
User.groups.through(
|
||||||
user_id=membership.user_id,
|
user_id=membership.user_id,
|
||||||
group_id=membership.club.members_group_id,
|
group_id=membership.club.members_group_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if membership.role.is_board:
|
if membership.role.is_board:
|
||||||
groups.append(
|
club_groups.append(
|
||||||
User.groups.through(
|
User.groups.through(
|
||||||
user_id=membership.user_id,
|
user_id=membership.user_id,
|
||||||
group_id=membership.club.board_group_id,
|
group_id=membership.club.board_group_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
groups.extend(
|
return User.groups.through.objects.bulk_create(
|
||||||
User.groups.through(user_id=membership.user_id, group_id=g.id)
|
club_groups, ignore_conflicts=True
|
||||||
for g in membership.role.linked_groups.all()
|
|
||||||
)
|
)
|
||||||
return User.groups.through.objects.bulk_create(groups, ignore_conflicts=True)
|
|
||||||
|
|
||||||
|
|
||||||
class Mailing(models.Model):
|
class Mailing(models.Model):
|
||||||
|
|||||||
@@ -49,20 +49,6 @@
|
|||||||
{{ subform.is_active.help_text }}
|
{{ subform.is_active.help_text }}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{% set groups = subform.instance.linked_groups.all()|list %}
|
|
||||||
{% if groups %}
|
|
||||||
<div>
|
|
||||||
<p>
|
|
||||||
<strong>{% trans %}Linked groups : {% endtrans %}</strong>
|
|
||||||
{{ groups|map(attribute="name")|join(", ") }}
|
|
||||||
</p>
|
|
||||||
<p class="helptext">
|
|
||||||
{% trans trimmed %}
|
|
||||||
Users receiving this role will also be assigned to those groups
|
|
||||||
{% endtrans %}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
</div>
|
||||||
</details>
|
</details>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from club.forms import ClubAddMemberForm, JoinClubForm
|
|||||||
from club.models import Club, ClubRole, Membership
|
from club.models import Club, ClubRole, Membership
|
||||||
from club.tests.base import TestClub
|
from club.tests.base import TestClub
|
||||||
from core.baker_recipes import subscriber_user
|
from core.baker_recipes import subscriber_user
|
||||||
from core.models import AnonymousUser, Group, User
|
from core.models import AnonymousUser, User
|
||||||
|
|
||||||
|
|
||||||
class TestMembershipQuerySet(TestClub):
|
class TestMembershipQuerySet(TestClub):
|
||||||
@@ -500,29 +500,6 @@ class TestMembership(TestClub):
|
|||||||
assert self.subscriber.groups.contains(self.club.members_group)
|
assert self.subscriber.groups.contains(self.club.members_group)
|
||||||
assert self.subscriber.groups.contains(self.club.board_group)
|
assert self.subscriber.groups.contains(self.club.board_group)
|
||||||
|
|
||||||
def test_add_to_club_role_group(self):
|
|
||||||
groups = baker.make(Group, _quantity=4)
|
|
||||||
self.subscriber.groups.set(groups[:1])
|
|
||||||
self.board_role.linked_groups.set(groups[1:3])
|
|
||||||
baker.make(
|
|
||||||
Membership, club=self.club, user=self.subscriber, role=self.board_role
|
|
||||||
)
|
|
||||||
assert set(self.subscriber.groups.all()) == {
|
|
||||||
*groups[:3],
|
|
||||||
self.club.board_group,
|
|
||||||
self.club.members_group,
|
|
||||||
}
|
|
||||||
|
|
||||||
def test_remove_from_club_role_group(self):
|
|
||||||
groups = baker.make(Group, _quantity=3)
|
|
||||||
baker.make(
|
|
||||||
Membership, club=self.club, user=self.subscriber, role=self.board_role
|
|
||||||
)
|
|
||||||
self.subscriber.groups.set(groups[:1])
|
|
||||||
self.board_role.linked_groups.set(groups[1:])
|
|
||||||
self.subscriber.memberships.update(end_date=localdate())
|
|
||||||
assert set(self.subscriber.groups.all()) == {groups[0]}
|
|
||||||
|
|
||||||
def test_change_position_in_club(self):
|
def test_change_position_in_club(self):
|
||||||
"""Test that when moving from board to members, club group change"""
|
"""Test that when moving from board to members, club group change"""
|
||||||
membership = baker.make(
|
membership = baker.make(
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2026-09-01 18:25+0200\n"
|
"POT-Creation-Date: 2026-08-21 14:10+0200\n"
|
||||||
"PO-Revision-Date: 2016-07-18\n"
|
"PO-Revision-Date: 2016-07-18\n"
|
||||||
"Last-Translator: Maréchal <thomas.girod@utbm.fr\n"
|
"Last-Translator: Maréchal <thomas.girod@utbm.fr\n"
|
||||||
"Language-Team: AE info <ae.info@utbm.fr>\n"
|
"Language-Team: AE info <ae.info@utbm.fr>\n"
|
||||||
@@ -260,18 +260,6 @@ msgstr ""
|
|||||||
"Si ce rôle est inactif, il ne pourra pas être attribué aux gens qui "
|
"Si ce rôle est inactif, il ne pourra pas être attribué aux gens qui "
|
||||||
"rejoignent le club."
|
"rejoignent le club."
|
||||||
|
|
||||||
#: club/models.py
|
|
||||||
msgid "Linked groups"
|
|
||||||
msgstr "Groupes liés"
|
|
||||||
|
|
||||||
#: club/models.py
|
|
||||||
msgid ""
|
|
||||||
"Groups that are automatically given or removed to user receiving or losing "
|
|
||||||
"this club role"
|
|
||||||
msgstr ""
|
|
||||||
"Les groupes qui sont automatiquement donnés ou retirés quand l'utilisateur "
|
|
||||||
"reçoit ou perd ce rôle de club"
|
|
||||||
|
|
||||||
#: club/models.py election/models.py
|
#: club/models.py election/models.py
|
||||||
msgid "club role"
|
msgid "club role"
|
||||||
msgstr "rôle de club"
|
msgstr "rôle de club"
|
||||||
@@ -497,14 +485,6 @@ msgstr "Du"
|
|||||||
msgid "To"
|
msgid "To"
|
||||||
msgstr "Au"
|
msgstr "Au"
|
||||||
|
|
||||||
#: club/templates/club/club_roles.jinja
|
|
||||||
msgid "Linked groups : "
|
|
||||||
msgstr "Groupes liés : "
|
|
||||||
|
|
||||||
#: club/templates/club/club_roles.jinja
|
|
||||||
msgid "Users receiving this role will also be assigned to those groups"
|
|
||||||
msgstr "Les utilisateurs recevant ce rôle seront aussi assignés à ces groupes"
|
|
||||||
|
|
||||||
#: club/templates/club/club_roles.jinja
|
#: club/templates/club/club_roles.jinja
|
||||||
msgid ""
|
msgid ""
|
||||||
"Roles give rights on the club. Higher roles grant more rights, and the "
|
"Roles give rights on the club. Higher roles grant more rights, and the "
|
||||||
@@ -5109,10 +5089,6 @@ msgstr "signalant"
|
|||||||
msgid "A guide of courses available at UTBM."
|
msgid "A guide of courses available at UTBM."
|
||||||
msgstr "Un guide de tous les cours disponibles à l'UTBM."
|
msgstr "Un guide de tous les cours disponibles à l'UTBM."
|
||||||
|
|
||||||
#: pedagogy/templates/pedagogy/guide.jinja
|
|
||||||
msgid "Search UE"
|
|
||||||
msgstr "Recherche d'UE"
|
|
||||||
|
|
||||||
#: pedagogy/templates/pedagogy/guide.jinja
|
#: pedagogy/templates/pedagogy/guide.jinja
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "%(display_name)s"
|
msgid "%(display_name)s"
|
||||||
@@ -5782,18 +5758,6 @@ msgstr "fin de la cotisation"
|
|||||||
msgid "location"
|
msgid "location"
|
||||||
msgstr "lieu"
|
msgstr "lieu"
|
||||||
|
|
||||||
#: subscription/models.py
|
|
||||||
msgid "created_at"
|
|
||||||
msgstr "créé le"
|
|
||||||
|
|
||||||
#: subscription/models.py
|
|
||||||
msgid ""
|
|
||||||
"When this subscription was created. This date may differ from the start of "
|
|
||||||
"the subscription."
|
|
||||||
msgstr ""
|
|
||||||
"Quand la cotisation a été créée. Cette date peut différer du début effectif de "
|
|
||||||
"la cotisation."
|
|
||||||
|
|
||||||
#: subscription/models.py
|
#: subscription/models.py
|
||||||
msgid "You can not subscribe many time for the same period"
|
msgid "You can not subscribe many time for the same period"
|
||||||
msgstr "Vous ne pouvez pas cotiser plusieurs fois pour la même période"
|
msgstr "Vous ne pouvez pas cotiser plusieurs fois pour la même période"
|
||||||
|
|||||||
@@ -153,6 +153,7 @@ class UeFilterSchema(FilterSchema):
|
|||||||
set[Literal["CS", "TM", "EC", "OM", "QC"]] | None,
|
set[Literal["CS", "TM", "EC", "OM", "QC"]] | None,
|
||||||
FilterLookup("credit_type__in"),
|
FilterLookup("credit_type__in"),
|
||||||
] = None
|
] = None
|
||||||
|
is_open: bool | None = None
|
||||||
language: str = "FR"
|
language: str = "FR"
|
||||||
department: Annotated[set[str] | None, FilterLookup("department__in")] = None
|
department: Annotated[set[str] | None, FilterLookup("department__in")] = None
|
||||||
|
|
||||||
@@ -187,3 +188,10 @@ class UeFilterSchema(FilterSchema):
|
|||||||
return Q()
|
return Q()
|
||||||
value.add("AUTUMN_AND_SPRING")
|
value.add("AUTUMN_AND_SPRING")
|
||||||
return Q(semester__in=value)
|
return Q(semester__in=value)
|
||||||
|
|
||||||
|
def filter_is_open(self, value: bool | None) -> Q: # noqa: FBT001
|
||||||
|
if value is None:
|
||||||
|
return Q()
|
||||||
|
if not value:
|
||||||
|
return Q(semester="CLOSED")
|
||||||
|
return ~Q(semester="CLOSED")
|
||||||
|
|||||||
@@ -1,118 +0,0 @@
|
|||||||
import {
|
|
||||||
getCurrentUrlParams,
|
|
||||||
History,
|
|
||||||
updateQueryString,
|
|
||||||
} from "#core:utils/history.ts";
|
|
||||||
import { ueFetchUeList } from "#openapi";
|
|
||||||
|
|
||||||
const pageDefault = 1;
|
|
||||||
const pageSizeDefault = 100;
|
|
||||||
|
|
||||||
document.addEventListener("alpine:init", () => {
|
|
||||||
Alpine.data("ue_search", () => ({
|
|
||||||
ues: {
|
|
||||||
count: 0,
|
|
||||||
next: null,
|
|
||||||
previous: null,
|
|
||||||
results: [],
|
|
||||||
},
|
|
||||||
loading: false,
|
|
||||||
page: pageDefault,
|
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
|
||||||
page_size: pageSizeDefault,
|
|
||||||
search: "",
|
|
||||||
department: [],
|
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
|
||||||
credit_type: [],
|
|
||||||
semester: [],
|
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
|
||||||
to_change: [],
|
|
||||||
pushstate: History.Push,
|
|
||||||
|
|
||||||
update: undefined,
|
|
||||||
|
|
||||||
initializeArgs() {
|
|
||||||
const url = getCurrentUrlParams();
|
|
||||||
this.pushstate = History.Replace;
|
|
||||||
|
|
||||||
this.page = Number.parseInt(url.get("page"), 10) || pageDefault;
|
|
||||||
this.page_size = Number.parseInt(url.get("page_size"), 10) || pageSizeDefault;
|
|
||||||
this.search = url.get("search") || "";
|
|
||||||
this.department = url.getAll("department");
|
|
||||||
this.credit_type = url.getAll("credit_type");
|
|
||||||
/* The semester is easier to use on the backend as an enum (spring/autumn/both/none)
|
|
||||||
and easier to use on the frontend as an array ([spring, autumn]).
|
|
||||||
Thus there is some conversion involved when both communicate together */
|
|
||||||
this.semester = url.has("semester") ? url.get("semester").split("_AND_") : [];
|
|
||||||
|
|
||||||
this.update();
|
|
||||||
},
|
|
||||||
|
|
||||||
async init() {
|
|
||||||
this.update = Alpine.debounce(async () => {
|
|
||||||
/* Create the whole url before changing everything all at once */
|
|
||||||
const first = this.to_change.shift();
|
|
||||||
let url = updateQueryString(first.param, first.value, History.None);
|
|
||||||
for (const value of this.to_change) {
|
|
||||||
url = updateQueryString(value.param, value.value, History.None, url);
|
|
||||||
}
|
|
||||||
updateQueryString(first.param, first.value, this.pushstate, url);
|
|
||||||
await this.fetchData(); /* reload data on form change */
|
|
||||||
this.to_change = [];
|
|
||||||
this.pushstate = History.Push;
|
|
||||||
}, 50);
|
|
||||||
|
|
||||||
const searchParams = ["search", "department", "credit_type", "semester"];
|
|
||||||
const paginationParams = ["page", "page_size"];
|
|
||||||
|
|
||||||
for (const param of searchParams) {
|
|
||||||
this.$watch(param, () => {
|
|
||||||
if (this.pushstate !== History.Push) {
|
|
||||||
/* This means that we are doing a mass param edit */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/* Reset pagination on search */
|
|
||||||
this.page = pageDefault;
|
|
||||||
this.page_size = pageSizeDefault;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
for (const param of searchParams.concat(paginationParams)) {
|
|
||||||
this.$watch(param, (value) => {
|
|
||||||
this.to_change.push({ param: param, value: value });
|
|
||||||
this.update();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
window.addEventListener("popstate", () => {
|
|
||||||
this.initializeArgs();
|
|
||||||
});
|
|
||||||
this.initializeArgs();
|
|
||||||
},
|
|
||||||
|
|
||||||
async fetchData() {
|
|
||||||
this.loading = true;
|
|
||||||
const args = {
|
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
|
||||||
page_size: this.page_size,
|
|
||||||
};
|
|
||||||
for (const [param, value] of new URL(
|
|
||||||
window.location.href,
|
|
||||||
).searchParams.entries()) {
|
|
||||||
// Deal with array type params
|
|
||||||
if (["credit_type", "department", "semester"].includes(param)) {
|
|
||||||
if (args[param] === undefined) {
|
|
||||||
args[param] = [];
|
|
||||||
}
|
|
||||||
args[param].push(value);
|
|
||||||
} else {
|
|
||||||
args[param] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.ues = (await ueFetchUeList({ query: args })).data;
|
|
||||||
this.loading = false;
|
|
||||||
},
|
|
||||||
|
|
||||||
maxPage() {
|
|
||||||
return Math.ceil(this.ues.count / this.page_size);
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
import { getCurrentUrlParams, updateQueryString } from "#core:utils/history";
|
||||||
|
import { type SimpleUeSchema, ueFetchUeList } from "#openapi";
|
||||||
|
|
||||||
|
const pageDefault = 1;
|
||||||
|
const pageSizeDefault = 100;
|
||||||
|
|
||||||
|
document.addEventListener("alpine:init", () => {
|
||||||
|
Alpine.data("ue_search", () => ({
|
||||||
|
ues: {
|
||||||
|
count: 0,
|
||||||
|
next: null as string | null,
|
||||||
|
previous: null as string | null,
|
||||||
|
results: [] as SimpleUeSchema[],
|
||||||
|
},
|
||||||
|
loading: false,
|
||||||
|
page: pageDefault,
|
||||||
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
|
page_size: pageSizeDefault,
|
||||||
|
search: "",
|
||||||
|
hideClosedUes: true,
|
||||||
|
department: [] as string[],
|
||||||
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
|
credit_type: [] as string[],
|
||||||
|
semester: [] as string[],
|
||||||
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
|
to_change: [] as { param: string; value: string }[],
|
||||||
|
|
||||||
|
// dummy implementation to make TS happy.
|
||||||
|
// The real function is initialized in init
|
||||||
|
update: () => {
|
||||||
|
console.warn("Update not yet initialized");
|
||||||
|
},
|
||||||
|
|
||||||
|
initializeArgs() {
|
||||||
|
const url = getCurrentUrlParams();
|
||||||
|
this.page = Number.parseInt(url.get("page") || pageDefault.toString(), 10);
|
||||||
|
this.page_size = Number.parseInt(
|
||||||
|
url.get("page_size") || pageSizeDefault.toString(),
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
this.search = url.get("search") || "";
|
||||||
|
this.hideClosedUes = url.get("hideClosed") || true;
|
||||||
|
this.department = url.getAll("department");
|
||||||
|
this.credit_type = url.getAll("credit_type");
|
||||||
|
/* The semester is easier to use on the backend as an enum (spring/autumn/both/none)
|
||||||
|
and easier to use on the frontend as an array ([spring, autumn]).
|
||||||
|
Thus there is some conversion involved when both communicate together */
|
||||||
|
this.semester = url.get("semester")?.split("_AND_") || [];
|
||||||
|
|
||||||
|
this.update();
|
||||||
|
},
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
this.update = Alpine.debounce(async () => {
|
||||||
|
/* Create the whole url before changing everything all at once */
|
||||||
|
for (const val of this.to_change) {
|
||||||
|
updateQueryString(val.param, val.value);
|
||||||
|
}
|
||||||
|
await this.fetchData(); /* reload data on form change */
|
||||||
|
this.to_change = [];
|
||||||
|
}, 50);
|
||||||
|
|
||||||
|
const searchParams = [
|
||||||
|
"search",
|
||||||
|
"hideClosedUes",
|
||||||
|
"department",
|
||||||
|
"credit_type",
|
||||||
|
"semester",
|
||||||
|
];
|
||||||
|
const paginationParams = ["page", "page_size"];
|
||||||
|
|
||||||
|
for (const param of searchParams) {
|
||||||
|
this.$watch(param, () => {
|
||||||
|
/* Reset pagination on search */
|
||||||
|
this.page = pageDefault;
|
||||||
|
this.page_size = pageSizeDefault;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (const param of searchParams.concat(paginationParams)) {
|
||||||
|
this.$watch(param, (value: string) => {
|
||||||
|
this.to_change.push({ param: param, value: value });
|
||||||
|
this.update();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.initializeArgs();
|
||||||
|
},
|
||||||
|
|
||||||
|
async fetchData() {
|
||||||
|
this.loading = true;
|
||||||
|
|
||||||
|
const res = await ueFetchUeList({
|
||||||
|
query: {
|
||||||
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
|
page_size: this.page_size,
|
||||||
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
|
credit_type: this.credit_type.length > 0 ? this.credit_type : undefined,
|
||||||
|
semester: this.semester.length > 0 ? this.semester : undefined,
|
||||||
|
// biome-ignore lint/style/useNamingConvention: api is snake_case
|
||||||
|
is_open: this.hideClosedUes ? true : undefined,
|
||||||
|
department: this.department.length > 0 ? this.department : undefined,
|
||||||
|
search: this.search || undefined,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (res.data !== undefined) {
|
||||||
|
this.ues = res.data;
|
||||||
|
}
|
||||||
|
this.loading = false;
|
||||||
|
},
|
||||||
|
|
||||||
|
maxPage() {
|
||||||
|
return Math.ceil(this.ues.count / this.page_size);
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
});
|
||||||
@@ -44,6 +44,10 @@
|
|||||||
x-model.debounce.500ms="search"
|
x-model.debounce.500ms="search"
|
||||||
/>
|
/>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
<fieldset>
|
||||||
|
<input type="checkbox" class="switch" x-model="hideClosedUes" id="hide-closed-ues" name="hide-closed-ues">
|
||||||
|
<label for="hide-closed-ues">{% trans %}Hide closed UEs{% endtrans %}</label>
|
||||||
|
</fieldset>
|
||||||
<div class="row gap-3x margin-bottom radio-guide">
|
<div class="row gap-3x margin-bottom radio-guide">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
{% set departments = [
|
{% set departments = [
|
||||||
|
|||||||
Reference in New Issue
Block a user