mirror of https://github.com/interlegis/sapl.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
8.1 KiB
250 lines
8.1 KiB
10 years ago
|
from braces.views import FormMessagesMixin
|
||
10 years ago
|
from crispy_forms.helper import FormHelper
|
||
|
from django import forms
|
||
10 years ago
|
from django.conf.urls import url
|
||
|
from django.core.urlresolvers import reverse, reverse_lazy
|
||
10 years ago
|
from django.utils.functional import cached_property
|
||
10 years ago
|
from django.utils.translation import ugettext_lazy as _
|
||
10 years ago
|
from django.views.generic import (CreateView, DeleteView, DetailView, ListView,
|
||
|
UpdateView)
|
||
10 years ago
|
|
||
10 years ago
|
from sapl.layout import SaplFormLayout
|
||
10 years ago
|
|
||
|
NO_ENTRIES_MSG = _('Não existem registros')
|
||
10 years ago
|
|
||
10 years ago
|
|
||
10 years ago
|
def from_to(start, end):
|
||
|
return list(range(start, end + 1))
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
def make_pagination(index, num_pages):
|
||
10 years ago
|
'''Make a list of adjacent page ranges interspersed with "None"s
|
||
|
|
||
|
The list starts with [1, 2] and end with [num_pages-1, num_pages].
|
||
|
The list includes [index-1, index, index+1]
|
||
|
"None"s separate those ranges and mean ellipsis (...)
|
||
|
|
||
|
Example: [1, 2, None, 10, 11, 12, None, 29, 30]
|
||
|
'''
|
||
|
|
||
10 years ago
|
PAGINATION_LENGTH = 10
|
||
|
if num_pages <= PAGINATION_LENGTH:
|
||
|
return from_to(1, num_pages)
|
||
10 years ago
|
else:
|
||
10 years ago
|
if index - 1 <= 5:
|
||
|
tail = [num_pages - 1, num_pages]
|
||
|
head = from_to(1, PAGINATION_LENGTH - 3)
|
||
|
else:
|
||
|
if index + 1 >= num_pages - 3:
|
||
|
tail = from_to(index - 1, num_pages)
|
||
|
else:
|
||
|
tail = [index - 1, index, index + 1,
|
||
|
None, num_pages - 1, num_pages]
|
||
|
head = from_to(1, PAGINATION_LENGTH - len(tail) - 1)
|
||
|
return head + [None] + tail
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
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)
|
||
9 years ago
|
|
||
9 years ago
|
if value is None:
|
||
|
display = ''
|
||
|
elif 'date' in str(type(value)):
|
||
|
display = value.strftime("%d/%m/%Y")
|
||
9 years ago
|
elif 'bool' in str(type(value)):
|
||
|
display = 'Sim' if value else 'Não'
|
||
9 years ago
|
else:
|
||
9 years ago
|
display = str(value)
|
||
|
|
||
10 years ago
|
return verbose_name, display
|
||
|
|
||
|
|
||
10 years ago
|
class Crud(object):
|
||
10 years ago
|
pass
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
def build_crud(model, help_path, layout):
|
||
10 years ago
|
crud = Crud()
|
||
10 years ago
|
crud.model = model
|
||
10 years ago
|
crud.help_path = help_path
|
||
10 years ago
|
crud.namespace = model._meta.model_name
|
||
|
|
||
|
class CrispyForm(forms.ModelForm):
|
||
|
|
||
|
class Meta:
|
||
|
model = crud.model
|
||
|
exclude = []
|
||
|
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
super(CrispyForm, self).__init__(*args, **kwargs)
|
||
|
self.helper = FormHelper()
|
||
|
self.helper.layout = SaplFormLayout(*layout)
|
||
|
|
||
|
crud.model_form = CrispyForm
|
||
10 years ago
|
|
||
10 years ago
|
def in_namespace(url_name):
|
||
|
return '%s:%s' % (crud.namespace, url_name)
|
||
10 years ago
|
|
||
10 years ago
|
def make_form_invalid_message(msg):
|
||
|
return '%s %s' % (_('Formulário inválido.'), msg)
|
||
10 years ago
|
|
||
10 years ago
|
class BaseMixin(object):
|
||
|
model = crud.model
|
||
10 years ago
|
|
||
10 years ago
|
verbose_name = crud.model._meta.verbose_name
|
||
|
verbose_name_plural = crud.model._meta.verbose_name_plural
|
||
10 years ago
|
|
||
10 years ago
|
list_url = reverse_lazy(in_namespace('list'))
|
||
|
create_url = reverse_lazy(in_namespace('create'))
|
||
10 years ago
|
help_path = crud.help_path # FIXME
|
||
10 years ago
|
|
||
10 years ago
|
def get_url_for_this_object(self, url_name):
|
||
|
return reverse(in_namespace(url_name), args=(self.object.id,))
|
||
10 years ago
|
|
||
10 years ago
|
@property
|
||
|
def detail_url(self):
|
||
|
return self.get_url_for_this_object('detail')
|
||
10 years ago
|
|
||
10 years ago
|
@property
|
||
|
def update_url(self):
|
||
|
return self.get_url_for_this_object('update')
|
||
10 years ago
|
|
||
10 years ago
|
@property
|
||
|
def delete_url(self):
|
||
|
return self.get_url_for_this_object('delete')
|
||
10 years ago
|
|
||
10 years ago
|
def get_template_names(self):
|
||
|
names = super(BaseMixin, self).get_template_names()
|
||
|
names.append("crud/%s.html" %
|
||
|
self.template_name_suffix.lstrip('_'))
|
||
|
return names
|
||
|
|
||
10 years ago
|
class CrudListView(BaseMixin, ListView):
|
||
|
title = BaseMixin.verbose_name_plural
|
||
10 years ago
|
paginate_by = 10
|
||
10 years ago
|
no_entries_msg = NO_ENTRIES_MSG
|
||
10 years ago
|
|
||
10 years ago
|
@cached_property
|
||
|
def field_names(self):
|
||
10 years ago
|
'''The list of field names to display on table
|
||
|
|
||
|
This base implementation returns the field names
|
||
|
in the first fieldset of the layout.
|
||
|
'''
|
||
|
rows = layout[0][1:]
|
||
|
return [fieldname for row in rows for fieldname, __ in row]
|
||
|
|
||
10 years ago
|
def get_rows(self, object_list):
|
||
10 years ago
|
return [[(get_field_display(obj, name)[1],
|
||
|
obj.pk if i == 0 else None)
|
||
|
for i, name in enumerate(self.field_names)]
|
||
|
for obj in object_list
|
||
|
]
|
||
|
|
||
10 years ago
|
def get_context_data(self, **kwargs):
|
||
10 years ago
|
context = super(CrudListView, self).get_context_data(**kwargs)
|
||
|
paginator = context['paginator']
|
||
|
page_obj = context['page_obj']
|
||
|
context['page_range'] = make_pagination(
|
||
|
page_obj.number, paginator.num_pages)
|
||
|
object_list = context['object_list']
|
||
10 years ago
|
context['headers'] = [
|
||
|
self.model._meta.get_field(fieldname).verbose_name
|
||
|
for fieldname in self.field_names]
|
||
|
context['rows'] = self.get_rows(object_list)
|
||
10 years ago
|
context['NO_ENTRIES_MSG'] = NO_ENTRIES_MSG
|
||
|
return context
|
||
10 years ago
|
|
||
10 years ago
|
class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView):
|
||
10 years ago
|
form_class = crud.model_form
|
||
10 years ago
|
title = _('Adicionar %(verbose_name)s') % {
|
||
|
'verbose_name': BaseMixin.verbose_name}
|
||
|
form_valid_message = _('Registro criado com sucesso!')
|
||
|
form_invalid_message = make_form_invalid_message(
|
||
|
_('O registro não foi criado.'))
|
||
10 years ago
|
cancel_url = BaseMixin.list_url
|
||
10 years ago
|
|
||
9 years ago
|
def form_invalid(self, form):
|
||
|
"""
|
||
|
If the form is invalid, re-render the context data with the
|
||
|
data-filled form and errors.
|
||
|
"""
|
||
|
return self.render_to_response(self.get_context_data(form=form))
|
||
|
|
||
10 years ago
|
def get_success_url(self):
|
||
|
return self.detail_url
|
||
10 years ago
|
|
||
10 years ago
|
class CrudDetailView(BaseMixin, DetailView):
|
||
10 years ago
|
|
||
10 years ago
|
@property
|
||
|
def title(self):
|
||
|
return self.get_object()
|
||
10 years ago
|
|
||
10 years ago
|
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 layout]
|
||
|
|
||
10 years ago
|
class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView):
|
||
10 years ago
|
form_class = crud.model_form
|
||
10 years ago
|
form_valid_message = _('Registro alterado com sucesso!')
|
||
|
form_invalid_message = make_form_invalid_message(
|
||
|
_('Suas alterações não foram salvas.'))
|
||
10 years ago
|
|
||
10 years ago
|
@property
|
||
|
def title(self):
|
||
|
return self.get_object()
|
||
10 years ago
|
|
||
10 years ago
|
def get_success_url(self):
|
||
|
return self.detail_url
|
||
10 years ago
|
|
||
10 years ago
|
def cancel_url(self):
|
||
|
return self.detail_url
|
||
|
|
||
10 years ago
|
class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView):
|
||
|
form_valid_message = _('Registro excluído com sucesso!')
|
||
|
form_invalid_message = make_form_invalid_message(
|
||
|
_('O registro não foi excluído.'))
|
||
10 years ago
|
|
||
10 years ago
|
def get_success_url(self):
|
||
|
return self.list_url
|
||
10 years ago
|
|
||
10 years ago
|
crud.CrudListView = CrudListView
|
||
|
crud.CrudCreateView = CrudCreateView
|
||
|
crud.CrudDetailView = CrudDetailView
|
||
|
crud.CrudUpdateView = CrudUpdateView
|
||
|
crud.CrudDeleteView = CrudDeleteView
|
||
10 years ago
|
|
||
10 years ago
|
# XXX transform into a property of Crud to enable override
|
||
10 years ago
|
crud.urlpatterns = [
|
||
10 years ago
|
url(r'^$', CrudListView.as_view(), name='list'),
|
||
|
url(r'^create$', CrudCreateView.as_view(), name='create'),
|
||
|
url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'),
|
||
|
url(r'^(?P<pk>\d+)/edit$',
|
||
|
CrudUpdateView.as_view(), name='update'),
|
||
|
url(r'^(?P<pk>\d+)/delete$',
|
||
|
CrudDeleteView.as_view(), name='delete'),
|
||
10 years ago
|
]
|
||
|
crud.urls = crud.urlpatterns, crud.namespace, crud.namespace
|
||
10 years ago
|
|
||
10 years ago
|
return crud
|