add user dids management

This commit is contained in:
Cayo Puigdefabregas 2023-10-27 11:19:10 +02:00
parent 29e5e66c5f
commit 33f51fcdbc
10 changed files with 195 additions and 26 deletions

View File

@ -460,7 +460,7 @@ class AdminDidRegisterView(Credentials, CreateView):
return form
def form_valid(self, form):
user = form.save()
form.save()
messages.success(self.request, _('DID created successfully'))
return super().form_valid(form)
@ -487,7 +487,7 @@ class AdminDidEditView(Credentials, UpdateView):
def form_valid(self, form):
user = form.save()
messages.success(self.request, _('DID created successfully'))
messages.success(self.request, _('DID updated successfully'))
return super().form_valid(form)
@ -499,10 +499,10 @@ class AdminDidDeleteView(Credentials, DeleteView):
success_url = reverse_lazy('idhub:admin_dids')
def get(self, request, *args, **kwargs):
# import pdb; pdb.set_trace()
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.object.delete()
messages.success(self.request, _('DID delete successfully'))
return redirect(self.success_url)

View File

@ -152,7 +152,7 @@ class Service(models.Model):
return ", ".join([x.name for x in self.rol.all()])
def __str__(self):
return "{} -> {}".format(self.domain, self.rol.name)
return "{} -> {}".format(self.domain, self.get_roles())
class UserRol(models.Model):

View File

