RecodeAllDocuments.py 2.44 KB
Newer Older
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
from Globals import get_request
from Acquisition import aq_base
from Products.ERP5Type.Base import Base
from zLOG import LOG

message = ''

def recodeDocumentRecursively(document, dry_run=0):
  global message

  klass = document.__class__
  if not issubclass(klass, Base):
    return

  id_list = []
  for property_sheet in klass.property_sheets:
    for property in property_sheet._properties:
      # Do not care about tokens, int, float, date or boolean because they should not have non-ASCII.
      if property['type'] in ('string', 'text', 'lines') and 'acquisition_base_category' not in property:
        id = property.get('storage_id', property['id'])
        # Make sure that ids are not duplicated.
        if id not in id_list:
          id_list.append(id)

  # Make sure working on the document itself.
  base = aq_base(document)
  for id in id_list:
    #LOG('RecodeAllDocuments', 0, 'Recoding %s of %s' % (id, document.getRelativeUrl()))
    value = getattr(base, id, None)
    if value is not None:
      if type(value) == type(''):
        if len(value) > 0:
          message += 'Recoding %s of %s\n' % (id, document.getRelativeUrl())
          if not dry_run: setattr(base, id, unicode(value, 'iso-8859-1').encode('utf-8'))
      elif type(value) in (type(()), type([])):
        if len(value) > 0:
          value_list = list(value)
          for i in range(len(value_list)):
            value = value_list[i]
            if type(value) == type('') and len(value) > 0:
              value_list[i] = unicode(value, 'iso-8859-1').encode('utf-8')
          message += 'Recoding %s of %s\n' % (id, document.getRelativeUrl())
          if not dry_run: setattr(base, id, tuple(value_list))
      else:
        raise RuntimeError, 'unknown type of value %r' % value

  # Call itself recursively.
  for object in document.objectValues():
    recodeDocumentRecursively(object)

def recodeAllDocuments(self, REQUEST=None, dry_run=0):
  global message
  message = ''

  if not REQUEST:
    REQUEST = get_request()

  try:
    dry_run = int(dry_run)
  except:
    pass

  portal = self.getPortalObject()
  #for folder in portal.objectValues('ERP5 Folder'):
  #  message += '# Checking the folder %s\n' % folder.getId()
  #  recodeDocumentRecursively(folder, dry_run=dry_run)
  for category in portal.portal_categories.objectValues('ERP5 Base Category'):
    message += '# Checking the category %s\n' % category.getId()
    recodeDocumentRecursively(category, dry_run=dry_run)
  return message