save a standar schema

This commit is contained in:
Cayo Puigdefabregas 2023-10-20 16:50:53 +02:00
parent 233d8dfcc6
commit 5fbffa873c
12 changed files with 192 additions and 37 deletions

View File

@ -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 = ''

View File

@ -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)),
],
),
]

View File

@ -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.

View File

@ -0,0 +1,34 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
<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 'Template available' %}</button></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for schema in schemas %}
<tr style="font-size:15px;">
<td>{{ schema }}</td>
<td><a class="text-primary" href="{% url 'idhub:admin_schemas_import_add' schema %}" title="{% trans 'Add' %}"><i class="bi bi-plus-circle"></i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="form-actions-no-box">
<a class="btn btn-green-admin" href="{# url 'idhub:admin_schemas_new' #}">{% translate "Add template" %} <i class="bi bi-plus"></i></a>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -1,9 +0,0 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
{% endblock %}

View File

@ -152,24 +152,24 @@
</ul>
</li>
<li class="nav-item">
<a class="admin nav-link {% if section == 'Schemes' %}active {% endif %}fw-bold" data-bs-toggle="collapse" data-bs-target="#schemes" aria-expanded="false" aria-controls="schemes" href="javascript:void()">
<a class="admin nav-link {% if section == 'Schemas' %}active {% endif %}fw-bold" data-bs-toggle="collapse" data-bs-target="#schemas" aria-expanded="false" aria-controls="schemas" href="javascript:void()">
<i class="bi bi-file-earmark-text icon_sidebar"></i>
Schemes
Templates
</a>
<ul class="flex-column mb-2 ul_sidebar accordion-collapse {% if section == 'Schemes' %}expanded{% else %}collapse{% endif %}" id="schemes" data-bs-parent="#sidebarMenu">
<ul class="flex-column mb-2 ul_sidebar accordion-collapse {% if section == 'Schemas' %}expanded{% else %}collapse{% endif %}" id="schemas" data-bs-parent="#sidebarMenu">
<li class="nav-item">
<a class="nav-link{% if path == 'admin_schemes' %} active2{% endif %}" href="{% url 'idhub:admin_schemes' %}">
List of schemes
<a class="nav-link{% if path == 'admin_schemas' %} active2{% endif %}" href="{% url 'idhub:admin_schemas' %}">
List of Templates
</a>
</li>
<li class="nav-item">
<a class="nav-link{% if path == 'admin_schemes_import' %} active2{% endif %}" href="{% url 'idhub:admin_schemes_import' %}">
Import scheme
<a class="nav-link{% if path == 'admin_schemas_import' %} active2{% endif %}" href="{% url 'idhub:admin_schemas_import' %}">
Import Templates
</a>
</li>
<li class="nav-item">
<a class="nav-link{% if path == 'admin_schemes_export' %} active2{% endif %}" href="{% url 'idhub:admin_schemes_export' %}">
Export scheme
<a class="nav-link{% if path == 'admin_schemas_export' %} active2{% endif %}" href="{% url 'idhub:admin_schemas_export' %}">
Export Templates
</a>
</li>
</ul>

View File

@ -135,12 +135,14 @@ urlpatterns = [
name='admin_wallet_config_issue'),
path('admin/wallet/config/issue/', views_admin.AdminWalletConfigIssuesView.as_view(),
name='admin_wallet_config_issue'),
path('admin/schemes/', views_admin.AdminSchemesView.as_view(),
name='admin_schemes'),
path('admin/schemes/import', views_admin.AdminSchemesImportView.as_view(),
name='admin_schemes_import'),
path('admin/schemes/export/', views_admin.AdminSchemesExportView.as_view(),
name='admin_schemes_export'),
path('admin/schemas/', views_admin.AdminSchemasView.as_view(),
name='admin_schemas'),
path('admin/schemas/import', views_admin.AdminSchemasImportView.as_view(),
name='admin_schemas_import'),
path('admin/schemas/import/<str:file_schema>', views_admin.AdminSchemasImportAddView.as_view(),
name='admin_schemas_import_add'),
path('admin/schemas/export/', views_admin.AdminSchemasExportView.as_view(),
name='admin_schemas_export'),
path('admin/import', views_admin.AdminImportView.as_view(),
name='admin_import'),
path('admin/export/', views_admin.AdminExportView.as_view(),

View File

@ -3,3 +3,4 @@ django-bootstrap5==23.3
django-extensions==3.2.3
black==23.9.1
python-decouple==3.8
jsonschema==4.19.1

40
schemas/member.json Normal file
View File

@ -0,0 +1,40 @@
{
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://www.w3.org/ns/credentials/examples/v2"
],
"id": "https://example.com/credentials/3734",
"type": ["VerifiableCredential", "JsonSchemaCredential"],
"issuer": "https://pangea.org/issuers/10",
"issuanceDate": "2023-09-01T19:23:24Z",
"credentialSchema": {
"id": "https://www.w3.org/2022/credentials/v2/json-schema-credential-schema.json",
"type": "JsonSchema",
"digestSRI": "sha384-S57yQDg1MTzF56Oi9DbSQ14u7jBy0RDdx0YbeV7shwhCS88G8SCXeFq82PafhCrW"
},
"credentialSubject": {
"id": "https://pangea.org/schemas/member-credential-schema.json",
"type": "JsonSchema",
"jsonSchema": {
"$id": "https://pangea.org/schemas/member-credential-schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"name": "MemberCredential",
"description": "MemberCredential using JsonSchemaCredential",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
},
"membershipType": {
"type": "string",
"enum": ["individual", "organization"]
}
},
"required": ["name", "email", "membershipType"]
}
}
}

View File

@ -159,6 +159,7 @@ MEDIA_URL = '/media/'
STATIC_ROOT = config('STATIC_ROOT')
MEDIA_ROOT = config('MEDIA_ROOT', default="idhub/upload")
FIXTURE_DIRS = (os.path.join(BASE_DIR, 'fixtures'),)
SCHEMAS_DIR = os.path.join(BASE_DIR, 'schemas')
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field