From c8b003c1b8c94e8d7c9a48252bad064bbab97949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Tue, 8 Feb 2022 11:37:50 -0300 Subject: [PATCH] =?UTF-8?q?Evolu=C3=A7=C3=A3o=20do=20CartMix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/utils/mixins.py | 75 +++++++++++++------ .../admin/cart/change_list_cart_export.html | 43 +++++++---- .../admin/cart/change_list_cart_item.html | 12 ++- .../templates/admin/cart/export_fields.html | 33 -------- .../change_list_export_item.html | 10 +++ .../templates/admin/import_export/export.html | 47 ++++++++++++ .../admin/import_export/export_report.html | 68 +++++++++++++++++ sigi/templates/admin/actions.html | 44 +++++++++++ sigi/templates/admin/change_list.html | 46 ++++++++++++ 9 files changed, 305 insertions(+), 73 deletions(-) delete mode 100644 sigi/apps/utils/templates/admin/cart/export_fields.html create mode 100644 sigi/apps/utils/templates/admin/import_export/change_list_export_item.html create mode 100644 sigi/apps/utils/templates/admin/import_export/export.html create mode 100644 sigi/apps/utils/templates/admin/import_export/export_report.html create mode 100644 sigi/templates/admin/actions.html create mode 100644 sigi/templates/admin/change_list.html diff --git a/sigi/apps/utils/mixins.py b/sigi/apps/utils/mixins.py index 1185f67..cf80194 100644 --- a/sigi/apps/utils/mixins.py +++ b/sigi/apps/utils/mixins.py @@ -4,16 +4,18 @@ from django import forms from django.contrib import admin from django.contrib.admin import helpers from django.contrib.admin.options import csrf_protect_m -from django.core.exceptions import PermissionDenied +from django.contrib.admin.utils import pretty_name +from django.core.exceptions import PermissionDenied, ImproperlyConfigured +from django.http import Http404 from django.http.response import HttpResponse, HttpResponseRedirect from django.template.response import TemplateResponse +from django.urls import path from django.utils.translation import gettext as _, ngettext from import_export import resources from import_export.admin import ExportMixin from import_export.forms import ExportForm from import_export.signals import post_export from sigi.apps.utils import field_label - class ExportFormFields(ExportForm): def __init__(self, formats, field_list, *args, **kwargs): super().__init__(formats, *args, **kwargs) @@ -88,32 +90,20 @@ class CartExportMixin(ExportMixin): cart_item_count = len(request.session.get(self._cart_session_name, [])) extra_context = extra_context or {} - extra_context['cart_item_count'] = ( - _('Vazio') if cart_item_count == 0 - else _(f'{cart_item_count} itens') - ) + extra_context['cart_item_count'] = cart_item_count if self._cart_viewing_name in request.session: extra_context['viewing_cart'] = True return super(CartExportMixin, self).changelist_view(request, extra_context) def get_urls(self): - from django.urls import path - def wrap(view): - def wrapper(*args, **kwargs): - return self.admin_site.admin_view(view)(*args, **kwargs) - wrapper.model_admin = self - return update_wrapper(wrapper, view) - - info = self.model._meta.app_label, self.model._meta.model_name - - paths = super(CartExportMixin, self).get_urls() - paths.insert(2, path( - 'clearcart/', - wrap(self.clear_cart), - name='%s_%s_clearcart' % info - )) - return paths + urls = super().get_urls() + my_urls = [ + path('clearcart/', + self.admin_site.admin_view(self.clear_cart), + name='%s_%s_clearcart' % self.get_model_info()), + ] + return my_urls + urls @csrf_protect_m def add_to_cart(self, request, queryset): @@ -177,6 +167,7 @@ class CartExportMixin(ExportMixin): self.message_user(request, _(u"Carrinho vazio")) return HttpResponseRedirect('..') + @csrf_protect_m def export_action(self, request, *args, **kwargs): if not self.has_export_permission(request): raise PermissionDenied @@ -217,3 +208,43 @@ class CartExportMixin(ExportMixin): request.current_app = self.admin_site.name return TemplateResponse(request, [self.export_template_name], context) + +class CartExportReportMixin(CartExportMixin): + export_template_name = 'admin/import_export/export_report.html' + reports = [] + + def get_urls(self): + urls = super().get_urls() + my_urls = [ + path('report//', + self.admin_site.admin_view(self.report), + name='%s_%s_report' % self.get_model_info()), + ] + return my_urls + urls + + def get_export_context_data(self): + context = super().get_export_context_data() + report_list = [] + for name in self.reports: + report = getattr(self, name, None) + if report is None: + continue + report_list.append( + {'name': name, + 'title': getattr(report, 'title', pretty_name(name)), + 'icon': getattr(report, 'icon', 'picture_as_pdf') + } + ) + + context['reports'] = report_list + + return context + + def report(self, request, name): + if (name not in self.reports or not hasattr(self, name) or + not callable(getattr(self, name))): + raise Http404(_(f"Report {name} not exists")) + + report_view = getattr(self, name) + + return report_view(request) \ No newline at end of file diff --git a/sigi/apps/utils/templates/admin/cart/change_list_cart_export.html b/sigi/apps/utils/templates/admin/cart/change_list_cart_export.html index c0950cc..413711d 100644 --- a/sigi/apps/utils/templates/admin/cart/change_list_cart_export.html +++ b/sigi/apps/utils/templates/admin/cart/change_list_cart_export.html @@ -1,24 +1,35 @@ -{% extends "admin/import_export/change_list_export.html" %} +{% extends "admin/change_list.html" %} {% load admin_list admin_urls i18n %} -{% block content_title %} - {% if viewing_cart %} - {% blocktrans with s=opts.verbose_name_plural|lower c=cart_item_count %} -

