add import datas

This commit is contained in:
Cayo Puigdefabregas 2023-10-23 19:24:48 +02:00
parent e292caf85f
commit 26a11fd84c
8 changed files with 200 additions and 2 deletions

View File

@ -29,3 +29,6 @@ class UserRolForm(forms.ModelForm):
class SchemaForm(forms.Form): class SchemaForm(forms.Form):
file_template = forms.FileField() file_template = forms.FileField()
class ImportForm(forms.Form):
file_import = forms.FileField()

View File

@ -10,6 +10,7 @@ from django.views.generic.edit import UpdateView, CreateView
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.http import HttpResponse
from django.contrib import messages from django.contrib import messages
from idhub.models import Membership, Rol, Service, UserRol, Schemas from idhub.models import Membership, Rol, Service, UserRol, Schemas
from idhub.mixins import AdminView from idhub.mixins import AdminView
@ -21,6 +22,7 @@ from idhub.admin.forms import (
ServiceForm, ServiceForm,
UserRolForm, UserRolForm,
SchemaForm, SchemaForm,
ImportForm,
) )
@ -451,6 +453,27 @@ class AdminSchemasView(SchemasMix):
}) })
return context 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): class AdminSchemasNewView(SchemasMix):
template_name = "idhub/admin/schemas_new.html" template_name = "idhub/admin/schemas_new.html"
@ -550,6 +573,68 @@ class AdminImportView(ImportExport):
subtitle = _('Import') subtitle = _('Import')
icon = '' 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): class AdminExportView(ImportExport):
template_name = "idhub/admin/export.html" template_name = "idhub/admin/export.html"

View File

@ -6,4 +6,29 @@
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </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_step2' schema.id %}" title="{% trans 'Import Dates' %}">{% trans 'Import Dates' %}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %} {% endblock %}

View File

@ -0,0 +1,32 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
{% load django_bootstrap5 %}
<form role="form" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
<button class="close" type="button" data-dismiss="alert" aria-label="Close">
<span class="mdi mdi-close" aria-hidden="true"></span>
</button>
{% for field, error in form.errors.items %}
{{ error }}
{% endfor %}
</div>
</div>
{% endif %}
{% bootstrap_form form %}
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'idhub:admin_import' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View File

@ -6,4 +6,50 @@
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </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_schemas_download' schema.id %}" target="_blank" title="{% trans 'View' %}">{% trans 'View' %}</a></td>
<td><a class="text-danger" href="jacascript:void()" data-bs-toggle="modal" data-bs-target="#confirm-delete-{{ schema.id }}" title="{% trans 'Remove' %}"><i class="bi bi-x-circle"></i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<!-- Modal -->
{% for schema in schemas.all %}
<div class="modal" id="confirm-delete-{{ schema.id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">{% trans 'Delete Template' %} {{ schema.file_schema }}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{% trans 'Are you sure that you want delete this template?' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Clancel</button>
<a href="{% url 'idhub:admin_schemas_del' schema.id %}" type="button" class="btn btn-danger">{% trans 'Delete' %}</a>
</div>
</div>
</div>
</div>
{% endfor %}
{% endblock %} {% endblock %}

View File

@ -152,11 +152,11 @@
</ul> </ul>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<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()"> <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()">
<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 == 'Schemas' %}expanded{% else %}collapse{% endif %}" id="schemas" data-bs-parent="#sidebarMenu"> <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"> <li class="nav-item">
<a class="nav-link{% if path == 'admin_schemas' %} active2{% endif %}" href="{% url 'idhub:admin_schemas' %}"> <a class="nav-link{% if path == 'admin_schemas' %} active2{% endif %}" href="{% url 'idhub:admin_schemas' %}">
List of Templates List of Templates

View File

@ -137,6 +137,10 @@ urlpatterns = [
name='admin_wallet_config_issue'), name='admin_wallet_config_issue'),
path('admin/schemas/', views_admin.AdminSchemasView.as_view(), path('admin/schemas/', views_admin.AdminSchemasView.as_view(),
name='admin_schemas'), name='admin_schemas'),
path('admin/schemas/<int:pk>/del/', views_admin.AdminSchemasDeleteView.as_view(),
name='admin_schemas_del'),
path('admin/schemas/<int:pk>/', views_admin.AdminSchemasDownloadView.as_view(),
name='admin_schemas_download'),
path('admin/schemas/new', views_admin.AdminSchemasNewView.as_view(), path('admin/schemas/new', views_admin.AdminSchemasNewView.as_view(),
name='admin_schemas_new'), name='admin_schemas_new'),
path('admin/schemas/import', views_admin.AdminSchemasImportView.as_view(), path('admin/schemas/import', views_admin.AdminSchemasImportView.as_view(),
@ -147,6 +151,8 @@ urlpatterns = [
name='admin_schemas_export'), 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(),
name='admin_import_step2'),
path('admin/export/', views_admin.AdminExportView.as_view(), path('admin/export/', views_admin.AdminExportView.as_view(),
name='admin_export'), name='admin_export'),
] ]

View File

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