web/views/basecomponents.py
branchstable
changeset 5148 ec0ea7366066
parent 4935 f710770e6e90
child 5195 e8235b2789fc
child 5421 8167de96c523
equal deleted inserted replaced
5147:70181998897f 5148:ec0ea7366066
    13 _ = unicode
    13 _ = unicode
    14 
    14 
    15 from logilab.mtconverter import xml_escape
    15 from logilab.mtconverter import xml_escape
    16 from rql import parse
    16 from rql import parse
    17 
    17 
    18 from cubicweb.selectors import yes, multi_etypes_rset, match_form_params
    18 from cubicweb.selectors import (yes, multi_etypes_rset, match_form_params,
       
    19                                 anonymous_user, authenticated_user)
    19 from cubicweb.schema import display_name
    20 from cubicweb.schema import display_name
    20 from cubicweb.uilib import toggle_action
    21 from cubicweb.uilib import toggle_action
    21 from cubicweb.web import component
    22 from cubicweb.web import component
    22 from cubicweb.web.htmlwidgets import (MenuWidget, PopupBoxMenu, BoxSeparator,
    23 from cubicweb.web.htmlwidgets import (MenuWidget, PopupBoxMenu, BoxSeparator,
    23                                       BoxLink)
    24                                       BoxLink)
    77         self.w(u'<a href="%s" class="help" title="%s">&#160;</a>'
    78         self.w(u'<a href="%s" class="help" title="%s">&#160;</a>'
    78                % (self._cw.build_url(_restpath='doc/main'),
    79                % (self._cw.build_url(_restpath='doc/main'),
    79                   self._cw._(u'help'),))
    80                   self._cw._(u'help'),))
    80 
    81 
    81 
    82 
    82 class UserLink(component.Component):
    83 class _UserLink(component.Component):
    83     """if the user is the anonymous user, build a link to login
    84     """if the user is the anonymous user, build a link to login else display a menu
    84     else a link to the connected user object with a loggout link
    85     with user'action (preference, logout, etc...)
    85     """
    86     """
    86     cw_property_defs = VISIBLE_PROP_DEF
    87     cw_property_defs = VISIBLE_PROP_DEF
    87     # don't want user to hide this component using an cwproperty
    88     # don't want user to hide this component using an cwproperty
    88     site_wide = True
    89     site_wide = True
    89     __regid__ = 'loggeduserlink'
    90     __regid__ = 'loggeduserlink'
    90 
    91 
    91     def call(self):
    92 
    92         if not self._cw.cnx.anonymous_connection:
    93 class AnonUserLink(_UserLink):
    93             # display useractions and siteactions
    94     __select__ = _UserLink.__select__ & anonymous_user()
    94             actions = self._cw.vreg['actions'].possible_actions(self._cw, rset=self.cw_rset)
    95     def call(self):
    95             box = MenuWidget('', 'userActionsBox', _class='', islist=False)
       
    96             menu = PopupBoxMenu(self._cw.user.login, isitem=False)
       
    97             box.append(menu)
       
    98             for action in actions.get('useractions', ()):
       
    99                 menu.append(BoxLink(action.url(), self._cw._(action.title),
       
   100                                     action.html_class()))
       
   101             if actions.get('useractions') and actions.get('siteactions'):
       
   102                 menu.append(BoxSeparator())
       
   103             for action in actions.get('siteactions', ()):
       
   104                 menu.append(BoxLink(action.url(), self._cw._(action.title),
       
   105                                     action.html_class()))
       
   106             box.render(w=self.w)
       
   107         else:
       
   108             self.anon_user_link()
       
   109 
       
   110     def anon_user_link(self):
       
   111         if self._cw.vreg.config['auth-mode'] == 'cookie':
    96         if self._cw.vreg.config['auth-mode'] == 'cookie':
   112             self.w(self._cw._('anonymous'))
    97             self.w(self._cw._('anonymous'))
   113             self.w(u'''&#160;[<a class="logout" href="javascript: popupLoginBox();">%s</a>]'''
    98             self.w(u'''&#160;[<a class="logout" href="javascript: popupLoginBox();">%s</a>]'''
   114                    % (self._cw._('i18n_login_popup')))
    99                    % (self._cw._('i18n_login_popup')))
   115         else:
   100         else:
   116             self.w(self._cw._('anonymous'))
   101             self.w(self._cw._('anonymous'))
   117             self.w(u'&#160;[<a class="logout" href="%s">%s</a>]'
   102             self.w(u'&#160;[<a class="logout" href="%s">%s</a>]'
   118                    % (self._cw.build_url('login'), self._cw._('login')))
   103                    % (self._cw.build_url('login'), self._cw._('login')))
       
   104 
       
   105 
       
   106 class UserLink(_UserLink):
       
   107     __select__ = _UserLink.__select__ & authenticated_user()
       
   108 
       
   109     def call(self):
       
   110         # display useractions and siteactions
       
   111         actions = self._cw.vreg['actions'].possible_actions(self._cw, rset=self.cw_rset)
       
   112         box = MenuWidget('', 'userActionsBox', _class='', islist=False)
       
   113         menu = PopupBoxMenu(self._cw.user.login, isitem=False)
       
   114         box.append(menu)
       
   115         for action in actions.get('useractions', ()):
       
   116             menu.append(BoxLink(action.url(), self._cw._(action.title),
       
   117                                 action.html_class()))
       
   118         if actions.get('useractions') and actions.get('siteactions'):
       
   119             menu.append(BoxSeparator())
       
   120         for action in actions.get('siteactions', ()):
       
   121             menu.append(BoxLink(action.url(), self._cw._(action.title),
       
   122                                 action.html_class()))
       
   123         box.render(w=self.w)
   119 
   124 
   120 
   125 
   121 class ApplicationMessage(component.Component):
   126 class ApplicationMessage(component.Component):
   122     """display messages given using the __message parameter into a special div
   127     """display messages given using the __message parameter into a special div
   123     section
   128     section