Visualizando carrinho de {{ s }} {{ c }}

- {% endblocktrans %} +{% block messages %} + {{ block.super }} + {% if viewing_cart %} + + {% endif %} {% endblock %} - {{ block.super }} {% block object-tools-items %} - {% if viewing_cart %} - {% include "admin/import_export/change_list_export_item.html" %} - {% include "admin/cart/change_list_cart_item.html" %} - {% else %} -
  • {% blocktrans with c=cart_item_count %}Ver Carrinho {{ c }}{% endblocktrans %}
  • - {{ block.super }} - {% endif %} + {% if viewing_cart %} + {% include "admin/import_export/change_list_export_item.html" %} + {% include "admin/cart/change_list_cart_item.html" %} + {% else %} + {{ block.super }} +
  • + + shopping_cart + +
  • + {% endif %} {% endblock %} diff --git a/sigi/apps/utils/templates/admin/cart/change_list_cart_item.html b/sigi/apps/utils/templates/admin/cart/change_list_cart_item.html index c785e3f..7f29765 100644 --- a/sigi/apps/utils/templates/admin/cart/change_list_cart_item.html +++ b/sigi/apps/utils/templates/admin/cart/change_list_cart_item.html @@ -1,5 +1,13 @@ {% load i18n %} {% load admin_urls %} -
  • {% trans 'Voltar à lista completa' %}
  • -
  • {% trans "Esvaziar carrinho" %}
  • +
  • + + undo + +
  • +
  • + + remove_shopping_cart + +
  • diff --git a/sigi/apps/utils/templates/admin/cart/export_fields.html b/sigi/apps/utils/templates/admin/cart/export_fields.html deleted file mode 100644 index 1075eef..0000000 --- a/sigi/apps/utils/templates/admin/cart/export_fields.html +++ /dev/null @@ -1,33 +0,0 @@ -{% extends "admin/base_site.html" %} -{% load i18n admin_urls %} - -{% block coltype %}flex{% endblock %} -{% block content_title %}

    {% trans "Exportar para CSV" %}

    {% endblock %} -{% block content %} -
    {% csrf_token %} -
    -
    -

    {% trans "Selecione os campos para exportar" %}

    -
    -
    -
    - {% for field, label in export_fields %} -
    - -
    - {% endfor %} -
    -
    - -
    - -
    -{% endblock %} \ No newline at end of file diff --git a/sigi/apps/utils/templates/admin/import_export/change_list_export_item.html b/sigi/apps/utils/templates/admin/import_export/change_list_export_item.html new file mode 100644 index 0000000..4fb7ccb --- /dev/null +++ b/sigi/apps/utils/templates/admin/import_export/change_list_export_item.html @@ -0,0 +1,10 @@ +{% load i18n %} +{% load admin_urls %} + +{% if has_export_permission %} +
  • + + open_in_new + +
  • +{% endif %} diff --git a/sigi/apps/utils/templates/admin/import_export/export.html b/sigi/apps/utils/templates/admin/import_export/export.html new file mode 100644 index 0000000..fdcf1ab --- /dev/null +++ b/sigi/apps/utils/templates/admin/import_export/export.html @@ -0,0 +1,47 @@ +{% extends "admin/base_site.html" %} +{% load i18n admin_urls %} + +{% block extrastyle %} + {{ block.super }} + +{% endblock %} + +{% block breadcrumbs %}{% endblock %} +{% block content_title %}
    {% blocktrans with name=opts.verbose_name_plural %}Exportar {{ name }}{% endblocktrans %}
    {% endblock %} +{% block content %} +
    {% csrf_token %} +
    +
    + {% for field in form %} +
    + {{ field.errors }} + + {{ field.label_tag }} + {{ field }} + + {% if field.field.help_text %} +

    {{ field.field.help_text|safe }}

    + {% endif %} +
    + {% endfor %} +
    + +
    + + + navigate_before + {% trans "Voltar" %} + +
    + +{% endblock %} \ No newline at end of file diff --git a/sigi/apps/utils/templates/admin/import_export/export_report.html b/sigi/apps/utils/templates/admin/import_export/export_report.html new file mode 100644 index 0000000..8832ecb --- /dev/null +++ b/sigi/apps/utils/templates/admin/import_export/export_report.html @@ -0,0 +1,68 @@ +{% extends "admin/import_export/export.html" %} +{% load i18n admin_urls %} + +{% block content %} +
    +
    + +
    + {% block tabs %} +
    +
    {% csrf_token %} +
    +
    + {% for field in form %} +
    + {{ field.errors }} + + {{ field.label_tag }} + {{ field }} + + {% if field.field.help_text %} +

    {{ field.field.help_text|safe }}

    + {% endif %} +
    + {% endfor %} +
    +
    + + + navigate_before + {% trans "Voltar" %} + +
    +
    +
    +
    +
    +
    +
    + {% for report in reports %} + + {{ report.icon }} + {{ report.title }} + + {% endfor %} +
    +
    +
    + {% endblock %} +
    +{% endblock %} + +{% block footer %} + {{ block.super }} + +{% endblock %} \ No newline at end of file diff --git a/sigi/templates/admin/actions.html b/sigi/templates/admin/actions.html new file mode 100644 index 0000000..922dc31 --- /dev/null +++ b/sigi/templates/admin/actions.html @@ -0,0 +1,44 @@ +{% load i18n %} + +
    + {% block actions %} +
    + {% block actions-form %} + {% for field in action_form %} + {% if field.label %} + {% endif %} + {% endfor %} + {% endblock %} + {% block actions-submit %} + {% block actions-counter %} + {% if actions_selection_counter %} + {{ selection_note }} + {% if cl.result_count != cl.result_list|length %} + {{ selection_note_all }} + + {% blocktrans with cl.result_count as total_count %}Select all {{ total_count }} {{ module_name }}{% endblocktrans %} + + {% trans "Clear selection" %} + {% endif %} + {% endif %} + {% endblock %} +
    +
    +
    + +
    +
    + {% endblock %} + {% endblock %} +
    \ No newline at end of file diff --git a/sigi/templates/admin/change_list.html b/sigi/templates/admin/change_list.html new file mode 100644 index 0000000..1675527 --- /dev/null +++ b/sigi/templates/admin/change_list.html @@ -0,0 +1,46 @@ +{% extends "admin/change_list.html" %} +{% load i18n static admin_list %} +{% load i18n admin_urls %} + +{% block breadcrumbs %}{% endblock %} + +{% block object-tools %} +
    + + build + +
      + {% block object-tools-items %} + {% if has_add_permission %} +
    • + {% url cl.opts|admin_urlname:'add' as add_url %} + + + +
    • + {% endif %} + {% endblock %} +
    +
    +{% endblock %} + +{% block footer %} + {{ block.super }} + +{% endblock footer %} + + + +{% comment %} +$(window).scroll(function() { + $('.tooltipped')._positionTooltip(); +}) + +
      +
    +{% endcomment %} \ No newline at end of file