Browse Source

Move CrispyLayoutFormMixin para crispy_layout_mixin

pull/237/head
Marcio Mazza 9 years ago
parent
commit
b5cda7d4a5
  1. 69
      crispy_layout_mixin.py
  2. 71
      crud.py
  3. 2
      crud_tests/test_flux.py
  4. 2
      templates/crud/detail.html

69
crispy_layout_mixin.py

@ -1,5 +1,7 @@
from crispy_forms.bootstrap import FormActions from crispy_forms.bootstrap import FormActions
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit
from django import forms
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -35,3 +37,70 @@ class SaplFormLayout(Layout):
' class="btn btn-inverse">%s</a>' % _('Cancelar'))]) ' class="btn btn-inverse">%s</a>' % _('Cancelar'))])
_fields = list(to_fieldsets(fields)) + [to_row([(buttons, 12)])] _fields = list(to_fieldsets(fields)) + [to_row([(buttons, 12)])]
super(SaplFormLayout, self).__init__(*_fields) 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]

71
crud.py

@ -1,13 +1,11 @@
from braces.views import FormMessagesMixin from braces.views import FormMessagesMixin
from crispy_forms.helper import FormHelper
from django import forms
from django.conf.urls import url from django.conf.urls import url
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import (CreateView, DeleteView, DetailView, ListView, from django.views.generic import (CreateView, DeleteView, DetailView, ListView,
UpdateView) UpdateView)
from crispy_layout_mixin import SaplFormLayout from crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display
def from_to(start, end): def from_to(start, end):
@ -41,73 +39,6 @@ def make_pagination(index, num_pages):
return head + [None] + tail 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): class BaseCrudMixin(CrispyLayoutFormMixin):
@property @property

2
crud_tests/test_flux.py

@ -108,7 +108,7 @@ def test_layout_detail_fieldsets(monkeypatch):
# to test None displayed as empty string # to test None displayed as empty string
assert stub.population is None assert stub.population is None
assert view.fieldsets == [ assert view.layout_display == [
{'legend': 'Basic Data', {'legend': 'Basic Data',
'rows': [[{'id': 'name', 'rows': [[{'id': 'name',
'span': 9, 'span': 9,

2
templates/crud/detail.html

@ -14,7 +14,7 @@
</div> </div>
{% block detail_content %} {% block detail_content %}
{% for fieldset in view.fieldsets %} {% for fieldset in view.layout_display %}
<h2 class="legend">{{ fieldset.legend }}</h2> <h2 class="legend">{{ fieldset.legend }}</h2>
{% for row in fieldset.rows %} {% for row in fieldset.rows %}
<div class="row-fluid"> <div class="row-fluid">

Loading…
Cancel
Save