Back to index

plone3  3.1.7
formatCatalogMetadata.py
Go to the documentation of this file.
00001 ## Script (Python) "formatCatalogMetadata"
00002 ##bind container=container
00003 ##bind context=context
00004 ##bind namespace=
00005 ##bind script=script
00006 ##bind subpath=traverse_subpath
00007 ##parameters=value,long_format=True
00008 ##title=Determine whether the input is a DateTime or ISO date and localize it if so, also convert lists and dicts into reasonable strings.
00009 from DateTime import DateTime
00010 from ZODB.POSException import ConflictError
00011 from Products.CMFPlone.utils import safe_callable
00012 from AccessControl import Unauthorized
00013 
00014 if value is None:
00015     return ''
00016 
00017 if same_type(value, DateTime()):
00018     return context.toLocalizedTime(value.ISO(), long_format = long_format)
00019 
00020 # Ugly but fast check for ISO format (ensure we have '-' and positions 4 and 7,
00021 #  ' ' at positiion 10 and ':' and 13 and 16), then convert just in case.
00022 if same_type(value, '') and value[4:-1:3] == '-- ::':
00023     try:
00024         DateTime(value)
00025     except ConflictError:
00026         raise
00027     except:
00028         # Bare excepts are ugly, but DateTime raises a whole bunch of different
00029         # errors for bad input (Syntax, Time, Date, Index, etc.), best to be
00030         # safe.
00031         return value
00032     return context.toLocalizedTime(value, long_format = long_format)
00033 
00034 try:
00035     # Missing.Value and others have items() but don't have security assertions
00036     # to support accessing it.
00037     items = getattr(value, 'items', None)
00038 except Unauthorized:
00039     items = None
00040 
00041 if items is not None and safe_callable(items):
00042     # For dictionaries return a string of the form 'key1: value1, key2: value2' 
00043     value = ', '.join(['%s: %s'%(a,b) for a,b in items()])
00044 if same_type(value,[]) or same_type(value,()):
00045     # Return list as comma separated values
00046     alist = []
00047     for item in value:
00048         if same_type(item, u''):
00049             alist.append(item)
00050         else:
00051             alist.append(str(item))
00052     value = ', '.join(alist)
00053 
00054 if not same_type(value, u''):
00055     value = str(value)
00056 
00057 pt = context.portal_properties
00058 site_props = getattr(pt, 'site_properties', None)
00059 if site_props is not None:
00060     max_length = site_props.getProperty(
00061         'search_results_description_length', 160  )
00062     ellipsis = site_props.getProperty('ellipsis', '...' )
00063 else:
00064     max_length = 160
00065     ellipsis = '...'
00066 if len(value) < max_length:
00067     return value
00068 else:
00069     return '%s%s'%(value[:max_length],ellipsis)