Browse Source

Merge pull request #249 from interlegis/232-templates-crud-usando-includes

Fix #232
pull/250/head
Marcio Mazza 9 years ago
parent
commit
c116704745
  1. 29
      crud.py
  2. 8
      crud_tests/templates/base.html
  3. 6
      crud_tests/test_flux.py
  4. 6
      templates/base.html
  5. 8
      templates/crud/confirm_delete.html
  6. 1
      templates/crud/detail.html
  7. 42
      templates/crud/list.html

29
crud.py

@ -88,10 +88,6 @@ class CrudListMixin():
paginate_by = 10 paginate_by = 10
no_entries_msg = _('Nenhum registro encontrado.') no_entries_msg = _('Nenhum registro encontrado.')
@property
def title(self):
return self.verbose_name_plural
def _as_row(self, obj): def _as_row(self, obj):
return [ return [
(get_field_display(obj, name)[1], obj.pk if i == 0 else None) (get_field_display(obj, name)[1], obj.pk if i == 0 else None)
@ -99,6 +95,7 @@ class CrudListMixin():
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(CrudListMixin, self).get_context_data(**kwargs) context = super(CrudListMixin, self).get_context_data(**kwargs)
context.setdefault('title', self.verbose_name_plural)
# pagination # pagination
page_obj = context['page_obj'] page_obj = context['page_obj']
@ -128,11 +125,6 @@ class CrudCreateMixin(FormMessagesMixin):
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(
_('O registro não foi criado.')) _('O registro não foi criado.'))
@property
def title(self):
return _('Adicionar %(verbose_name)s') % {
'verbose_name': self.verbose_name}
@property @property
def cancel_url(self): def cancel_url(self):
return self.list_url return self.list_url
@ -140,13 +132,14 @@ class CrudCreateMixin(FormMessagesMixin):
def get_success_url(self): def get_success_url(self):
return self.detail_url return self.detail_url
def get_context_data(self, **kwargs):
kwargs.setdefault('title', _('Adicionar %(verbose_name)s') % {
'verbose_name': self.verbose_name})
return super(CrudCreateMixin, self).get_context_data(**kwargs)
class CrudDetailMixin():
@property class CrudDetailMixin():
def title(self): pass
# TODO: can we just use view.object in the templates????
return str(self.object)
class CrudUpdateMixin(FormMessagesMixin): class CrudUpdateMixin(FormMessagesMixin):
@ -155,10 +148,6 @@ class CrudUpdateMixin(FormMessagesMixin):
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(
_('Suas alterações não foram salvas.')) _('Suas alterações não foram salvas.'))
@property
def title(self):
return str(self.object)
@property @property
def cancel_url(self): def cancel_url(self):
return self.detail_url return self.detail_url
@ -173,6 +162,10 @@ class CrudDeleteMixin(FormMessagesMixin):
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(
_('O registro não foi excluído.')) _('O registro não foi excluído.'))
@property
def cancel_url(self):
return self.detail_url
def get_success_url(self): def get_success_url(self):
return self.list_url return self.list_url

8
crud_tests/templates/base.html

@ -25,9 +25,13 @@
<div class="clearfix"> <div class="clearfix">
{% block title %} {% block title %}
{% if view.title %} <h1 class="page-header">
<h1 class="page-header">{{ view.title|linebreaksbr }}</h1> {% if title %}
{{ title|safe|linebreaksbr }}
{% elif object %}
{{ object|safe|linebreaksbr }}
{% endif %} {% endif %}
</h1>
{% endblock %} {% endblock %}
</div> </div>

6
crud_tests/test_flux.py

@ -148,7 +148,7 @@ def test_reverse():
def assert_h1(res, title): def assert_h1(res, title):
assert res.html.find('main').find('h1').text == title assert res.html.find('main').find('h1').text.strip() == title
NO_ENTRIES_MSG = str(CrudListMixin.no_entries_msg) # "unlazy" NO_ENTRIES_MSG = str(CrudListMixin.no_entries_msg) # "unlazy"
@ -178,8 +178,8 @@ def assert_on_detail_page(res, stub_name):
@pytest.mark.parametrize("num_entries, page_size, ranges, page_list", [ @pytest.mark.parametrize("num_entries, page_size, ranges, page_list", [
(0, 6, [], []), (0, 6, [], []),
(5, 5, [(0, 5)], []), (5, 5, [(0, 5)], []),
(10, 5, [(0, 5), (5, 10)], ['«', '1', '2', '»']), (10, 5, [(0, 5), (5, 10)], ['Anterior', '1', '2', 'Próxima']),
(9, 4, [(0, 4), (4, 8), (8, 9)], ['«', '1', '2', '3', '»']), (9, 4, [(0, 4), (4, 8), (8, 9)], ['Anterior', '1', '2', '3', 'Próxima']),
]) ])
def test_flux_list_paginate_detail( def test_flux_list_paginate_detail(
app, monkeypatch, num_entries, page_size, ranges, page_list): app, monkeypatch, num_entries, page_size, ranges, page_list):

