view MPIWGHelper.py @ 284:1a103b073c72 default tip

make favicon url host and schema relative.
author casties
date Thu, 25 Jun 2015 17:44:57 +0200
parents 938add25f81b
children
line wrap: on
line source

from Products.PageTemplates.PageTemplateFile import PageTemplateFile

import SrvTxtUtils
import time
import email
import logging

#ersetzt logging
def logger(txt,method,txt2):
    """logging""" 
    logging.info(txt+ txt2)
                 
def getTextFromNode(nodename):
    
    nodelist=nodename.childNodes
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

def getTemplate(self, tpName):
    """get a template file either form the instance or from the product"""
    #ext=self.ZopeFind(self.aq_parent,obj_ids=[tpName])
    if hasattr(self,tpName):
        pt = getattr(self,tpName)
    else:
        pt=PageTemplateFile('zpt/'+tpName, globals()).__of__(self)
    assert(pt)
    return pt

def sortStopWordsF(self,xo,yo):
    if not hasattr(self,'_v_stopWords'):
        self._v_stopWords=self.stopwords_en.data.split("\n")
    
    x=unicodify(xo[1])
    y=unicodify(yo[1])
    
    strx=x.split(" ")
    stry=y.split(" ")
    
    for tmp in strx:
        if tmp.lower() in self._v_stopWords:
            del strx[strx.index(tmp)]
    
    for tmp in stry:
        if tmp.lower() in self._v_stopWords:
            del stry[stry.index(tmp)]
            
    return cmp(" ".join(strx)," ".join(stry))
    
def sortStopWords(self):
    return lambda x,y : sortStopWordsF(self,x,y)
    
def sortF(x,y):
    try:
        return cmp(x[1],y[1])
    except:
        try:
            return cmp(str(x[1]),str(y[1]))
        except:           
            
            return 0 
    
def sortI(x,y):
    xsplit=x[1].split(".")
    ysplit=y[1].split(".")
    xret=""
    yret=""
    try:
        for i in range(5):
            try:
                yret=yret+"%04i"%int(xsplit[i])
            except:
                yret=yret+"%04i"%0

            try:
                xret=xret+"%04i"%int(ysplit[i])
            except:
                xret=xret+"%04i"%0
                
        
        return cmp(int(yret),int(xret))
    except:
        return cmp(x[1],y[1])


# decode str (utf-8 or latin-1 representation) into unicode object
unicodify = SrvTxtUtils.unicodify

# encode unicode object or string into byte string in utf-8 representation.
utf8ify = SrvTxtUtils.utf8ify

# returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis.
shortenString = SrvTxtUtils.shortenString


#
# navigation methods (should better be a mixin class)
#
def getBreadcrumbs(self):
    """return list of breadcrumbs from here to the root"""
    #logging.debug("getBreadcrumbs of %s"%repr(self))
    crumbs = [{'text':self.title, 'url':self.absolute_url(), 'object':self}]
    parent = self.aq_parent
    if hasattr(parent, 'getBreadcrumbs'):
        if self.title:
            return parent.getBreadcrumbs() + crumbs
        else:
            # if there's no title, skip this level
            return parent.getBreadcrumbs()
        
    return crumbs


def getSection(self, crumbs=None):
    """returns the current section name"""
    # use breadcrumbs if available
    if crumbs is not None and len(crumbs) > 0:
        return crumbs[0]['object'].getId()

    p = self
    sec = None
    # descend parents to the root (and remember the last id)
    while p is not None and p.meta_type != 'MPIWGRoot':
        sec = p.getId()
        p = p.aq_parent
    
    return sec

def getSubSection(self, crumbs=None):
    """returns the current subsection name"""
    # use breadcrumbs if available
    if crumbs is not None and len(crumbs) > 1:
        return crumbs[1]['object'].getId()

    p = self
    sec = None
    subsec = None
    # descend parents to the root (and remember the last id)
    while p is not None and p.meta_type != 'MPIWGRoot':
        subsec = sec
        sec = p.getId()
        p = p.aq_parent
    
    return subsec


def getPathStyle(self, path, selected, style=""):
    """returns a string with the given style + 'sel' if path == selected."""
    if path == selected:
        return style + 'sel'
    else:
        return style    



def getUrl(self, baseUrl=None):
    """returns URL to this object"""
    if baseUrl is None:
        return self.absolute_url()

    return '%s/%s' % (baseUrl, self.getId())
                

def redirect(self, RESPONSE, url):
    """mache ein redirect mit einem angehaengten time stamp um ein reload zu erzwingen"""        
    timeStamp = time.time()
    
    if url.find("?") > -1:  # giebt es schon parameter
        addStr = "&time=%s"
    else:
        addStr = "?time=%s"
        
    RESPONSE.setHeader('Last-Modified', email.Utils.formatdate().split("-")[0] + 'GMT')
    logging.debug(email.Utils.formatdate() + ' GMT')
    RESPONSE.redirect(url + addStr % timeStamp)