Annotation of MPIWGWeb/MPIWGHelper.py, revision 1.1.2.11

1.1.2.11! casties     1: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
        !             2: 
1.1.2.4   dwinter     3: import logging
1.1.2.11! casties     4: 
1.1.2.1   dwinter     5: definedFields=['WEB_title','xdata_01','xdata_02','xdata_03','xdata_04','xdata_05','xdata_06','xdata_07','xdata_08','xdata_09','xdata_10','xdata_11','xdata_12','xdata_13','WEB_project_header','WEB_project_description','WEB_related_pub']
                      6: 
                      7: checkFields = ['xdata_01']
                      8: 
                      9: #ersetzt logging
                     10: def logger(txt,method,txt2):
                     11:     """logging""" 
                     12:     logging.info(txt+ txt2)
                     13:                  
                     14: def getTextFromNode(nodename):
                     15:     
                     16:     nodelist=nodename.childNodes
                     17:     rc = ""
                     18:     for node in nodelist:
                     19:         if node.nodeType == node.TEXT_NODE:
                     20:            rc = rc + node.data
                     21:     return rc
                     22: 
1.1.2.7   dwinter    23: def getTemplate(self, tpName):
                     24:     """get a template file either form the instance or from the product"""
1.1.2.11! casties    25:     #ext=self.ZopeFind(self.aq_parent,obj_ids=[tpName])
        !            26:     if hasattr(self,tpName):
        !            27:         pt = getattr(self,tpName)
1.1.2.7   dwinter    28:     else:
1.1.2.11! casties    29:         pt=PageTemplateFile('zpt/'+tpName, globals()).__of__(self)
1.1.2.7   dwinter    30:     assert(pt)
                     31:     return pt
1.1.2.1   dwinter    32: 
                     33: def sortStopWordsF(self,xo,yo):
                     34:     if not hasattr(self,'_v_stopWords'):
                     35:         self._v_stopWords=self.stopwords_en.data.split("\n")
                     36:     
1.1.2.5   dwinter    37:     x=unicodify(xo[1])
                     38:     y=unicodify(yo[1])
1.1.2.1   dwinter    39:     
                     40:     strx=x.split(" ")
                     41:     stry=y.split(" ")
                     42:     
                     43:     for tmp in strx:
                     44:         if tmp.lower() in self._v_stopWords:
                     45:             del strx[strx.index(tmp)]
                     46:     
                     47:     for tmp in stry:
                     48:         if tmp.lower() in self._v_stopWords:
                     49:             del stry[stry.index(tmp)]
                     50:             
                     51:     return cmp(" ".join(strx)," ".join(stry))
                     52:     
                     53: def sortStopWords(self):
                     54:     return lambda x,y : sortStopWordsF(self,x,y)
                     55:     
                     56: def sortF(x,y):
                     57:     try:
                     58:         return cmp(x[1],y[1])
                     59:     except:
                     60:         try:
                     61:             return cmp(str(x[1]),str(y[1]))
                     62:         except:           
                     63:             
                     64:             return 0 
                     65:     
                     66: def sortI(x,y):
                     67:     xsplit=x[1].split(".")
                     68:     ysplit=y[1].split(".")
                     69:     xret=""
                     70:     yret=""
                     71:     try:
                     72:         for i in range(5):
                     73:             try:
                     74:                 yret=yret+"%04i"%int(xsplit[i])
                     75:             except:
                     76:                 yret=yret+"%04i"%0
                     77: 
                     78:             try:
                     79:                 xret=xret+"%04i"%int(ysplit[i])
                     80:             except:
                     81:                 xret=xret+"%04i"%0
                     82:                 
                     83:         
                     84:         return cmp(int(yret),int(xret))
                     85:     except:
                     86:         return cmp(x[1],y[1])
                     87: 
                     88: 
1.1.2.10  casties    89: def unicodify(s):
1.1.2.1   dwinter    90:     """decode str (utf-8 or latin-1 representation) into unicode object"""
1.1.2.10  casties    91:     if not s:
                     92:         return u""
                     93:     if isinstance(s, str):
1.1.2.1   dwinter    94:         try:
1.1.2.10  casties    95:             return s.decode('utf-8')
1.1.2.1   dwinter    96:         except:
1.1.2.10  casties    97:             return s.decode('latin-1')
1.1.2.1   dwinter    98:     else:
1.1.2.10  casties    99:         return s
1.1.2.1   dwinter   100: 
1.1.2.10  casties   101: def utf8ify(s):
1.1.2.11! casties   102:     """encode unicode object or string into byte string in utf-8 representation.
        !           103:        assumes string objects to be utf-8"""
1.1.2.10  casties   104:     if not s:
1.1.2.1   dwinter   105:         return ""
1.1.2.10  casties   106:     if isinstance(s, str):
                    107:         return s
1.1.2.1   dwinter   108:     else:
1.1.2.10  casties   109:         return s.encode('utf-8')
1.1.2.1   dwinter   110: 
1.1.2.6   casties   111: 
                    112: def shortenString(s, l, ellipsis='...'):
                    113:     """returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis."""
                    114:     l1 = int((l - len(ellipsis)) / 2)
                    115:     return "%s%s%s"%(s[:l1],ellipsis,s[-l1:])
1.1.2.8   casties   116: 
                    117: 
                    118: #
                    119: # navigation methods (should better be a mixin class)
                    120: #
                    121: def getBreadcrumbs(self):
                    122:     """return list of breadcrumbs from here to the root"""
                    123:     crumbs = [(self.title, self.absolute_url(), self)]
                    124:     parent = self.aq_parent
                    125:     if hasattr(parent, 'getBreadcrumbs'):
                    126:         if self.title:
                    127:             return parent.getBreadcrumbs() + crumbs
                    128:         else:
                    129:             # if there's no title, skip this level
                    130:             return parent.getBreadcrumbs()
                    131:         
                    132:     return crumbs
                    133: 
                    134: 
1.1.2.9   casties   135: def getSection(self, crumbs=None):
1.1.2.8   casties   136:     """returns the current section name"""
1.1.2.9   casties   137:     # use breadcrumbs if available
                    138:     if crumbs is not None and len(crumbs) > 0:
                    139:         return crumbs[0][2].getId()
                    140: 
1.1.2.8   casties   141:     p = self
                    142:     sec = None
                    143:     # descend parents to the root (and remember the last id)
                    144:     while p is not None and p.meta_type != 'MPIWGRoot':
                    145:         sec = p.getId()
                    146:         p = p.aq_parent
                    147:     
                    148:     return sec
                    149: 
1.1.2.9   casties   150: def getSubSection(self, crumbs=None):
1.1.2.8   casties   151:     """returns the current subsection name"""
1.1.2.9   casties   152:     # use breadcrumbs if available
                    153:     if crumbs is not None and len(crumbs) > 1:
                    154:         return crumbs[1][2].getId()
                    155: 
1.1.2.8   casties   156:     p = self
                    157:     sec = None
                    158:     subsec = None
                    159:     # descend parents to the root (and remember the last id)
                    160:     while p is not None and p.meta_type != 'MPIWGRoot':
                    161:         subsec = sec
                    162:         sec = p.getId()
                    163:         p = p.aq_parent
                    164:     
                    165:     return subsec
                    166: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>