From 021410e31629001a71fe5dbfcbe456900d2f249d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 17 Oct 2023 10:53:03 +0200 Subject: [PATCH] edit and delete memberships --- idhub/admin/views.py | 49 +++++++++++++++++++++- idhub/models.py | 3 ++ idhub/templates/idhub/admin_user.html | 25 +++++++++++ idhub/templates/idhub/admin_user_edit.html | 32 ++++++++++++++ idhub/urls.py | 4 ++ 5 files changed, 111 insertions(+), 2 deletions(-) diff --git a/idhub/admin/views.py b/idhub/admin/views.py index edda398..05cc8b3 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -125,7 +125,6 @@ class AdminPeopleRegisterView(People, CreateView): success_url = reverse_lazy('idhub:admin_people_list') def get_success_url(self): - # import pdb; pdb.set_trace() self.success_url = reverse_lazy( 'idhub:admin_people_membership_new', kwargs={"pk": self.object.id} @@ -162,7 +161,53 @@ class AdminPeopleMembershipRegisterView(People, CreateView): self.object = self.model(user=self.user) kwargs = super().get_form_kwargs() return kwargs - + + def get_success_url(self): + self.success_url = reverse_lazy( + 'idhub:admin_people_edit', + kwargs={"pk": self.user.id} + ) + return self.success_url + + +class AdminPeopleMembershipEditView(People, CreateView): + template_name = "idhub/admin_people_membership_register.html" + subtitle = _('People add membership') + icon = 'bi bi-person' + model = Membership + from_class = MembershipForm + fields = ('type', 'start_date', 'end_date') + success_url = reverse_lazy('idhub:admin_people_list') + + def get_form(self): + form = super().get_form() + form.fields['start_date'].widget.input_type = 'date' + form.fields['end_date'].widget.input_type = 'date' + return form + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') + if pk: + self.object = get_object_or_404(self.model, pk=pk) + kwargs = super().get_form_kwargs() + return kwargs + + +class AdminPeopleMembershipDeleteView(AdminPeopleView): + model = Membership + + def get(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.object = get_object_or_404(self.model, pk=self.pk) + + if self.object != self.request.user: + user = self.object.user + self.object.delete() + else: + messages.error(self.request, _('Is not possible delete your account!')) + + return redirect('idhub:admin_people_edit', user.id) + class AdminRolesView(AccessControl): template_name = "idhub/admin_roles.html" diff --git a/idhub/models.py b/idhub/models.py index 0db78b7..ba5d953 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -77,3 +77,6 @@ class Membership(models.Model): on_delete=models.CASCADE, related_name='memberships', ) + + def get_type(self): + return dict(self.Types.choices).get(self.type) diff --git a/idhub/templates/idhub/admin_user.html b/idhub/templates/idhub/admin_user.html index 92ee4e1..7706a92 100644 --- a/idhub/templates/idhub/admin_user.html +++ b/idhub/templates/idhub/admin_user.html @@ -47,6 +47,31 @@ +
+
+
+ + + + + + + + + + {% for membership in object.memberships.all %} + + + + + + {% endfor %} + +
{{ membership.get_type }}{{ membership.start_date|default:'' }}{{ membership.end_date|default:'' }}
+
+
+
+