diff --git a/modules/webstyle/lib/webpage.py b/modules/webstyle/lib/webpage.py index be997bc26..12e2daa94 100644 --- a/modules/webstyle/lib/webpage.py +++ b/modules/webstyle/lib/webpage.py @@ -1,171 +1,172 @@ ## $Id$ ## This file is part of the CERN Document Server Software (CDSware). ## Copyright (C) 2002, 2003, 2004, 2005 CERN. ## ## The CDSware is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License as ## published by the Free Software Foundation; either version 2 of the ## License, or (at your option) any later version. ## ## The CDSware is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with CDSware; if not, write to the Free Software Foundation, Inc., ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. """CDSware Web Page Functions""" from config import * from messages import gettext_set_language from webuser import create_userinfobox_body import re import string import sys import time import traceback import urllib from errorlib import get_msgs_for_code_list, register_errors import template webstyle_templates = template.load('webstyle') def create_navtrailbox_body(title, previous_links, prolog="", separator=""" > """, epilog="", language=cdslang): """Create navigation trail box body input: title = page title; previous_links = the trail content from site title until current page (both ends exlusive). output: text containing the navtrail """ return webstyle_templates.tmpl_navtrailbox_body(weburl = weburl, ln = language, title = title, previous_links = previous_links, separator = separator, prolog = prolog, epilog = epilog) def page(title, body, navtrail="", description="", keywords="", uid=0, cdspageheaderadd="", cdspageboxlefttopadd="", cdspageboxleftbottomadd="", cdspageboxrighttopadd="", cdspageboxrightbottomadd="", cdspagefooteradd="", lastupdated="", language=cdslang, urlargs="", verbose=1, titleprologue="", titleepilogue="", req=None, errors=[], warnings=[]): """page(): display CDS web page input: title of the page body of the page in html format description goes to the metadata in the header of the HTML page keywords goes to the metadata in the header of the html page cdspageheaderadd is a message to be displayed just under the page header cdspageboxlefttopadd is a message to be displayed in the page body on left top cdspageboxleftbottomadd is a message to be displayed in the page body on left bottom cdspageboxrighttopadd is a message to be displayed in the page body on right top cdspageboxrightbottomadd is a message to be displayed in the page body on right bottom cdspagefooteradd is a message to be displayed on the top of the page footer lastupdated is a text containing the info on last update (optional) language is the language version of the page urlargs are the URL arguments of the page to display (useful to affect languages) verbose is verbosity of the page (useful for debugging) titleprologue is to be printed right before page title titleepilogue is to be printed right after page title req is the mod_python request errors is the list of error codes as defined in the moduleName_config.py file of the calling module log is the string of data that should be appended to the log file (errors automatically logged) output: the final cds page with header, footer, etc. """ _ = gettext_set_language(language) if title == cdsnameintl[language]: headerstitle = _("Home") else: headerstitle = title # if there are event if warnings: - warnings= get_msgs_for_code_list(warnings, 'warning') + warnings = get_msgs_for_code_list(warnings, 'warning', language) register_errors(warnings, 'warning') # if there are errors if errors: - errors = get_msgs_for_code_list(errors, 'error') + errors = get_msgs_for_code_list(errors, 'error', language) register_errors(errors, 'error', req) - body = create_error_box(req, errors=errors) + body = create_error_box(req, errors=errors, ln=language) return webstyle_templates.tmpl_page(weburl = weburl, ln = language, headertitle = headerstitle, sitename = cdsnameintl[language], supportemail = supportemail, description = description, keywords = keywords, userinfobox = create_userinfobox_body(uid, language), navtrailbox = create_navtrailbox_body(title, navtrail, language=language), uid = uid, # pageheader = cdspageheader, pageheaderadd = cdspageheaderadd, boxlefttop = cdspageboxlefttop, boxlefttopadd = cdspageboxlefttopadd, boxleftbottomadd = cdspageboxleftbottomadd, boxleftbottom = cdspageboxleftbottom, boxrighttop = cdspageboxrighttop, boxrighttopadd = cdspageboxrighttopadd, boxrightbottomadd = cdspageboxrightbottomadd, boxrightbottom = cdspageboxrightbottom, titleprologue = titleprologue, title = title, titleepilogue = titleepilogue, body = body, # pagefooter = cdspagefooter, languagebox = webstyle_templates.tmpl_language_selection_box(urlargs, language), version = version, lastupdated = lastupdated, pagefooteradd = cdspagefooteradd) def pageheaderonly(title, navtrail="", description="", keywords="", uid=0, cdspageheaderadd="", language=cdslang, urlargs="", verbose=1): """Return just the beginning of page(), with full headers. Suitable for the search results page and any long-taking scripts.""" return webstyle_templates.tmpl_pageheader(weburl = weburl, ln = language, headertitle = title, sitename = cdsnameintl[language], supportemail = supportemail, description = description, keywords = keywords, userinfobox = create_userinfobox_body(uid, language), navtrailbox = create_navtrailbox_body(title, navtrail, language=language), uid = uid, # pageheader = cdspageheader, pageheaderadd = cdspageheaderadd, languagebox = webstyle_templates.tmpl_language_selection_box(urlargs, language)) def pagefooteronly(cdspagefooteradd="", lastupdated="", language=cdslang, urlargs="", verbose=1): """Return just the ending of page(), with full footer. Suitable for the search results page and any long-taking scripts.""" return webstyle_templates.tmpl_pagefooter(weburl = weburl, ln = language, sitename = cdsnameintl[language], supportemail = supportemail, # pagefooter = cdspagefooter, languagebox = webstyle_templates.tmpl_language_selection_box(urlargs, language), version = version, lastupdated = lastupdated, pagefooteradd = cdspagefooteradd) def create_error_box(req, title=None, verbose=1, ln=cdslang, errors=None): """Analyse the req object and the sys traceback and return a text message box with internal information that would be suitful to display when something bad has happened. """ + _ = gettext_set_language(ln) return webstyle_templates.tmpl_error_box(title = title, ln = ln, verbose = verbose, req = req, supportemail = supportemail, errors = errors) diff --git a/modules/webstyle/lib/webstyle_templates.py b/modules/webstyle/lib/webstyle_templates.py index 2cb73f90e..8c54c358b 100644 --- a/modules/webstyle/lib/webstyle_templates.py +++ b/modules/webstyle/lib/webstyle_templates.py @@ -1,596 +1,592 @@ ## $Id$ ## CDSware WebStyle templates. ## This file is part of the CERN Document Server Software (CDSware). ## Copyright (C) 2002, 2003, 2004, 2005 CERN. ## ## The CDSware is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License as ## published by the Free Software Foundation; either version 2 of the ## License, or (at your option) any later version. ## ## The CDSware is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with CDSware; if not, write to the Free Software Foundation, Inc., ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. import urllib import time import cgi import gettext import traceback import sre import urllib import sys from config import * from messages import gettext_set_language, language_list_long class Template: def tmpl_navtrailbox_body(self, weburl, ln, title, previous_links, separator, prolog, epilog): """Create navigation trail box body Parameters: - 'weburl' *string* - The base URL for the site - 'ln' *string* - The language to display - 'title' *string* - page title; - 'previous_links' *string* - the trail content from site title until current page (both ends exlusive) - 'prolog' *string* - HTML code to prefix the navtrail item with - 'epilog' *string* - HTML code to suffix the navtrail item with - 'separator' *string* - HTML code that separates two navtrail items Output: - text containing the navtrail """ # load the right message language _ = gettext_set_language(ln) out = "" if title != cdsnameintl[ln]: out += """%(msg_home)s""" % { 'weburl' : weburl, 'ln' : ln, 'msg_home' : _("Home")} if previous_links: if out: out += separator out += previous_links if title: if out: out += separator if title == cdsnameintl[ln]: # hide site name, print Home instead out += _("Home") else: out += title return prolog + out + epilog def tmpl_page(self, weburl, ln, headertitle, sitename = "", supportemail = "", description = "", keywords = "", userinfobox = "", navtrailbox = "", pageheaderadd = "", boxlefttop = "", boxlefttopadd = "", boxleftbottom = "", boxleftbottomadd = "", boxrighttop = "", boxrighttopadd = "", boxrightbottom = "", boxrightbottomadd = "", titleprologue = "", title = "", titleepilogue = "", body = "", version = "", lastupdated = None, languagebox = "", pagefooteradd = "", uid = 0, ): """Creates a complete page Parameters: - 'weburl' *string* - The base URL for the site - 'ln' *string* - The language to display - 'sitename' *string* - the first part of the page HTML title - 'headertitle' *string* - the second part of the page HTML title - 'supportemail' *string* - email of the support team - 'description' *string* - description goes to the metadata in the header of the HTML page - 'keywords' *string* - keywords goes to the metadata in the header of the HTML page - 'userinfobox' *string* - the HTML code for the user information box - 'navtrailbox' *string* - the HTML code for the navigation trail box - 'pageheaderadd' *string* - additional page header HTML code - 'boxlefttop' *string* - left-top box HTML code - 'boxlefttopadd' *string* - additional left-top box HTML code - 'boxleftbottom' *string* - left-bottom box HTML code - 'boxleftbottomadd' *string* - additional left-bottom box HTML code - 'boxrighttop' *string* - right-top box HTML code - 'boxrighttopadd' *string* - additional right-top box HTML code - 'boxrightbottom' *string* - right-bottom box HTML code - 'boxrightbottomadd' *string* - additional right-bottom box HTML code - 'title' *string* - the title of the page - 'body' *string* - the body of the page - 'version' *string* - the version number of CDSware - 'lastupdated' *string* - when the page was last updated - 'languagebox' *string* - the HTML code for the language box - 'pagefooteradd' *string* - additional page footer HTML code Output: - HTML code of the page """ # load the right message language _ = gettext_set_language(ln) if lastupdated: msg_lastupdated = _("Last updated:") + " " + lastupdated else: msg_lastupdated = "" out = self.tmpl_pageheader( weburl = weburl, ln = ln, headertitle = headertitle, sitename = sitename, supportemail = supportemail, description = description, keywords = keywords, userinfobox = userinfobox, navtrailbox = navtrailbox, pageheaderadd = pageheaderadd, languagebox = languagebox, ) + """
%(boxlefttop)s
%(boxlefttopadd)s
%(boxleftbottomadd)s
%(boxleftbottom)s
%(boxrighttop)s
%(boxrighttopadd)s
%(boxrightbottomadd)s
%(boxrightbottom)s

