add User model custumized

This commit is contained in:
Cayo Puigdefabregas 2023-10-25 17:49:17 +02:00
parent ae48e75133
commit 4eb02e9218
22 changed files with 287 additions and 249 deletions

View File

@ -1,5 +0,0 @@
from django.contrib import admin
from .models import AppUser
admin.site.register(AppUser)

View File

@ -1,10 +1,10 @@
from django import forms from django import forms
from django.contrib.auth.models import User from idhub_auth.models import User
from idhub.models import Rol from idhub.models import Rol
class ProfileForm(forms.ModelForm): class ProfileForm(forms.ModelForm):
MANDATORY_FIELDS = ['first_name', 'last_name', 'email', 'username'] MANDATORY_FIELDS = ['first_name', 'last_name', 'email']
class Meta: class Meta:
model = User model = User

View File

@ -11,11 +11,11 @@ from django.conf import settings
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.views.generic.edit import UpdateView, CreateView from django.views.generic.edit import UpdateView, CreateView
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib import messages from django.contrib import messages
from idhub_auth.models import User
from idhub.mixins import AdminView from idhub.mixins import AdminView
from idhub.email.views import NotifyActivateUserByEmail from idhub.email.views import NotifyActivateUserByEmail
from idhub.models import ( from idhub.models import (
@ -137,7 +137,7 @@ class AdminPeopleDeleteView(AdminPeopleView):
class AdminPeopleEditView(AdminPeopleView, UpdateView): class AdminPeopleEditView(AdminPeopleView, UpdateView):
template_name = "idhub/admin/user_edit.html" template_name = "idhub/admin/user_edit.html"
from_class = ProfileForm from_class = ProfileForm
fields = ('first_name', 'last_name', 'email', 'username') fields = ('first_name', 'last_name', 'email')
success_url = reverse_lazy('idhub:admin_people_list') success_url = reverse_lazy('idhub:admin_people_list')
@ -147,7 +147,7 @@ class AdminPeopleRegisterView(NotifyActivateUserByEmail, People, CreateView):
icon = 'bi bi-person' icon = 'bi bi-person'
model = User model = User
from_class = ProfileForm from_class = ProfileForm
fields = ('first_name', 'last_name', 'email', 'username') 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):
@ -646,7 +646,7 @@ class AdminImportStep3View(ImportExport):
return return
file_name = f.name file_name = f.name
if File_datas.objects.filter(file_name=file_name).exists(): if File_datas.objects.filter(file_name=file_name, success=True).exists():
messages.error(self.request, _("This file already exists!")) messages.error(self.request, _("This file already exists!"))
return return

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-10-16 09:41 # Generated by Django 4.2.5 on 2023-10-25 15:47
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -14,115 +14,211 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name="VCTemplate", name='File_datas',
fields=[ fields=[
( (
"id", 'id',
models.BigAutoField( models.BigAutoField(
auto_created=True, auto_created=True,
primary_key=True, primary_key=True,
serialize=False, serialize=False,
verbose_name="ID", verbose_name='ID',
), ),
), ),
("wkit_template_id", models.CharField(max_length=250)), ('file_name', models.CharField(max_length=250)),
("data", models.TextField()), ('success', models.BooleanField(default=True)),
('created_at', models.DateTimeField(auto_now=True)),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name="VerifiableCredential", name='Rol',
fields=[ fields=[
( (
"id", 'id',
models.BigAutoField( models.BigAutoField(
auto_created=True, auto_created=True,
primary_key=True, primary_key=True,
serialize=False, serialize=False,
verbose_name="ID", verbose_name='ID',
), ),
), ),
("id_string", models.CharField(max_length=250)), ('name', models.CharField(max_length=250)),
("verified", models.BooleanField()), ],
("created_on", models.DateTimeField(auto_now=True)), ),
("did_issuer", models.CharField(max_length=250)), migrations.CreateModel(
("did_subject", models.CharField(max_length=250)), name='Schemas',
("data", models.TextField()), fields=[
( (
"user", 'id',
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID',
),
),
('file_schema', models.CharField(max_length=250)),
('data', models.TextField()),
('created_at', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='Service',
fields=[
(
'id',
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID',
),
),
('domain', models.CharField(max_length=250)),
('description', models.CharField(max_length=250)),
('rol', models.ManyToManyField(to='idhub.rol')),
],
),
migrations.CreateModel(
name='VCTemplate',
fields=[
(
'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',
fields=[
(
'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(auto_now=True)),
('did_issuer', models.CharField(max_length=250)),
('did_subject', models.CharField(max_length=250)),
('data', models.TextField()),
(
'user',
models.ForeignKey( models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, on_delete=django.db.models.deletion.CASCADE,
related_name="vcredentials", related_name='vcredentials',
to=settings.AUTH_USER_MODEL, to=settings.AUTH_USER_MODEL,
), ),
), ),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name="Membership", name='UserRol',
fields=[ fields=[
( (
"id", 'id',
models.BigAutoField( models.BigAutoField(
auto_created=True, auto_created=True,
primary_key=True, primary_key=True,
serialize=False, serialize=False,
verbose_name="ID", verbose_name='ID',
), ),
), ),
( (
"type", 'service',
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='users',
to='idhub.service',
),
),
(
'user',
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='roles',
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.CreateModel(
name='Membership',
fields=[
(
'id',
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID',
),
),
(
'type',
models.PositiveSmallIntegerField( models.PositiveSmallIntegerField(
choices=[(1, "Beneficiary"), (2, "Employee"), (3, "Partner")], choices=[(1, 'Beneficiary'), (2, 'Employee'), (3, 'Partner')],
verbose_name="Type of membership", verbose_name='Type of membership',
), ),
), ),
( (
"start_date", 'start_date',
models.DateField( models.DateField(
blank=True, blank=True,
help_text="What date did the membership start?", help_text='What date did the membership start?',
null=True, null=True,
verbose_name="Start date", verbose_name='Start date',
), ),
), ),
( (
"end_date", 'end_date',
models.DateField( models.DateField(
blank=True, blank=True,
help_text="What date did the membership end?", help_text='What date did the membership end?',
null=True, null=True,
verbose_name="End date", verbose_name='End date',
), ),
), ),
( (
"user", 'user',
models.ForeignKey( models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, on_delete=django.db.models.deletion.CASCADE,
related_name="memberships", related_name='memberships',
to=settings.AUTH_USER_MODEL, to=settings.AUTH_USER_MODEL,
), ),
), ),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name="DID", name='DID',
fields=[ fields=[
( (
"id", 'id',
models.BigAutoField( models.BigAutoField(
auto_created=True, auto_created=True,
primary_key=True, primary_key=True,
serialize=False, serialize=False,
verbose_name="ID", verbose_name='ID',
), ),
), ),
("did_string", models.CharField(max_length=250)), ('did_string', models.CharField(max_length=250)),
("label", models.CharField(max_length=50)), ('label', models.CharField(max_length=50)),
( (
"user", 'user',
models.ForeignKey( models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, on_delete=django.db.models.deletion.CASCADE,
related_name="dids", related_name='dids',
to=settings.AUTH_USER_MODEL, to=settings.AUTH_USER_MODEL,
), ),
), ),

View File

@ -1,27 +0,0 @@
# Generated by Django 4.2.5 on 2023-10-17 11:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("idhub", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="Rol",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=250)),
],
),
]

View File

@ -1,37 +0,0 @@
# Generated by Django 4.2.5 on 2023-10-17 13:29
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
("idhub", "0002_rol"),
]
operations = [
migrations.CreateModel(
name="Service",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("domain", models.CharField(max_length=250)),
("description", models.CharField(max_length=250)),
(
"rol",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="services",
to="idhub.rol",
),
),
],
),
]

View File

@ -1,45 +0,0 @@
# Generated by Django 4.2.5 on 2023-10-17 14:24
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("idhub", "0003_service"),
]
operations = [
migrations.CreateModel(
name="UserRol",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"service",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="users",
to="idhub.service",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="roles",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]

View File

@ -1,21 +0,0 @@
# Generated by Django 4.2.5 on 2023-10-19 13:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("idhub", "0004_userrol"),
]
operations = [
migrations.RemoveField(
model_name="service",
name="rol",
),
migrations.AddField(
model_name="service",
name="rol",
field=models.ManyToManyField(to="idhub.rol"),
),
]

View File

@ -1,29 +0,0 @@
# Generated by Django 4.2.5 on 2023-10-20 13:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("idhub", "0005_remove_service_rol_service_rol"),
]
operations = [
migrations.CreateModel(
name="Schemas",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("file_schema", models.CharField(max_length=250)),
("data", models.TextField()),
("created_at", models.DateTimeField(auto_now=True)),
],
),
]

View File

@ -1,29 +0,0 @@
# Generated by Django 4.2.5 on 2023-10-25 09:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('idhub', '0006_schemas'),
]
operations = [
migrations.CreateModel(
name='File_datas',
fields=[
(
'id',
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID',
),
),
('file_name', models.CharField(max_length=250)),
('success', models.BooleanField(default=True)),
('created_at', models.DateTimeField(auto_now=True)),
],
),
]

