|
@@ -1,20 +1,32 @@
|
|
|
+# coding=utf-8
|
|
|
+
|
|
|
__author__ = 'marcos'
|
|
|
|
|
|
+from django.contrib.contenttypes.models import ContentType
|
|
|
+from rapid.forms import getDocumentTemplateForm
|
|
|
+from rapid.models import DocumentTemplate
|
|
|
from rapid.views import bare_or_main, ListView, ReadView, UpdateView, CreateView, SelectView, DeleteView,\
|
|
|
- update_form_class
|
|
|
-from rapid import permissions
|
|
|
+ update_form_class, RenderDocumentView, add_form_class, get_delete_document_template_view
|
|
|
from rapid.registry import Action, MenuEntry, registry
|
|
|
|
|
|
|
|
|
def _rvw(view_class, action_name):
|
|
|
view_class.action_name = action_name
|
|
|
|
|
|
+ # noinspection PyShadowingNames
|
|
|
def vw(model):
|
|
|
return bare_or_main(view_class.as_view(model=model))
|
|
|
return vw
|
|
|
|
|
|
|
|
|
-_default_actions = [
|
|
|
+def _un_none(v):
|
|
|
+ if v is None:
|
|
|
+ return []
|
|
|
+ else:
|
|
|
+ return [v]
|
|
|
+
|
|
|
+
|
|
|
+_crud_actions = [
|
|
|
(False, Action("list", "", [], _rvw(ListView, 'list'), "listar", "fa-list")),
|
|
|
(False, Action("view", "(?P<pk>[0-9]+)", ['pk'], _rvw(ReadView, 'view'), "ver", "fa-eye")),
|
|
|
(True, Action("edit", "(?P<pk>[0-9]+)", ['pk'], _rvw(UpdateView, 'edit'), "editar", "fa-pencil")),
|
|
@@ -24,11 +36,12 @@ _default_actions = [
|
|
|
]
|
|
|
|
|
|
|
|
|
-def model(model, read_set, write_set, actions=None, url_name=None, can_erase=False):
|
|
|
+# noinspection PyShadowingNames
|
|
|
+def crud(model, read_set, write_set, actions=None, url_name=None, can_erase=False):
|
|
|
if actions:
|
|
|
- actions = [(edt, a) for edt, a in _default_actions if a.name in actions]
|
|
|
+ actions = [(edt, a) for edt, a in _crud_actions if a.name in actions]
|
|
|
else:
|
|
|
- actions = _default_actions
|
|
|
+ actions = _crud_actions
|
|
|
ret = []
|
|
|
for edt, a in actions:
|
|
|
if edt:
|
|
@@ -36,24 +49,55 @@ def model(model, read_set, write_set, actions=None, url_name=None, can_erase=Fal
|
|
|
else:
|
|
|
ret.append(registry.register_action(a, MenuEntry(model, read_set, url_name=url_name)))
|
|
|
if can_erase:
|
|
|
- a = Action("delete", "(?P<pk>[0-9]+)", ['pk'], _rvw(DeleteView, 'delete'), "apagar", "fa-trash-o")
|
|
|
+ a = Action("delete", "(?P<pk>[0-9]+)", ['pk'], _rvw(DeleteView, 'delete'), "apagar", "fa-trash-o")
|
|
|
ret.append(registry.register_action(a, MenuEntry(model, write_set, url_name=url_name)))
|
|
|
return [u for u in ret if u]
|
|
|
|
|
|
-
|
|
|
-def instance_form(model, action_name, entry_name, form, permission_set, url_name=None, icon=None, visibility=None):
|
|
|
+# noinspection PyShadowingNames
|
|
|
+def instance_form(model, action_name, entry_name, form, permission_set,
|
|
|
+ url_name=None, icon=None, visibility=None):
|
|
|
if not icon:
|
|
|
icon = ''
|
|
|
a = Action(action_name, "(?P<pk>[0-9]+)", ['pk'], _rvw(update_form_class(form), action_name),
|
|
|
entry_name, icon, visibility)
|
|
|
u = registry.register_action(a, MenuEntry(model, permission_set, url_name=url_name))
|
|
|
- return [u] if u else []
|
|
|
+ return _un_none(u)
|
|
|
|
|
|
|
|
|
+# noinspection PyShadowingNames
|
|
|
def select(model, visible_fields, permission_set, url_name=None):
|
|
|
class Vw(SelectView):
|
|
|
fields = visible_fields
|
|
|
a = Action("select", "", [], _rvw(Vw, 'select'), "selecionar", "fa-hand-o-up",
|
|
|
visibility=Action.Visibility.hidden)
|
|
|
u = registry.register_action(a, MenuEntry(model, permission_set, url_name=url_name))
|
|
|
- return [u] if u else []
|
|
|
+ return _un_none(u)
|
|
|
+
|
|
|
+
|
|
|
+# noinspection PyShadowingNames
|
|
|
+def custom_action(model, action, permission, url_name=None):
|
|
|
+ u = registry.register_action(action, MenuEntry(model, permission, url_name))
|
|
|
+ return _un_none(u)
|
|
|
+
|
|
|
+
|
|
|
+# noinspection PyShadowingNames
|
|
|
+def document_templates(model, templates_permission, render_permission, url_name=None,
|
|
|
+ render_visibility=Action.Visibility.details):
|
|
|
+ ct_model = ContentType.objects.get_for_model(model)
|
|
|
+ template_generator = lambda n: lambda: ((d.name, [(n, d.pk),],) for
|
|
|
+ d in DocumentTemplate.objects.filter(model=ct_model))
|
|
|
+ model_class = model # renamimg because of clash with the next function argument
|
|
|
+ add_act = Action("add-template", "", (), _rvw(add_form_class(getDocumentTemplateForm(model)), 'add-template'),
|
|
|
+ "Novo Template", "fa-upload")
|
|
|
+ del_act = Action("del-template", "(?P<pk>[0-9]+)", (),
|
|
|
+ _rvw(get_delete_document_template_view(model), 'del-template'), "Excluir Template",
|
|
|
+ 'fa-trash-o', pick_generator=template_generator('pk'))
|
|
|
+ render_act = Action("render-template", "(?P<pk>[0-9]+)/(?P<template>[0-9]+)", ('pk',),
|
|
|
+ RenderDocumentView.as_view,
|
|
|
+ "Gerar Documento", 'fa-file-word-o', render_visibility,
|
|
|
+ pick_generator=template_generator('template'),
|
|
|
+ overlay=Action.Overlay.clear_overlays)
|
|
|
+ add_r = registry.register_action(add_act, MenuEntry(model, templates_permission, url_name=url_name))
|
|
|
+ del_r = registry.register_action(del_act, MenuEntry(model, templates_permission, url_name=url_name))
|
|
|
+ render_r = registry.register_action(render_act, MenuEntry(model, render_permission, url_name=url_name))
|
|
|
+ return _un_none(add_r) + _un_none(del_r) + _un_none(render_r)
|