feat(dgsi/profile): Switch to htmx for generating/reseting the WiFi password
This commit is contained in:
parent
e278b44d0c
commit
45321b98df
2 changed files with 17 additions and 22 deletions
|
@ -4,10 +4,9 @@
|
|||
<h3 class="has-text-weight-bold mb-3">
|
||||
<span>{% trans "Mot de passe WiFi :" %}</span>
|
||||
{% if user.kanidm.radius_secret %}
|
||||
{% trans "Êtes-vous sûr·e de vouloir réinitialiser votre mot de passe WiFi ?" as confirm_wifi_reset %}
|
||||
<a href="{% url "dgsi:dgn-generate_wifi_password" %}"
|
||||
class="button is-small is-danger is-pulled-right"
|
||||
onclick="return confirm('{{ confirm_wifi_reset }}')">
|
||||
<a class="button is-small is-danger is-pulled-right"
|
||||
hx-post="{% url "dgsi:dgn-generate_wifi_password" %}"
|
||||
hx-confirm="{% trans "Êtes-vous sûr·e de vouloir réinitialiser votre mot de passe WiFi ?" %}">
|
||||
<span class="icon"><i class="ti ti-refresh"></i></span>
|
||||
<span class="has-text-weight-normal">{% trans "Réinitialiser le mot de passe WiFi" %}</span>
|
||||
</a>
|
||||
|
@ -25,7 +24,7 @@
|
|||
<a id="secret-toggle" class="button is-size-4 is-warning is-light"><span class="icon"><i class="ti ti-eye"></i></span></a>
|
||||
</div>
|
||||
{% else %}
|
||||
<a href="{% url "dgsi:dgn-generate_wifi_password" %}"
|
||||
<a hx-post="{% url "dgsi:dgn-generate_wifi_password" %}"
|
||||
class="button is-fullwidth is-primary is-light is-size-4 block">{% trans "Générer un mot de passe WiFi" %}</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
|
|
@ -23,7 +23,7 @@ from dgsi.mixins import (
|
|||
StaffRequiredMixin,
|
||||
)
|
||||
from dgsi.models import Archive, Bylaws, Service, Statutes, User
|
||||
from shared.kanidm import klient
|
||||
from shared.kanidm import klient, sync_call
|
||||
|
||||
|
||||
class Link(NamedTuple):
|
||||
|
@ -90,27 +90,23 @@ class AppleProfileView(KanidmAccountRequiredMixin, TemplateView):
|
|||
return super().render_to_response(context, headers=headers, **response_kwargs)
|
||||
|
||||
|
||||
class GenerateWiFiPasswordView(LoginRequiredMixin, RedirectView):
|
||||
class GenerateWiFiPasswordView(KanidmAccountRequiredMixin, View):
|
||||
url = reverse_lazy("dgsi:dgn-profile")
|
||||
http_method_names = ["post"]
|
||||
|
||||
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponseBase:
|
||||
user = User.from_request(self.request)
|
||||
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponseBase:
|
||||
assert self._user.kanidm is not None
|
||||
|
||||
if user.kanidm is None:
|
||||
messages.error(self.request, _("Compte DGNum inexistant."))
|
||||
# Give access to the wifi network when the user creates its first password
|
||||
if not self._user.kanidm.radius_secret:
|
||||
message = _("Mot de passe Wi-Fi généré avec succès.")
|
||||
sync_call("group_add_members", "radius_access", [self._user.username])
|
||||
else:
|
||||
# Give access to the wifi network when the user creates its first password
|
||||
if not user.kanidm.radius_secret:
|
||||
message = _("Mot de passe Wi-Fi généré avec succès.")
|
||||
async_to_sync(klient.group_add_members)(
|
||||
"radius_access", [user.username]
|
||||
)
|
||||
else:
|
||||
message = _("Mot de passe Wi-Fi reinitialisé avec succès.")
|
||||
async_to_sync(klient.call_post)(f"/v1/person/{user.username}/_radius")
|
||||
messages.add_message(request, messages.SUCCESS, message)
|
||||
message = _("Mot de passe Wi-Fi reinitialisé avec succès.")
|
||||
sync_call("call_post", f"/v1/person/{self._user.username}/_radius")
|
||||
messages.add_message(request, messages.SUCCESS, message)
|
||||
|
||||
return super().get(request, *args, **kwargs)
|
||||
return HttpResponse(*args, headers={"HX-Redirect": self.url}, **kwargs)
|
||||
|
||||
|
||||
# INFO: We subclass AccessMixin and not LoginRequiredMixin because the way we want to
|
||||
|
|
Loading…
Add table
Reference in a new issue