Browse Source

Melhorias no sistema de import/export

pull/159/head
Sesostris Vieira 3 years ago
parent
commit
7d84c4d471
  1. 11
      sigi/apps/utils/mixins.py
  2. 6
      sigi/apps/utils/templates/admin/cart/change_list_import_cart_export.html
  3. 1
      sigi/apps/utils/templates/admin/import_export/change_list.html
  4. 2
      sigi/apps/utils/templates/admin/import_export/change_list_export_item.html
  5. 10
      sigi/apps/utils/templates/admin/import_export/change_list_import_item.html
  6. 2
      sigi/apps/utils/templates/admin/import_export/export.html
  7. 165
      sigi/apps/utils/templates/admin/import_export/import.html

11
sigi/apps/utils/mixins.py

@ -12,7 +12,7 @@ from django.template.response import TemplateResponse
from django.urls import path from django.urls import path
from django.utils.translation import gettext as _, ngettext from django.utils.translation import gettext as _, ngettext
from import_export import resources from import_export import resources
from import_export.admin import ExportMixin from import_export.admin import ImportMixin, ExportMixin
from import_export.fields import Field from import_export.fields import Field
from import_export.forms import ExportForm from import_export.forms import ExportForm
from import_export.signals import post_export from import_export.signals import post_export
@ -243,6 +243,15 @@ class CartExportMixin(ExportMixin):
return TemplateResponse(request, [self.export_template_name], context) return TemplateResponse(request, [self.export_template_name], context)
class ImportCartExportMixin(ImportMixin, CartExportMixin):
"""
Import and export mixin.
"""
#: template for change_list view
change_list_template = "admin/cart/change_list_import_cart_export.html"
class CartExportReportMixin(CartExportMixin): class CartExportReportMixin(CartExportMixin):
export_template_name = "admin/import_export/export_report.html" export_template_name = "admin/import_export/export_report.html"
reports = [] reports = []

6
sigi/apps/utils/templates/admin/cart/change_list_import_cart_export.html

@ -0,0 +1,6 @@
{% extends "admin/cart/change_list_cart_export.html" %}
{% block object-tools-items %}
{% include "admin/import_export/change_list_import_item.html" %}
{{ block.super }}
{% endblock %}

1
sigi/apps/utils/templates/admin/import_export/change_list.html

@ -0,0 +1 @@
{% extends "admin/change_list.html" %}

2
sigi/apps/utils/templates/admin/import_export/change_list_export_item.html

@ -4,7 +4,7 @@
{% if has_export_permission %} {% if has_export_permission %}
<li> <li>
<a class="btn-floating tooltipped waves-effect waves-light" href="{% url opts|admin_urlname:'export' %}{{cl.get_query_string}}" data-position="left" data-tooltip="{% trans "Export" %}"> <a class="btn-floating tooltipped waves-effect waves-light" href="{% url opts|admin_urlname:'export' %}{{cl.get_query_string}}" data-position="left" data-tooltip="{% trans "Export" %}">
<i class="material-icons">open_in_new</i> <i class="material-icons">file_download</i>
</a> </a>
</li> </li>
{% endif %} {% endif %}

10
sigi/apps/utils/templates/admin/import_export/change_list_import_item.html

@ -0,0 +1,10 @@
{% load i18n %}
{% load admin_urls %}
{% if has_import_permission %}
<li>
<a class="btn-floating tooltipped waves-effect waves-light" href="{% url opts|admin_urlname:'import' %}{{cl.get_query_string}}" data-position="left" data-tooltip="{% trans "Import" %}">
<i class="material-icons">file_upload</i>
</a>
</li>
{% endif %}

2
sigi/apps/utils/templates/admin/import_export/export.html

@ -14,7 +14,7 @@
{% endblock %} {% endblock %}
{% block breadcrumbs %}{% endblock %} {% block breadcrumbs %}{% endblock %}
{% block content_title %}<h6>{% blocktrans with name=opts.verbose_name_plural %}Exportar {{ name }}{% endblocktrans %}</h6>{% endblock %} {% block content_title %}<h4>{% blocktrans with name=opts.verbose_name_plural %}Exportar {{ name }}{% endblocktrans %}</h4>{% endblock %}
{% block content %} {% block content %}
<form action="" method="POST" novalidate>{% csrf_token %} <form action="" method="POST" novalidate>{% csrf_token %}
<div class="container"> <div class="container">

