IdHub/idhub/admin/views.py

814 lines
24 KiB
Python
Raw Normal View History

2023-10-20 14:50:53 +00:00
import os
2023-10-25 10:18:42 +00:00
import csv
2023-10-24 12:08:24 +00:00
import json
2023-10-26 11:33:13 +00:00
import copy
2023-10-09 15:59:15 +00:00
import logging
2023-10-25 10:18:42 +00:00
import pandas as pd
2023-10-20 14:50:53 +00:00
from pathlib import Path
2023-10-25 10:18:42 +00:00
from jsonschema import validate
2023-10-18 15:30:11 +00:00
from smtplib import SMTPException
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
from django.conf import settings
2023-10-09 15:59:15 +00:00
from django.utils.translation import gettext_lazy as _
2023-10-11 07:52:05 +00:00
from django.views.generic.base import TemplateView
2023-10-26 16:06:52 +00:00
from django.views.generic.edit import UpdateView, CreateView, DeleteView
2023-10-13 10:35:06 +00:00
from django.shortcuts import get_object_or_404, redirect
2023-10-09 15:59:15 +00:00
from django.urls import reverse_lazy
2023-10-23 17:24:48 +00:00
from django.http import HttpResponse
2023-10-09 15:59:15 +00:00
from django.contrib import messages
2023-11-07 16:44:15 +00:00
from utils.apiregiter import iota
from utils import credtools
2023-10-25 15:49:17 +00:00
from idhub_auth.models import User
2023-10-09 15:59:15 +00:00
from idhub.mixins import AdminView
2023-10-18 15:30:11 +00:00
from idhub.email.views import NotifyActivateUserByEmail
2023-11-02 09:59:15 +00:00
from idhub.admin.forms import ImportForm, SchemaForm
2023-10-25 10:18:42 +00:00
from idhub.models import (
2023-10-26 11:33:13 +00:00
DID,
2023-10-25 10:18:42 +00:00
File_datas,
Membership,
Rol,
Service,
Schemas,
UserRol,
2023-10-30 12:53:19 +00:00
VerificableCredential,
2023-10-25 10:18:42 +00:00
)
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class DashboardView(AdminView, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/dashboard.html"
2023-10-09 15:59:15 +00:00
title = _('Dashboard')
subtitle = _('Success')
icon = 'bi bi-bell'
section = "Home"
2023-10-13 11:37:26 +00:00
class People(AdminView):
2023-10-09 15:59:15 +00:00
title = _("People Management")
section = "People"
2023-10-11 07:52:05 +00:00
class AccessControl(AdminView, TemplateView):
2023-10-09 15:59:15 +00:00
title = _("Access Control Management")
section = "AccessControl"
2023-10-11 07:52:05 +00:00
class Credentials(AdminView, TemplateView):
2023-10-09 15:59:15 +00:00
title = _("Credentials Management")
section = "Credentials"
2023-10-20 14:50:53 +00:00
class SchemasMix(AdminView, TemplateView):
title = _("Templates Management")
section = "Templates"
2023-10-09 15:59:15 +00:00
2023-10-11 07:52:05 +00:00
class ImportExport(AdminView, TemplateView):
2023-10-09 15:59:15 +00:00
title = _("Massive Data Management")
section = "ImportExport"
2023-11-03 15:42:45 +00:00
class PeopleListView(People, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people.html"
2023-10-09 15:59:15 +00:00
subtitle = _('People list')
icon = 'bi bi-person'
2023-10-11 14:23:45 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'users': User.objects.filter(),
})
return context
2023-10-09 15:59:15 +00:00
2023-10-11 16:08:20 +00:00
2023-11-03 15:42:45 +00:00
class PeopleView(People, TemplateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/user.html"
2023-10-11 16:08:20 +00:00
subtitle = _('User Profile')
icon = 'bi bi-person'
model = User
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_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'object': self.object,
})
return context
2023-11-03 15:42:45 +00:00
class PeopleActivateView(PeopleView):
2023-10-13 10:35:06 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
2023-10-13 19:04:44 +00:00
if self.object == self.request.user:
messages.error(self.request, _('Is not possible deactivate your account!'))
return redirect('idhub:admin_people', self.object.id)
2023-10-13 10:35:06 +00:00
if self.object.is_active:
self.object.is_active = False
else:
self.object.is_active = True
self.object.save()
return redirect('idhub:admin_people', self.object.id)
2023-11-03 15:42:45 +00:00
class PeopleDeleteView(PeopleView):
2023-10-13 10:35:06 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
2023-10-13 19:04:44 +00:00
if self.object != self.request.user:
self.object.delete()
else:
messages.error(self.request, _('Is not possible delete your account!'))
2023-10-13 10:35:06 +00:00
return redirect('idhub:admin_people_list')
2023-11-03 15:42:45 +00:00
class PeopleEditView(PeopleView, UpdateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/user_edit.html"
2023-10-25 15:49:17 +00:00
fields = ('first_name', 'last_name', 'email')
2023-10-13 11:37:26 +00:00
success_url = reverse_lazy('idhub:admin_people_list')
2023-10-13 10:35:06 +00:00
2023-10-13 11:37:26 +00:00
2023-11-03 15:42:45 +00:00
class PeopleRegisterView(NotifyActivateUserByEmail, People, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_register.html"
2023-10-09 15:59:15 +00:00
subtitle = _('People Register')
icon = 'bi bi-person'
2023-10-13 11:37:26 +00:00
model = User
2023-10-25 15:49:17 +00:00
fields = ('first_name', 'last_name', 'email')
2023-10-13 11:37:26 +00:00
success_url = reverse_lazy('idhub:admin_people_list')
2023-10-09 15:59:15 +00:00
2023-10-16 17:08:18 +00:00
def get_success_url(self):
self.success_url = reverse_lazy(
'idhub:admin_people_membership_new',
kwargs={"pk": self.object.id}
)
return self.success_url
2023-10-18 15:30:11 +00:00
def form_valid(self, form):
user = form.save()
messages.success(self.request, _('The account is created successfully'))
if user.is_active:
try:
self.send_email(user)
except SMTPException as e:
messages.error(self.request, e)
return super().form_valid(form)
2023-10-16 17:08:18 +00:00
2023-11-03 15:42:45 +00:00
class PeopleMembershipRegisterView(People, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_membership_register.html"
2023-10-16 17:08:18 +00:00
subtitle = _('People add membership')
icon = 'bi bi-person'
model = Membership
fields = ('type', 'start_date', 'end_date')
success_url = reverse_lazy('idhub:admin_people_list')
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().post(request, *args, **kwargs)
def get_form(self):
form = super().get_form()
form.fields['start_date'].widget.input_type = 'date'
form.fields['end_date'].widget.input_type = 'date'
return form
def get_form_kwargs(self):
self.object = self.model(user=self.user)
kwargs = super().get_form_kwargs()
return kwargs
2023-10-17 08:53:03 +00:00
def get_success_url(self):
self.success_url = reverse_lazy(
2023-10-17 15:42:48 +00:00
'idhub:admin_people_rol_new',
2023-10-17 08:53:03 +00:00
kwargs={"pk": self.user.id}
)
return self.success_url
2023-11-03 15:42:45 +00:00
class PeopleMembershipEditView(People, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_membership_register.html"
2023-10-17 08:53:03 +00:00
subtitle = _('People add membership')
icon = 'bi bi-person'
model = Membership
fields = ('type', 'start_date', 'end_date')
success_url = reverse_lazy('idhub:admin_people_list')
def get_form(self):
form = super().get_form()
form.fields['start_date'].widget.input_type = 'date'
form.fields['end_date'].widget.input_type = 'date'
return form
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
if pk:
self.object = get_object_or_404(self.model, pk=pk)
kwargs = super().get_form_kwargs()
return kwargs
2023-11-03 15:42:45 +00:00
class PeopleMembershipDeleteView(PeopleView):
2023-10-17 08:53:03 +00:00
model = Membership
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
if self.object != self.request.user:
user = self.object.user
self.object.delete()
else:
messages.error(self.request, _('Is not possible delete your account!'))
return redirect('idhub:admin_people_edit', user.id)
2023-10-17 15:42:48 +00:00
2023-11-03 15:42:45 +00:00
class PeopleRolRegisterView(People, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_rol_register.html"
subtitle = _('Add Rol to User')
icon = 'bi bi-person'
model = UserRol
fields = ('service',)
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.user = get_object_or_404(User, pk=self.pk)
return super().post(request, *args, **kwargs)
def get_form_kwargs(self):
self.object = self.model(user=self.user)
kwargs = super().get_form_kwargs()
return kwargs
def get_success_url(self):
self.success_url = reverse_lazy(
'idhub:admin_people_edit',
kwargs={"pk": self.user.id}
)
return self.success_url
2023-11-03 15:42:45 +00:00
class PeopleRolEditView(People, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/people_rol_register.html"
subtitle = _('Edit Rol to User')
icon = 'bi bi-person'
model = UserRol
fields = ('service',)
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
if pk:
self.object = get_object_or_404(self.model, pk=pk)
kwargs = super().get_form_kwargs()
return kwargs
def get_success_url(self):
self.success_url = reverse_lazy(
'idhub:admin_people_edit',
kwargs={"pk": self.object.user.id}
)
return self.success_url
2023-11-03 15:42:45 +00:00
class PeopleRolDeleteView(PeopleView):
2023-10-17 15:42:48 +00:00
model = UserRol
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
user = self.object.user
self.object.delete()
return redirect('idhub:admin_people_edit', user.id)
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class RolesView(AccessControl):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/roles.html"
2023-10-09 15:59:15 +00:00
subtitle = _('Roles Management')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-17 11:40:34 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'roles': Rol.objects,
})
return context
2023-11-03 15:42:45 +00:00
class RolRegisterView(AccessControl, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/rol_register.html"
2023-10-17 11:40:34 +00:00
subtitle = _('Add Rol')
2023-10-17 13:49:56 +00:00
icon = ''
2023-10-17 11:40:34 +00:00
model = Rol
fields = ('name',)
success_url = reverse_lazy('idhub:admin_roles')
object = None
2023-11-03 15:42:45 +00:00
class RolEditView(AccessControl, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/rol_register.html"
2023-10-17 13:49:56 +00:00
subtitle = _('Edit Rol')
icon = ''
2023-10-17 11:40:34 +00:00
model = Rol
fields = ('name',)
success_url = reverse_lazy('idhub:admin_roles')
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
if pk:
self.object = get_object_or_404(self.model, pk=pk)
kwargs = super().get_form_kwargs()
return kwargs
2023-11-03 15:42:45 +00:00
class RolDeleteView(AccessControl):
2023-10-17 11:40:34 +00:00
model = Rol
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.object.delete()
return redirect('idhub:admin_roles')
2023-11-03 15:42:45 +00:00
class ServicesView(AccessControl):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/services.html"
2023-10-09 15:59:15 +00:00
subtitle = _('Service Management')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-17 13:49:56 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'services': Service.objects,
})
return context
2023-11-03 15:42:45 +00:00
class ServiceRegisterView(AccessControl, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/service_register.html"
2023-10-17 13:49:56 +00:00
subtitle = _('Add Service')
icon = ''
model = Service
fields = ('domain', 'description', 'rol')
success_url = reverse_lazy('idhub:admin_services')
object = None
2023-11-03 15:42:45 +00:00
class ServiceEditView(AccessControl, CreateView):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/service_register.html"
2023-10-17 13:49:56 +00:00
subtitle = _('Edit Service')
icon = ''
model = Service
fields = ('domain', 'description', 'rol')
success_url = reverse_lazy('idhub:admin_services')
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
if pk:
self.object = get_object_or_404(self.model, pk=pk)
kwargs = super().get_form_kwargs()
return kwargs
2023-11-03 15:42:45 +00:00
class ServiceDeleteView(AccessControl):
2023-10-17 13:49:56 +00:00
model = Service
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk)
self.object.delete()
return redirect('idhub:admin_services')
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class CredentialsView(Credentials):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/credentials.html"
2023-10-09 15:59:15 +00:00
subtitle = _('Credentials list')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-26 11:33:13 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
2023-10-30 12:53:19 +00:00
'credentials': VerificableCredential.objects,
2023-10-26 11:33:13 +00:00
})
return context
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class CredentialView(Credentials):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/issue_credentials.html"
2023-10-31 09:59:19 +00:00
subtitle = _('Change status of Credential')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-31 09:59:19 +00:00
model = VerificableCredential
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_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'object': self.object,
})
return context
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class CredentialJsonView(Credentials):
def get(self, request, *args, **kwargs):
pk = kwargs['pk']
self.object = get_object_or_404(
VerificableCredential,
pk=pk,
)
response = HttpResponse(self.object.data, content_type="application/json")
response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json")
return response
class RevokeCredentialsView(Credentials):
2023-11-03 16:10:09 +00:00
success_url = reverse_lazy('idhub:admin_credentials')
def get(self, request, *args, **kwargs):
pk = kwargs['pk']
self.object = get_object_or_404(
VerificableCredential,
pk=pk,
)
if self.object.status == VerificableCredential.Status.ISSUED:
self.object.status = VerificableCredential.Status.REVOKED
self.object.save()
messages.success(self.request, _('Credential revoked successfully'))
return redirect(self.success_url)
class DeleteCredentialsView(Credentials):
success_url = reverse_lazy('idhub:admin_credentials')
def get(self, request, *args, **kwargs):
pk = kwargs['pk']
self.object = get_object_or_404(
VerificableCredential,
pk=pk,
)
status = [
VerificableCredential.Status.REVOKED,
VerificableCredential.Status.ISSUED
]
if self.object.status in status:
self.object.delete()
messages.success(self.request, _('Credential deleted successfully'))
return redirect(self.success_url)
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class DidsView(Credentials):
2023-10-26 16:06:52 +00:00
template_name = "idhub/admin/dids.html"
2023-10-09 16:12:18 +00:00
subtitle = _('Organization Identities (DID)')
icon = 'bi bi-patch-check-fill'
2023-10-09 16:38:28 +00:00
wallet = True
2023-10-09 15:59:15 +00:00
2023-10-26 11:33:13 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'dids': DID.objects,
})
return context
2023-11-03 15:42:45 +00:00
class DidRegisterView(Credentials, CreateView):
2023-10-26 16:06:52 +00:00
template_name = "idhub/admin/did_register.html"
subtitle = _('Add a new Organization Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
2023-11-03 15:27:40 +00:00
fields = ('label',)
2023-10-26 16:06:52 +00:00
success_url = reverse_lazy('idhub:admin_dids')
object = None
def form_valid(self, form):
2023-11-03 15:27:40 +00:00
form.instance.user = self.request.user
form.instance.did = iota.issue_did()
2023-10-27 09:19:10 +00:00
form.save()
2023-10-26 16:06:52 +00:00
messages.success(self.request, _('DID created successfully'))
return super().form_valid(form)
2023-11-03 15:42:45 +00:00
class DidEditView(Credentials, UpdateView):
2023-10-26 16:06:52 +00:00
template_name = "idhub/admin/did_register.html"
subtitle = _('Organization Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
2023-11-03 15:27:40 +00:00
fields = ('label',)
2023-10-26 16:06:52 +00:00
success_url = reverse_lazy('idhub:admin_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 form_valid(self, form):
user = form.save()
2023-10-27 09:19:10 +00:00
messages.success(self.request, _('DID updated successfully'))
2023-10-26 16:06:52 +00:00
return super().form_valid(form)
2023-11-03 15:42:45 +00:00
class DidDeleteView(Credentials, DeleteView):
2023-10-26 16:06:52 +00:00
subtitle = _('Organization Identities (DID)')
icon = 'bi bi-patch-check-fill'
wallet = True
model = DID
success_url = reverse_lazy('idhub:admin_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()
2023-10-27 09:19:10 +00:00
messages.success(self.request, _('DID delete successfully'))
2023-10-26 16:06:52 +00:00
return redirect(self.success_url)
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class WalletCredentialsView(Credentials):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/wallet_credentials.html"
2023-10-09 15:59:15 +00:00
subtitle = _('Credentials')
2023-10-09 16:12:18 +00:00
icon = 'bi bi-patch-check-fill'
2023-10-09 16:38:28 +00:00
wallet = True
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class WalletConfigIssuesView(Credentials):
2023-10-17 15:42:48 +00:00
template_name = "idhub/admin/wallet_issues.html"
2023-10-09 15:59:15 +00:00
subtitle = _('Configure Issues')
2023-10-09 16:12:18 +00:00
icon = 'bi bi-patch-check-fill'
2023-10-09 16:38:28 +00:00
wallet = True
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class SchemasView(SchemasMix):
2023-10-20 14:50:53 +00:00
template_name = "idhub/admin/schemas.html"
subtitle = _('Template List')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'schemas': Schemas.objects,
})
return context
2023-10-23 17:24:48 +00:00
2023-11-03 15:42:45 +00:00
class SchemasDeleteView(SchemasMix):
2023-10-23 17:24:48 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(Schemas, pk=self.pk)
self.object.delete()
return redirect('idhub:admin_schemas')
2023-11-03 15:42:45 +00:00
class SchemasDownloadView(SchemasMix):
2023-10-23 17:24:48 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = get_object_or_404(Schemas, pk=self.pk)
response = HttpResponse(self.object.data, content_type="application/json")
response['Content-Disposition'] = 'inline; filename={}'.format(self.object.file_schema)
return response
2023-10-09 15:59:15 +00:00
2023-11-03 15:42:45 +00:00
class SchemasNewView(SchemasMix):
2023-10-22 18:01:41 +00:00
template_name = "idhub/admin/schemas_new.html"
subtitle = _('Upload Template')
icon = ''
success_url = reverse_lazy('idhub:admin_schemas')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'form': SchemaForm(),
})
return context
def post(self, request, *args, **kwargs):
form = SchemaForm(request.POST, request.FILES)
if form.is_valid():
schema = self.handle_uploaded_file()
if not schema:
messages.error(request, _("There are some errors in the file"))
return super().get(request, *args, **kwargs)
return redirect(self.success_url)
else:
return super().get(request, *args, **kwargs)
return super().post(request, *args, **kwargs)
def handle_uploaded_file(self):
f = self.request.FILES.get('file_template')
if not f:
return
file_name = f.name
if Schemas.objects.filter(file_schema=file_name).exists():
messages.error(self.request, _("This template already exists!"))
return
2023-10-24 12:08:24 +00:00
try:
data = f.read().decode('utf-8')
json.loads(data)
except Exception:
messages.error(self.request, _('This is not a schema valid!'))
return
2023-10-22 18:01:41 +00:00
schema = Schemas.objects.create(file_schema=file_name, data=data)
schema.save()
return schema
2023-11-03 15:42:45 +00:00
class SchemasImportView(SchemasMix):
2023-10-20 14:50:53 +00:00
template_name = "idhub/admin/schemas_import.html"
subtitle = _('Import Template')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'schemas': self.get_schemas(),
})
return context
def get_schemas(self):
schemas_files = os.listdir(settings.SCHEMAS_DIR)
schemas = [x for x in schemas_files
if not Schemas.objects.filter(file_schema=x).exists()]
return schemas
2023-10-09 15:59:15 +00:00
2023-10-20 14:50:53 +00:00
2023-11-03 15:42:45 +00:00
class SchemasImportAddView(SchemasMix):
2023-10-20 14:50:53 +00:00
def get(self, request, *args, **kwargs):
file_name = kwargs['file_schema']
schemas_files = os.listdir(settings.SCHEMAS_DIR)
if not file_name in schemas_files:
messages.error(self.request, f"The schema {file_name} not exist!")
return redirect('idhub:admin_schemas_import')
2023-10-24 12:08:24 +00:00
schema = self.create_schema(file_name)
if schema:
messages.success(self.request, _("The schema add successfully!"))
2023-10-20 14:50:53 +00:00
return redirect('idhub:admin_schemas_import')
def create_schema(self, file_name):
data = self.open_file(file_name)
2023-10-24 12:08:24 +00:00
try:
json.loads(data)
except Exception:
messages.error(self.request, _('This is not a schema valid!'))
return
2023-10-20 14:50:53 +00:00
schema = Schemas.objects.create(file_schema=file_name, data=data)
schema.save()
return schema
def open_file(self, file_name):
data = ''
filename = Path(settings.SCHEMAS_DIR).joinpath(file_name)
with filename.open() as schema_file:
data = schema_file.read()
return data
2023-11-03 15:42:45 +00:00
class ImportView(ImportExport):
2023-10-24 12:08:24 +00:00
template_name = "idhub/admin/import.html"
subtitle = _('Import')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-24 12:08:24 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
2023-10-25 10:18:42 +00:00
'dates': File_datas.objects,
2023-10-24 12:08:24 +00:00
})
return context
2023-10-09 15:59:15 +00:00
2023-10-24 12:08:24 +00:00
2023-11-03 15:42:45 +00:00
class ImportStep2View(ImportExport):
2023-10-24 12:08:24 +00:00
template_name = "idhub/admin/import_step2.html"
2023-10-09 15:59:15 +00:00
subtitle = _('Import')
2023-10-09 16:12:18 +00:00
icon = ''
2023-10-09 15:59:15 +00:00
2023-10-23 17:24:48 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'schemas': Schemas.objects,
})
return context
2023-10-24 12:08:24 +00:00
2023-11-03 15:42:45 +00:00
class ImportStep3View(ImportExport):
2023-10-24 12:08:24 +00:00
template_name = "idhub/admin/import_step3.html"
2023-10-23 17:24:48 +00:00
subtitle = _('Import')
icon = ''
success_url = reverse_lazy('idhub:admin_import')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'form': ImportForm(),
})
return context
def post(self, request, *args, **kwargs):
self.pk = kwargs['pk']
2023-10-24 12:08:24 +00:00
self.schema = get_object_or_404(Schemas, pk=self.pk)
2023-10-23 17:24:48 +00:00
form = ImportForm(request.POST, request.FILES)
if form.is_valid():
2023-10-25 10:18:42 +00:00
result = self.handle_uploaded_file()
if not result:
2023-10-23 17:24:48 +00:00
messages.error(request, _("There are some errors in the file"))
return super().get(request, *args, **kwargs)
return redirect(self.success_url)
else:
return super().get(request, *args, **kwargs)
return super().post(request, *args, **kwargs)
def handle_uploaded_file(self):
f = self.request.FILES.get('file_import')
2023-10-25 10:18:42 +00:00
if not f:
messages.error(self.request, _("There aren't file"))
return
2023-10-23 17:24:48 +00:00
2023-10-25 10:18:42 +00:00
file_name = f.name
2023-10-25 15:49:17 +00:00
if File_datas.objects.filter(file_name=file_name, success=True).exists():
2023-10-25 10:18:42 +00:00
messages.error(self.request, _("This file already exists!"))
return
2023-10-23 17:24:48 +00:00
2023-10-25 10:18:42 +00:00
self.json_schema = json.loads(self.schema.data)
df = pd.read_csv (f, delimiter="\t", quotechar='"', quoting=csv.QUOTE_ALL)
2023-10-23 17:24:48 +00:00
data_pd = df.fillna('').to_dict()
2023-10-25 10:18:42 +00:00
rows = {}
2023-10-23 17:24:48 +00:00
if not data_pd:
2023-10-25 10:18:42 +00:00
File_datas.objects.create(file_name=file_name, success=False)
2023-10-23 17:24:48 +00:00
return
for n in range(df.last_valid_index()+1):
row = {}
for k in data_pd.keys():
row[k] = data_pd[k][n]
2023-10-25 10:18:42 +00:00
user = self.validate(n, row)
if not user:
File_datas.objects.create(file_name=file_name, success=False)
2023-10-24 12:08:24 +00:00
return
2023-10-25 10:18:42 +00:00
rows[user] = row
File_datas.objects.create(file_name=file_name)
for k, v in rows.items():
self.create_credential(k, v)
return True
def validate(self, line, row):
try:
validate(instance=row, schema=self.json_schema)
except Exception as e:
messages.error(self.request, "line {}: {}".format(line+1, e))
return
user = User.objects.filter(email=row.get('email'))
if not user:
txt = _('The user not exist!')
messages.error(self.request, "line {}: {}".format(line+1, txt))
return
return user.first()
def create_credential(self, user, row):
2023-10-26 11:33:13 +00:00
d = copy.copy(self.json_schema)
d['instance'] = row
2023-10-30 12:53:19 +00:00
return VerificableCredential.objects.create(
2023-10-25 10:18:42 +00:00
verified=False,
user=user,
2023-10-26 11:33:13 +00:00
data=json.dumps(d)
2023-10-25 10:18:42 +00:00
)
2023-10-23 17:24:48 +00:00