IdHub/idhub/user/views.py

158 lines
4.5 KiB
Python
Raw Normal View History

2023-10-10 08:54:13 +00:00
import logging
from django.utils.translation import gettext_lazy as _
2023-10-27 09:19:10 +00:00
from django.views.generic.edit import UpdateView, CreateView, DeleteView
2023-10-11 07:52:05 +00:00
from django.views.generic.base import TemplateView
2023-10-27 09:19:10 +00:00
from django.shortcuts import get_object_or_404, redirect
2023-10-10 08:54:13 +00:00
from django.urls import reverse_lazy
from django.contrib import messages
2023-10-27 09:19:10 +00:00
from apiregiter import iota
2023-10-11 07:52:05 +00:00
from idhub.user.forms import ProfileForm
2023-10-10 08:54:13 +00:00
from idhub.mixins import UserView
2023-10-27 09:19:10 +00:00
from idhub.models import DID
2023-10-10 08:54:13 +00:00
class MyProfile(UserView):
title = _("My profile")
section = "MyProfile"
2023-10-27 09:19:10 +00:00
class MyWallet(UserView):
2023-10-10 08:54:13 +00:00
title = _("My Wallet")
section = "MyWallet"
2023-10-11 07:52:05 +00:00
class UserDashboardView(UserView, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/user/dashboard.html"
2023-10-10 08:54:13 +00:00
title = _('Dashboard')
subtitle = _('Success')
icon = 'bi bi-bell'
section = "Home"
2023-10-11 07:52:05 +00:00
class UserProfileView(MyProfile, UpdateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/user/profile.html"
2023-10-10 08:54:13 +00:00
subtitle = _('My personal Data')
icon = 'bi bi-person'
2023-10-11 07:52:05 +00:00
from_class = ProfileForm
fields = ('first_name', 'last_name', 'email')
success_url = reverse_lazy('idhub:user_profile')
2023-10-10 08:54:13 +00:00
2023-10-11 07:52:05 +00:00
def get_object(self):
return self.request.user
2023-10-10 08:54:13 +00:00
2023-10-11 07:52:05 +00:00
class UserRolesView(MyProfile, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/user/roles.html"
2023-10-10 08:54:13 +00:00
subtitle = _('My roles')
icon = 'fa-brands fa-critical-role'
2023-10-11 07:52:05 +00:00
class UserGDPRView(MyProfile, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/user/gdpr.html"
2023-10-10 08:54:13 +00:00
subtitle = _('GDPR info')
icon = 'bi bi-file-earmark-medical'
2023-10-27 09:19:10 +00:00
class UserCredentialsView(MyWallet, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/user/credentials.html"
2023-10-10 08:54:13 +00:00
subtitle = _('Credentials')
icon = 'bi bi-patch-check-fill'
2023-10-27 09:19:10 +00:00
class UserCredentialsRequiredView(MyWallet, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/user/credentials_required.html"
2023-10-10 08:54:13 +00:00
subtitle = _('Credentials required')
icon = 'bi bi-patch-check-fill'
2023-10-27 09:19:10 +00:00
class UserCredentialsPresentationView(MyWallet, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/user/credentials_presentation.html"
2023-10-10 08:54:13 +00:00
subtitle = _('Credentials Presentation')
icon = 'bi bi-patch-check-fill'
2023-10-27 09:19:10 +00:00
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)