diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 13031b0..50c492f 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -1,6 +1,9 @@ +import os import logging +from pathlib import Path from smtplib import SMTPException +from django.conf import settings from django.utils.translation import gettext_lazy as _ from django.views.generic.base import TemplateView from django.views.generic.edit import UpdateView, CreateView @@ -8,7 +11,7 @@ from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.contrib import messages -from idhub.models import Membership, Rol, Service, UserRol +from idhub.models import Membership, Rol, Service, UserRol, Schemas from idhub.mixins import AdminView from idhub.email.views import NotifyActivateUserByEmail from idhub.admin.forms import ( @@ -42,9 +45,9 @@ class Credentials(AdminView, TemplateView): section = "Credentials" -class Schemes(AdminView, TemplateView): - title = _("Schemes Management") - section = "Schemes" +class SchemasMix(AdminView, TemplateView): + title = _("Templates Management") + section = "Templates" class ImportExport(AdminView, TemplateView): @@ -435,21 +438,69 @@ class AdminWalletConfigIssuesView(Credentials): wallet = True -class AdminSchemesView(Schemes): - template_name = "idhub/admin/schemes.html" - subtitle = _('Schemes List') +class AdminSchemasView(SchemasMix): + template_name = "idhub/admin/schemas.html" + subtitle = _('Template List') icon = '' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'schemas': Schemas.objects, + }) + return context -class AdminSchemesImportView(Schemes): - template_name = "idhub/admin/schemes_import.html" - subtitle = _('Import Schemes') + +class AdminSchemasImportView(SchemasMix): + template_name = "idhub/admin/schemas_import.html" + subtitle = _('Import Template') icon = '' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'schemas': self.get_schemas(), + }) + return context -class AdminSchemesExportView(Schemes): - template_name = "idhub/admin/schemes_export.html" - subtitle = _('Export Schemes') + 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 + + +class AdminSchemasImportAddView(SchemasMix): + + 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') + + self.create_schema(file_name) + messages.success(self.request, _("The schema add successfully!")) + return redirect('idhub:admin_schemas_import') + + def create_schema(self, file_name): + data = self.open_file(file_name) + 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 + + +class AdminSchemasExportView(SchemasMix): + template_name = "idhub/admin/schemas_export.html" + subtitle = _('Export Template') icon = '' diff --git a/idhub/migrations/0006_schemas.py b/idhub/migrations/0006_schemas.py new file mode 100644 index 0000000..71362b4 --- /dev/null +++ b/idhub/migrations/0006_schemas.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2.5 on 2023-10-20 13:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("idhub", "0005_remove_service_rol_service_rol"), + ] + + operations = [ + migrations.CreateModel( + name="Schemas", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("file_schema", models.CharField(max_length=250)), + ("data", models.TextField()), + ("created_at", models.DateTimeField(auto_now=True)), + ], + ), + ] diff --git a/idhub/models.py b/idhub/models.py index 3c187b3..96357c4 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -49,6 +49,12 @@ class VCTemplate(models.Model): data = models.TextField() +class Schemas(models.Model): + file_schema = models.CharField(max_length=250) + data = models.TextField() + created_at = models.DateTimeField(auto_now=True) + + class Membership(models.Model): """ This model represent the relation of this user with the ecosystem. diff --git a/idhub/templates/idhub/admin/schemes.html b/idhub/templates/idhub/admin/schemas.html similarity index 100% rename from idhub/templates/idhub/admin/schemes.html rename to idhub/templates/idhub/admin/schemas.html diff --git a/idhub/templates/idhub/admin/schemes_export.html b/idhub/templates/idhub/admin/schemas_export.html similarity index 100% rename from idhub/templates/idhub/admin/schemes_export.html rename to idhub/templates/idhub/admin/schemas_export.html diff --git a/idhub/templates/idhub/admin/schemas_import.html b/idhub/templates/idhub/admin/schemas_import.html new file mode 100644 index 0000000..e301e76 --- /dev/null +++ b/idhub/templates/idhub/admin/schemas_import.html @@ -0,0 +1,34 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+
+
+
+ + + + + + + + + {% for schema in schemas %} + + + + + {% endfor %} + +
{{ schema }}
+ +
+
+
+{% endblock %} diff --git a/idhub/templates/idhub/admin/schemes_import.html b/idhub/templates/idhub/admin/schemes_import.html deleted file mode 100644 index f5849fd..0000000 --- a/idhub/templates/idhub/admin/schemes_import.html +++ /dev/null @@ -1,9 +0,0 @@ -{% extends "idhub/base_admin.html" %} -{% load i18n %} - -{% block content %} -

- - {{ subtitle }} -

-{% endblock %} diff --git a/idhub/templates/idhub/base_admin.html b/idhub/templates/idhub/base_admin.html index 8f8e3aa..22a8874 100644 --- a/idhub/templates/idhub/base_admin.html +++ b/idhub/templates/idhub/base_admin.html @@ -152,24 +152,24 @@