From b178f3460a1bd3fde545c495505de2295b0f1e7b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 13 Nov 2023 18:09:37 +0100 Subject: [PATCH] update mildred review --- idhub/admin/forms.py | 32 +++++++++- idhub/admin/views.py | 81 +++++++++++++++++++++---- idhub/models.py | 4 +- idhub/templates/idhub/admin/people.html | 20 +++--- idhub/templates/idhub/admin/user.html | 4 +- idhub_auth/forms.py | 32 ++++++++++ idhub_auth/models.py | 13 ++++ 7 files changed, 156 insertions(+), 30 deletions(-) create mode 100644 idhub_auth/forms.py diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index d20d6d7..044bcde 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -4,10 +4,12 @@ import pandas as pd from jsonschema import validate from django import forms +from django.utils.translation import gettext_lazy as _ from django.core.exceptions import ValidationError from idhub.models import ( DID, - File_datas, + File_datas, + Membership, Schemas, VerificableCredential, ) @@ -125,3 +127,31 @@ class ImportForm(forms.Form): class SchemaForm(forms.Form): file_template = forms.FileField() + + +class MembershipForm(forms.ModelForm): + + class Meta: + model = Membership + fields = ['type', 'start_date', 'end_date'] + + def clean_end_date(self): + data = super().clean() + start_date = data['start_date'] + end_date = data.get('end_date') + if (start_date and end_date): + if start_date > end_date: + msg = _("The end date is less than the start date") + raise forms.ValidationError(msg) + + members = Membership.objects.filter( + type=data['type'], + start_date__lte=end_date, + end_date__gte=start_date, + user=self.instance.user + ) + if members.exists() and not self.instance.id: + msg = _("This membership already exists!") + raise forms.ValidationError(msg) + + return end_date diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 634a703..a899544 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -22,9 +22,10 @@ from django.contrib import messages from utils.apiregiter import iota from utils import credtools from idhub_auth.models import User +from idhub_auth.forms import ProfileForm from idhub.mixins import AdminView from idhub.email.views import NotifyActivateUserByEmail -from idhub.admin.forms import ImportForm, SchemaForm +from idhub.admin.forms import ImportForm, SchemaForm, MembershipForm from idhub.models import ( DID, Event, @@ -145,11 +146,35 @@ class PeopleDeleteView(PeopleView): return redirect('idhub:admin_people_list') -class PeopleEditView(PeopleView, UpdateView): +class PeopleEditView(People, FormView): template_name = "idhub/admin/user_edit.html" - fields = ('first_name', 'last_name', 'email') + subtitle = _('Update user') + icon = 'bi bi-person' + form_class = ProfileForm success_url = reverse_lazy('idhub:admin_people_list') + def get(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.user = get_object_or_404(User, pk=self.pk) + return super().get(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.user = get_object_or_404(User, pk=self.pk) + return super().post(request, *args, **kwargs) + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs['instance'] = self.user + return kwargs + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'object': self.user, + }) + return context + def form_valid(self, form): user = form.save() messages.success(self.request, _('The account is updated successfully')) @@ -163,8 +188,7 @@ class PeopleRegisterView(NotifyActivateUserByEmail, People, CreateView): template_name = "idhub/admin/people_register.html" subtitle = _('Add user') icon = 'bi bi-person' - model = User - fields = ('first_name', 'last_name', 'email') + form_class = ProfileForm success_url = reverse_lazy('idhub:admin_people_list') def get_success_url(self): @@ -188,12 +212,12 @@ class PeopleRegisterView(NotifyActivateUserByEmail, People, CreateView): return super().form_valid(form) -class PeopleMembershipRegisterView(People, CreateView): +class PeopleMembershipRegisterView(People, FormView): template_name = "idhub/admin/people_membership_register.html" subtitle = _('Associate a membership to the user') icon = 'bi bi-person' + form_class = MembershipForm model = Membership - fields = ('type', 'start_date', 'end_date') success_url = reverse_lazy('idhub:admin_people_list') def get(self, request, *args, **kwargs): @@ -210,12 +234,19 @@ class PeopleMembershipRegisterView(People, CreateView): form = super().get_form() form.fields['start_date'].widget.input_type = 'date' form.fields['end_date'].widget.input_type = 'date' + form.fields['start_date'].required = True return form def get_form_kwargs(self): self.object = self.model(user=self.user) kwargs = super().get_form_kwargs() + kwargs['instance'] = self.object return kwargs + + def form_valid(self, form): + form.save() + messages.success(self.request, _('Membership created successfully')) + return super().form_valid(form) def get_success_url(self): self.success_url = reverse_lazy( @@ -225,27 +256,44 @@ class PeopleMembershipRegisterView(People, CreateView): return self.success_url -class PeopleMembershipEditView(People, CreateView): +class PeopleMembershipEditView(People, FormView): template_name = "idhub/admin/people_membership_register.html" - subtitle = _('People add membership') + subtitle = _('Associate a membership to the user') icon = 'bi bi-person' + form_class = MembershipForm model = Membership - fields = ('type', 'start_date', 'end_date') success_url = reverse_lazy('idhub:admin_people_list') + def get(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.object = get_object_or_404(self.model, pk=self.pk) + self.user = self.object.user + return super().get(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.object = get_object_or_404(self.model, pk=self.pk) + self.user = self.object.user + return super().post(request, *args, **kwargs) + def get_form(self): form = super().get_form() form.fields['start_date'].widget.input_type = 'date' form.fields['end_date'].widget.input_type = 'date' + form.fields['start_date'].required = True 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() + kwargs['instance'] = self.object + # import pdb; pdb.set_trace() return kwargs + def form_valid(self, form): + form.save() + messages.success(self.request, _('Membership updated successfully')) + return super().form_valid(form) + class PeopleMembershipDeleteView(PeopleView): model = Membership @@ -285,6 +333,13 @@ class PeopleRolRegisterView(People, CreateView): kwargs = super().get_form_kwargs() return kwargs + def get_form(self): + form = super().get_form() + choices = form.fields['service'].choices + choices.queryset = choices.queryset.exclude(users__user=self.user) + form.fields['service'].choices = choices + return form + def get_success_url(self): self.success_url = reverse_lazy( 'idhub:admin_people_edit', diff --git a/idhub/models.py b/idhub/models.py index 88b098c..7a62664 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -532,8 +532,8 @@ class Rol(models.Model): class Service(models.Model): - domain = models.CharField(max_length=250) - description = models.CharField(max_length=250) + domain = models.CharField(_("Domain"), max_length=250) + description = models.CharField(_("Description"), max_length=250) rol = models.ManyToManyField( Rol, ) diff --git a/idhub/templates/idhub/admin/people.html b/idhub/templates/idhub/admin/people.html index 2df8577..a165bd3 100644 --- a/idhub/templates/idhub/admin/people.html +++ b/idhub/templates/idhub/admin/people.html @@ -13,26 +13,22 @@ - - + + {% for user in users %} - {{ user.last_name }} - {{ user.first_name }} + {{ user.last_name|default:'' }} + {{ user.first_name|default:'' }} {{ user.email }} - - {% for m in user.memberships.all %} - {{ m.get_type }} - {% endfor %} + + {{ user.get_memberships }} - - {% for r in user.roles.all %} - {{ r.service.get_roles }} - {% endfor %} + + {{ user.get_roles }} {% trans 'View' %} diff --git a/idhub/templates/idhub/admin/user.html b/idhub/templates/idhub/admin/user.html index bad25c7..71b5828 100644 --- a/idhub/templates/idhub/admin/user.html +++ b/idhub/templates/idhub/admin/user.html @@ -24,7 +24,7 @@ First Name:
- {{ object.first_name }} + {{ object.first_name|default:'' }}
@@ -32,7 +32,7 @@ Last Name:
- {{ object.last_name }} + {{ object.last_name|default:'' }}
diff --git a/idhub_auth/forms.py b/idhub_auth/forms.py new file mode 100644 index 0000000..1bc1881 --- /dev/null +++ b/idhub_auth/forms.py @@ -0,0 +1,32 @@ +import re + +from django import forms +from django.utils.translation import gettext_lazy as _ +from idhub_auth.models import User + + +class ProfileForm(forms.ModelForm): + first_name = forms.CharField(required=True) + last_name = forms.CharField(required=True) + + class Meta: + model = User + fields = ['first_name', 'last_name', 'email'] + + def clean_first_name(self): + first_name = super().clean()['first_name'] + if not re.match(r'^[a-zA-Z\s]+$', first_name): + txt = _("The string must contain only characters and spaces") + raise forms.ValidationError(txt) + + return first_name + + def clean_last_name(self): + last_name = super().clean()['last_name'] + if not re.match(r'^[a-zA-Z\s]+$', last_name): + txt = _("The string must contain only characters and spaces") + raise forms.ValidationError(txt) + + return last_name + + diff --git a/idhub_auth/models.py b/idhub_auth/models.py index 64f5491..ccda94c 100644 --- a/idhub_auth/models.py +++ b/idhub_auth/models.py @@ -72,3 +72,16 @@ class User(AbstractBaseUser): def username(self): "Is the email of the user" return self.email + + def get_memberships(self): + members = set( + str(dict(x.Types.choices)[x.type]) for x in self.memberships.all() + ) + return ", ".join(members) + + def get_roles(self): + roles = [] + for s in self.roles.all(): + for r in s.service.rol.all(): + roles.append(r.name) + return ", ".join(set(roles))