Repository: interlegis/sapl

Issue:
Refatorar templates do crud usando includes
Refatorar templates do crud, destacando partes reutilizáveis via `include`.
Isso é importante para reutilizar trechos no novo crud mestre-detalhe.

Necessária #321.

Refatorar templates do crud usando includes
Refatorar templates do crud, destacando partes reutilizáveis via `include`.
Isso é importante para reutilizar trechos no novo crud mestre-detalhe.

Necessária #321.



Discussion:

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


Test patch:
diff --git a/crud_tests/templates/base.html b/crud_tests/templates/base.html
--- a/crud_tests/templates/base.html
+++ b/crud_tests/templates/base.html
@@ -24,11 +24,15 @@
         {% block base_header %}
           <div class="clearfix">
 
-            {% block title %}
-              {% if view.title %}
-                <h1 class="page-header">{{ view.title|linebreaksbr }}</h1>
+          {% block title %}
+            <h1 class="page-header">
+              {% if title %}
+                {{ title|safe|linebreaksbr }}
+              {% elif object %}
+                {{ object|safe|linebreaksbr }}
               {% endif %}
-            {% endblock %}
+              </h1>
+          {% endblock %}
 
           </div>
         {% endblock base_header %}
diff --git a/crud_tests/test_flux.py b/crud_tests/test_flux.py
--- a/crud_tests/test_flux.py
+++ b/crud_tests/test_flux.py
@@ -148,7 +148,7 @@ def test_reverse():
 
 
 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"
@@ -178,8 +178,8 @@ def assert_on_detail_page(res, stub_name):
 @pytest.mark.parametrize("num_entries, page_size, ranges, page_list", [
     (0, 6, [], []),
     (5, 5, [(0, 5)], []),
-    (10, 5, [(0, 5), (5, 10)], ['«', '1', '2', '»']),
-    (9, 4, [(0, 4), (4, 8), (8, 9)], ['«', '1', '2', '3', '»']),
+    (10, 5, [(0, 5), (5, 10)], ['Anterior', '1', '2', 'Próxima']),
+    (9, 4, [(0, 4), (4, 8), (8, 9)], ['Anterior', '1', '2', '3', 'Próxima']),
 ])
 def test_flux_list_paginate_detail(
         app, monkeypatch, num_entries, page_size, ranges, page_list):