/
controller.py
86 lines (73 loc) · 3.05 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Admin Controller"""
from sqlalchemy.orm import class_mapper
import inspect
from tg.controllers import TGController
from tg.decorators import with_trailing_slash, override_template, expose
from tg.exceptions import HTTPNotFound
Rum = None
from tgext.crud import CrudRestController
from config import AdminConfig
engine = 'genshi'
try:
import chameleon.genshi
import pylons.config
if 'renderers' in pylons.config and 'chameleon_genshi' in pylons.config['renderers']:
engine = 'chameleon_genshi'
except ImportError:
pass
from repoze.what.predicates import in_group
class AdminController(TGController):
"""
A basic controller that handles User Groups and Permissions for a TG application.
"""
allow_only = in_group('managers')
def __init__(self, models, session, config_type=None, translations=None):
super(AdminController, self).__init__()
if translations is None:
translations = {}
if config_type is None:
config = AdminConfig(models, translations)
else:
config = config_type(models, translations)
if config.allow_only:
self.allow_only = config.allow_only
self.config = config
self.session = session
self.default_index_template = ':'.join(self.index.decoration.engines.get('text/html')[:2])
if self.config.default_index_template:
self.default_index_template = self.config.default_index_template
@with_trailing_slash
@expose('tgext.admin.templates.index')
def index(self):
#overrides the template for this method
original_index_template = self.index.decoration.engines['text/html']
new_engine = self.default_index_template.split(':')
new_engine.extend(original_index_template[2:])
self.index.decoration.engines['text/html'] = new_engine
return dict(models=[model.__name__ for model in self.config.models.values()])
def _make_controller(self, config, session):
m = config.model
Controller = config.defaultCrudRestController
class ModelController(Controller):
model = m
table = config.table_type(session)
table_filler = config.table_filler_type(session)
new_form = config.new_form_type(session)
new_filler = config.new_filler_type(session)
edit_form = config.edit_form_type(session)
edit_filler = config.edit_filler_type(session)
allow_only = config.allow_only
menu_items = None
if self.config.include_left_menu:
menu_items = self.config.models
return ModelController(session, menu_items)
@expose()
def lookup(self, model_name, *args):
model_name = model_name[:-1]
try:
model = self.config.models[model_name]
except KeyError:
raise HTTPNotFound().exception
config = self.config.lookup_controller_config(model_name)
controller = self._make_controller(config, self.session)
return controller, args