From a43b522fafe29f5398271e436de0b6c965a48d7c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 16 Oct 2023 19:08:18 +0200 Subject: [PATCH] add registar of membership --- idhub/admin/forms.py | 8 +- idhub/admin/views.py | 42 +++++- idhub/migrations/0001_initial.py | 133 ++++++++++++++---- idhub/models.py | 55 ++++++-- .../admin_people_membership_register.html | 34 +++++ idhub/urls.py | 2 + idhub/views.py | 4 +- requirements.txt | 1 + 8 files changed, 237 insertions(+), 42 deletions(-) create mode 100644 idhub/templates/idhub/admin_people_membership_register.html diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 8354422..3e2a1f0 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -1,5 +1,6 @@ from django import forms from django.contrib.auth.models import User +from idhub.models import Membership class ProfileForm(forms.ModelForm): @@ -7,4 +8,9 @@ class ProfileForm(forms.ModelForm): class Meta: model = User - fields = ('first_name', 'last_name', 'email') \ No newline at end of file + fields = ('first_name', 'last_name', 'email') + + +class MembershipForm(forms.ModelForm): + MANDATORY_FIELDS = ['type'] + diff --git a/idhub/admin/views.py b/idhub/admin/views.py index a6f9807..edda398 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -7,8 +7,9 @@ from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.contrib import messages +from idhub.models import Membership from idhub.mixins import AdminView -from idhub.admin.forms import ProfileForm +from idhub.admin.forms import ProfileForm, MembershipForm class AdminDashboardView(AdminView, TemplateView): @@ -123,6 +124,45 @@ class AdminPeopleRegisterView(People, CreateView): fields = ('first_name', 'last_name', 'email', 'username') 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} + ) + return self.success_url + + +class AdminPeopleMembershipRegisterView(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(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(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): + self.object = self.model(user=self.user) + kwargs = super().get_form_kwargs() + return kwargs + class AdminRolesView(AccessControl): template_name = "idhub/admin_roles.html" diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index df5fa3c..81b98e5 100644 --- a/idhub/migrations/0001_initial.py +++ b/idhub/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-10-03 15:28 +# Generated by Django 4.2.5 on 2023-10-16 09:41 from django.conf import settings from django.db import migrations, models @@ -6,7 +6,6 @@ import django.db.models.deletion class Migration(migrations.Migration): - initial = True dependencies = [ @@ -15,42 +14,118 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='DID', + name="VCTemplate", fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('did_string', models.CharField(max_length=250)), - ], - options={ - 'abstract': False, - }, - ), - migrations.CreateModel( - name='Event', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('timestamp', models.DateTimeField()), + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("wkit_template_id", models.CharField(max_length=250)), + ("data", models.TextField()), ], ), migrations.CreateModel( - name='VerifiableCredential', + name="VerifiableCredential", fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('id_string', models.CharField(max_length=250)), - ('data', models.TextField()), - ('verified', models.BooleanField()), - ('created_on', models.DateTimeField()), - ('did_issuer', models.CharField(max_length=250)), - ('did_subject', models.CharField(max_length=250)), + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("id_string", models.CharField(max_length=250)), + ("verified", models.BooleanField()), + ("created_on", models.DateTimeField()), + ("did_issuer", models.CharField(max_length=250)), + ("did_subject", models.CharField(max_length=250)), + ("data", models.TextField()), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="vcredentials", + to=settings.AUTH_USER_MODEL, + ), + ), ], - options={ - 'abstract': False, - }, ), migrations.CreateModel( - name='AppUser', + name="Membership", fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('django_user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "type", + models.PositiveSmallIntegerField( + choices=[(1, "Beneficiary"), (2, "Employee"), (3, "Partner")], + verbose_name="Type of membership", + ), + ), + ( + "start_date", + models.DateField( + blank=True, + help_text="What date did the membership start?", + null=True, + verbose_name="Start date", + ), + ), + ( + "end_date", + models.DateField( + blank=True, + help_text="What date did the membership end?", + null=True, + verbose_name="End date", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="memberships", + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.CreateModel( + name="DID", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("did_string", models.CharField(max_length=250)), + ("label", models.CharField(max_length=50)), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="dids", + to=settings.AUTH_USER_MODEL, + ), + ), ], ), ] diff --git a/idhub/models.py b/idhub/models.py index fb053bc..0db78b7 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -1,27 +1,32 @@ from django.db import models -from django.contrib.auth.models import User as DjangoUser +from django.utils.translation import gettext_lazy as _ +from django.contrib.auth.models import User -class AppUser(models.Model): +# class AppUser(models.Model): # Ya incluye "first_name", "last_name", "email", y "date_joined" heredando de la clase User de django. # Falta ver que más información hay que añadir a nuestros usuarios, como los roles etc. - django_user = models.OneToOneField(DjangoUser, on_delete=models.CASCADE) + # django_user = models.OneToOneField(DjangoUser, on_delete=models.CASCADE) # Extra data, segun entidad/organizacion - pass + # pass -class Event(models.Model): +# class Event(models.Model): # Para los "audit logs" que se requieren en las pantallas. - timestamp = models.DateTimeField() + # timestamp = models.DateTimeField() # Los eventos no tienen relación con otros objetos a nivel de BBDD. - event_data = models.CharField(max_length=250) + # event_data = models.CharField(max_length=250) class DID(models.Model): did_string = models.CharField(max_length=250) label = models.CharField(max_length=50) - owner = models.ForeignKey(AppUser, on_delete=models.CASCADE) + user = models.ForeignKey( + User, + on_delete=models.CASCADE, + related_name='dids', + ) # kind = "KEY|WEB" @@ -31,7 +36,11 @@ class VerifiableCredential(models.Model): created_on = models.DateTimeField() did_issuer = models.CharField(max_length=250) did_subject = models.CharField(max_length=250) - owner = models.ForeignKey(AppUser, on_delete=models.CASCADE) + user = models.ForeignKey( + User, + on_delete=models.CASCADE, + related_name='vcredentials', + ) data = models.TextField() @@ -40,3 +49,31 @@ class VCTemplate(models.Model): data = models.TextField() +class Membership(models.Model): + """ + This model represent the relation of this user with the ecosystem. + """ + class Types(models.IntegerChoices): + BENEFICIARY = 1, _('Beneficiary') + EMPLOYEE = 2, _('Employee') + PARTNER = 3, _('Partner') + + type = models.PositiveSmallIntegerField(_('Type of membership'), choices=Types.choices) + start_date = models.DateField( + _('Start date'), + help_text=_('What date did the membership start?'), + blank=True, + null=True + ) + end_date = models.DateField( + _('End date'), + help_text=_('What date did the membership end?'), + blank=True, + null=True + ) + + user = models.ForeignKey( + User, + on_delete=models.CASCADE, + related_name='memberships', + ) diff --git a/idhub/templates/idhub/admin_people_membership_register.html b/idhub/templates/idhub/admin_people_membership_register.html new file mode 100644 index 0000000..ebee6f1 --- /dev/null +++ b/idhub/templates/idhub/admin_people_membership_register.html @@ -0,0 +1,34 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +
+
+ {% bootstrap_form form %} +
+
+ + +
+{% endblock %} diff --git a/idhub/urls.py b/idhub/urls.py index 326790c..0a87d1a 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -64,6 +64,8 @@ urlpatterns = [ name='admin_people_activate'), path('admin/people/new/', views_admin.AdminPeopleRegisterView.as_view(), name='admin_people_new'), + path('admin/people//membership/new/', views_admin.AdminPeopleMembershipRegisterView.as_view(), + name='admin_people_membership_new'), path('admin/roles/', views_admin.AdminRolesView.as_view(), name='admin_roles'), path('admin/services/', views_admin.AdminServicesView.as_view(), diff --git a/idhub/views.py b/idhub/views.py index 83d7ff3..8a2a1d9 100644 --- a/idhub/views.py +++ b/idhub/views.py @@ -10,7 +10,7 @@ class LoginView(auth_views.LoginView): 'success_url': reverse_lazy('idhub:user_dashboard'), } - def get(self, request): + def get(self, request, *args, **kwargs): if request.GET.get('next'): self.extra_context['success_url'] = request.GET.get('next') - return super().get(request) + return super().get(request, *args, **kwargs) diff --git a/requirements.txt b/requirements.txt index 93b7ce1..230eed1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ django==4.2.5 django-bootstrap5==23.3 django-extensions==3.2.3 +black==23.9.1