%(title)s

%(body)s
""" % { 'boxlefttop' : boxlefttop, 'boxlefttopadd' : boxlefttopadd, 'boxleftbottom' : boxleftbottom, 'boxleftbottomadd' : boxleftbottomadd, 'boxrighttop' : boxrighttop, 'boxrighttopadd' : boxrighttopadd, 'boxrightbottom' : boxrightbottom, 'boxrightbottomadd' : boxrightbottomadd, 'title' : title, 'body' : body, } + self.tmpl_pagefooter( weburl = weburl, ln = ln, sitename = sitename, supportemail = supportemail, version = version, lastupdated = lastupdated, languagebox = languagebox, pagefooteradd = pagefooteradd ) return out def tmpl_pageheader(self, weburl, ln, headertitle = "", sitename = "", supportemail = "", description = "", keywords = "", userinfobox = "", navtrailbox = "", pageheaderadd = "", languagebox = "", uid = 0, ): """Creates a page header Parameters: - 'weburl' *string* - The base URL for the site - 'ln' *string* - The language to display - 'sitename' *string* - the first part of the page HTML title - 'headertitle' *string* - the second part of the page HTML title - 'supportemail' *string* - email of the support team - 'description' *string* - description goes to the metadata in the header of the HTML page - 'keywords' *string* - keywords goes to the metadata in the header of the HTML page - 'userinfobox' *string* - the HTML code for the user information box - 'navtrailbox' *string* - the HTML code for the navigation trail box - 'pageheaderadd' *string* - additional page header HTML code - 'languagebox' *string* - the HTML code for the language box Output: - HTML code of the page headers """ # load the right message language _ = gettext_set_language(ln) out = """ %(sitename)s: %(headertitle)s """ % { 'weburl' : weburl, 'ln' : ln, 'sitename' : sitename, 'headertitle' : headertitle, 'supportemail' : supportemail, 'description' : description, 'keywords' : keywords, 'userinfobox' : userinfobox, 'navtrailbox' : navtrailbox, 'pageheaderadd' : pageheaderadd, 'msg_search' : _("Search"), 'msg_submit' : _("Submit"), 'msg_personalize' : _("Personalize"), 'msg_help' : _("Help"), 'languagebox' : languagebox, } return out def tmpl_pagefooter(self, weburl, ln, sitename = "", supportemail = "", version = "", lastupdated = None, languagebox = "", pagefooteradd = "" ): """Creates a page footer Parameters: - 'weburl' *string* - The base URL for the site - 'ln' *string* - The language to display - 'sitename' *string* - the first part of the page HTML title - 'supportemail' *string* - email of the support team - 'version' *string* - the version number of CDSware - 'lastupdated' *string* - when the page was last updated - 'languagebox' *string* - the HTML code for the language box - 'pagefooteradd' *string* - additional page footer HTML code Output: - HTML code of the page headers """ # load the right message language _ = gettext_set_language(ln) if lastupdated: msg_lastupdated = _("Last updated:") + " " + lastupdated else: msg_lastupdated = "" out = """ """ % { 'weburl' : weburl, 'ln' : ln, 'sitename' : sitename, 'supportemail' : supportemail, 'msg_search' : _("Search"), 'msg_submit' : _("Submit"), 'msg_personalize' : _("Personalize"), 'msg_help' : _("Help"), 'msg_poweredby' : _("Powered by"), 'msg_maintainedby' : _("Maintained by"), 'msg_lastupdated' : msg_lastupdated, 'languagebox' : languagebox, 'version' : version, 'pagefooteradd' : pagefooteradd, } return out def tmpl_language_selection_box(self, urlargs="", language="en"): """Take URLARGS and LANGUAGE and return textual language selection box for the given page. Parameters: - 'urlargs' *string* - The url args that helped produce this page - 'language' *string* - The selected language """ # load the right message language _ = gettext_set_language(language) out = "" for (lang, lang_namelong) in language_list_long(): if lang == language: out += """ %s   """ % lang_namelong else: if urlargs: urlargs = sre.sub(r'ln=.*?(&|$)', '', urlargs) if urlargs: if urlargs.endswith('&'): urlargs += "ln=%s" % lang else: urlargs += "&ln=%s" % lang else: urlargs = "ln=%s" % lang out += """ %s   """ % (urlargs, lang_namelong) return _("This site is also available in the following languages:") + "
" + out def tmpl_error_box(self, ln, title, verbose, req, supportemail, errors): """Produces an error box. Parameters: - 'title' *string* - The title of the error box - 'ln' *string* - The selected language - 'verbose' *bool* - If lots of information should be displayed - 'req' *object* - the request object - 'supportemail' *string* - the supportemail for this installation - 'errors' list of tuples (error_code, error_message) - #! todo """ # load the right message language _ = gettext_set_language(ln) - - info_not_available = "NA" + info_not_available = _("N/A") if title == None: if errors: - title = "Error: %s" % errors[0][1] + title = _("Error: %s") % errors[0][1] else: title = _("Internal Error") + browser_s = _("Browser") if req: try: - browser_s = '' if req.headers_in.has_key('User-Agent'): - browser_s = """Browser: %s\n""" % req.headers_in['User-Agent'] + browser_s += ': ' + req.headers_in['User-Agent'] + else: + browser_s += ': ' + info_not_available host_s = req.hostname page_s = req.unparsed_uri client_s = req.connection.remote_ip except: pass else: - browser_s = "Browser: NA\n" + browser_s += ': ' + info_not_available host_s = page_s = client_s = info_not_available error_s = '' sys_error_s = '' traceback_s = '' if verbose >= 1: - sys_error_s = """System Error: %s %s\n""" % (sys.exc_info()[0], sys.exc_info()[1]) + if sys.exc_info()[0]: + sys_error_s = _("System Error: %s %s\n") % (sys.exc_info()[0], sys.exc_info()[1]) if errors: errs = '' for error_tuple in errors: try: errs += "%s%s : %s\n " % (' '*6, error_tuple[0], error_tuple[1]) except: errs += "%s%s\n" % (' '*6, error_tuple) errs = errs[6:-2] # get rid of trainling ',' - error_s = "Error: %s \n" % errs + error_s = _("Error: %s")% errs + "\n" else: - error_s = "Error: None None\n" + error_s = _("Error") + ': ' + info_not_available if verbose >= 9: - traceback_s = "Traceback: \n%s" % string.join(traceback.format_tb(sys.exc_info()[2]),"\n") + traceback_s = _("Traceback: \n%s") % string.join(traceback.format_tb(sys.exc_info()[2]),"\n") out = """ - - - - - - - - + + + + + + + - - - - - + + + + +
-

