# -*- coding: utf-8 -*- ## ## ## This file is part of CDS Indico. ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN. ## ## CDS Indico 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. ## ## CDS Indico 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 CDS Indico; if not, write to the Free Software Foundation, Inc., ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. from MaKaC.user import Avatar import sys, os, StringIO, traceback import MaKaC from MaKaC.common.Configuration import Config from MaKaC.services.interface.rpc.common import ServiceAccessError from MaKaC.errors import MaKaCError, AccessError from MaKaC.common.utils import formatDateTime, formatDate, formatTime import MaKaC.common.info as info from MaKaC.common.logger import Logger ERROR_PATH = Config.getInstance().getTempDir() class TemplateExecException( Exception ): def __init__( self, value ): self.value = value if hasattr(value, 'template_tracebacks'): self.template_tracebacks = value.template_tracebacks if hasattr(value, 'problematic_templates'): self.problematic_templates = value.problematic_templates def __str__(self): s = str( type(self.value).__name__) + ": " + str(self.value ) if s[-1] != "}": if hasattr(self, 'problematic_templates'): s += """ - - - -> {Python code generated from templates is available in %s.tpl.error.py""" % (os.path.join(ERROR_PATH, self.problematic_templates[0])) for template_name in self.problematic_templates[1:]: s += ", %s.py" % (os.path.join(ERROR_PATH, template_name)) s += " file(s)}" return s def includeTpl( tplFileName, *args, **kwargs ): """ Use: <% includeTpl( 'TemplateName', paramA = 2, paramB = 3 ) %> """ # Resurrect dictionary from globals - ugly hack. # In the recurrent exec, we still need an old dictCopy. # This old dictCopy is resurrected from the global scope. objDict = globals()['_dictCopy'] for k, v in kwargs.iteritems(): objDict[k] = v objDict["isIncluded"] = True from MaKaC.webinterface import wcomponents result = wcomponents.WTemplated(tplFileName).getHTML(objDict) print result def contextHelp( helpId ): """ Allows you to put [?], the context help marker. Help content is defined in
. """ includeTpl( 'ContextHelp', helpId = helpId, imgSrc = Config.getInstance().getSystemIconURL( "help" ) ) def inlineContextHelp( helpContent ): """ Allows you to put [?], the context help marker. Help content passed as argument helpContent. """ includeTpl( 'InlineContextHelp', helpContent = helpContent, imgSrc = Config.getInstance().getSystemIconURL( "help" ) ) def escapeAttrVal( s ): """ Just escapes the apostrophes, new lines, etc. """ # if s == None: # return None # s = s.replace( "'", r"\'" ) # s = s.strip() from xml.sax.saxutils import quoteattr s = quoteattr( s )[1:-1] s = s.replace( "'", "`" ) s = s.replace( "\"", "`" ) s = s.replace( "\n", "" ) s = s.replace( "\r", "" ) s = s.replace( " ", " " ) s = s.replace( " ", " " ) return s def verbose( s, default = "" ): """ Purpose: avoid showing "None" to user; show default value instead. """ if isinstance( s, bool ) and s != None: if s: return "yes" else: return "no" return s or default def verbose_dt( dt, default = "" ): if dt == None: return default return ( "%s_%2s:%2s" % ( formatDate(dt.date()).replace(' ','_'), dt.hour, dt.minute ) ).replace( ' ', '0' ).replace( '_', ' ' ) def verbose_t( t, default = "" ): if t == None: return default return ( "%2d:%2d" % ( t.hour, t.minute ) ).replace( ' ', '0' ) def escape( s ): """HTML escape""" from xml.sax.saxutils import escape return escape( s ) def jsBoolean ( b ): if b: return 'true' else: return 'false' def quoteattr( s ): """quotes escape""" from xml.sax.saxutils import quoteattr return quoteattr( s ) def roomClass( room ): if room.isReservable: roomClass = "basicRoom" if not room.isReservable: roomClass = "privateRoom" if room.isReservable and room.resvsNeedConfirmation: roomClass = "moderatedRoom" return roomClass def dequote( s ): if ( s.startswith( '"' ) or s.startswith( "'" ) ) and ( s.endswith( '"' ) or s.endswith( "'" ) ): return s[1:-1] return s def linkify( s ): urlIxStart = s.find( 'http://' ) if urlIxStart == -1: return s urlIxEnd = s.find( ' ', urlIxStart + 1 ) s = s[0:urlIxStart] + '' + s[urlIxStart:urlIxEnd] + " " + s[urlIxEnd:] return s def deepstr(obj): """ obj is any kind of object This method will loop through the object and turn objects into strings through their __str__ method. If a list or a dictionary is found during the loop, a recursive call is made. However this method does not support objects that are not lists or dictionaries. Author: David Martin Clavo """ # stringfy objects inside a list if isinstance(obj,list): for i in range(0, len(obj)): obj[i] = deepstr(obj[i]) #stringfy objects inside a dictionary if isinstance(obj,dict): for k,v in obj.items(): del obj[k] #wAvatare delete the old key obj[deepstr(k)] = deepstr(v) return str(obj) def beautify(obj, classNames = {"UlClassName": "optionList", "KeyClassName": "optionKey"}, level = 0): """ Turns list or dicts into beautiful