|
@ -2,16 +2,13 @@ from braces.views import FormMessagesMixin |
|
|
from crispy_forms.helper import FormHelper |
|
|
from crispy_forms.helper import FormHelper |
|
|
from django import forms |
|
|
from django import forms |
|
|
from django.conf.urls import url |
|
|
from django.conf.urls import url |
|
|
from django.core.urlresolvers import reverse, reverse_lazy |
|
|
from django.core.urlresolvers import reverse |
|
|
from django.utils.functional import cached_property |
|
|
|
|
|
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 sapl.layout import SaplFormLayout |
|
|
from sapl.layout import SaplFormLayout |
|
|
|
|
|
|
|
|
NO_ENTRIES_MSG = _('Não existem registros') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def from_to(start, end): |
|
|
def from_to(start, end): |
|
|
return list(range(start, end + 1)) |
|
|
return list(range(start, end + 1)) |
|
@ -51,33 +48,27 @@ def get_field_display(obj, fieldname): |
|
|
value = getattr(obj, 'get_%s_display' % fieldname)() |
|
|
value = getattr(obj, 'get_%s_display' % fieldname)() |
|
|
else: |
|
|
else: |
|
|
value = getattr(obj, fieldname) |
|
|
value = getattr(obj, fieldname) |
|
|
|
|
|
|
|
|
if value is None: |
|
|
if value is None: |
|
|
display = '' |
|
|
display = '' |
|
|
elif 'date' in str(type(value)): |
|
|
elif 'date' in str(type(value)): |
|
|
display = value.strftime("%d/%m/%Y") |
|
|
display = value.strftime("%d/%m/%Y") # TODO: localize |
|
|
elif 'bool' in str(type(value)): |
|
|
elif 'bool' in str(type(value)): |
|
|
display = 'Sim' if value else 'Não' |
|
|
display = 'Sim' if value else 'Não' |
|
|
else: |
|
|
else: |
|
|
display = str(value) |
|
|
display = str(value) |
|
|
|
|
|
|
|
|
return verbose_name, display |
|
|
return verbose_name, display |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Crud(object): |
|
|
class CrispyLayoutFormMixin(object): |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_form_class(self): |
|
|
|
|
|
|
|
|
def build_crud(model, help_path, layout): |
|
|
layout = self.layout |
|
|
crud = Crud() |
|
|
|
|
|
crud.model = model |
|
|
|
|
|
crud.help_path = help_path |
|
|
|
|
|
crud.namespace = model._meta.model_name |
|
|
|
|
|
|
|
|
|
|
|
class CrispyForm(forms.ModelForm): |
|
|
class CrispyForm(forms.ModelForm): |
|
|
|
|
|
|
|
|
class Meta: |
|
|
class Meta: |
|
|
model = crud.model |
|
|
model = self.model |
|
|
exclude = [] |
|
|
exclude = [] |
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
def __init__(self, *args, **kwargs): |
|
@ -85,142 +76,168 @@ def build_crud(model, help_path, layout): |
|
|
self.helper = FormHelper() |
|
|
self.helper = FormHelper() |
|
|
self.helper.layout = SaplFormLayout(*layout) |
|
|
self.helper.layout = SaplFormLayout(*layout) |
|
|
|
|
|
|
|
|
crud.model_form = CrispyForm |
|
|
return CrispyForm |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
|
def list_field_names(self): |
|
|
|
|
|
'''The list of field names to display on table |
|
|
|
|
|
|
|
|
def in_namespace(url_name): |
|
|
This base implementation returns the field names |
|
|
return '%s:%s' % (crud.namespace, url_name) |
|
|
in the first fieldset of the layout. |
|
|
|
|
|
''' |
|
|
|
|
|
rows = self.layout[0][1:] |
|
|
|
|
|
return [fieldname for row in rows for fieldname, __ in row] |
|
|
|
|
|
|
|
|
def make_form_invalid_message(msg): |
|
|
def get_column(self, fieldname, span): |
|
|
return '%s %s' % (_('Formulário inválido.'), msg) |
|
|
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 BaseMixin(object): |
|
|
|
|
|
model = crud.model |
|
|
|
|
|
|
|
|
|
|
|
verbose_name = crud.model._meta.verbose_name |
|
|
class BaseCrudMixin(CrispyLayoutFormMixin): |
|
|
verbose_name_plural = crud.model._meta.verbose_name_plural |
|
|
|
|
|
|
|
|
|
|
|
list_url = reverse_lazy(in_namespace('list')) |
|
|
@property |
|
|
create_url = reverse_lazy(in_namespace('create')) |
|
|
def namespace(self): |
|
|
help_path = crud.help_path # FIXME |
|
|
return self.model._meta.model_name |
|
|
|
|
|
|
|
|
|
|
|
def resolve_url(self, url_name, args=None): |
|
|
|
|
|
return reverse('%s:%s' % (self.namespace, url_name), args=args) |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
|
def list_url(self): |
|
|
|
|
|
return self.resolve_url('list') |
|
|
|
|
|
|
|
|
def get_url_for_this_object(self, url_name): |
|
|
@property |
|
|
return reverse(in_namespace(url_name), args=(self.object.id,)) |
|
|
def create_url(self): |
|
|
|
|
|
return self.resolve_url('create') |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def detail_url(self): |
|
|
def detail_url(self): |
|
|
return self.get_url_for_this_object('detail') |
|
|
return self.resolve_url('detail', args=(self.object.id,)) |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def update_url(self): |
|
|
def update_url(self): |
|
|
return self.get_url_for_this_object('update') |
|
|
return self.resolve_url('update', args=(self.object.id,)) |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def delete_url(self): |
|
|
def delete_url(self): |
|
|
return self.get_url_for_this_object('delete') |
|
|
return self.resolve_url('delete', args=(self.object.id,)) |
|
|
|
|
|
|
|
|
def get_template_names(self): |
|
|
def get_template_names(self): |
|
|
names = super(BaseMixin, self).get_template_names() |
|
|
names = super(BaseCrudMixin, self).get_template_names() |
|
|
names.append("crud/%s.html" % |
|
|
names.append("crud/%s.html" % |
|
|
self.template_name_suffix.lstrip('_')) |
|
|
self.template_name_suffix.lstrip('_')) |
|
|
return names |
|
|
return names |
|
|
|
|
|
|
|
|
class CrudListView(BaseMixin, ListView): |
|
|
@property |
|
|
title = BaseMixin.verbose_name_plural |
|
|
def verbose_name(self): |
|
|
paginate_by = 10 |
|
|
return self.model._meta.verbose_name |
|
|
no_entries_msg = NO_ENTRIES_MSG |
|
|
|
|
|
|
|
|
|
|
|
@cached_property |
|
|
@property |
|
|
def field_names(self): |
|
|
def verbose_name_plural(self): |
|
|
'''The list of field names to display on table |
|
|
return self.model._meta.verbose_name_plural |
|
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
|
|
|
def get_rows(self, object_list): |
|
|
class CrudListMixin(): |
|
|
return [[(get_field_display(obj, name)[1], |
|
|
|
|
|
obj.pk if i == 0 else None) |
|
|
paginate_by = 10 |
|
|
for i, name in enumerate(self.field_names)] |
|
|
no_entries_msg = _('Nenhum registro encontrado.') |
|
|
for obj in object_list |
|
|
|
|
|
] |
|
|
@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) |
|
|
|
|
|
for i, name in enumerate(self.list_field_names)] |
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs): |
|
|
def get_context_data(self, **kwargs): |
|
|
context = super(CrudListView, self).get_context_data(**kwargs) |
|
|
context = super(CrudListMixin, self).get_context_data(**kwargs) |
|
|
paginator = context['paginator'] |
|
|
|
|
|
|
|
|
# pagination |
|
|
page_obj = context['page_obj'] |
|
|
page_obj = context['page_obj'] |
|
|
|
|
|
paginator = context['paginator'] |
|
|
context['page_range'] = make_pagination( |
|
|
context['page_range'] = make_pagination( |
|
|
page_obj.number, paginator.num_pages) |
|
|
page_obj.number, paginator.num_pages) |
|
|
|
|
|
|
|
|
|
|
|
# rows |
|
|
object_list = context['object_list'] |
|
|
object_list = context['object_list'] |
|
|
|
|
|
context['rows'] = [self._as_row(obj) for obj in object_list] |
|
|
|
|
|
|
|
|
context['headers'] = [ |
|
|
context['headers'] = [ |
|
|
self.model._meta.get_field(fieldname).verbose_name |
|
|
self.model._meta.get_field(fieldname).verbose_name |
|
|
for fieldname in self.field_names] |
|
|
for fieldname in self.list_field_names] |
|
|
context['rows'] = self.get_rows(object_list) |
|
|
context['NO_ENTRIES_MSG'] = self.no_entries_msg |
|
|
context['NO_ENTRIES_MSG'] = NO_ENTRIES_MSG |
|
|
|
|
|
return context |
|
|
return context |
|
|
|
|
|
|
|
|
class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): |
|
|
|
|
|
form_class = crud.model_form |
|
|
def make_form_invalid_message(msg): |
|
|
title = _('Adicionar %(verbose_name)s') % { |
|
|
return '%s %s' % (_('Formulário inválido.'), msg) |
|
|
'verbose_name': BaseMixin.verbose_name} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CrudCreateMixin(FormMessagesMixin): |
|
|
|
|
|
|
|
|
form_valid_message = _('Registro criado com sucesso!') |
|
|
form_valid_message = _('Registro criado com sucesso!') |
|
|
form_invalid_message = make_form_invalid_message( |
|
|
form_invalid_message = make_form_invalid_message( |
|
|
_('O registro não foi criado.')) |
|
|
_('O registro não foi criado.')) |
|
|
cancel_url = BaseMixin.list_url |
|
|
|
|
|
|
|
|
|
|
|
def form_invalid(self, form): |
|
|
@property |
|
|
""" |
|
|
def title(self): |
|
|
If the form is invalid, re-render the context data with the |
|
|
return _('Adicionar %(verbose_name)s') % { |
|
|
data-filled form and errors. |
|
|
'verbose_name': self.verbose_name} |
|
|
""" |
|
|
|
|
|
return self.render_to_response(self.get_context_data(form=form)) |
|
|
@property |
|
|
|
|
|
def cancel_url(self): |
|
|
|
|
|
return self.list_url |
|
|
|
|
|
|
|
|
def get_success_url(self): |
|
|
def get_success_url(self): |
|
|
return self.detail_url |
|
|
return self.detail_url |
|
|
|
|
|
|
|
|
class CrudDetailView(BaseMixin, DetailView): |
|
|
|
|
|
|
|
|
class CrudDetailMixin(): |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def title(self): |
|
|
def title(self): |
|
|
return self.get_object() |
|
|
# TODO: can we just use view.object in the templates???? |
|
|
|
|
|
return str(self.object) |
|
|
|
|
|
|
|
|
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 |
|
|
class CrudUpdateMixin(FormMessagesMixin): |
|
|
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] |
|
|
|
|
|
|
|
|
|
|
|
class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView): |
|
|
|
|
|
form_class = crud.model_form |
|
|
|
|
|
form_valid_message = _('Registro alterado com sucesso!') |
|
|
form_valid_message = _('Registro alterado com sucesso!') |
|
|
form_invalid_message = make_form_invalid_message( |
|
|
form_invalid_message = make_form_invalid_message( |
|
|
_('Suas alterações não foram salvas.')) |
|
|
_('Suas alterações não foram salvas.')) |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def title(self): |
|
|
def title(self): |
|
|
return self.get_object() |
|
|
return str(self.object) |
|
|
|
|
|
|
|
|
def get_success_url(self): |
|
|
@property |
|
|
|
|
|
def cancel_url(self): |
|
|
return self.detail_url |
|
|
return self.detail_url |
|
|
|
|
|
|
|
|
def cancel_url(self): |
|
|
def get_success_url(self): |
|
|
return self.detail_url |
|
|
return self.detail_url |
|
|
|
|
|
|
|
|
class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView): |
|
|
|
|
|
|
|
|
class CrudDeleteMixin(FormMessagesMixin): |
|
|
|
|
|
|
|
|
form_valid_message = _('Registro excluído com sucesso!') |
|
|
form_valid_message = _('Registro excluído com sucesso!') |
|
|
form_invalid_message = make_form_invalid_message( |
|
|
form_invalid_message = make_form_invalid_message( |
|
|
_('O registro não foi excluído.')) |
|
|
_('O registro não foi excluído.')) |
|
@ -228,14 +245,39 @@ def build_crud(model, help_path, layout): |
|
|
def get_success_url(self): |
|
|
def get_success_url(self): |
|
|
return self.list_url |
|
|
return self.list_url |
|
|
|
|
|
|
|
|
crud.CrudListView = CrudListView |
|
|
|
|
|
crud.CrudCreateView = CrudCreateView |
|
|
|
|
|
crud.CrudDetailView = CrudDetailView |
|
|
|
|
|
crud.CrudUpdateView = CrudUpdateView |
|
|
|
|
|
crud.CrudDeleteView = CrudDeleteView |
|
|
|
|
|
|
|
|
|
|
|
# XXX transform into a property of Crud to enable override |
|
|
class Crud(object): |
|
|
crud.urlpatterns = [ |
|
|
|
|
|
|
|
|
def __init__(self, model, help_path, layout, |
|
|
|
|
|
base_mixin=BaseCrudMixin, |
|
|
|
|
|
list_mixin=CrudListMixin, |
|
|
|
|
|
create_mixin=CrudCreateMixin, |
|
|
|
|
|
detail_mixin=CrudDetailMixin, |
|
|
|
|
|
update_mixin=CrudUpdateMixin, |
|
|
|
|
|
delete_mixin=CrudDeleteMixin): |
|
|
|
|
|
|
|
|
|
|
|
class CrudMixin(base_mixin): |
|
|
|
|
|
pass |
|
|
|
|
|
CrudMixin.model = model |
|
|
|
|
|
CrudMixin.help_path = help_path |
|
|
|
|
|
CrudMixin.layout = layout |
|
|
|
|
|
|
|
|
|
|
|
class CrudListView(CrudMixin, list_mixin, ListView): |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
class CrudCreateView(CrudMixin, create_mixin, CreateView): |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
class CrudDetailView(CrudMixin, detail_mixin, DetailView): |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
class CrudUpdateView(CrudMixin, update_mixin, UpdateView): |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
class CrudDeleteView(CrudMixin, delete_mixin, DeleteView): |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
self.urlpatterns = [ |
|
|
url(r'^$', CrudListView.as_view(), name='list'), |
|
|
url(r'^$', CrudListView.as_view(), name='list'), |
|
|
url(r'^create$', CrudCreateView.as_view(), name='create'), |
|
|
url(r'^create$', CrudCreateView.as_view(), name='create'), |
|
|
url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'), |
|
|
url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'), |
|
@ -244,6 +286,9 @@ def build_crud(model, help_path, layout): |
|
|
url(r'^(?P<pk>\d+)/delete$', |
|
|
url(r'^(?P<pk>\d+)/delete$', |
|
|
CrudDeleteView.as_view(), name='delete'), |
|
|
CrudDeleteView.as_view(), name='delete'), |
|
|
] |
|
|
] |
|
|
crud.urls = crud.urlpatterns, crud.namespace, crud.namespace |
|
|
|
|
|
|
|
|
|
|
|
return crud |
|
|
self.namespace = CrudMixin().namespace |
|
|
|
|
|
self.urls = self.urlpatterns, self.namespace, self.namespace |
|
|
|
|
|
|
|
|
|
|
|
# FIXME Refatorar código que precisa desse atributo e remover |
|
|
|
|
|
self.CrudDetailView = CrudDetailView |
|
|