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

View File

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

View File

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

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-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">{% 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 'Membership' %}</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>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.last_name }}</td>
<td>{{ user.first_name }}</td>
<td>{{ user.last_name|default:'' }}</td>
<td>{{ user.first_name|default:'' }}</td>
<td>{{ user.email }}</td>
<td>
{% for m in user.memberships.all %}
{{ m.get_type }}
{% endfor %}
<td class="text-center">
{{ user.get_memberships }}
</td>
<td>
{% for r in user.roles.all %}
{{ r.service.get_roles }}
{% endfor %}
<td class="text-center">
{{ user.get_roles }}
</td>
<td><a type="button" class="btn btn-green-admin rounded-pill" href="{% url 'idhub:admin_people' user.id %}">{% trans 'View' %}</td>
</tr>

View File

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