add registar of membership

This commit is contained in:
Cayo Puigdefabregas 2023-10-16 19:08:18 +02:00
parent e3ac939afc
commit a43b522faf
8 changed files with 237 additions and 42 deletions

View File

@ -1,5 +1,6 @@
from django import forms
from django.contrib.auth.models import User
from idhub.models import Membership
class ProfileForm(forms.ModelForm):
@ -8,3 +9,8 @@ class ProfileForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
class MembershipForm(forms.ModelForm):
MANDATORY_FIELDS = ['type']

View File

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

View File

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

View File

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

View File

@ -0,0 +1,34 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
{% load django_bootstrap5 %}
<form role="form" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-sm-4">
{% bootstrap_form form %}
</div>
</div>
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'idhub:admin_people_list' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View File

@ -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/<int:pk>/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(),

View File

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

View File

@ -1,3 +1,4 @@
django==4.2.5
django-bootstrap5==23.3
django-extensions==3.2.3
black==23.9.1