update mildred review

This commit is contained in:
Cayo Puigdefabregas 2023-11-13 18:09:37 +01:00
parent 5c08cd2c3d
commit b178f3460a
7 changed files with 156 additions and 30 deletions

View File

@ -4,10 +4,12 @@ import pandas as pd
from jsonschema import validate from jsonschema import validate
from django import forms from django import forms
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from idhub.models import ( from idhub.models import (
DID, DID,
File_datas, File_datas,
Membership,
Schemas, Schemas,
VerificableCredential, VerificableCredential,
) )
@ -125,3 +127,31 @@ class ImportForm(forms.Form):
class SchemaForm(forms.Form): class SchemaForm(forms.Form):
file_template = forms.FileField() 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

View File

@ -22,9 +22,10 @@ from django.contrib import messages
from utils.apiregiter import iota from utils.apiregiter import iota
from utils import credtools from utils import credtools
from idhub_auth.models import User from idhub_auth.models import User
from idhub_auth.forms import ProfileForm
from idhub.mixins import AdminView from idhub.mixins import AdminView
from idhub.email.views import NotifyActivateUserByEmail 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 ( from idhub.models import (
DID, DID,
Event, Event,
@ -145,11 +146,35 @@ class PeopleDeleteView(PeopleView):
return redirect('idhub:admin_people_list') return redirect('idhub:admin_people_list')
class PeopleEditView(PeopleView, UpdateView): class PeopleEditView(People, FormView):
template_name = "idhub/admin/user_edit.html" 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') 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): def form_valid(self, form):
user = form.save() user = form.save()
messages.success(self.request, _('The account is updated successfully')) 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" template_name = "idhub/admin/people_register.html"
subtitle = _('Add user') subtitle = _('Add user')
icon = 'bi bi-person' icon = 'bi bi-person'
model = User form_class = ProfileForm
fields = ('first_name', 'last_name', 'email')
success_url = reverse_lazy('idhub:admin_people_list') success_url = reverse_lazy('idhub:admin_people_list')
def get_success_url(self): def get_success_url(self):
@ -188,12 +212,12 @@ class PeopleRegisterView(NotifyActivateUserByEmail, People, CreateView):
return super().form_valid(form) return super().form_valid(form)
class PeopleMembershipRegisterView(People, CreateView): class PeopleMembershipRegisterView(People, FormView):
template_name = "idhub/admin/people_membership_register.html" template_name = "idhub/admin/people_membership_register.html"
subtitle = _('Associate a membership to the user') subtitle = _('Associate a membership to the user')
icon = 'bi bi-person' icon = 'bi bi-person'
form_class = MembershipForm
model = Membership model = Membership
fields = ('type', 'start_date', 'end_date')
success_url = reverse_lazy('idhub:admin_people_list') success_url = reverse_lazy('idhub:admin_people_list')
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -210,13 +234,20 @@ class PeopleMembershipRegisterView(People, CreateView):
form = super().get_form() form = super().get_form()
form.fields['start_date'].widget.input_type = 'date' form.fields['start_date'].widget.input_type = 'date'
form.fields['end_date'].widget.input_type = 'date' form.fields['end_date'].widget.input_type = 'date'
form.fields['start_date'].required = True
return form return form
def get_form_kwargs(self): def get_form_kwargs(self):
self.object = self.model(user=self.user) self.object = self.model(user=self.user)
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
kwargs['instance'] = self.object
return kwargs 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): def get_success_url(self):
self.success_url = reverse_lazy( self.success_url = reverse_lazy(
'idhub:admin_people_rol_new', 'idhub:admin_people_rol_new',
@ -225,27 +256,44 @@ class PeopleMembershipRegisterView(People, CreateView):
return self.success_url return self.success_url
class PeopleMembershipEditView(People, CreateView): class PeopleMembershipEditView(People, FormView):
template_name = "idhub/admin/people_membership_register.html" template_name = "idhub/admin/people_membership_register.html"
subtitle = _('People add membership') subtitle = _('Associate a membership to the user')
icon = 'bi bi-person' icon = 'bi bi-person'
form_class = MembershipForm
model = Membership model = Membership
fields = ('type', 'start_date', 'end_date')
success_url = reverse_lazy('idhub:admin_people_list') 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): def get_form(self):
form = super().get_form() form = super().get_form()
form.fields['start_date'].widget.input_type = 'date' form.fields['start_date'].widget.input_type = 'date'
form.fields['end_date'].widget.input_type = 'date' form.fields['end_date'].widget.input_type = 'date'
form.fields['start_date'].required = True
return form return form
def get_form_kwargs(self): 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 = super().get_form_kwargs()
kwargs['instance'] = self.object
# import pdb; pdb.set_trace()
return kwargs return kwargs
def form_valid(self, form):
form.save()
messages.success(self.request, _('Membership updated successfully'))
return super().form_valid(form)
class PeopleMembershipDeleteView(PeopleView): class PeopleMembershipDeleteView(PeopleView):
model = Membership model = Membership
@ -285,6 +333,13 @@ class PeopleRolRegisterView(People, CreateView):
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
return 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): def get_success_url(self):
self.success_url = reverse_lazy( self.success_url = reverse_lazy(
'idhub:admin_people_edit', 'idhub:admin_people_edit',

View File

@ -532,8 +532,8 @@ class Rol(models.Model):
class Service(models.Model): class Service(models.Model):
domain = models.CharField(max_length=250) domain = models.CharField(_("Domain"), max_length=250)
description = models.CharField(max_length=250) description = models.CharField(_("Description"), max_length=250)
rol = models.ManyToManyField( rol = models.ManyToManyField(
Rol, Rol,
) )

View File

@ -13,26 +13,22 @@
<th scope="col"><button type="button" class="btn btn-green-admin border border-dark">{% trans 'Last name' %}</button></th> <th scope="col"><button type="button" class="btn btn-green-admin border border-dark">{% trans 'Last name' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'First name' %}</button></th> <th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'First name' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">Email</button></th> <th scope="col"><button type="button" class="btn btn-grey border border-dark">Email</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Membership' %}</button></th> <th scope="col" class="text-center"><button type="button" class="btn btn-grey border border-dark">{% trans 'Membership' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th> <th scope="col" class="text-center"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th>
<th scope="col"></th> <th scope="col"></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for user in users %} {% for user in users %}
<tr> <tr>
<td>{{ user.last_name }}</td> <td>{{ user.last_name|default:'' }}</td>
<td>{{ user.first_name }}</td> <td>{{ user.first_name|default:'' }}</td>
<td>{{ user.email }}</td> <td>{{ user.email }}</td>
<td> <td class="text-center">
{% for m in user.memberships.all %} {{ user.get_memberships }}
{{ m.get_type }}
{% endfor %}
</td> </td>
<td> <td class="text-center">
{% for r in user.roles.all %} {{ user.get_roles }}
{{ r.service.get_roles }}
{% endfor %}
</td> </td>
<td><a type="button" class="btn btn-green-admin rounded-pill" href="{% url 'idhub:admin_people' user.id %}">{% trans 'View' %}</td> <td><a type="button" class="btn btn-green-admin rounded-pill" href="{% url 'idhub:admin_people' user.id %}">{% trans 'View' %}</td>
</tr> </tr>

View File

@ -24,7 +24,7 @@
First Name: First Name:
</div> </div>
<div class="col-9 text-secondary"> <div class="col-9 text-secondary">
{{ object.first_name }} {{ object.first_name|default:'' }}
</div> </div>
</div> </div>
<div class="row border-bottom mt-3"> <div class="row border-bottom mt-3">
@ -32,7 +32,7 @@
Last Name: Last Name:
</div> </div>
<div class="col-9 text-secondary"> <div class="col-9 text-secondary">
{{ object.last_name }} {{ object.last_name|default:'' }}
</div> </div>
</div> </div>
<div class="row mt-3"> <div class="row mt-3">

32
idhub_auth/forms.py Normal file
View File

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

View File

@ -72,3 +72,16 @@ class User(AbstractBaseUser):
def username(self): def username(self):
"Is the email of the user" "Is the email of the user"
return self.email 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))