forked from ptahproject/ptah
/
uiactions.py
107 lines (84 loc) · 2.99 KB
/
uiactions.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
""" ui actions """
import ptah
from ptah import config
from zope.interface import implementer, providedBy, Interface
ID_UIACTION = 'ptah:uiaction'
class IAction(Interface):
""" marker interface for actions """
@implementer(IAction)
class Action(object):
""" UI Action implementation """
id = ''
title = ''
description = ''
category = ''
action = ''
action_factory = None
condition = None
permission = None
sort_weight = 1.0,
data = None
def __init__(self, id='', **kw):
self.id = id
self.__dict__.update(kw)
def url(self, context, request, url=''):
if self.action_factory is not None:
return self.action_factory(context, request)
if self.action.startswith('/'):
return '%s%s'%(request.application_url, self.action)
else:
return '%s%s'%(url, self.action)
def check(self, context, request):
if self.permission:
if not ptah.check_permission(
self.permission, context, request):
return False
if self.condition is not None:
return self.condition(context, request)
return True
def uiaction(context, id, title, description='',
action='', condition=None, permission=None,
category='', sort_weight = 1.0, **kw):
""" decorator for uiaction registration """
kwargs = {'id': id,
'title': title,
'description': description,
'category': category,
'condition': condition,
'permission': permission,
'sort_weight': sort_weight,
'data': kw}
if callable(action):
kwargs['action_factory'] = action
else:
kwargs['action'] = action
ac = Action(**kwargs)
info = config.DirectiveInfo()
discr = (ID_UIACTION, id, context, category)
intr = ptah.config.Introspectable(
ID_UIACTION, discr, title, ID_UIACTION)
intr['action'] = ac
intr['codeinfo'] = info.codeinfo
info.attach(
config.Action(
lambda cfg, id, category, context, ac: \
cfg.registry.registerAdapter(\
ac, (context,), IAction, '%s-%s'%(category, id)),
(id, category, context, ac,),
discriminator = discr, introspectables = (intr,))
)
def list_uiactions(content, request, category=''):
""" list uiactions for specific content """
url = request.resource_url(content)
actions = []
for name, action in request.registry.adapters.lookupAll(
(providedBy(content),), IAction):
if (action.category == category) and action.check(content, request):
actions.append(
(action.sort_weight,
{'id': action.id,
'url': action.url(content, request, url),
'title': action.title,
'description': action.description,
'data': action.data}))
return [ac for _w, ac in sorted(actions)]