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 @@