web/views/actions.py
changeset 401 bc55a104d636
parent 328 02abf429dd91
child 431 18b4dd650ef8
equal deleted inserted replaced
400:b05a36678265 401:bc55a104d636
     1 """Set of HTML base actions
     1 """Set of HTML base actions
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 from cubicweb import UnknownEid
     9 from cubicweb import UnknownEid
    14 from cubicweb.web.views import linksearch_select_url, linksearch_match
    14 from cubicweb.web.views import linksearch_select_url, linksearch_match
    15 from cubicweb.web.views.baseviews import vid_from_rset
    15 from cubicweb.web.views.baseviews import vid_from_rset
    16 
    16 
    17 _ = unicode
    17 _ = unicode
    18 
    18 
       
    19 # generic primary actions #####################################################
    19 
    20 
    20 class SelectAction(EntityAction):
    21 class SelectAction(EntityAction):
    21     """base class for link search actions. By default apply on
    22     """base class for link search actions. By default apply on
    22     any size entity result search it the current state is 'linksearch'
    23     any size entity result search it the current state is 'linksearch'
    23     if accept match.
    24     if accept match.
    71     
    72     
    72     def url(self):
    73     def url(self):
    73         params = self.req.form.copy()
    74         params = self.req.form.copy()
    74         params.pop('vid', None)
    75         params.pop('vid', None)
    75         params.pop('__message', None)
    76         params.pop('__message', None)
    76         return self.build_url(self.req.relative_path(includeparams=False), **params)
    77         return self.build_url(self.req.relative_path(includeparams=False),
       
    78                               **params)
    77 
    79 
    78 
    80 
    79 class ModifyAction(EntityAction):
    81 class ModifyAction(EntityAction):
    80     category = 'mainactions'
    82     category = 'mainactions'
    81     __selectors__ = (one_line_rset, searchstate_accept)
    83     __selectors__ = (one_line_rset, searchstate_accept)
    90     def has_permission(cls, entity, action):
    92     def has_permission(cls, entity, action):
    91         if entity.has_perm(action):
    93         if entity.has_perm(action):
    92             return True
    94             return True
    93         # if user has no update right but it can modify some relation,
    95         # if user has no update right but it can modify some relation,
    94         # display action anyway
    96         # display action anyway
    95         for dummy in entity.srelations_by_category(('generic', 'metadata'), 'add'):
    97         for dummy in entity.srelations_by_category(('generic', 'metadata'),
       
    98                                                    'add'):
    96             return True
    99             return True
    97         for rschema, targetschemas, role in entity.relations_by_category(('primary', 'secondary'), 'add'):
   100         for rschema, targetschemas, role in entity.relations_by_category(
       
   101             ('primary', 'secondary'), 'add'):
    98             if not rschema.is_final():
   102             if not rschema.is_final():
    99                 return True
   103                 return True
   100         return False
   104         return False
   101 
   105 
   102     def url(self):
   106     def url(self):
   103         entity = self.rset.get_entity(self.row or 0, self.col or 0)
   107         entity = self.rset.get_entity(self.row or 0, self.col or 0)
   104         return entity.absolute_url(vid='edition')
   108         return entity.absolute_url(vid='edition')
       
   109         
       
   110 
       
   111 class MultipleEditAction(EntityAction):
       
   112     category = 'mainactions'
       
   113     __selectors__ = (two_lines_rset, oneetyperset_selector,
       
   114                      searchstate_accept)
       
   115     schema_action = 'update'
       
   116     order = 10
       
   117     
       
   118     id = 'muledit' # XXX get strange conflicts if id='edit'
       
   119     title = _('modify')
       
   120     
       
   121     def url(self):
       
   122         return self.build_url('view', rql=self.rset.rql, vid='muledit')
       
   123 
       
   124 
       
   125 # generic secondary actions ###################################################
       
   126 
       
   127 class ManagePermissions(LinkToEntityAction):
       
   128     accepts = ('Any',)
       
   129     category = 'moreactions'
       
   130     id = 'addpermission'
       
   131     title = _('manage permissions')
       
   132     order = 100
       
   133 
       
   134     etype = 'EPermission'
       
   135     rtype = 'require_permission'
       
   136     target = 'object'
       
   137     
       
   138     def url(self):
       
   139         return self.rset.get_entity(0, 0).absolute_url(vid='security')
   105 
   140 
   106     
   141     
   107 class DeleteAction(EntityAction):
   142 class DeleteAction(EntityAction):
   108     category = 'moreactions' 
   143     category = 'moreactions' 
   109     __selectors__ = (searchstate_accept,)
   144     __selectors__ = (searchstate_accept,)
   129     title = _('copy')
   164     title = _('copy')
   130     
   165     
   131     def url(self):
   166     def url(self):
   132         entity = self.rset.get_entity(self.row or 0, self.col or 0)
   167         entity = self.rset.get_entity(self.row or 0, self.col or 0)
   133         return entity.absolute_url(vid='copy')
   168         return entity.absolute_url(vid='copy')
   134         
       
   135 
       
   136 class MultipleEditAction(EntityAction):
       
   137     category = 'mainactions'
       
   138     __selectors__ = (two_lines_rset, oneetyperset_selector,
       
   139                      searchstate_accept)
       
   140     schema_action = 'update'
       
   141     order = 10
       
   142     
       
   143     id = 'muledit' # XXX get strange conflicts if id='edit'
       
   144     title = _('modify')
       
   145     
       
   146     def url(self):
       
   147         return self.build_url('view', rql=self.rset.rql, vid='muledit')
       
   148 
   169 
   149 
   170 
   150 class AddNewAction(MultipleEditAction):
   171 class AddNewAction(MultipleEditAction):
   151     """when we're seeing more than one entity with the same type, propose to
   172     """when we're seeing more than one entity with the same type, propose to
   152     add a new one
   173     add a new one
   153     """
   174     """
   154     category = 'moreactions'
   175     category = 'moreactions'
       
   176     id = 'addentity'
       
   177     order = 40
   155     
   178     
   156     def etype_rset_selector(cls, req, rset, **kwargs):
   179     def etype_rset_selector(cls, req, rset, **kwargs):
   157         if rset is not None and not rset.rowcount:
   180         if rset is not None and not rset.rowcount:
   158             rqlst = rset.syntax_tree()
   181             rqlst = rset.syntax_tree()
   159             if len(rqlst.children) > 1:
   182             if len(rqlst.children) > 1:
   175         return 0
   198         return 0
   176     __selectors__ = (match_search_state,
   199     __selectors__ = (match_search_state,
   177                      chainfirst(etype_rset_selector,
   200                      chainfirst(etype_rset_selector,
   178                                 chainall(two_lines_rset, oneetyperset_selector,
   201                                 chainall(two_lines_rset, oneetyperset_selector,
   179                                          has_add_perm_selector)))
   202                                          has_add_perm_selector)))
   180     order = 40
       
   181     id = 'addentity'
       
   182 
   203 
   183     @property
   204     @property
   184     def rsettype(self):
   205     def rsettype(self):
   185         if self.rset:
   206         if self.rset:
   186             return self.rset.description[0][0]
   207             return self.rset.description[0][0]
   192 
   213 
   193     def url(self):
   214     def url(self):
   194         return self.build_url('add/%s' % self.rsettype)
   215         return self.build_url('add/%s' % self.rsettype)
   195 
   216 
   196 
   217 
   197 class FollowAction(EntityAction):
   218 # logged user actions #########################################################
   198     category = 'mainactions'
       
   199     accepts = ('Bookmark',)
       
   200     
       
   201     id = 'follow'
       
   202     title = _('follow')
       
   203     
       
   204     def url(self):
       
   205         return self.rset.get_entity(self.row or 0, self.col or 0).actual_url()
       
   206 
   219 
   207 class UserPreferencesAction(Action):
   220 class UserPreferencesAction(Action):
   208     category = 'useractions'
   221     category = 'useractions'
   209     __selectors__ = not_anonymous_selector,
   222     __selectors__ = not_anonymous_selector,
   210     order = 10
   223     order = 10
   213     title = _('user preferences')
   226     title = _('user preferences')
   214 
   227 
   215     def url(self):
   228     def url(self):
   216         return self.build_url(self.id)
   229         return self.build_url(self.id)
   217 
   230 
   218 class UserPreferencesEntityAction(EntityAction):
       
   219     __selectors__ = EntityAction.__selectors__ + (one_line_rset, in_group_selector,)
       
   220     require_groups = ('owners', 'managers')
       
   221     category = 'mainactions'
       
   222     accepts = ('EUser',)
       
   223     
       
   224     id = 'prefs'
       
   225     title = _('preferences')
       
   226     
       
   227     def url(self):
       
   228         login = self.rset.get_entity(self.row or 0, self.col or 0).login
       
   229         return self.build_url('euser/%s'%login, vid='epropertiesform')
       
   230 
   231 
   231 class UserInfoAction(Action):
   232 class UserInfoAction(Action):
   232     category = 'useractions'
   233     category = 'useractions'
   233     __selectors__ = not_anonymous_selector,
   234     __selectors__ = not_anonymous_selector,
   234     order = 20
   235     order = 20
   237     title = _('personnal informations')
   238     title = _('personnal informations')
   238 
   239 
   239     def url(self):
   240     def url(self):
   240         return self.build_url('euser/%s'%self.req.user.login, vid='edition')
   241         return self.build_url('euser/%s'%self.req.user.login, vid='edition')
   241 
   242 
       
   243 
   242 class LogoutAction(Action):
   244 class LogoutAction(Action):
   243     category = 'useractions'
   245     category = 'useractions'
   244     __selectors__ = not_anonymous_selector,
   246     __selectors__ = not_anonymous_selector,
   245     order = 30
   247     order = 30
   246     
   248     
   247     id = 'logout'
   249     id = 'logout'
   248     title = _('logout')
   250     title = _('logout')
   249 
   251 
   250     def url(self):
   252     def url(self):
   251         return self.build_url(self.id)
   253         return self.build_url(self.id)
   252     
   254 
       
   255     
       
   256 # site actions ################################################################
   253 
   257 
   254 class ManagersAction(Action):
   258 class ManagersAction(Action):
   255     category = 'siteactions'
   259     category = 'siteactions'
   256     __abstract__ = True
   260     __abstract__ = True
   257     __selectors__ = in_group_selector,
   261     __selectors__ = in_group_selector,
   258     require_groups = ('managers',)
   262     require_groups = ('managers',)
   259 
   263 
   260     def url(self):
   264     def url(self):
   261         return self.build_url(self.id)
   265         return self.build_url(self.id)
       
   266 
   262     
   267     
   263 class SiteConfigurationAction(ManagersAction):
   268 class SiteConfigurationAction(ManagersAction):
   264     order = 10
   269     order = 10
   265     
       
   266     id = 'siteconfig'
   270     id = 'siteconfig'
   267     title = _('site configuration')
   271     title = _('site configuration')
       
   272 
   268     
   273     
   269 class ManageAction(ManagersAction):
   274 class ManageAction(ManagersAction):
   270     order = 20
   275     order = 20
   271     
       
   272     id = 'manage'
   276     id = 'manage'
   273     title = _('manage')
   277     title = _('manage')
   274 
   278 
   275 
   279 
   276 class ViewSchemaAction(Action):
   280 class ViewSchemaAction(Action):
   277     category = 'siteactions'
   281     category = 'siteactions'
       
   282     id = 'schema'
       
   283     title = _("site schema")
   278     __selectors__ = yes,
   284     __selectors__ = yes,
   279     order = 30
   285     order = 30
   280     
   286     
   281     id = 'schema'
       
   282     title = _("site schema")
       
   283 
       
   284     def url(self):
   287     def url(self):
   285         return self.build_url(self.id)
   288         return self.build_url(self.id)
   286 
   289 
       
   290 
       
   291 # content type specific actions ###############################################
       
   292 
       
   293 class FollowAction(EntityAction):
       
   294     category = 'mainactions'
       
   295     accepts = ('Bookmark',)
       
   296     
       
   297     id = 'follow'
       
   298     title = _('follow')
       
   299     
       
   300     def url(self):
       
   301         return self.rset.get_entity(self.row or 0, self.col or 0).actual_url()
       
   302 
       
   303 class UserPreferencesEntityAction(EntityAction):
       
   304     __selectors__ = EntityAction.__selectors__ + (one_line_rset, in_group_selector,)
       
   305     require_groups = ('owners', 'managers')
       
   306     category = 'mainactions'
       
   307     accepts = ('EUser',)
       
   308     
       
   309     id = 'prefs'
       
   310     title = _('preferences')
       
   311     
       
   312     def url(self):
       
   313         login = self.rset.get_entity(self.row or 0, self.col or 0).login
       
   314         return self.build_url('euser/%s'%login, vid='epropertiesform')
       
   315