Browse Source

Evolução do CartMix

revisaoSidenav
Sesostris Vieira 3 years ago
parent
commit
c8b003c1b8
  1. 75
      sigi/apps/utils/mixins.py
  2. 43
      sigi/apps/utils/templates/admin/cart/change_list_cart_export.html
  3. 12
      sigi/apps/utils/templates/admin/cart/change_list_cart_item.html
  4. 33
      sigi/apps/utils/templates/admin/cart/export_fields.html
  5. 10
      sigi/apps/utils/templates/admin/import_export/change_list_export_item.html
  6. 47
      sigi/apps/utils/templates/admin/import_export/export.html
  7. 68
      sigi/apps/utils/templates/admin/import_export/export_report.html
  8. 44
      sigi/templates/admin/actions.html
  9. 46
      sigi/templates/admin/change_list.html

75
sigi/apps/utils/mixins.py

@ -4,16 +4,18 @@ from django import forms
from django.contrib import admin from django.contrib import admin
from django.contrib.admin import helpers from django.contrib.admin import helpers
from django.contrib.admin.options import csrf_protect_m 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.http.response import HttpResponse, HttpResponseRedirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
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 ExportMixin
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
from sigi.apps.utils import field_label from sigi.apps.utils import field_label
class ExportFormFields(ExportForm): class ExportFormFields(ExportForm):
def __init__(self, formats, field_list, *args, **kwargs): def __init__(self, formats, field_list, *args, **kwargs):
super().__init__(formats, *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, [])) cart_item_count = len(request.session.get(self._cart_session_name, []))
extra_context = extra_context or {} extra_context = extra_context or {}
extra_context['cart_item_count'] = ( extra_context['cart_item_count'] = cart_item_count
_('Vazio') if cart_item_count == 0
else _(f'{cart_item_count} itens')
)
if self._cart_viewing_name in request.session: if self._cart_viewing_name in request.session:
extra_context['viewing_cart'] = True extra_context['viewing_cart'] = True
return super(CartExportMixin, self).changelist_view(request, extra_context) return super(CartExportMixin, self).changelist_view(request, extra_context)
def get_urls(self): def get_urls(self):
from django.urls import path urls = super().get_urls()
def wrap(view): my_urls = [
def wrapper(*args, **kwargs): path('clearcart/',
return self.admin_site.admin_view(view)(*args, **kwargs) self.admin_site.admin_view(self.clear_cart),
wrapper.model_admin = self name='%s_%s_clearcart' % self.get_model_info()),
return update_wrapper(wrapper, view) ]
return my_urls + urls
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
@csrf_protect_m @csrf_protect_m
def add_to_cart(self, request, queryset): def add_to_cart(self, request, queryset):
@ -177,6 +167,7 @@ class CartExportMixin(ExportMixin):
self.message_user(request, _(u"Carrinho vazio")) self.message_user(request, _(u"Carrinho vazio"))
return HttpResponseRedirect('..') return HttpResponseRedirect('..')
@csrf_protect_m
def export_action(self, request, *args, **kwargs): def export_action(self, request, *args, **kwargs):
if not self.has_export_permission(request): if not self.has_export_permission(request):
raise PermissionDenied raise PermissionDenied
@ -217,3 +208,43 @@ class CartExportMixin(ExportMixin):
request.current_app = self.admin_site.name request.current_app = self.admin_site.name
return TemplateResponse(request, [self.export_template_name], return TemplateResponse(request, [self.export_template_name],
context) 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/<str:name>/',
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)

