web/views/basetemplates.py
branchtls-sprint
changeset 816 9cd49a910fce
parent 811 c6ebabe9db91
child 819 7fd66f389ef7
equal deleted inserted replaced
813:69c623aa39ed 816:9cd49a910fce
     9 
     9 
    10 
    10 
    11 from logilab.mtconverter import html_escape
    11 from logilab.mtconverter import html_escape
    12 
    12 
    13 from cubicweb import NoSelectableObject, ObjectNotFound
    13 from cubicweb import NoSelectableObject, ObjectNotFound
    14 from cubicweb.view import Template, MainTemplate,  NOINDEX, NOFOLLOW
    14 from cubicweb.view import View, MainTemplate,  NOINDEX, NOFOLLOW
    15 from cubicweb.utils import make_uid, UStringIO
    15 from cubicweb.utils import make_uid, UStringIO
    16 from cubicweb.web.views.baseviews import vid_from_rset
    16 from cubicweb.web.views.baseviews import vid_from_rset
    17 
    17 
    18 # main templates ##############################################################
    18 # main templates ##############################################################
    19 
    19 
    36         w(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
    36         w(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
    37           % (content_type, self.req.encoding))
    37           % (content_type, self.req.encoding))
    38         w(NOINDEX)
    38         w(NOINDEX)
    39         w(NOFOLLOW)
    39         w(NOFOLLOW)
    40         w(u'\n'.join(additional_headers) + u'\n')
    40         w(u'\n'.join(additional_headers) + u'\n')
    41         self.template('htmlheader', rset=self.rset)
    41         self.view('htmlheader', rset=self.rset)
    42         w(u'<title>%s</title>\n' % html_escape(page_title))
    42         w(u'<title>%s</title>\n' % html_escape(page_title))
    43 
    43 
    44 
    44 
    45 class LogInTemplate(LogInOutTemplate):
    45 class LogInTemplate(LogInOutTemplate):
    46     id = 'login'
    46     id = 'login'
    47     title = 'log in'
    47     title = 'log in'
    48 
    48 
    49     def content(self, w):
    49     def content(self, w):
    50         self.template('logform', rset=self.rset, id='loginBox', klass='')
    50         self.view('logform', rset=self.rset, id='loginBox', klass='')
    51 
    51 
    52 
    52 
    53 class LoggedOutTemplate(LogInOutTemplate):
    53 class LoggedOutTemplate(LogInOutTemplate):
    54     id = 'loggedout'
    54     id = 'loggedout'
    55     title = 'logged out'
    55     title = 'logged out'
   126             self.set_stream(templatable=False)
   126             self.set_stream(templatable=False)
   127         else:
   127         else:
   128             self.set_request_content_type()
   128             self.set_request_content_type()
   129             content_type = self.content_type
   129             content_type = self.content_type
   130             self.template_header(content_type, view)
   130             self.template_header(content_type, view)
   131         self.template('page-content', view=view, rset=rset)
   131         self.view('page-content', view=view, rset=rset)
   132         if with_templates:
   132         if with_templates:
   133             self.template_footer(view)
   133             self.template_footer(view)
   134 
   134 
   135     def with_templates(self, view):
   135     def with_templates(self, view):
   136         return (not view.binary and view.templatable and
   136         return (not view.binary and view.templatable and
   159         self.write_doctype()
   159         self.write_doctype()
   160         w(u'<base href="%s" />' % html_escape(self.req.base_url()))
   160         w(u'<base href="%s" />' % html_escape(self.req.base_url()))
   161         w(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
   161         w(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
   162           % (content_type, self.req.encoding))
   162           % (content_type, self.req.encoding))
   163         w(u'\n'.join(additional_headers) + u'\n')
   163         w(u'\n'.join(additional_headers) + u'\n')
   164         self.template('htmlheader', rset=self.rset)
   164         self.view('htmlheader', rset=self.rset)
   165         if page_title:
   165         if page_title:
   166             w(u'<title>%s</title>\n' % html_escape(page_title))
   166             w(u'<title>%s</title>\n' % html_escape(page_title))
   167 
   167 
   168     def template_body_header(self, view):
   168     def template_body_header(self, view):
   169         w = self.w
   169         w = self.w
   170         w(u'<body>\n')
   170         w(u'<body>\n')
   171         self.template('header', rset=self.rset, view=view)
   171         self.view('header', rset=self.rset, view=view)
   172         w(u'<div id="page"><table width="100%" border="0" id="mainLayout"><tr>\n')
   172         w(u'<div id="page"><table width="100%" border="0" id="mainLayout"><tr>\n')
   173         self.nav_column(view, 'left')
   173         self.nav_column(view, 'left')
   174         w(u'<td id="contentcol">\n')
   174         w(u'<td id="contentcol">\n')
   175         rqlcomp = self.vreg.select_component('rqlinput', self.req, self.rset)
   175         rqlcomp = self.vreg.select_component('rqlinput', self.req, self.rset)
   176         if rqlcomp:
   176         if rqlcomp:
   183     def template_footer(self, view=None):
   183     def template_footer(self, view=None):
   184         self.content_footer(view)
   184         self.content_footer(view)
   185         self.w(u'</td>\n')
   185         self.w(u'</td>\n')
   186         self.nav_column(view, 'right')
   186         self.nav_column(view, 'right')
   187         self.w(u'</tr></table></div>\n')
   187         self.w(u'</tr></table></div>\n')
   188         self.template('footer', rset=self.rset)
   188         self.view('footer', rset=self.rset)
   189         self.w(u'</body>')
   189         self.w(u'</body>')
   190 
   190 
   191     def nav_column(self, view, context):
   191     def nav_column(self, view, context):
   192         boxes = list(self.vreg.possible_vobjects('boxes', self.req, self.rset,
   192         boxes = list(self.vreg.possible_vobjects('boxes', self.req, self.rset,
   193                                                  view=view, context=context))
   193                                                  view=view, context=context))
   197                 box.dispatch(w=self.w, view=view)
   197                 box.dispatch(w=self.w, view=view)
   198             self.w(u'</div></td>\n')
   198             self.w(u'</div></td>\n')
   199 
   199 
   200     def content_header(self, view=None):
   200     def content_header(self, view=None):
   201         """by default, display informal messages in content header"""
   201         """by default, display informal messages in content header"""
   202         self.template('contentheader', rset=self.rset, view=view)
   202         self.view('contentheader', rset=self.rset, view=view)
   203 
   203 
   204     def content_footer(self, view=None):
   204     def content_footer(self, view=None):
   205         self.template('contentfooter', rset=self.rset, view=view)
   205         self.view('contentfooter', rset=self.rset, view=view)
   206 
   206 
   207 
   207 
   208 class PageContentTemplate(TheMainTemplate):
   208 class PageContentTemplate(TheMainTemplate):
   209     id = 'page-content'
   209     id = 'page-content'
   210 
   210 
   264         lang = self.req.lang
   264         lang = self.req.lang
   265         self.write_doctype()
   265         self.write_doctype()
   266         w(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
   266         w(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
   267           % (content_type, self.req.encoding))
   267           % (content_type, self.req.encoding))
   268         w(u'\n'.join(additional_headers))
   268         w(u'\n'.join(additional_headers))
   269         self.template('htmlheader', rset=self.rset)
   269         self.view('htmlheader', rset=self.rset)
   270         w(u'<title>%s</title>\n' % html_escape(page_title))
   270         w(u'<title>%s</title>\n' % html_escape(page_title))
   271         self.w(u'<body>\n')
   271         self.w(u'<body>\n')
   272 
   272 
   273     def template_footer(self, view=None):
   273     def template_footer(self, view=None):
   274         self.w(u'</body>')
   274         self.w(u'</body>')
   285         lang = self.req.lang
   285         lang = self.req.lang
   286         self.write_doctype()
   286         self.write_doctype()
   287         whead(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
   287         whead(u'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
   288               % (content_type, self.req.encoding))
   288               % (content_type, self.req.encoding))
   289         whead(u'\n'.join(additional_headers) + u'\n')
   289         whead(u'\n'.join(additional_headers) + u'\n')
   290         self.template('htmlheader', rset=self.rset)
   290         self.view('htmlheader', rset=self.rset)
   291         w = self.w
   291         w = self.w
   292         w(u'<title>%s</title>\n' % html_escape(page_title))
   292         w(u'<title>%s</title>\n' % html_escape(page_title))
   293         w(u'<body>\n')
   293         w(u'<body>\n')
   294         w(u'<div id="page">')
   294         w(u'<div id="page">')
   295         w(u'<table width="100%" height="100%" border="0"><tr>\n')
   295         w(u'<table width="100%" height="100%" border="0"><tr>\n')
   316         self.w(u'</td>\n')
   316         self.w(u'</td>\n')
   317         self.w(u'</tr></table>\n')
   317         self.w(u'</tr></table>\n')
   318 
   318 
   319 # page parts templates ########################################################
   319 # page parts templates ########################################################
   320 
   320 
   321 class HTMLHeader(Template):
   321 class HTMLHeader(View):
   322     """default html headers"""
   322     """default html headers"""
   323     id = 'htmlheader'
   323     id = 'htmlheader'
   324 
   324 
   325     def call(self, **kwargs):
   325     def call(self, **kwargs):
   326         self.favicon()
   326         self.favicon()
   360         pid = make_uid(id(req))
   360         pid = make_uid(id(req))
   361         req.pageid = pid
   361         req.pageid = pid
   362         req.html_headers.define_var('pageid', pid);
   362         req.html_headers.define_var('pageid', pid);
   363 
   363 
   364 
   364 
   365 class HTMLPageHeader(Template):
   365 class HTMLPageHeader(View):
   366     """default html page header"""
   366     """default html page header"""
   367     id = 'header'
   367     id = 'header'
   368 
   368 
   369     def call(self, view, **kwargs):
   369     def call(self, view, **kwargs):
   370         self.main_header(view)
   370         self.main_header(view)
   401         self.w(u'</td>')
   401         self.w(u'</td>')
   402         # lastcolumn
   402         # lastcolumn
   403         self.w(u'<td id="lastcolumn">')
   403         self.w(u'<td id="lastcolumn">')
   404         self.w(u'</td>\n')
   404         self.w(u'</td>\n')
   405         self.w(u'</tr></table>\n')
   405         self.w(u'</tr></table>\n')
   406         self.template('logform', rset=self.rset, id='popupLoginBox', klass='hidden',
   406         self.view('logform', rset=self.rset, id='popupLoginBox', klass='hidden',
   407                       title=False, message=False)
   407                   title=False, message=False)
   408 
   408 
   409     def state_header(self):
   409     def state_header(self):
   410         state = self.req.search_state
   410         state = self.req.search_state
   411         if state[0] == 'normal':
   411         if state[0] == 'normal':
   412             return
   412             return
   420                         '"'))
   420                         '"'))
   421         return self.w(u'<div class="stateMessage">%s</div>' % msg)
   421         return self.w(u'<div class="stateMessage">%s</div>' % msg)
   422 
   422 
   423 
   423 
   424 
   424 
   425 class HTMLPageFooter(Template):
   425 class HTMLPageFooter(View):
   426     """default html page footer: include logo if any, and close the HTML body
   426     """default html page footer: include logo if any, and close the HTML body
   427     """
   427     """
   428     id = 'footer'
   428     id = 'footer'
   429 
   429 
   430     def call(self, **kwargs):
   430     def call(self, **kwargs):
   439                                             req._('about this site')))
   439                                             req._('about this site')))
   440         self.w(u'© 2001-2009 <a href="http://www.logilab.fr">Logilab S.A.</a>')
   440         self.w(u'© 2001-2009 <a href="http://www.logilab.fr">Logilab S.A.</a>')
   441         self.w(u'</div>')
   441         self.w(u'</div>')
   442 
   442 
   443 
   443 
   444 class HTMLContentHeader(Template):
   444 class HTMLContentHeader(View):
   445     """default html page content header:
   445     """default html page content header:
   446     * include message component if selectable for this request
   446     * include message component if selectable for this request
   447     * include selectable content navigation components
   447     * include selectable content navigation components
   448     """
   448     """
   449     id = 'contentheader'
   449     id = 'contentheader'
   458             for comp in components:
   458             for comp in components:
   459                 comp.dispatch(w=self.w, view=view)
   459                 comp.dispatch(w=self.w, view=view)
   460             self.w(u'</div><div class="clear"></div>')
   460             self.w(u'</div><div class="clear"></div>')
   461 
   461 
   462 
   462 
   463 class HTMLContentFooter(Template):
   463 class HTMLContentFooter(View):
   464     """default html page content footer: include selectable content navigation
   464     """default html page content footer: include selectable content navigation
   465     components
   465     components
   466     """
   466     """
   467     id = 'contentfooter'
   467     id = 'contentfooter'
   468 
   468 
   475             for comp in components:
   475             for comp in components:
   476                 comp.dispatch(w=self.w, view=view)
   476                 comp.dispatch(w=self.w, view=view)
   477             self.w(u'</div>')
   477             self.w(u'</div>')
   478 
   478 
   479 
   479 
   480 class LogFormTemplate(Template):
   480 class LogFormTemplate(View):
   481     id = 'logform'
   481     id = 'logform'
   482     title = 'log in'
   482     title = 'log in'
   483 
   483 
   484     def call(self, id, klass, title=True, message=True):
   484     def call(self, id, klass, title=True, message=True):
   485         self.req.add_css('cubicweb.login.css')
   485         self.req.add_css('cubicweb.login.css')