actions.py 8.96 KB
Newer Older
Adrien Di Mascio's avatar
Adrien Di Mascio committed
1
2
3
"""Set of HTML base actions

:organization: Logilab
4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Adrien Di Mascio's avatar
Adrien Di Mascio committed
5
6
7
8
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"

9
10
11
12
from cubicweb.common.selectors import (searchstate_accept, match_user_group, yes,
                                       one_line_rset, two_lines_rset, one_etype_rset,
                                       authenticated_user,
                                       match_search_state, chainfirst, chainall)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
13

14
from cubicweb.web.action import Action, EntityAction,  LinkToEntityAction
Adrien Di Mascio's avatar
Adrien Di Mascio committed
15
16
17
18
19
from cubicweb.web.views import linksearch_select_url, linksearch_match
from cubicweb.web.views.baseviews import vid_from_rset

_ = unicode

20
# generic primary actions #####################################################
Adrien Di Mascio's avatar
Adrien Di Mascio committed
21
22
23
24
25
26
27

class SelectAction(EntityAction):
    """base class for link search actions. By default apply on
    any size entity result search it the current state is 'linksearch'
    if accept match.
    """
    category = 'mainactions'    
Sylvain Thenault's avatar
Sylvain Thenault committed
28
    __selectors__ = (searchstate_accept,)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
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
    search_states = ('linksearch',)
    order = 0
    
    id = 'select'
    title = _('select')
    
    @classmethod
    def accept_rset(cls, req, rset, row, col):
        return linksearch_match(req, rset)
    
    def url(self):
        return linksearch_select_url(self.req, self.rset)


class CancelSelectAction(Action):
    category = 'mainactions'
    search_states = ('linksearch',)
    order = 10
    
    id = 'cancel'
    title = _('cancel select')
    
    def url(self):
        target, link_eid, r_type, searched_type = self.req.search_state[1]
        return self.build_url(rql="Any X WHERE X eid %s" % link_eid,
                              vid='edition', __mode='normal')


class ViewAction(Action):
    category = 'mainactions'    
59
    __selectors__ = (match_user_group, searchstate_accept)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    require_groups = ('users', 'managers')
    order = 0
    
    id = 'view'
    title = _('view')
    
    @classmethod
    def accept_rset(cls, req, rset, row, col):
        # interesting if it propose another view than the current one
        vid = req.form.get('vid')
        if vid and vid != vid_from_rset(req, rset, cls.schema):
            return 1
        return 0
    
    def url(self):
        params = self.req.form.copy()
        params.pop('vid', None)
        params.pop('__message', None)
78
79
        return self.build_url(self.req.relative_path(includeparams=False),
                              **params)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
80
81
82
83


class ModifyAction(EntityAction):
    category = 'mainactions'
Sylvain Thenault's avatar
Sylvain Thenault committed
84
85
    __selectors__ = (one_line_rset, searchstate_accept)
    #__selectors__ = searchstate_accept,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
86
87
88
89
90
91
92
93
94
95
96
97
    schema_action = 'update'
    order = 10
    
    id = 'edit'
    title = _('modify')
    
    @classmethod
    def has_permission(cls, entity, action):
        if entity.has_perm(action):
            return True
        # if user has no update right but it can modify some relation,
        # display action anyway
98
99
        for dummy in entity.srelations_by_category(('generic', 'metadata'),
                                                   'add'):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
100
            return True
101
102
        for rschema, targetschemas, role in entity.relations_by_category(
            ('primary', 'secondary'), 'add'):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
103
104
105
106
107
108
109
            if not rschema.is_final():
                return True
        return False

    def url(self):
        entity = self.rset.get_entity(self.row or 0, self.col or 0)
        return entity.absolute_url(vid='edition')
110
111
112
113
        

class MultipleEditAction(EntityAction):
    category = 'mainactions'
114
    __selectors__ = (two_lines_rset, one_etype_rset,
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
                     searchstate_accept)
    schema_action = 'update'
    order = 10
    
    id = 'muledit' # XXX get strange conflicts if id='edit'
    title = _('modify')
    
    def url(self):
        return self.build_url('view', rql=self.rset.rql, vid='muledit')


# generic secondary actions ###################################################

class ManagePermissions(LinkToEntityAction):
    accepts = ('Any',)
    category = 'moreactions'
    id = 'addpermission'
    title = _('manage permissions')
    order = 100

    etype = 'EPermission'
    rtype = 'require_permission'
    target = 'object'
    
    def url(self):
        return self.rset.get_entity(0, 0).absolute_url(vid='security')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
141
142
143
144

    
class DeleteAction(EntityAction):
    category = 'moreactions' 
Sylvain Thenault's avatar
Sylvain Thenault committed
145
    __selectors__ = (searchstate_accept,)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    schema_action = 'delete'
    order = 20
    
    id = 'delete'
    title = _('delete')
    
    def url(self):
        if len(self.rset) == 1:
            entity = self.rset.get_entity(0, 0)
            return self.build_url(entity.rest_path(), vid='deleteconf')
        return self.build_url(rql=self.rset.printable_rql(), vid='deleteconf')
    
        
class CopyAction(EntityAction):
    category = 'moreactions'
    schema_action = 'add'
    order = 30
    
    id = 'copy'
    title = _('copy')
    
    def url(self):
        entity = self.rset.get_entity(self.row or 0, self.col or 0)
        return entity.absolute_url(vid='copy')


class AddNewAction(MultipleEditAction):
    """when we're seeing more than one entity with the same type, propose to
    add a new one
    """
    category = 'moreactions'
177
178
    id = 'addentity'
    order = 40
Adrien Di Mascio's avatar
Adrien Di Mascio committed
179
180
181
182
183
184
185
186
    
    def etype_rset_selector(cls, req, rset, **kwargs):
        if rset is not None and not rset.rowcount:
            rqlst = rset.syntax_tree()
            if len(rqlst.children) > 1:
                return 0
            select = rqlst.children[0]
            if len(select.defined_vars) == 1 and len(select.solutions) == 1:
187
188
189
190
191
                rset._searched_etype = select.solutions[0].itervalues().next()
                eschema = cls.schema.eschema(rset._searched_etype)
                if not (eschema.is_final() or eschema.is_subobject(strict=True)) \
                       and eschema.has_perm(req, 'add'):
                    return 1
Adrien Di Mascio's avatar
Adrien Di Mascio committed
192
193
194
195
        return 0

    def has_add_perm_selector(cls, req, rset, **kwargs):
        eschema = cls.schema.eschema(rset.description[0][0])
196
197
        if not (eschema.is_final() or eschema.is_subobject(strict=True)) \
               and eschema.has_perm(req, 'add'):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
198
199
            return 1
        return 0
Sylvain Thenault's avatar
Sylvain Thenault committed
200
    __selectors__ = (match_search_state,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
201
                     chainfirst(etype_rset_selector,
202
                                chainall(two_lines_rset, one_etype_rset,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
                                         has_add_perm_selector)))

    @property
    def rsettype(self):
        if self.rset:
            return self.rset.description[0][0]
        return self.rset._searched_etype

    @property
    def title(self):
        return self.req.__('add a %s' % self.rsettype) # generated msgid

    def url(self):
        return self.build_url('add/%s' % self.rsettype)


219
# logged user actions #########################################################
Adrien Di Mascio's avatar
Adrien Di Mascio committed
220
221
222

class UserPreferencesAction(Action):
    category = 'useractions'
223
    __selectors__ = authenticated_user,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
224
225
226
227
228
229
230
231
232
233
234
    order = 10
    
    id = 'myprefs'
    title = _('user preferences')

    def url(self):
        return self.build_url(self.id)


class UserInfoAction(Action):
    category = 'useractions'
235
    __selectors__ = authenticated_user,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
236
237
238
239
240
241
242
243
    order = 20
    
    id = 'myinfos'
    title = _('personnal informations')

    def url(self):
        return self.build_url('euser/%s'%self.req.user.login, vid='edition')

244

Adrien Di Mascio's avatar
Adrien Di Mascio committed
245
246
class LogoutAction(Action):
    category = 'useractions'
247
    __selectors__ = authenticated_user,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
248
249
250
251
252
253
254
    order = 30
    
    id = 'logout'
    title = _('logout')

    def url(self):
        return self.build_url(self.id)
255

Adrien Di Mascio's avatar
Adrien Di Mascio committed
256
    
257
# site actions ################################################################
Adrien Di Mascio's avatar
Adrien Di Mascio committed
258
259
260
261

class ManagersAction(Action):
    category = 'siteactions'
    __abstract__ = True
262
    __selectors__ = match_user_group,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
263
264
265
266
    require_groups = ('managers',)

    def url(self):
        return self.build_url(self.id)
267

Adrien Di Mascio's avatar
Adrien Di Mascio committed
268
269
270
271
272
    
class SiteConfigurationAction(ManagersAction):
    order = 10
    id = 'siteconfig'
    title = _('site configuration')
273

Adrien Di Mascio's avatar
Adrien Di Mascio committed
274
275
276
277
278
279
280
281
282
    
class ManageAction(ManagersAction):
    order = 20
    id = 'manage'
    title = _('manage')


class ViewSchemaAction(Action):
    category = 'siteactions'
283
284
    id = 'schema'
    title = _("site schema")
285
    __selectors__ = yes,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
286
287
288
289
290
    order = 30
    
    def url(self):
        return self.build_url(self.id)

291
292
293
294
295
296
297
298
299
300
301
302
303
304

# content type specific actions ###############################################

class FollowAction(EntityAction):
    category = 'mainactions'
    accepts = ('Bookmark',)
    
    id = 'follow'
    title = _('follow')
    
    def url(self):
        return self.rset.get_entity(self.row or 0, self.col or 0).actual_url()

class UserPreferencesEntityAction(EntityAction):
305
    __selectors__ = EntityAction.__selectors__ + (one_line_rset, match_user_group,)
306
307
308
309
310
311
312
313
314
315
316
    require_groups = ('owners', 'managers')
    category = 'mainactions'
    accepts = ('EUser',)
    
    id = 'prefs'
    title = _('preferences')
    
    def url(self):
        login = self.rset.get_entity(self.row or 0, self.col or 0).login
        return self.build_url('euser/%s'%login, vid='epropertiesform')