rebuild flow of import datas

This commit is contained in:
Cayo Puigdefabregas 2023-10-24 14:08:24 +02:00
parent 26a11fd84c
commit e7e17496d5
8 changed files with 98 additions and 65 deletions

View File

@ -1,4 +1,5 @@
import os import os
import json
import logging import logging
from pathlib import Path from pathlib import Path
from smtplib import SMTPException from smtplib import SMTPException
@ -509,7 +510,12 @@ class AdminSchemasNewView(SchemasMix):
if Schemas.objects.filter(file_schema=file_name).exists(): if Schemas.objects.filter(file_schema=file_name).exists():
messages.error(self.request, _("This template already exists!")) messages.error(self.request, _("This template already exists!"))
return return
try:
data = f.read().decode('utf-8') 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 = Schemas.objects.create(file_schema=file_name, data=data)
schema.save() schema.save()
return schema return schema
@ -543,12 +549,18 @@ class AdminSchemasImportAddView(SchemasMix):
messages.error(self.request, f"The schema {file_name} not exist!") messages.error(self.request, f"The schema {file_name} not exist!")
return redirect('idhub:admin_schemas_import') return redirect('idhub:admin_schemas_import')
self.create_schema(file_name) schema = self.create_schema(file_name)
if schema:
messages.success(self.request, _("The schema add successfully!")) messages.success(self.request, _("The schema add successfully!"))
return redirect('idhub:admin_schemas_import') return redirect('idhub:admin_schemas_import')
def create_schema(self, file_name): def create_schema(self, file_name):
data = self.open_file(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 = Schemas.objects.create(file_schema=file_name, data=data)
schema.save() schema.save()
return schema return schema
@ -562,17 +574,24 @@ class AdminSchemasImportAddView(SchemasMix):
return data return data
class AdminSchemasExportView(SchemasMix):
template_name = "idhub/admin/schemas_export.html"
subtitle = _('Export Template')
icon = ''
class AdminImportView(ImportExport): class AdminImportView(ImportExport):
template_name = "idhub/admin/import.html" template_name = "idhub/admin/import.html"
subtitle = _('Import') subtitle = _('Import')
icon = '' 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): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context.update({ context.update({
@ -580,8 +599,9 @@ class AdminImportView(ImportExport):
}) })
return context return context
class AdminImportStep2View(ImportExport):
template_name = "idhub/admin/import_new.html" class AdminImportStep3View(ImportExport):
template_name = "idhub/admin/import_step3.html"
subtitle = _('Import') subtitle = _('Import')
icon = '' icon = ''
success_url = reverse_lazy('idhub:admin_import') success_url = reverse_lazy('idhub:admin_import')
@ -595,7 +615,7 @@ class AdminImportStep2View(ImportExport):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
self.pk = kwargs['pk'] 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) form = ImportForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
schema = self.handle_uploaded_file() schema = self.handle_uploaded_file()
@ -610,12 +630,12 @@ class AdminImportStep2View(ImportExport):
def handle_uploaded_file(self): def handle_uploaded_file(self):
f = self.request.FILES.get('file_import') f = self.request.FILES.get('file_import')
data = f.read().decode('utf-8') # if not f:
if not f: # return
return
# data = f.read().decode('utf-8')
from jsonschema import validate from jsonschema import validate
import json
import csv import csv
import pandas as pd import pandas as pd
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
@ -632,11 +652,10 @@ class AdminImportStep2View(ImportExport):
for k in data_pd.keys(): for k in data_pd.keys():
row[k] = data_pd[k][n] row[k] = data_pd[k][n]
try:
validate(instance=row, schema=schema) validate(instance=row, schema=schema)
except Exception as e:
messages.error(self.request, e)
return
return return
class AdminExportView(ImportExport):
template_name = "idhub/admin/export.html"
subtitle = _('Export')
icon = ''

View File

@ -13,21 +13,23 @@
<thead> <thead>
<tr> <tr>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Created at' %}</button></th> <th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Created at' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Template file' %}</button></th> <th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'File' %}</button></th>
<th scope="col"></th>
<th scope="col"></th> <th scope="col"></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for schema in schemas.all %} {% for f in dates.all %}
<tr style="font-size:15px;"> <tr style="font-size:15px;">
<td>{{ schema.created_at }}</td> <td>{{ f.created_at }}</td>
<td>{{ schema.file_schema }}</td> <td>{{ f.file_name }}</td>
<td><a class="btn btn-green-admin" href="{% url 'idhub:admin_import_step2' schema.id %}" title="{% trans 'Import Dates' %}">{% trans 'Import Dates' %}</a></td> <td></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<div class="form-actions-no-box">
<a class="btn btn-green-admin" href="{% url 'idhub:admin_import_step2' %}">{% translate "Import Datas" %} <i class="bi bi-plus"></i></a>
</div>
</div> </div>
</div> </div>
</div> </div>

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 'Created at' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Template file' %}</button></th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for schema in schemas.all %}
<tr style="font-size:15px;">
<td>{{ schema.created_at }}</td>
<td>{{ schema.file_schema }}</td>
<td><a class="btn btn-green-admin" href="{% url 'idhub:admin_import_step3' schema.id %}" title="{% trans 'Import Dates' %}">{% trans 'Import Dates' %}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}