%(title)s %(sys1)s %(sys2)s -

-

%(contact)s -

+                
+

%(title)s %(sys1)s %(sys2)s

+
+

%(contact)s

+
 URI: http://%(host)s%(page)s
-Time: %(time)s
-%(browser)sClient: %(client)s
+%(time_label)s: %(time)s
+%(browser)s
+%(client_label)s: %(client)s
 %(error)s%(sys_error)s%(traceback)s
 
-
- Please send an error report to the Administrator
- - - - - - - - - -
+
+ %(send_error_label)s + + + + + + + + + + +
+
""" % { 'title' : title, - 'sys1' : sys.exc_info()[0], - 'sys2' : sys.exc_info()[1], + 'time_label': _("Time"), + 'client_label': _("Client"), + 'send_error_label': _("Please send an error report to the Administrator"), + 'send_label': _("Send error report"), + 'sys1' : sys.exc_info()[0] or '', + 'sys2' : sys.exc_info()[1] or '', 'contact' : _("Please contact %s quoting the following information:") % (urllib.quote(supportemail), supportemail), 'host' : host_s, 'page' : page_s, 'time' : time.strftime("%02d/%b/%Y:%H:%M:%S %z"), 'browser' : browser_s, 'client' : client_s, 'error' : error_s, 'traceback' : traceback_s, 'sys_error' : sys_error_s, 'weburl' : weburl, 'referer' : page_s!=info_not_available and ("http://" + host_s + page_s) or info_not_available } return out - - - - - - - - - - - - -