web/views/basetemplates.py
changeset 4594 7b9824d1b734
parent 4279 e6b9fd79a247
child 4596 13fd398916c2
equal deleted inserted replaced
4593:d0b5ef72a492 4594:7b9824d1b734
    13 from cubicweb.appobject import objectify_selector
    13 from cubicweb.appobject import objectify_selector
    14 from cubicweb.selectors import match_kwargs
    14 from cubicweb.selectors import match_kwargs
    15 from cubicweb.view import View, MainTemplate, NOINDEX, NOFOLLOW
    15 from cubicweb.view import View, MainTemplate, NOINDEX, NOFOLLOW
    16 from cubicweb.utils import UStringIO, can_do_pdf_conversion
    16 from cubicweb.utils import UStringIO, can_do_pdf_conversion
    17 from cubicweb.schema import display_name
    17 from cubicweb.schema import display_name
       
    18 from cubicweb.web import formfields as ff, formwidgets as fw
       
    19 from cubicweb.web.views import forms
    18 
    20 
    19 # main templates ##############################################################
    21 # main templates ##############################################################
    20 
    22 
    21 class LogInOutTemplate(MainTemplate):
    23 class LogInOutTemplate(MainTemplate):
    22 
    24 
   387         # lastcolumn
   389         # lastcolumn
   388         self.w(u'<td id="lastcolumn">')
   390         self.w(u'<td id="lastcolumn">')
   389         self.w(u'</td>\n')
   391         self.w(u'</td>\n')
   390         self.w(u'</tr></table>\n')
   392         self.w(u'</tr></table>\n')
   391         self.wview('logform', rset=self.cw_rset, id='popupLoginBox', klass='hidden',
   393         self.wview('logform', rset=self.cw_rset, id='popupLoginBox', klass='hidden',
   392                    title=False, message=False)
   394                    title=False, showmessage=False)
   393 
   395 
   394     def state_header(self):
   396     def state_header(self):
   395         state = self._cw.search_state
   397         state = self._cw.search_state
   396         if state[0] == 'normal':
   398         if state[0] == 'normal':
   397             return
   399             return
   458             for comp in components:
   460             for comp in components:
   459                 comp.render(w=self.w, view=view)
   461                 comp.render(w=self.w, view=view)
   460             self.w(u'</div>')
   462             self.w(u'</div>')
   461 
   463 
   462 
   464 
   463 class LogFormTemplate(View):
   465 class LogForm(forms.FieldsForm):
       
   466     __regid__ = 'logform'
       
   467     # XXX have to recall fields name since python is mangling __login/__password
       
   468     __login = ff.StringField('__login', widget=fw.TextInput({'class': 'data'}))
       
   469     __password = ff.StringField('__password', label=_('password'),
       
   470                                 widget=fw.PasswordSingleInput({'class': 'data'}))
       
   471     form_buttons = [fw.SubmitButton(label=_('log in'),
       
   472                                     attrs={'class': 'loginButton right'})]
       
   473 
       
   474     @property
       
   475     def action(self):
       
   476         return xml_escape(login_form_url(self._cw))
       
   477 
       
   478 
       
   479 class LogFormView(View):
   464     __regid__ = 'logform'
   480     __regid__ = 'logform'
   465     __select__ = match_kwargs('id', 'klass')
   481     __select__ = match_kwargs('id', 'klass')
   466 
   482 
   467     title = 'log in'
   483     title = 'log in'
   468 
   484 
   469     def call(self, id, klass, title=True, message=True):
   485     def call(self, id, klass, title=True, showmessage=True):
   470         self._cw.add_css('cubicweb.login.css')
   486         self._cw.add_css('cubicweb.login.css')
   471         self.w(u'<div id="%s" class="%s">' % (id, klass))
   487         self.w(u'<div id="%s" class="%s">' % (id, klass))
   472         if title:
   488         if title:
   473             stitle = self._cw.property_value('ui.site-title')
   489             stitle = self._cw.property_value('ui.site-title')
   474             if stitle:
   490             if stitle:
   475                 stitle = xml_escape(stitle)
   491                 stitle = xml_escape(stitle)
   476             else:
   492             else:
   477                 stitle = u'&#160;'
   493                 stitle = u'&#160;'
   478             self.w(u'<div id="loginTitle">%s</div>' % stitle)
   494             self.w(u'<div id="loginTitle">%s</div>' % stitle)
   479         self.w(u'<div id="loginContent">\n')
   495         self.w(u'<div id="loginContent">\n')
   480 
   496         if showmessage and self._cw.message:
   481         if message:
   497             self.w(u'<div class="simpleMessage">%s</div>\n' % self._cw.message)
   482             self.display_message()
   498         if self._cw.vreg.config['auth-mode'] != 'http':
   483         if self._cw.vreg.config['auth-mode'] == 'http':
       
   484             # HTTP authentication
       
   485             pass
       
   486         else:
       
   487             # Cookie authentication
   499             # Cookie authentication
   488             self.login_form(id)
   500             self.login_form(id)
   489         self.w(u'</div></div>\n')
   501         self.w(u'</div></div>\n')
   490 
   502 
   491     def display_message(self):
       
   492         message = self._cw.message
       
   493         if message:
       
   494             self.w(u'<div class="simpleMessage">%s</div>\n' % message)
       
   495 
       
   496     def login_form(self, id):
   503     def login_form(self, id):
   497         _ = self._cw._
   504         cw = self._cw
   498         # XXX turn into a form
   505         form = cw.vreg['forms'].select('logform', cw)
   499         self.w(u'<form method="post" action="%s" id="login_form">\n'
   506         if cw.vreg.config['allow-email-login']:
   500                % xml_escape(login_form_url(self._cw.vreg.config, self._cw)))
   507             label = cw._('login or email')
   501         self.w(u'<table>\n')
   508         else:
   502         self.add_fields()
   509             label = cw._('login')
   503         self.w(u'<tr>\n')
   510         form.field_by_name('__login').label = label
   504         self.w(u'<td>&#160;</td><td><input type="submit" class="loginButton right" value="%s" />\n</td>' % _('log in'))
   511         self.w(form.render(table_class=''))
   505         self.w(u'</tr>\n')
   512         cw.html_headers.add_onload('jQuery("#__login:visible").focus()')
   506         self.w(u'</table>\n')
   513 
   507         self.w(u'</form>\n')
   514 def login_form_url(req):
   508         self._cw.html_headers.add_onload('jQuery("#__login:visible").focus()')
       
   509 
       
   510     def add_fields(self):
       
   511         msg = (self._cw.vreg.config['allow-email-login'] and _('login or email')) or _('login')
       
   512         self.add_field('__login', msg, 'text')
       
   513         self.add_field('__password', self._cw._('password'), 'password')
       
   514 
       
   515     def add_field(self, name, label, inputtype):
       
   516         self.w(u'<tr>\n')
       
   517         self.w(u'<td><label for="%s" >%s</label></td>' % (name, label))
       
   518         self.w(u'<td><input name="%s" id="%s" class="data" type="%s" /></td>\n' %
       
   519                (name, name, inputtype))
       
   520         self.w(u'</tr>\n')
       
   521 
       
   522 
       
   523 def login_form_url(config, req):
       
   524     if req.https:
   515     if req.https:
   525         return req.url()
   516         return req.url()
   526     if config.get('https-url'):
   517     httpsurl = req.vreg.config.get('https-url')
   527         return req.url().replace(req.base_url(), config['https-url'])
   518     if httpsurl:
       
   519         return req.url().replace(req.base_url(), httpsurl)
   528     return req.url()
   520     return req.url()