diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 478a192..341cd90 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -1,4 +1,5 @@ import os +import json import logging from pathlib import Path from smtplib import SMTPException @@ -509,7 +510,12 @@ class AdminSchemasNewView(SchemasMix): if Schemas.objects.filter(file_schema=file_name).exists(): messages.error(self.request, _("This template already exists!")) return - data = f.read().decode('utf-8') + try: + data = f.read().decode('utf-8') + json.loads(data) + except Exception: + messages.error(self.request, _('This is not a schema valid!')) + return schema = Schemas.objects.create(file_schema=file_name, data=data) schema.save() return schema @@ -543,12 +549,18 @@ class AdminSchemasImportAddView(SchemasMix): 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!")) + schema = self.create_schema(file_name) + if schema: + 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) + try: + json.loads(data) + except Exception: + messages.error(self.request, _('This is not a schema valid!')) + return schema = Schemas.objects.create(file_schema=file_name, data=data) schema.save() return schema @@ -562,17 +574,24 @@ class AdminSchemasImportAddView(SchemasMix): return data -class AdminSchemasExportView(SchemasMix): - template_name = "idhub/admin/schemas_export.html" - subtitle = _('Export Template') - icon = '' - - class AdminImportView(ImportExport): template_name = "idhub/admin/import.html" subtitle = _('Import') icon = '' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'dates': [], + }) + return context + + +class AdminImportStep2View(ImportExport): + template_name = "idhub/admin/import_step2.html" + subtitle = _('Import') + icon = '' + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ @@ -580,8 +599,9 @@ class AdminImportView(ImportExport): }) return context -class AdminImportStep2View(ImportExport): - template_name = "idhub/admin/import_new.html" + +class AdminImportStep3View(ImportExport): + template_name = "idhub/admin/import_step3.html" subtitle = _('Import') icon = '' success_url = reverse_lazy('idhub:admin_import') @@ -595,7 +615,7 @@ class AdminImportStep2View(ImportExport): def post(self, request, *args, **kwargs): self.pk = kwargs['pk'] - self.schema = get_object_or_404(Schema, pk=self.pk) + self.schema = get_object_or_404(Schemas, pk=self.pk) form = ImportForm(request.POST, request.FILES) if form.is_valid(): schema = self.handle_uploaded_file() @@ -610,12 +630,12 @@ class AdminImportStep2View(ImportExport): def handle_uploaded_file(self): f = self.request.FILES.get('file_import') - data = f.read().decode('utf-8') - if not f: - return + # if not f: + # return + + # data = f.read().decode('utf-8') from jsonschema import validate - import json import csv import pandas as pd # import pdb; pdb.set_trace() @@ -632,11 +652,10 @@ class AdminImportStep2View(ImportExport): for k in data_pd.keys(): row[k] = data_pd[k][n] - validate(instance=row, schema=schema) + try: + validate(instance=row, schema=schema) + except Exception as e: + messages.error(self.request, e) + return return - -class AdminExportView(ImportExport): - template_name = "idhub/admin/export.html" - subtitle = _('Export') - icon = '' diff --git a/idhub/templates/idhub/admin/import.html b/idhub/templates/idhub/admin/import.html index 1f933b3..975d289 100644 --- a/idhub/templates/idhub/admin/import.html +++ b/idhub/templates/idhub/admin/import.html @@ -13,21 +13,23 @@ - - + - {% for schema in schemas.all %} + {% for f in dates.all %} - {{ schema.created_at }} - {{ schema.file_schema }} - {% trans 'Import Dates' %} + {{ f.created_at }} + {{ f.file_name }} + {% endfor %} +
+ {% translate "Import Datas" %} +
diff --git a/idhub/templates/idhub/admin/import_step2.html b/idhub/templates/idhub/admin/import_step2.html new file mode 100644 index 0000000..f18821f --- /dev/null +++ b/idhub/templates/idhub/admin/import_step2.html @@ -0,0 +1,34 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ 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_step3.html similarity index 100% rename from idhub/templates/idhub/admin/import_new.html rename to idhub/templates/idhub/admin/import_step3.html diff --git a/idhub/templates/idhub/admin/schemas.html b/idhub/templates/idhub/admin/schemas.html index 9039c11..8b799fe 100644 --- a/idhub/templates/idhub/admin/schemas.html +++ b/idhub/templates/idhub/admin/schemas.html @@ -29,6 +29,9 @@ {% endfor %} +
+ {% translate "Add Template" %} +
diff --git a/idhub/templates/idhub/base_admin.html b/idhub/templates/idhub/base_admin.html index a28e995..e496825 100644 --- a/idhub/templates/idhub/base_admin.html +++ b/idhub/templates/idhub/base_admin.html @@ -152,45 +152,16 @@ diff --git a/idhub/urls.py b/idhub/urls.py index 0038ca0..3d219a4 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -147,12 +147,10 @@ urlpatterns = [ name='admin_schemas_import'), path('admin/schemas/import/', 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/import//', views_admin.AdminImportStep2View.as_view(), + path('admin/import/new', views_admin.AdminImportStep2View.as_view(), name='admin_import_step2'), - path('admin/export/', views_admin.AdminExportView.as_view(), - name='admin_export'), + path('admin/import//', views_admin.AdminImportStep3View.as_view(), + name='admin_import_step3'), ] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e3b8acf --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,6 @@ +[tool.black] +skip-string-normalization = true +target-version = ['py311'] + +[tool.isort] +profile = "black"