Browse Source

add hook_ em Details do Crud

pull/2996/head
Leandro Roberto 5 years ago
parent
commit
5026633cc7
  1. 25
      sapl/crispy_layout_mixin.py
  2. 28
      sapl/crud/base.py

25
sapl/crispy_layout_mixin.py

@ -55,28 +55,28 @@ def form_actions(more=[Div(css_class='clearfix')],
class SaplFormHelper(FormHelper): class SaplFormHelper(FormHelper):
render_hidden_fields = True # default = False render_hidden_fields = True # default = False
""" """
até a release 1.6.1 do django-crispy-forms, os fields em Meta.Fields eram até a release 1.6.1 do django-crispy-forms, os fields em Meta.Fields eram
renderizados mesmo se não mencionados no helper. renderizados mesmo se não mencionados no helper.
Com esta mudança (https://github.com/django-crispy-forms/django-crispy-forms/commit/6b93e8a362422db8fe54aa731319c7cbc39990ba) Com esta mudança (https://github.com/django-crispy-forms/django-crispy-forms/commit/6b93e8a362422db8fe54aa731319c7cbc39990ba)
render_hidden_fields foi adicionado uma condição em que a cada render_hidden_fields foi adicionado uma condição em que a cada
instância do Helper, fosse decidido se os fields não mencionados serião ou instância do Helper, fosse decidido se os fields não mencionados serião ou
não renderizados... não renderizados...
O Sapl até este commit: https://github.com/interlegis/sapl/commit/22b87f36ebc8659a6ecaf8831ab0f425206b0993 O Sapl até este commit: https://github.com/interlegis/sapl/commit/22b87f36ebc8659a6ecaf8831ab0f425206b0993
utilizou o django-crispy-forms na versão 1.6.1, ou seja, utilizou o django-crispy-forms na versão 1.6.1, ou seja,
sem a condição render_hidden_fields o que fazia o FormHelper, na 1.6.1 sem a condição render_hidden_fields o que fazia o FormHelper, na 1.6.1
set comportar como se, agora, na 1.7.2 o default fosse True. set comportar como se, agora, na 1.7.2 o default fosse True.
Como todos os Forms do Sapl foram construídos assumindo que fields Como todos os Forms do Sapl foram construídos assumindo que fields
não incluídos explicitamente no Helper, o helper o incluiria implicitamente, não incluídos explicitamente no Helper, o helper o incluiria implicitamente,
e assim o era, de acordo com commit acima do django-crispy-forms, então e assim o era, de acordo com commit acima do django-crispy-forms, então
cria-se essa classe: cria-se essa classe:
class SaplFormHelper(FormHelper): class SaplFormHelper(FormHelper):
render_hidden_fields = True render_hidden_fields = True
onde torna o default, antes False, agora = True, o esperado pelos forms do sapl, onde torna o default, antes False, agora = True, o esperado pelos forms do sapl,
e substituí-se todos os FormHelper por SaplFormHelper dentro do projeto Sapl e substituí-se todos os FormHelper por SaplFormHelper dentro do projeto Sapl
esta explicação ficará aqui dentro do código, via commit, e na issue #2456. esta explicação ficará aqui dentro do código, via commit, e na issue #2456.
""" """
@ -258,7 +258,12 @@ class CrispyLayoutFormMixin:
if func: if func:
verbose_name, text = getattr(self, func)(obj, fieldname) verbose_name, text = getattr(self, func)(obj, fieldname)
else: else:
verbose_name, text = get_field_display(obj, fieldname) hook_fieldname = 'hook_%s' % fieldname
if hasattr(self, hook_fieldname):
verbose_name, text = getattr(
self, hook_fieldname)(obj)
else:
verbose_name, text = get_field_display(obj, fieldname)
return { return {
'id': fieldname, 'id': fieldname,

28
sapl/crud/base.py

@ -30,10 +30,8 @@ from sapl.rules.map_rules import (RP_ADD, RP_CHANGE, RP_DELETE, RP_DETAIL,
RP_LIST) RP_LIST)
from sapl.utils import normalize from sapl.utils import normalize
logger = logging.getLogger(settings.BASE_DIR.name) logger = logging.getLogger(settings.BASE_DIR.name)
ACTION_LIST, ACTION_CREATE, ACTION_DETAIL, ACTION_UPDATE, ACTION_DELETE = \ ACTION_LIST, ACTION_CREATE, ACTION_DETAIL, ACTION_UPDATE, ACTION_DELETE = \
'list', 'create', 'detail', 'update', 'delete' 'list', 'create', 'detail', 'update', 'delete'
@ -82,7 +80,6 @@ def make_pagination(index, num_pages):
head = from_to(1, PAGINATION_LENGTH - len(tail) - 1) head = from_to(1, PAGINATION_LENGTH - len(tail) - 1)
return head + [None] + tail return head + [None] + tail
""" """
variáveis do crud: variáveis do crud:
help_topic help_topic
@ -386,12 +383,13 @@ class CrudBaseMixin(CrispyLayoutFormMixin):
class CrudListView(PermissionRequiredContainerCrudMixin, ListView): class CrudListView(PermissionRequiredContainerCrudMixin, ListView):
permission_required = (RP_LIST, ) permission_required = (RP_LIST,)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@classmethod @classmethod
def get_url_regex(cls): def get_url_regex(cls):
return r'^$' return r'^$'
paginate_by = 10 paginate_by = 10
no_entries_msg = _('Nenhum registro encontrado.') no_entries_msg = _('Nenhum registro encontrado.')
@ -423,7 +421,13 @@ class CrudListView(PermissionRequiredContainerCrudMixin, ListView):
if hasattr(f, 'related_model') and f.related_model: if hasattr(f, 'related_model') and f.related_model:
m = f.related_model m = f.related_model
if f: if f:
s.append(force_text(f.verbose_name)) hook = 'hook_header_{}'.format(''.join(fn))
if hasattr(self, hook):
header = getattr(self, hook)()
s.append(header)
else:
s.append(force_text(f.verbose_name))
s = ' / '.join(s) s = ' / '.join(s)
r.append(s) r.append(s)
return r return r
@ -598,7 +602,7 @@ class CrudListView(PermissionRequiredContainerCrudMixin, ListView):
model_ordering = (model_ordering,) model_ordering = (model_ordering,)
for mo in model_ordering: for mo in model_ordering:
if mo not in ordering: if mo not in ordering:
ordering = ordering + (mo, ) ordering = ordering + (mo,)
queryset = queryset.order_by(*ordering) queryset = queryset.order_by(*ordering)
# print(ordering) # print(ordering)
@ -649,7 +653,7 @@ class AuditLogMixin(object):
class CrudCreateView(PermissionRequiredContainerCrudMixin, class CrudCreateView(PermissionRequiredContainerCrudMixin,
FormMessagesMixin, AuditLogMixin, CreateView): FormMessagesMixin, AuditLogMixin, CreateView):
permission_required = (RP_ADD, ) permission_required = (RP_ADD,)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@classmethod @classmethod
@ -721,7 +725,7 @@ class CrudCreateView(PermissionRequiredContainerCrudMixin,
class CrudDetailView(PermissionRequiredContainerCrudMixin, class CrudDetailView(PermissionRequiredContainerCrudMixin,
DetailView, MultipleObjectMixin): DetailView, MultipleObjectMixin):
permission_required = (RP_DETAIL, ) permission_required = (RP_DETAIL,)
no_entries_msg = _('Nenhum registro Associado.') no_entries_msg = _('Nenhum registro Associado.')
paginate_by = 10 paginate_by = 10
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -867,7 +871,7 @@ class CrudDetailView(PermissionRequiredContainerCrudMixin,
class CrudUpdateView(PermissionRequiredContainerCrudMixin, class CrudUpdateView(PermissionRequiredContainerCrudMixin,
FormMessagesMixin, AuditLogMixin, UpdateView): FormMessagesMixin, AuditLogMixin, UpdateView):
permission_required = (RP_CHANGE, ) permission_required = (RP_CHANGE,)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def form_valid(self, form): def form_valid(self, form):
@ -898,7 +902,7 @@ class CrudUpdateView(PermissionRequiredContainerCrudMixin,
class CrudDeleteView(PermissionRequiredContainerCrudMixin, class CrudDeleteView(PermissionRequiredContainerCrudMixin,
FormMessagesMixin, AuditLogMixin, DeleteView): FormMessagesMixin, AuditLogMixin, DeleteView):
permission_required = (RP_DELETE, ) permission_required = (RP_DELETE,)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@classmethod @classmethod
@ -958,10 +962,12 @@ class Crud:
def _add_base(view): def _add_base(view):
if view: if view:
class CrudViewWithBase(cls.BaseMixin, view): class CrudViewWithBase(cls.BaseMixin, view):
model = cls.model model = cls.model
help_topic = cls.help_topic help_topic = cls.help_topic
crud = cls crud = cls
CrudViewWithBase.__name__ = view.__name__ CrudViewWithBase.__name__ = view.__name__
return CrudViewWithBase return CrudViewWithBase
@ -995,11 +1001,13 @@ class Crud:
def build(cls, _model, _help_topic, _model_set=None, list_field_names=[]): def build(cls, _model, _help_topic, _model_set=None, list_field_names=[]):
def create_class(_list_field_names): def create_class(_list_field_names):
class ModelCrud(cls): class ModelCrud(cls):
model = _model model = _model
model_set = _model_set model_set = _model_set
help_topic = _help_topic help_topic = _help_topic
list_field_names = _list_field_names list_field_names = _list_field_names
return ModelCrud return ModelCrud
ModelCrud = create_class(list_field_names) ModelCrud = create_class(list_field_names)

Loading…
Cancel
Save