View File

@ -26,7 +26,7 @@ class UserView(LoginRequiredMixin):
class AdminView(UserView): class AdminView(UserView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
if not request.user.is_superuser: if not request.user.is_admin:
url = reverse_lazy('idhub:user_dashboard') url = reverse_lazy('idhub:user_dashboard')
return redirect(url) return redirect(url)

View File

@ -1,6 +1,6 @@
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User from idhub_auth.models import User
# class AppUser(models.Model): # class AppUser(models.Model):
@ -30,6 +30,12 @@ class DID(models.Model):
# kind = "KEY|WEB" # kind = "KEY|WEB"
class Schemas(models.Model):
file_schema = models.CharField(max_length=250)
data = models.TextField()
created_at = models.DateTimeField(auto_now=True)
class VerifiableCredential(models.Model): class VerifiableCredential(models.Model):
id_string = models.CharField(max_length=250) id_string = models.CharField(max_length=250)
verified = models.BooleanField() verified = models.BooleanField()
@ -49,12 +55,6 @@ class VCTemplate(models.Model):
data = models.TextField() data = models.TextField()
class Schemas(models.Model):
file_schema = models.CharField(max_length=250)
data = models.TextField()
created_at = models.DateTimeField(auto_now=True)
class File_datas(models.Model): class File_datas(models.Model):
file_name = models.CharField(max_length=250) file_name = models.CharField(max_length=250)
success = models.BooleanField(default=True) success = models.BooleanField(default=True)

View File

@ -12,7 +12,7 @@
<tr> <tr>
<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">Username</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 'Membership' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th> <th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th>
<th scope="col"></th> <th scope="col"></th>
@ -23,7 +23,7 @@
<tr> <tr>
<td>{{ user.last_name }}</td> <td>{{ user.last_name }}</td>
<td>{{ user.first_name }}</td> <td>{{ user.first_name }}</td>
<td>{{ user.username }}</td> <td>{{ user.email }}</td>
<td> <td>
{% for m in user.memberships.all %} {% for m in user.memberships.all %}
{{ m.get_type }} {{ m.get_type }}

View File

@ -109,7 +109,7 @@
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">{% trans 'Delete user' %} {{ object.username }}</h5> <h5 class="modal-title" id="exampleModalLabel">{% trans 'Delete user' %} {{ object.email }}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div> </div>
<div class="modal-body"> <div class="modal-body">

View File

@ -1,5 +1,5 @@
from django import forms from django import forms
from django.contrib.auth.models import User from idhub_auth.models import User
class ProfileForm(forms.ModelForm): class ProfileForm(forms.ModelForm):

0
idhub_auth/__init__.py Normal file
View File

7
idhub_auth/admin.py Normal file
View File

@ -0,0 +1,7 @@
from django.contrib import admin
from django.contrib.auth import get_user_model
User = get_user_model()
admin.site.register(User)

6
idhub_auth/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class AuthConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'idhub_auth'

View File

@ -0,0 +1,46 @@
# Generated by Django 4.2.5 on 2023-10-25 15:16
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name='User',
fields=[
(
'id',
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID',
),
),
('password', models.CharField(max_length=128, verbose_name='password')),
(
'last_login',
models.DateTimeField(
blank=True, null=True, verbose_name='last login'
),
),
(
'email',
models.EmailField(
max_length=255, unique=True, verbose_name='email address'
),
),
('is_active', models.BooleanField(default=True)),
('is_admin', models.BooleanField(default=False)),
('first_name', models.CharField(blank=True, max_length=255, null=True)),
('last_name', models.CharField(blank=True, max_length=255, null=True)),
],
options={
'abstract': False,
},
),
]

View File

74
idhub_auth/models.py Normal file
View File

@ -0,0 +1,74 @@
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
class UserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError("Users must have an email address")
user = self.model(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(
email,
password=password,
)
user.is_admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name="email address",
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
first_name = models.CharField(max_length=255, blank=True, null=True)
last_name = models.CharField(max_length=255, blank=True, null=True)
objects = UserManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
@property
def username(self):
"Is the email of the user"
return self.email

View File

@ -69,6 +69,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django_extensions', 'django_extensions',
'django_bootstrap5', 'django_bootstrap5',
'idhub_auth',
'idhub' 'idhub'
] ]
@ -176,3 +177,4 @@ MESSAGE_TAGS = {
messages.ERROR: 'alert-danger', messages.ERROR: 'alert-danger',
} }
AUTH_USER_MODEL = 'idhub_auth.User'