43
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 %} {% load admin_list admin_urls i18n %}
{% block content_title %} {% block messages %}
{% if viewing_cart %} {{ block.super }}
{% blocktrans with s=opts.verbose_name_plural|lower c=cart_item_count %} {% if viewing_cart %}
<h1>Visualizando carrinho de {{ s }} <span class="badge">{{ c }}</span></h1> <ul>
{% endblocktrans %} <li class="amber lighten-4 flow-text valign-wrapper">
<i class="material-icons medium">shopping_cart</i>
{% if cart_item_count > 0 %}
{% blocktrans with s=opts.verbose_name_plural|lower c=cart_item_count %}
{{ c }} {{ s }} no carrinho
{% endblocktrans %}
{% else %} {% else %}
{% if title %}<h1>{{ title }}</h1>{% endif %} Carrinho vazio
{% endif %} {% endif %}
</li>
</ul>
{% endif %}
{% endblock %} {% endblock %}
{{ block.super }}
{% block object-tools-items %} {% block object-tools-items %}
{% if viewing_cart %} {% if viewing_cart %}
{% include "admin/import_export/change_list_export_item.html" %} {% include "admin/import_export/change_list_export_item.html" %}
{% include "admin/cart/change_list_cart_item.html" %} {% include "admin/cart/change_list_cart_item.html" %}
{% else %} {% else %}
<li><a class="btn" href="?cart=1"><span class="glyphicon glyphicon-shopping-cart"></span>{% blocktrans with c=cart_item_count %}Ver Carrinho <span class="badge">{{ c }}</span>{% endblocktrans %}</a></li> {{ block.super }}
{{ block.super }} <li>
{% endif %} <a class="btn-floating tooltipped waves-effect waves-light" href="?cart=1" data-position="left" data-tooltip="{% trans "Ver itens no carrinho" %}">
<i class="material-icons">shopping_cart</i>
</a>
</li>
{% endif %}
{% endblock %} {% endblock %}

12
sigi/apps/utils/templates/admin/cart/change_list_cart_item.html

@ -1,5 +1,13 @@
{% load i18n %} {% load i18n %}
{% load admin_urls %} {% load admin_urls %}
<li><a role="button" class="btn" href="?cart=0"><span class="glyphicon glyphicon-chevron-left"></span>{% trans 'Voltar à lista completa' %}</a></li> <li>
<li><a role="button" class="btn" href="{% url opts|admin_urlname:'clearcart' %}"><span class="glyphicon glyphicon-trash"></span>{% trans "Esvaziar carrinho" %}</a></li> <a class="btn-floating tooltipped waves-effect waves-light" href="?cart=0" data-position="left" data-tooltip="{% trans 'Voltar à lista completa' %}">
<i class="material-icons">undo</i>
</a>
</li>
<li>
<a class="btn-floating tooltipped waves-effect waves-light" href="{% url opts|admin_urlname:'clearcart' %}" data-position="left" data-tooltip="{% trans "Esvaziar carrinho" %}">
<i class="material-icons">remove_shopping_cart</i>
</a>
</li>

33
sigi/apps/utils/templates/admin/cart/export_fields.html

@ -1,33 +0,0 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_urls %}
{% block coltype %}flex{% endblock %}
{% block content_title %}<h1>{% trans "Exportar para CSV" %}</h1>{% endblock %}
{% block content %}
<form class="form-horizontal" id="changelist-form" action="" method="post">{% csrf_token %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{% trans "Selecione os campos para exportar" %}</h3>
</div>
<div class="panel-body">
<div class="form-group">
{% for field, label in export_fields %}
<div class="checkbox">
<label>
<input type="checkbox" name="selected_fields" value="{{ field }}" checked/>
{{ label }}
</label>
</div>
{% endfor %}
</div>
</div>
<div class="panel-footer">
<div class="form-group">
<button type="submit" class="btn">{% trans "Exportar" %}</button>
<a role="button" class="btn" href="{% url opts|admin_urlname:'changelist' %}">{% trans "Voltar" %}</a>
</div>
</div>
</div>
</div>
</form>
{% endblock %}

10
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 %}
<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" %}">
<i class="material-icons">open_in_new</i>
</a>
</li>
{% endif %}