6
templates/base.html

@ -155,10 +155,10 @@
{% block title %} {% block title %}
<h1 class="page-header"> <h1 class="page-header">
{% if view.title %} {% if title %}
{{ view.title|safe|linebreaksbr }}
{% else %}
{{ title|safe|linebreaksbr }} {{ title|safe|linebreaksbr }}
{% elif object %}
{{ object|safe|linebreaksbr }}
{% endif %} {% endif %}
</h1> </h1>
{% endblock %} {% endblock %}

8
templates/crud/confirm_delete.html

@ -1,12 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load i18n %} {% load i18n %}
{% block title %}
{% with object as title %}
{{ block.super }}
{% endwith %}
{% endblock %}
{% block base_content %} {% block base_content %}
<form action="" method="post">{% csrf_token %} <form action="" method="post">{% csrf_token %}
<div class="panel panel-danger"> <div class="panel panel-danger">
@ -16,7 +10,7 @@
{% endblocktrans %} {% endblocktrans %}
</div> </div>
<div class="panel-body text-center"> <div class="panel-body text-center">
<a href="{{ view.detail_url }}" class="btn btn-inverse">{% trans 'Cancelar' %}</a> <a href="{{ view.cancel_url }}" class="btn btn-inverse">{% trans 'Cancelar' %}</a>
<input name="submit" value="{% trans 'Confirmar' %}" class="btn btn-danger" type="submit"></li> <input name="submit" value="{% trans 'Confirmar' %}" class="btn btn-danger" type="submit"></li>
</div> </div>
</div> </div>

1
templates/crud/detail.html

@ -3,7 +3,6 @@
{% block base_content %} {% block base_content %}
{# FIXME is this the best markup to use? #}
<div class="clearfix"> <div class="clearfix">
{% block actions %} {% block actions %}
<div class="actions btn-group pull-right" role="group"> <div class="actions btn-group pull-right" role="group">

42
templates/crud/list.html

@ -3,15 +3,11 @@
{% block base_content %} {% block base_content %}
{# FIXME is this the best markup to use? #}
<div class="actions btn-group pull-right" role="group"> <div class="actions btn-group pull-right" role="group">
<a href="{{ view.create_url }}" class="btn btn-default"> <a href="{{ view.create_url }}" class="btn btn-default">
{% blocktrans with verbose_name=view.verbose_name %} Adicionar {{ verbose_name }} {% endblocktrans %} {% blocktrans with verbose_name=view.verbose_name %} Adicionar {{ verbose_name }} {% endblocktrans %}
</a> </a>
{% block more_buttons %}{% endblock more_buttons %}
{% block more_buttons %}
{% endblock more_buttons %}
</div> </div>
{% if not rows %} {% if not rows %}
@ -43,40 +39,6 @@
</table> </table>
{% endif %} {% endif %}
{% include "paginacao.html" %}
{% if is_paginated %}
<div class="pagination-centered">
<ul class="pagination" role="menubar" aria-label="Pagination">
{% if page_obj.has_previous %}
<li>
<a href="?page={{ page_obj.previous_page_number }}">
<span class="arrow">&laquo;</span>
</a>
</li>
{% else %}
<li class="arrow unavailable" aria-disabled="true"><a href="">&laquo;</a></li>
{% endif %}
{% for page in page_range %}
{% if page %}
<li {% if page == page_obj.number %}class="current"{% endif %}>
<a href="?page={{ page }}">{{ page }}</a>
</li>
{% else %}
<li class="unavailable" aria-disabled="true"><a href="">&hellip;</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>
<a href="?page={{ page_obj.next_page_number }}">
<span class="arrow">&raquo;</span>
</a>
</li>
{% else %}
<li class="arrow unavailable" aria-disabled="true"><a href="">&raquo;</a></li>
{% endif %}
</ul>
</div>
{% endif %}
{% endblock %} {% endblock %}

Loading…
Cancel
Save