@ -43,11 +43,11 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">{% trans 'Delete DID' %} {{ d.file_schema }}</h5>
<h5 class="modal-title" id="exampleModalLabel">{% trans 'Delete DID' %} {{ d.did }}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{% trans 'Are you sure that you want delete this template?' %}
{% trans 'Are you sure that you want delete this DID?' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Clancel</button>

View File

@ -100,7 +100,7 @@
</span>
<ul class="flex-column mb-2 ul_sidebar">
<li class="nav-item">
<a class="nav-link {% if path == 'user_identities' %}active2{% endif %}" href="{% url 'idhub:user_identities' %}">
<a class="nav-link {% if path == 'user_dids' %}active2{% endif %}" href="{% url 'idhub:user_dids' %}">
Identities (DID)
</a>
</li>

View File

@ -0,0 +1,30 @@
{% extends "idhub/base.html" %}
{% load i18n %}
{% block content %}
{% 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:user_dids' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View File

@ -0,0 +1,56 @@
{% extends "idhub/base.html" %}
{% load i18n %}
{% block content %}
<div class="row mt-5">
<div class="col">
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Date' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Label' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">ID</button></th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for d in dids.all %}
<tr style="font-size:15px;">
<td>{{ d.created_at }}</td>
<td>{{ d.label }}</td>
<td>{{ d.did }}</td>
<td><a class="text-primary" href="{% url 'idhub:user_dids_edit' d.id %}" title="{% trans 'Edit' %}"><i class="bi bi-pencil-square"></i></a></td>
<td><a class="text-danger" href="jacascript:void()" data-bs-toggle="modal" data-bs-target="#confirm-delete-{{ d.id }}" title="{% trans 'Remove' %}"><i class="bi bi-x-circle"></i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="form-actions-no-box">
<a class="btn btn-green-user" href="{% url 'idhub:user_dids_new' %}">{% translate "Add Identity" %} <i class="bi bi-plus"></i></a>
</div>
</div>
</div>
</div>
<!-- Modal -->
{% for d in dids.all %}
<div class="modal" id="confirm-delete-{{ d.id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">{% trans 'Delete DID' %} {{ d.did }}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{% trans 'Are you sure that you want delete this DID?' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Clancel</button>
<a href="{% url 'idhub:user_dids_del' d.id %}" type="button" class="btn btn-danger">{% trans 'Delete' %}</a>
</div>
</div>
</div>
</div>
{% endfor %}
{% endblock %}

View File

@ -1,5 +0,0 @@
{% extends "idhub/base.html" %}
{% load i18n %}
{% block content %}
{% endblock %}

View File

@ -16,7 +16,7 @@
<tbody>
{% for rol in user.roles.all %}
<tr>
<td>{{ rol.service.rol.name }}</td>
<td>{{ rol.service.get_roles }}</td>
<td>{{ rol.service.description }}</td>
<td>{{ rol.service.domain }}</td>
</tr>

View File

@ -67,8 +67,14 @@ urlpatterns = [
name='user_roles'),
path('user/gdpr/', views_user.UserGDPRView.as_view(),
name='user_gdpr'),
path('user/identities/', views_user.UserIdentitiesView.as_view(),
name='user_identities'),
path('user/identities/', views_user.UserDidsView.as_view(),
name='user_dids'),
path('user/dids/new/', views_user.UserDidRegisterView.as_view(),
name='user_dids_new'),
path('user/dids/<int:pk>/', views_user.UserDidEditView.as_view(),
name='user_dids_edit'),
path('user/dids/<int:pk>/del/', views_user.UserDidDeleteView.as_view(),
name='user_dids_del'),
path('user/credentials/', views_user.UserCredentialsView.as_view(),
name='user_credentials'),
path('user/credentials_required/',

View File

@ -1,12 +1,15 @@
import logging
from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import UpdateView
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.contrib import messages
from apiregiter import iota
from idhub.user.forms import ProfileForm
from idhub.mixins import UserView
from idhub.models import DID
class MyProfile(UserView):
@ -14,7 +17,7 @@ class MyProfile(UserView):
section = "MyProfile"
class MyWallet(UserView, TemplateView):
class MyWallet(UserView):
title = _("My Wallet")
section = "MyWallet"
@ -51,25 +54,104 @@ class UserGDPRView(MyProfile, TemplateView):
icon = 'bi bi-file-earmark-medical'
class UserIdentitiesView(MyWallet):
template_name = "idhub/user/identities.html"
subtitle = _('Identities (DID)')
icon = 'bi bi-patch-check-fill'
class UserCredentialsView(MyWallet):
class UserCredentialsView(MyWallet, TemplateView):
template_name = "idhub/user/credentials.html"
subtitle = _('Credentials')
icon = 'bi bi-patch-check-fill'
class UserCredentialsRequiredView(MyWallet):
class UserCredentialsRequiredView(MyWallet, TemplateView):
template_name = "idhub/user/credentials_required.html"
subtitle = _('Credentials required')
icon = 'bi bi-patch-check-fill'
class UserCredentialsPresentationView(MyWallet):
class UserCredentialsPresentationView(MyWallet, TemplateView):
template_name = "idhub/user/credentials_presentation.html"
subtitle = _('Credentials Presentation')
icon = 'bi bi-patch-check-fill'
class UserDidsView(MyWallet, TemplateView):
template_name = "idhub/user/dids.html"
subtitle = _('Identities (DID)')
icon = 'bi bi-patch-check-fill'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'dids': self.request.user.dids,
})
return context
class UserDidRegisterView(MyWallet, CreateView):
template_name = "idhub/user/did_register.html"
subtitle = _('Add a new Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
fields = ('did', 'label')
success_url = reverse_lazy('idhub:user_dids')
object = None
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['initial'] = {
'did': iota.issue_did(),
'user': self.request.user
}
return kwargs
def get_form(self):
form = super().get_form()
form.fields['did'].required = False
form.fields['did'].disabled = True
return form
def form_valid(self, form):
form.instance.user = self.request.user
form.save()
messages.success(self.request, _('DID created successfully'))
return super().form_valid(form)
class UserDidEditView(MyWallet, UpdateView):
template_name = "idhub/user/did_register.html"
subtitle = _('Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
fields = ('did', 'label')
success_url = reverse_lazy('idhub:user_dids')
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
return super().get(request, *args, **kwargs)
def get_form(self):
form = super().get_form()
form.fields['did'].required = False
form.fields['did'].disabled = True
return form
def form_valid(self, form):
user = form.save()
messages.success(self.request, _('DID updated successfully'))
return super().form_valid(form)
class UserDidDeleteView(MyWallet, DeleteView):
subtitle = _('Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
success_url = reverse_lazy('idhub:user_dids')
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.object.delete()
messages.success(self.request, _('DID delete successfully'))
return redirect(self.success_url)