diff --git a/idhub/admin.py b/idhub/admin.py index 8c38f3f..049dca3 100644 --- a/idhub/admin.py +++ b/idhub/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin -# Register your models here. +from .models import AppUser + +admin.site.register(AppUser) diff --git a/idhub/forms.py b/idhub/forms.py new file mode 100644 index 0000000..0c11b6a --- /dev/null +++ b/idhub/forms.py @@ -0,0 +1,24 @@ +from django import forms +from .models import AppUser + + +class UserForm(forms.Form): + first_name = forms.CharField() + last_name = forms.CharField() + email = forms.EmailField() + date_joined = forms.DateField() + + # Extra data: + afiliacio = forms.CharField() + + @classmethod + def from_user(cls, user: AppUser): + d = { + "first_name": user.django_user.first_name, + "last_name": user.django_user.last_name, + "email": user.django_user.email, + "date_joined": user.django_user.date_joined, + + "afiliacio": "lareputa" + } + return cls(d) diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py new file mode 100644 index 0000000..05fa67a --- /dev/null +++ b/idhub/migrations/0001_initial.py @@ -0,0 +1,56 @@ +# Generated by Django 4.2.5 on 2023-10-02 15:55 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + 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)), + ], + 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()), + ], + ), + 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)), + ('data', models.TextField()), + ('verified', models.BooleanField()), + ('created_on', models.DateTimeField()), + ('did_issuer', models.CharField(max_length=250)), + ('did_subject', models.CharField(max_length=250)), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='AppUser', + 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)), + ], + ), + ] diff --git a/idhub/models.py b/idhub/models.py index 88f6ec2..41f0de0 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -2,9 +2,12 @@ from django.db import models from django.contrib.auth.models import User as DjangoUser -class User(DjangoUser): +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) + + # Extra data, segun entidad/organizacion pass @@ -14,15 +17,39 @@ class Event(models.Model): kind = "PLACEHOLDER" -class DID(models.Model): + + + + + + + + + + +class ExternallyStoredModel(models.Model): + pass + + # Any models which inherit from this class are stored in wallet-kit, not in the Django ORM + class Meta: + abstract = True + + @staticmethod + def from_json(json_serialization): + # Construct an instance of this class by de-serialization from data returned by wallet-kit. + # Must be implemented by any deriving class. + raise NotImplementedError() + + +class DID(ExternallyStoredModel): did_string = models.CharField(max_length=250) # kind = "KEY|JWK|WEB|EBSI|CHEQD|IOTA" -class VerifiableCredential(models.Model): +class VerifiableCredential(ExternallyStoredModel): id_string = models.CharField(max_length=250) data = models.TextField() verified = models.BooleanField() created_on = models.DateTimeField() - did_issuer = models.ForeignKey(DID, on_delete=models.PROTECT) - did_subject = models.ForeignKey(DID, on_delete=models.PROTECT) + did_issuer = models.CharField(max_length=250) # Probably not a FK but the DID directly + did_subject = models.CharField(max_length=250) # Probably not a FK but the DID directly diff --git a/idhub/templates/idhub/user-details.html b/idhub/templates/idhub/user-details.html new file mode 100644 index 0000000..4f9b2b1 --- /dev/null +++ b/idhub/templates/idhub/user-details.html @@ -0,0 +1,14 @@ + + + + + Title + + +
+ {% csrf_token %} + {{ form }} + +
+ + \ No newline at end of file diff --git a/idhub/templates/registration/login.html b/idhub/templates/registration/login.html new file mode 100644 index 0000000..12aa9cf --- /dev/null +++ b/idhub/templates/registration/login.html @@ -0,0 +1,7 @@ + +

Log In

+
+{% csrf_token %} +{{ form.as_p }} + +
\ No newline at end of file diff --git a/idhub/urls.py b/idhub/urls.py index f314005..e8f101a 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -1,5 +1,4 @@ from django.urls import path - from . import views urlpatterns = [ diff --git a/idhub/views.py b/idhub/views.py index 840c3a0..f412c5b 100644 --- a/idhub/views.py +++ b/idhub/views.py @@ -1,15 +1,30 @@ +from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render -from .models import User - +from django.urls import reverse +from .models import AppUser +from .forms import UserForm from django.shortcuts import redirect, render +from django.contrib.auth.decorators import login_required + def index(request): return redirect("/user") +@login_required def user(request): - uid = request.user - user = User.get(uid) - context = { userdata: user } - return render(request, "polls/user.html", context) \ No newline at end of file + current_user: AppUser = request.user.appuser + if request.method == "POST": + form = UserForm(request.POST) + if form.is_valid(): + cdata = form.cleaned_data + current_user.django_user.first_name = cdata['first_name'] + current_user.save() + current_user.django_user.save() + return HttpResponseRedirect(reverse("user")) + else: + return render(request, "idhub/user-details.html", {"form": form}) + elif request.method == "GET": + form = UserForm.from_user(current_user) + return render(request, "idhub/user-details.html", {"form": form}) diff --git a/trustchain_idhub/settings_orig.py b/trustchain_idhub/settings_orig.py new file mode 100644 index 0000000..232dde7 --- /dev/null +++ b/trustchain_idhub/settings_orig.py @@ -0,0 +1,123 @@ +""" +Django settings for trustchain_idhub project. + +Generated by 'django-admin startproject' using Django 4.2.5. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-os^a#c(i*z8*=o4#b%xsno97_!pqsv*or_5&lcga7&+u53(p92' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'trustchain_idhub.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'trustchain_idhub.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/trustchain_idhub/urls.py b/trustchain_idhub/urls.py index 3519a39..64c0f9a 100644 --- a/trustchain_idhub/urls.py +++ b/trustchain_idhub/urls.py @@ -16,8 +16,11 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include +from django.contrib.auth import views as auth_views + urlpatterns = [ path('django-admin/', admin.site.urls), - path('/', include("idhub.urls")) + path("accounts/login/", auth_views.LoginView.as_view()), + path('', include("idhub.urls")) ] diff --git a/urls_provisional b/urls_provisional index 38921fb..845f917 100644 --- a/urls_provisional +++ b/urls_provisional @@ -4,8 +4,8 @@ /user/roles [GET] -> vista de rols (????) /user/gdpr [GET] -> info de la gdpr -/user/wallet/dids [GET, PUT] -/user/wallet/dids/ [DELETE] +/user/wallet/dids [GET, POST] +/user/wallet/dids/ [GET, DELETE] /user/credentials [GET] /user/credentials/ [GET, DELETE] /user/credentials/request [GET, POST]