47
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 }}
<style>
#content {
display: block;
}
.submit-row>a {
color: #fff;
}
</style>
{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block content_title %}<h6>{% blocktrans with name=opts.verbose_name_plural %}Exportar {{ name }}{% endblocktrans %}</h6>{% endblock %}
{% block content %}
<form action="" method="POST" novalidate>{% csrf_token %}
<div class="container">
<fieldset>
{% 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 class="btn waves-effect waves-light" type="submit" name="export">
<i class="material-icons left">done</i>
{% trans "Export" %}
</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>
{% endblock %}

68
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 %}
<div class="row">
<div class="col s12">
<ul class="tabs">
{% block tab-items %}
<li class="tab col"><a href="#export">{% trans "Exportar" %}</a></li>
<li class="tab col"><a href="#report">{% trans "Relatórios" %}</a></li>
{% endblock %}
</ul>
</div>
{% block tabs %}
<div id="export" class="col s12">
<form action="" method="POST" novalidate>{% csrf_token %}
<div class="container">
<fieldset>
{% 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 class="btn waves-effect waves-light" type="submit" name="export">
<i class="material-icons left">done</i>
{% trans "Export" %}
</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>
</div>
</form>
</div>
<div id="report" class="col s12">
<div class="container">
<div class="collection">
{% for report in reports %}
<a class="collection-item" href="{% url opts|admin_urlname:'report' report.name %}">
<i class="material-icons left">{{ report.icon }}</i>
{{ report.title }}
</a>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
</div>
{% endblock %}
{% block footer %}
{{ block.super }}
<script>
$(document).ready(function(){
var instance = M.Tabs.init($('.tabs'), {});
});
</script>
{% endblock %}

44
sigi/templates/admin/actions.html

@ -0,0 +1,44 @@
{% load i18n %}
<div class="actions">
{% block actions %}
<div class="card-content">
{% block actions-form %}
{% for field in action_form %}
{% if field.label %}
<label class="label">{{ field.label }}
{% endif %}
{% if field.field.widget.choices %}
<select form="changelist-form" name="action" required tabindex="-1">
{% for option in field.field.widget.choices %}
<option value="{{ option.0 }}">{{ option.1 }}</option>
{% endfor %}
</select>
{% else %}
<input type="hidden" name="{{ field.name }}" value="{{ field.value }}" class="select-across" form="changelist-form"/>
{% endif %}
{% if field.label %}</label>{% endif %}
{% endfor %}
{% endblock %}
{% block actions-submit %}
{% block actions-counter %}
{% if actions_selection_counter %}
<span class="action-counter" data-actions-icnt="{{ cl.result_list|length }}">{{ selection_note }}</span>
{% if cl.result_count != cl.result_list|length %}
<span class="all">{{ selection_note_all }}</span>
<span class="question">
<a href="#" title="{% trans "Click here to select the objects across all pages" %}">{% blocktrans with cl.result_count as total_count %}Select all {{ total_count }} {{ module_name }}{% endblocktrans %}</a>
</span>
<span class="clear"><a href="#">{% trans "Clear selection" %}</a></span>
{% endif %}
{% endif %}
{% endblock %}
</div>
<div class="card-action">
<div class="right-align">
<button type="submit" form="changelist-form" class="waves-effect waves-light btn action-button" title="{% trans "Run the selected action" %}" name="index" value="{{ action_index|default:0 }}">{% trans "Go" %}</button>
</div>
</div>
{% endblock %}
{% endblock %}
</div>

46
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 %}
<div class="fixed-action-btn">
<a class="btn-floating btn-large">
<i class="large material-icons">build</i>
</a>
<ul>
{% block object-tools-items %}
{% if has_add_permission %}
<li>
{% url cl.opts|admin_urlname:'add' as add_url %}
<a href="{% add_preserved_filters add_url is_popup to_field %}" class="btn-floating tooltipped waves-effect waves-light" data-position="left" data-tooltip="{% trans 'Add' %}">
<i class="material-icons" aria-hidden="true">add</i>
</a>
</li>
{% endif %}
{% endblock %}
</ul>
</div>
{% endblock %}
{% block footer %}
{{ block.super }}
<script>
$(document).ready(function(){
$('.fixed-action-btn').floatingActionButton();
M.Tooltip.init($('.tooltipped'), {});
});
</script>
{% endblock footer %}
{% comment %}
$(window).scroll(function() {
$('.tooltipped')._positionTooltip();
})
<ul>
</ul>
{% endcomment %}
Loading…
Cancel
Save