From ef476773ac11ff140d06a189f07024dadaa9a07a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 30 Oct 2023 13:53:19 +0100 Subject: [PATCH] user credentials view --- idhub/admin/forms.py | 5 ++ idhub/admin/views.py | 7 +-- idhub/migrations/0001_initial.py | 4 +- idhub/models.py | 7 ++- idhub/templates/idhub/base.html | 4 -- idhub/templates/idhub/user/credential.html | 46 ++++++++++++++++ idhub/templates/idhub/user/credentials.html | 36 +++++++++++++ .../idhub/user/credentials_presentation.html | 4 ++ .../idhub/user/credentials_required.html | 4 ++ idhub/templates/idhub/user/dashboard.html | 4 ++ idhub/templates/idhub/user/did_register.html | 4 ++ idhub/templates/idhub/user/dids.html | 4 ++ idhub/templates/idhub/user/gdpr.html | 4 ++ idhub/templates/idhub/user/profile.html | 44 +++++++++++++++ idhub/templates/idhub/user/roles.html | 4 ++ idhub/urls.py | 4 ++ idhub/user/views.py | 53 ++++++++++++++++++- idhub_auth/migrations/0001_initial.py | 2 +- initial_datas.py | 4 +- 19 files changed, 231 insertions(+), 13 deletions(-) create mode 100644 idhub/admin/forms.py create mode 100644 idhub/templates/idhub/user/credential.html diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py new file mode 100644 index 0000000..cebefd8 --- /dev/null +++ b/idhub/admin/forms.py @@ -0,0 +1,5 @@ +from django import forms + + +class ImportForm(forms.Form): + file_import = forms.FileField() diff --git a/idhub/admin/views.py b/idhub/admin/views.py index e1c0fef..3e148d9 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -20,6 +20,7 @@ from apiregiter import iota from idhub_auth.models import User from idhub.mixins import AdminView from idhub.email.views import NotifyActivateUserByEmail +from idhub.admin.forms import ImportForm from idhub.models import ( DID, File_datas, @@ -28,7 +29,7 @@ from idhub.models import ( Service, Schemas, UserRol, - VerifiableCredential, + VerificableCredential, ) @@ -406,7 +407,7 @@ class AdminCredentialsView(Credentials): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ - 'credentials': VerifiableCredential.objects, + 'credentials': VerificableCredential.objects, }) return context @@ -763,7 +764,7 @@ class AdminImportStep3View(ImportExport): def create_credential(self, user, row): d = copy.copy(self.json_schema) d['instance'] = row - return VerifiableCredential.objects.create( + return VerificableCredential.objects.create( verified=False, user=user, data=json.dumps(d) diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index 26780eb..24b6c3c 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-26 13:46 +# Generated by Django 4.2.5 on 2023-10-27 09:32 from django.conf import settings from django.db import migrations, models @@ -96,7 +96,7 @@ class Migration(migrations.Migration): ], ), migrations.CreateModel( - name='VerifiableCredential', + name='VerificableCredential', fields=[ ( 'id', diff --git a/idhub/models.py b/idhub/models.py index f58f1a2..3b72f3f 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -48,7 +48,7 @@ class Schemas(models.Model): return self.get_schema.get('description', '') -class VerifiableCredential(models.Model): +class VerificableCredential(models.Model): """ Definition of Verificable Credentials """ @@ -90,6 +90,11 @@ class VerifiableCredential(models.Model): def get_status(self): return self.Status(self.status).label + def get_datas(self): + data = json.loads(self.data).get('instance').items() + return data + # import pdb; pdb.set_trace() + class VCTemplate(models.Model): wkit_template_id = models.CharField(max_length=250) data = models.TextField() diff --git a/idhub/templates/idhub/base.html b/idhub/templates/idhub/base.html index eca65be..29ccb1d 100644 --- a/idhub/templates/idhub/base.html +++ b/idhub/templates/idhub/base.html @@ -144,10 +144,6 @@ -

- - {{ subtitle }} -

{% block content %} {% endblock content %} diff --git a/idhub/templates/idhub/user/credential.html b/idhub/templates/idhub/user/credential.html new file mode 100644 index 0000000..4593adf --- /dev/null +++ b/idhub/templates/idhub/user/credential.html @@ -0,0 +1,46 @@ +{% extends "idhub/base.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+
+
+
+
+ {% for k, v in object.get_datas %} +
+
+ {{ k|capfirst }}: +
+
+ {{ v }} +
+
+ {% endfor %} +
+
+ Date of Issue: +
+
+ {{ object.issuer_on|default_if_none:"" }} +
+
+
+
+ Status: +
+
+ {{ object.get_status}} +
+
+ +
+
+{% endblock %} diff --git a/idhub/templates/idhub/user/credentials.html b/idhub/templates/idhub/user/credentials.html index d6520a0..f6f694d 100644 --- a/idhub/templates/idhub/user/credentials.html +++ b/idhub/templates/idhub/user/credentials.html @@ -2,4 +2,40 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

+
+
+
+ + + + + + + + + + + + {% for f in credentials.all %} + + + + + + + + {% endfor %} + +
{{ f.type }}{{ f.description }}{{ f.issue_on }}{{ f.get_status }} + + + +
+
+
+
{% endblock %} diff --git a/idhub/templates/idhub/user/credentials_presentation.html b/idhub/templates/idhub/user/credentials_presentation.html index d6520a0..1ac25fd 100644 --- a/idhub/templates/idhub/user/credentials_presentation.html +++ b/idhub/templates/idhub/user/credentials_presentation.html @@ -2,4 +2,8 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

{% endblock %} diff --git a/idhub/templates/idhub/user/credentials_required.html b/idhub/templates/idhub/user/credentials_required.html index d6520a0..1ac25fd 100644 --- a/idhub/templates/idhub/user/credentials_required.html +++ b/idhub/templates/idhub/user/credentials_required.html @@ -2,4 +2,8 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

{% endblock %} diff --git a/idhub/templates/idhub/user/dashboard.html b/idhub/templates/idhub/user/dashboard.html index 8fa4a87..29154f4 100644 --- a/idhub/templates/idhub/user/dashboard.html +++ b/idhub/templates/idhub/user/dashboard.html @@ -2,6 +2,10 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

diff --git a/idhub/templates/idhub/user/did_register.html b/idhub/templates/idhub/user/did_register.html index 79f7c08..019b966 100644 --- a/idhub/templates/idhub/user/did_register.html +++ b/idhub/templates/idhub/user/did_register.html @@ -2,6 +2,10 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

{% load django_bootstrap5 %} {% csrf_token %} diff --git a/idhub/templates/idhub/user/dids.html b/idhub/templates/idhub/user/dids.html index ea6b1ea..eae3db6 100644 --- a/idhub/templates/idhub/user/dids.html +++ b/idhub/templates/idhub/user/dids.html @@ -2,6 +2,10 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

diff --git a/idhub/templates/idhub/user/gdpr.html b/idhub/templates/idhub/user/gdpr.html index d6520a0..1ac25fd 100644 --- a/idhub/templates/idhub/user/gdpr.html +++ b/idhub/templates/idhub/user/gdpr.html @@ -2,4 +2,8 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

{% endblock %} diff --git a/idhub/templates/idhub/user/profile.html b/idhub/templates/idhub/user/profile.html index fb129ba..da1597a 100644 --- a/idhub/templates/idhub/user/profile.html +++ b/idhub/templates/idhub/user/profile.html @@ -2,6 +2,18 @@ {% load i18n %} {% block content %} +
+
+

+ + {{ subtitle }} +

+
+ +
{% load django_bootstrap5 %} {% csrf_token %} @@ -25,4 +37,36 @@
+
+ +
+
+
+
+ + + + + + + + + + {% for membership in object.memberships.all %} + + + + + + + {% endfor %} + +
{{ membership.get_type }}{{ membership.start_date|default:'' }}{{ membership.end_date|default:'' }} + + + +
+
+ + {% endblock %} diff --git a/idhub/templates/idhub/user/roles.html b/idhub/templates/idhub/user/roles.html index b1860f9..0295b53 100644 --- a/idhub/templates/idhub/user/roles.html +++ b/idhub/templates/idhub/user/roles.html @@ -2,6 +2,10 @@ {% load i18n %} {% block content %} +

+ + {{ subtitle }} +

diff --git a/idhub/urls.py b/idhub/urls.py index bf3b99a..d38cd6f 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -77,6 +77,10 @@ urlpatterns = [ name='user_dids_del'), path('user/credentials/', views_user.UserCredentialsView.as_view(), name='user_credentials'), + path('user/credentials/', views_user.UserCredentialView.as_view(), + name='user_credential'), + path('user/credentials//json', views_user.UserCredentialJsonView.as_view(), + name='user_credential_json'), path('user/credentials_required/', views_user.UserCredentialsRequiredView.as_view(), name='user_credentials_required'), diff --git a/idhub/user/views.py b/idhub/user/views.py index aa95270..7a992b7 100644 --- a/idhub/user/views.py +++ b/idhub/user/views.py @@ -5,11 +5,12 @@ from django.views.generic.edit import UpdateView, CreateView, DeleteView from django.views.generic.base import TemplateView 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 apiregiter import iota from idhub.user.forms import ProfileForm from idhub.mixins import UserView -from idhub.models import DID +from idhub.models import DID, VerificableCredential class MyProfile(UserView): @@ -59,12 +60,62 @@ class UserCredentialsView(MyWallet, TemplateView): subtitle = _('Credentials') icon = 'bi bi-patch-check-fill' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'credentials': VerificableCredential.objects, + }) + return context + + +class UserCredentialView(MyWallet, TemplateView): + template_name = "idhub/user/credential.html" + subtitle = _('Credential') + icon = 'bi bi-patch-check-fill' + + def get(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.object = get_object_or_404( + VerificableCredential, + pk=self.pk, + user=self.request.user + ) + return super().get(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'object': self.object, + }) + return context + + +class UserCredentialJsonView(MyWallet, TemplateView): + + def get(self, request, *args, **kwargs): + pk = kwargs['pk'] + self.object = get_object_or_404( + VerificableCredential, + pk=pk, + user=self.request.user + ) + response = HttpResponse(self.object.data, content_type="application/json") + response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json") + return response + class UserCredentialsRequiredView(MyWallet, TemplateView): template_name = "idhub/user/credentials_required.html" subtitle = _('Credentials required') icon = 'bi bi-patch-check-fill' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'credentials': VerificableCredential.objects, + }) + return context + class UserCredentialsPresentationView(MyWallet, TemplateView): template_name = "idhub/user/credentials_presentation.html" diff --git a/idhub_auth/migrations/0001_initial.py b/idhub_auth/migrations/0001_initial.py index 1f53988..8251b11 100644 --- a/idhub_auth/migrations/0001_initial.py +++ b/idhub_auth/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-10-26 13:46 +# Generated by Django 4.2.5 on 2023-10-27 09:32 from django.db import migrations, models diff --git a/initial_datas.py b/initial_datas.py index 66e0463..b9a4fca 100644 --- a/initial_datas.py +++ b/initial_datas.py @@ -14,7 +14,9 @@ def create_admin_users(email, password): def create_users(email, password): - User.objects.create(email=email, password=password) + u= User.objects.create(email=email, password=password) + u.set_password(password) + u.save() def main():