From b5cda7d4a51f3fc7073e08d4a8abc295332634bc Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Fri, 26 Feb 2016 16:47:12 -0300 Subject: [PATCH] Move CrispyLayoutFormMixin para crispy_layout_mixin --- crispy_layout_mixin.py | 69 ++++++++++++++++++++++++++++++++++++ crud.py | 71 +------------------------------------- crud_tests/test_flux.py | 2 +- templates/crud/detail.html | 2 +- 4 files changed, 72 insertions(+), 72 deletions(-) diff --git a/crispy_layout_mixin.py b/crispy_layout_mixin.py index 6d0c4c7f5..f70ff2e72 100644 --- a/crispy_layout_mixin.py +++ b/crispy_layout_mixin.py @@ -1,5 +1,7 @@ from crispy_forms.bootstrap import FormActions +from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit +from django import forms from django.utils.translation import ugettext as _ @@ -35,3 +37,70 @@ class SaplFormLayout(Layout): ' class="btn btn-inverse">%s' % _('Cancelar'))]) _fields = list(to_fieldsets(fields)) + [to_row([(buttons, 12)])] super(SaplFormLayout, self).__init__(*_fields) + + +def get_field_display(obj, fieldname): + field = obj._meta.get_field(fieldname) + verbose_name = str(field.verbose_name) + if field.choices: + value = getattr(obj, 'get_%s_display' % fieldname)() + else: + value = getattr(obj, fieldname) + if value is None: + display = '' + elif 'date' in str(type(value)): + display = value.strftime("%d/%m/%Y") # TODO: localize + elif 'bool' in str(type(value)): + display = 'Sim' if value else 'Não' + else: + display = str(value) + return verbose_name, display + + +class CrispyLayoutFormMixin(object): + + def get_form_class(self): + + layout = self.layout + + class CrispyForm(forms.ModelForm): + + class Meta: + model = self.model + exclude = [] + + def __init__(self, *args, **kwargs): + super(CrispyForm, self).__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.layout = SaplFormLayout(*layout) + + return CrispyForm + + @property + def list_field_names(self): + '''The list of field names to display on table + + This base implementation returns the field names + in the first fieldset of the layout. + ''' + rows = self.layout[0][1:] + return [fieldname for row in rows for fieldname, __ in row] + + def get_column(self, fieldname, span): + obj = self.get_object() + verbose_name, text = get_field_display(obj, fieldname) + return { + 'id': fieldname, + 'span': span, + 'verbose_name': verbose_name, + 'text': text, + } + + @property + def layout_display(self): + return [ + {'legend': legend, + 'rows': [[self.get_column(fieldname, span) + for fieldname, span in row] + for row in rows] + } for legend, *rows in self.layout] diff --git a/crud.py b/crud.py index ce5013f30..115d1b9fb 100644 --- a/crud.py +++ b/crud.py @@ -1,13 +1,11 @@ from braces.views import FormMessagesMixin -from crispy_forms.helper import FormHelper -from django import forms from django.conf.urls import url from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from django.views.generic import (CreateView, DeleteView, DetailView, ListView, UpdateView) -from crispy_layout_mixin import SaplFormLayout +from crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display def from_to(start, end): @@ -41,73 +39,6 @@ def make_pagination(index, num_pages): return head + [None] + tail -def get_field_display(obj, fieldname): - field = obj._meta.get_field(fieldname) - verbose_name = str(field.verbose_name) - if field.choices: - value = getattr(obj, 'get_%s_display' % fieldname)() - else: - value = getattr(obj, fieldname) - if value is None: - display = '' - elif 'date' in str(type(value)): - display = value.strftime("%d/%m/%Y") # TODO: localize - elif 'bool' in str(type(value)): - display = 'Sim' if value else 'Não' - else: - display = str(value) - return verbose_name, display - - -class CrispyLayoutFormMixin(object): - - def get_form_class(self): - - layout = self.layout - - class CrispyForm(forms.ModelForm): - - class Meta: - model = self.model - exclude = [] - - def __init__(self, *args, **kwargs): - super(CrispyForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.helper.layout = SaplFormLayout(*layout) - - return CrispyForm - - @property - def list_field_names(self): - '''The list of field names to display on table - - This base implementation returns the field names - in the first fieldset of the layout. - ''' - rows = self.layout[0][1:] - return [fieldname for row in rows for fieldname, __ in row] - - def get_column(self, fieldname, span): - obj = self.get_object() - verbose_name, text = get_field_display(obj, fieldname) - return { - 'id': fieldname, - 'span': span, - 'verbose_name': verbose_name, - 'text': text, - } - - @property - def fieldsets(self): - return [ - {'legend': legend, - 'rows': [[self.get_column(fieldname, span) - for fieldname, span in row] - for row in rows] - } for legend, *rows in self.layout] - - class BaseCrudMixin(CrispyLayoutFormMixin): @property diff --git a/crud_tests/test_flux.py b/crud_tests/test_flux.py index 4bca1943b..06d529c4a 100644 --- a/crud_tests/test_flux.py +++ b/crud_tests/test_flux.py @@ -108,7 +108,7 @@ def test_layout_detail_fieldsets(monkeypatch): # to test None displayed as empty string assert stub.population is None - assert view.fieldsets == [ + assert view.layout_display == [ {'legend': 'Basic Data', 'rows': [[{'id': 'name', 'span': 9, diff --git a/templates/crud/detail.html b/templates/crud/detail.html index 8ef6cd351..4e5d6b019 100644 --- a/templates/crud/detail.html +++ b/templates/crud/detail.html @@ -14,7 +14,7 @@ {% block detail_content %} - {% for fieldset in view.fieldsets %} + {% for fieldset in view.layout_display %}

{{ fieldset.legend }}

{% for row in fieldset.rows %}