165
sigi/apps/utils/templates/admin/import_export/import.html

@ -0,0 +1,165 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% load admin_urls %}
{% load import_export_tags %}
{% load static %}
{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "import_export/import.css" %}" />
<style>
#content {
display: block;
}
.submit-row>a {
color: #fff;
}
</style>
{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block content_title %}<h4>{% blocktrans with name=opts.verbose_name_plural %}Importar {{ name }}{% endblocktrans %}</h4>{% endblock %}
{% block content %}
{% if confirm_form %}
<form action="{% url opts|admin_urlname:"process_import" %}" method="POST">{% csrf_token %}
{{ confirm_form.as_p }}
<p>
{% trans "Below is a preview of data to be imported. If you are satisfied with the results, click 'Confirm import'" %}
</p>
<div class="submit-row">
<input type="submit" class="default" name="confirm" value="{% trans "Confirm import" %}">
</div>
</form>
{% else %}
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
<p>
{% trans "This importer will import the following fields: " %}
<code>{{ fields|join:", " }}</code>
</p>
<fieldset class="module aligned">
{% for field in form %}
<div class="form-row">
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
{% if field.field.help_text %}
<p class="help">{{ field.field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
</fieldset>
<div class="submit-row">
<button type="submit" class="btn waves-effect waves-light" value="{% trans "Submit" %}">
<i class="material-icons left">done</i>
{% trans "Import" %}
</button>
<a class="btn waves-effect waves-light" href="{% url opts|admin_urlname:'changelist' %}">
<i class="material-icons left">navigate_before</i>
{% trans "Voltar" %}
</a>
</div>
</form>
{% endif %}
{% if result %}
{% if result.has_errors %}
<h5>{% trans "Errors" %}</h5>
{% for error in result.base_errors %}
<blockquote>
{{ error.error }}
<div class="traceback">{{ error.traceback|linebreaks }}</div>
</blockquote>
{% endfor %}
{% for line, errors in result.row_errors %}
{% for error in errors %}
<blockquote>
{% trans "Line number" %}: {{ line }} - {{ error.error }}
<div><code>{{ error.row.values|join:", " }}</code></div>
<div class="traceback">{{ error.traceback|linebreaks }}</div>
</blockquote>
{% endfor %}
{% endfor %}
{% elif result.has_validation_errors %}
<h5>{% trans "Some rows failed to validate" %}</h5>
<p>{% trans "Please correct these errors in your data where possible, then reupload it using the form above." %}</p>
<table class="striped import-preview">
<thead>
<tr>
<th>{% trans "Row" %}</th>
<th>{% trans "Errors" %}</th>
{% for field in result.diff_headers %}
<th>{{ field }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in result.invalid_rows %}
<tr>
<td>{{ row.number }} </td>
<td class="errors">
<span class="validation-error-count">{{ row.error_count }}</span>
<div class="validation-error-container">
<ul class="validation-error-list">
{% for field_name, error_list in row.field_specific_errors.items %}
<li>
<span class="validation-error-field-label">{{ field_name }}</span>
<ul>
{% for error in error_list %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
{% if row.non_field_specific_errors %}
<li>
<span class="validation-error-field-label">{% trans "Non field specific" %}</span>
<ul>
{% for error in row.non_field_specific_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</li>
{% endif %}
</ul>
</div>
</td>
{% for field in row.values %}
<td>{{ field }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<h5>{% trans "Preview" %}</h5>
<table class="striped import-preview">
<thead>
<tr>
<th></th>
{% for field in result.diff_headers %}
<th>{{ field }}</th>
{% endfor %}
</tr>
</thead>
{% for row in result.valid_rows %}
<tr class="{{ row.import_type }}">
<td class="import-type">
{% if row.import_type == 'new' %}
{% trans "New" %}
{% elif row.import_type == 'skip' %}
{% trans "Skipped" %}
{% elif row.import_type == 'delete' %}
{% trans "Delete" %}
{% elif row.import_type == 'update' %}
{% trans "Update" %}
{% endif %}
</td>
{% for field in row.diff %}
<td>{{ field }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
{% endif %}
{% endblock %}
Loading…
Cancel
Save