diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 9660341..5270aca 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -29,3 +29,6 @@ class UserRolForm(forms.ModelForm): class SchemaForm(forms.Form): file_template = forms.FileField() + +class ImportForm(forms.Form): + file_import = forms.FileField() diff --git a/idhub/admin/views.py b/idhub/admin/views.py index c0ac73a..478a192 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -10,6 +10,7 @@ from django.views.generic.edit import UpdateView, CreateView from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy +from django.http import HttpResponse from django.contrib import messages from idhub.models import Membership, Rol, Service, UserRol, Schemas from idhub.mixins import AdminView @@ -21,6 +22,7 @@ from idhub.admin.forms import ( ServiceForm, UserRolForm, SchemaForm, + ImportForm, ) @@ -451,6 +453,27 @@ class AdminSchemasView(SchemasMix): }) return context + +class AdminSchemasDeleteView(SchemasMix): + + 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') + + +class AdminSchemasDownloadView(SchemasMix): + + 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 + class AdminSchemasNewView(SchemasMix): template_name = "idhub/admin/schemas_new.html" @@ -550,6 +573,68 @@ class AdminImportView(ImportExport): subtitle = _('Import') icon = '' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'schemas': Schemas.objects, + }) + return context + +class AdminImportStep2View(ImportExport): + template_name = "idhub/admin/import_new.html" + 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'] + self.schema = get_object_or_404(Schema, pk=self.pk) + form = ImportForm(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_import') + data = f.read().decode('utf-8') + if not f: + return + + from jsonschema import validate + import json + import csv + import pandas as pd + # import pdb; pdb.set_trace() + + schema = json.loads(self.schema.data) + df = pd.read_csv (r'examples/import1.csv', delimiter="\t", quotechar='"', quoting=csv.QUOTE_ALL) + data_pd = df.fillna('').to_dict() + + if not data_pd: + return + + for n in range(df.last_valid_index()+1): + row = {} + for k in data_pd.keys(): + row[k] = data_pd[k][n] + + validate(instance=row, schema=schema) + return + class AdminExportView(ImportExport): template_name = "idhub/admin/export.html" diff --git a/idhub/templates/idhub/admin/import.html b/idhub/templates/idhub/admin/import.html index f5849fd..1f933b3 100644 --- a/idhub/templates/idhub/admin/import.html +++ b/idhub/templates/idhub/admin/import.html @@ -6,4 +6,29 @@ {{ subtitle }} +
+
+
+ + + + + + + + + + + {% for schema in schemas.all %} + + + + + + {% endfor %} + +
{{ schema.created_at }}{{ schema.file_schema }}{% trans 'Import Dates' %}
+
+
+
{% endblock %} diff --git a/idhub/templates/idhub/admin/import_new.html b/idhub/templates/idhub/admin/import_new.html new file mode 100644 index 0000000..7cb6270 --- /dev/null +++ b/idhub/templates/idhub/admin/import_new.html @@ -0,0 +1,32 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +{% bootstrap_form form %} +
+ {% translate "Cancel" %} + +
+ +
+{% endblock %} diff --git a/idhub/templates/idhub/admin/schemas.html b/idhub/templates/idhub/admin/schemas.html index f5849fd..9039c11 100644 --- a/idhub/templates/idhub/admin/schemas.html +++ b/idhub/templates/idhub/admin/schemas.html @@ -6,4 +6,50 @@ {{ subtitle }} +
+
+
+ + + + + + + + + + + {% for schema in schemas.all %} + + + + + + + {% endfor %} + +
{{ schema.created_at }}{{ schema.file_schema }}{% trans 'View' %}
+
+
+
+ +{% for schema in schemas.all %} + +{% endfor %} {% endblock %} diff --git a/idhub/templates/idhub/base_admin.html b/idhub/templates/idhub/base_admin.html index 22a8874..a28e995 100644 --- a/idhub/templates/idhub/base_admin.html +++ b/idhub/templates/idhub/base_admin.html @@ -152,11 +152,11 @@