From 4eb02e92182615b172b03cae4631de6610b2b144 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 25 Oct 2023 17:49:17 +0200 Subject: [PATCH] add User model custumized --- idhub/admin.py | 5 - idhub/admin/forms.py | 4 +- idhub/admin/views.py | 8 +- idhub/migrations/0001_initial.py | 172 ++++++++++++++---- idhub/migrations/0002_rol.py | 27 --- idhub/migrations/0003_service.py | 37 ---- idhub/migrations/0004_userrol.py | 45 ----- .../0005_remove_service_rol_service_rol.py | 21 --- idhub/migrations/0006_schemas.py | 29 --- idhub/migrations/0007_file_datas.py | 29 --- idhub/mixins.py | 2 +- idhub/models.py | 14 +- idhub/templates/idhub/admin/people.html | 4 +- idhub/templates/idhub/admin/user.html | 2 +- idhub/user/forms.py | 2 +- idhub_auth/__init__.py | 0 idhub_auth/admin.py | 7 + idhub_auth/apps.py | 6 + idhub_auth/migrations/0001_initial.py | 46 +++++ idhub_auth/migrations/__init__.py | 0 idhub_auth/models.py | 74 ++++++++ trustchain_idhub/settings.py | 2 + 22 files changed, 287 insertions(+), 249 deletions(-) delete mode 100644 idhub/admin.py delete mode 100644 idhub/migrations/0002_rol.py delete mode 100644 idhub/migrations/0003_service.py delete mode 100644 idhub/migrations/0004_userrol.py delete mode 100644 idhub/migrations/0005_remove_service_rol_service_rol.py delete mode 100644 idhub/migrations/0006_schemas.py delete mode 100644 idhub/migrations/0007_file_datas.py create mode 100644 idhub_auth/__init__.py create mode 100644 idhub_auth/admin.py create mode 100644 idhub_auth/apps.py create mode 100644 idhub_auth/migrations/0001_initial.py create mode 100644 idhub_auth/migrations/__init__.py create mode 100644 idhub_auth/models.py diff --git a/idhub/admin.py b/idhub/admin.py deleted file mode 100644 index 049dca3..0000000 --- a/idhub/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.contrib import admin - -from .models import AppUser - -admin.site.register(AppUser) diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 5270aca..dd531a4 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -1,10 +1,10 @@ from django import forms -from django.contrib.auth.models import User +from idhub_auth.models import User from idhub.models import Rol class ProfileForm(forms.ModelForm): - MANDATORY_FIELDS = ['first_name', 'last_name', 'email', 'username'] + MANDATORY_FIELDS = ['first_name', 'last_name', 'email'] class Meta: model = User diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 0ff7a2f..b78c3f8 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -11,11 +11,11 @@ from django.conf import settings from django.utils.translation import gettext_lazy as _ from django.views.generic.base import TemplateView 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.urls import reverse_lazy from django.http import HttpResponse from django.contrib import messages +from idhub_auth.models import User from idhub.mixins import AdminView from idhub.email.views import NotifyActivateUserByEmail from idhub.models import ( @@ -137,7 +137,7 @@ class AdminPeopleDeleteView(AdminPeopleView): class AdminPeopleEditView(AdminPeopleView, UpdateView): template_name = "idhub/admin/user_edit.html" from_class = ProfileForm - fields = ('first_name', 'last_name', 'email', 'username') + fields = ('first_name', 'last_name', 'email') success_url = reverse_lazy('idhub:admin_people_list') @@ -147,7 +147,7 @@ class AdminPeopleRegisterView(NotifyActivateUserByEmail, People, CreateView): icon = 'bi bi-person' model = User from_class = ProfileForm - fields = ('first_name', 'last_name', 'email', 'username') + fields = ('first_name', 'last_name', 'email') success_url = reverse_lazy('idhub:admin_people_list') def get_success_url(self): @@ -646,7 +646,7 @@ class AdminImportStep3View(ImportExport): return 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!")) return diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index 45660e5..ffe4a09 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-16 09:41 +# Generated by Django 4.2.5 on 2023-10-25 15:47 from django.conf import settings from django.db import migrations, models @@ -14,115 +14,211 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name="VCTemplate", + name='File_datas', fields=[ ( - "id", + 'id', models.BigAutoField( auto_created=True, primary_key=True, serialize=False, - verbose_name="ID", + verbose_name='ID', ), ), - ("wkit_template_id", models.CharField(max_length=250)), - ("data", models.TextField()), + ('file_name', models.CharField(max_length=250)), + ('success', models.BooleanField(default=True)), + ('created_at', models.DateTimeField(auto_now=True)), ], ), migrations.CreateModel( - name="VerifiableCredential", + name='Rol', fields=[ ( - "id", + 'id', models.BigAutoField( auto_created=True, primary_key=True, serialize=False, - verbose_name="ID", + 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()), + ('name', models.CharField(max_length=250)), + ], + ), + migrations.CreateModel( + name='Schemas', + 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( on_delete=django.db.models.deletion.CASCADE, - related_name="vcredentials", + related_name='vcredentials', to=settings.AUTH_USER_MODEL, ), ), ], ), migrations.CreateModel( - name="Membership", + name='UserRol', fields=[ ( - "id", + 'id', models.BigAutoField( auto_created=True, primary_key=True, 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( - choices=[(1, "Beneficiary"), (2, "Employee"), (3, "Partner")], - verbose_name="Type of membership", + choices=[(1, 'Beneficiary'), (2, 'Employee'), (3, 'Partner')], + verbose_name='Type of membership', ), ), ( - "start_date", + 'start_date', models.DateField( blank=True, - help_text="What date did the membership start?", + help_text='What date did the membership start?', null=True, - verbose_name="Start date", + verbose_name='Start date', ), ), ( - "end_date", + 'end_date', models.DateField( blank=True, - help_text="What date did the membership end?", + help_text='What date did the membership end?', null=True, - verbose_name="End date", + verbose_name='End date', ), ), ( - "user", + 'user', models.ForeignKey( on_delete=django.db.models.deletion.CASCADE, - related_name="memberships", + related_name='memberships', to=settings.AUTH_USER_MODEL, ), ), ], ), migrations.CreateModel( - name="DID", + name='DID', fields=[ ( - "id", + 'id', models.BigAutoField( auto_created=True, primary_key=True, serialize=False, - verbose_name="ID", + verbose_name='ID', ), ), - ("did_string", models.CharField(max_length=250)), - ("label", models.CharField(max_length=50)), + ('did_string', models.CharField(max_length=250)), + ('label', models.CharField(max_length=50)), ( - "user", + 'user', models.ForeignKey( on_delete=django.db.models.deletion.CASCADE, - related_name="dids", + related_name='dids', to=settings.AUTH_USER_MODEL, ), ), diff --git a/idhub/migrations/0002_rol.py b/idhub/migrations/0002_rol.py deleted file mode 100644 index 41f8aba..0000000 --- a/idhub/migrations/0002_rol.py +++ /dev/null @@ -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)), - ], - ), - ] diff --git a/idhub/migrations/0003_service.py b/idhub/migrations/0003_service.py deleted file mode 100644 index 2c908de..0000000 --- a/idhub/migrations/0003_service.py +++ /dev/null @@ -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", - ), - ), - ], - ), - ] diff --git a/idhub/migrations/0004_userrol.py b/idhub/migrations/0004_userrol.py deleted file mode 100644 index 5955a70..0000000 --- a/idhub/migrations/0004_userrol.py +++ /dev/null @@ -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, - ), - ), - ], - ), - ] diff --git a/idhub/migrations/0005_remove_service_rol_service_rol.py b/idhub/migrations/0005_remove_service_rol_service_rol.py deleted file mode 100644 index a383ca4..0000000 --- a/idhub/migrations/0005_remove_service_rol_service_rol.py +++ /dev/null @@ -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"), - ), - ] diff --git a/idhub/migrations/0006_schemas.py b/idhub/migrations/0006_schemas.py deleted file mode 100644 index 71362b4..0000000 --- a/idhub/migrations/0006_schemas.py +++ /dev/null @@ -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)), - ], - ), - ] diff --git a/idhub/migrations/0007_file_datas.py b/idhub/migrations/0007_file_datas.py deleted file mode 100644 index 5c290fa..0000000 --- a/idhub/migrations/0007_file_datas.py +++ /dev/null @@ -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)), - ], - ), - ] diff --git a/idhub/mixins.py b/idhub/mixins.py index c888327..68b7344 100644 --- a/idhub/mixins.py +++ b/idhub/mixins.py @@ -26,7 +26,7 @@ class UserView(LoginRequiredMixin): class AdminView(UserView): def get(self, request, *args, **kwargs): - if not request.user.is_superuser: + if not request.user.is_admin: url = reverse_lazy('idhub:user_dashboard') return redirect(url) diff --git a/idhub/models.py b/idhub/models.py index 054d001..f64a126 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -1,6 +1,6 @@ from django.db import models 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): @@ -30,6 +30,12 @@ class DID(models.Model): # 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): id_string = models.CharField(max_length=250) verified = models.BooleanField() @@ -49,12 +55,6 @@ class VCTemplate(models.Model): 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): file_name = models.CharField(max_length=250) success = models.BooleanField(default=True) diff --git a/idhub/templates/idhub/admin/people.html b/idhub/templates/idhub/admin/people.html index ff19975..2df8577 100644 --- a/idhub/templates/idhub/admin/people.html +++ b/idhub/templates/idhub/admin/people.html @@ -12,7 +12,7 @@ - + @@ -23,7 +23,7 @@ {{ user.last_name }} {{ user.first_name }} - {{ user.username }} + {{ user.email }} {% for m in user.memberships.all %} {{ m.get_type }} diff --git a/idhub/templates/idhub/admin/user.html b/idhub/templates/idhub/admin/user.html index 3bee80f..410146f 100644 --- a/idhub/templates/idhub/admin/user.html +++ b/idhub/templates/idhub/admin/user.html @@ -109,7 +109,7 @@