|
@ -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 |
|
|