View File

@ -29,6 +29,9 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<div class="form-actions-no-box">
<a class="btn btn-green-admin" href="{% url 'idhub:admin_schemas_import' %}">{% translate "Add Template" %} <i class="bi bi-plus"></i></a>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -152,45 +152,16 @@
</ul> </ul>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="admin nav-link {% if section == 'Templates' %}active {% endif %}fw-bold" data-bs-toggle="collapse" data-bs-target="#schemas" aria-expanded="false" aria-controls="schemas" href="javascript:void()"> <a class="admin nav-link {% if section == 'Templates' %}active {% endif %}fw-bold" href="{% url 'idhub:admin_schemas' %}">
<i class="bi bi-file-earmark-text icon_sidebar"></i> <i class="bi bi-file-earmark-text icon_sidebar"></i>
Templates Templates
</a> </a>
<ul class="flex-column mb-2 ul_sidebar accordion-collapse {% if section == 'Templates' %}expanded{% else %}collapse{% endif %}" id="schemas" data-bs-parent="#sidebarMenu">
<li class="nav-item">
<a class="nav-link{% if path == 'admin_schemas' %} active2{% endif %}" href="{% url 'idhub:admin_schemas' %}">
List of Templates
</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link{% if path == 'admin_schemas_import' %} active2{% endif %}" href="{% url 'idhub:admin_schemas_import' %}"> <a class="admin nav-link {% if section == 'ImportExport' %}active {% endif %}fw-bold" href="{% url 'idhub:admin_import' %}">
Import Templates
</a>
</li>
<li class="nav-item">
<a class="nav-link{% if path == 'admin_schemas_export' %} active2{% endif %}" href="{% url 'idhub:admin_schemas_export' %}">
Export Templates
</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="admin nav-link {% if section == 'ImportExport' %}active {% endif %}fw-bold" data-bs-toggle="collapse" data-bs-target="#import-export" aria-expanded="false" aria-controls="import-export" href="javascript:void()">
<i class="bi bi-arrow-down-square icon_sidebar"></i> <i class="bi bi-arrow-down-square icon_sidebar"></i>
Imports/Exports Import Datas
</a> </a>
<ul class="flex-column mb-2 ul_sidebar accordion-collapse {% if section == 'ImportExport' %}expanded{% else %}collapse{% endif %}" id="import-export" data-bs-parent="#sidebarMenu">
<li class="nav-item">
<a class="nav-link{% if path == 'admin_import' %} active2{% endif %}" href="{% url 'idhub:admin_import' %}">
Import
</a>
</li>
<li class="nav-item">
<a class="nav-link{% if path == 'admin_export' %} active2{% endif %}" href="{% url 'idhub:admin_export' %}">
Export
</a>
</li>
</ul>
</li> </li>
</ul> </ul>
</div> </div>

View File

@ -147,12 +147,10 @@ urlpatterns = [
name='admin_schemas_import'), name='admin_schemas_import'),
path('admin/schemas/import/<str:file_schema>', views_admin.AdminSchemasImportAddView.as_view(), path('admin/schemas/import/<str:file_schema>', views_admin.AdminSchemasImportAddView.as_view(),
name='admin_schemas_import_add'), 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(), path('admin/import', views_admin.AdminImportView.as_view(),
name='admin_import'), name='admin_import'),
path('admin/import/<int:pk>/', views_admin.AdminImportStep2View.as_view(), path('admin/import/new', views_admin.AdminImportStep2View.as_view(),
name='admin_import_step2'), name='admin_import_step2'),
path('admin/export/', views_admin.AdminExportView.as_view(), path('admin/import/<int:pk>/', views_admin.AdminImportStep3View.as_view(),
name='admin_export'), name='admin_import_step3'),
] ]

6
pyproject.toml Normal file
View File

@ -0,0 +1,6 @@
[tool.black]
skip-string-normalization = true
target-version = ['py311']
[tool.isort]
profile = "black"