## Script (Python) "fillApplicationCategory"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: fillApplicationCategory.py 3438 2008-04-16 07:04:19Z henrik $
"""
"""
try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass

mtool = context.portal_membership
member = mtool.getAuthenticatedMember()
if str(member) not in ('admin','joachim'):
    return

import logging
import DateTime
logger = logging.getLogger('Skins.fillApplicationCategory')
from Products.AdvancedQuery import Eq, Between, Le,In
#aq_students = context.students_catalog.evalAdvancedQuery
aq_portal = context.portal_catalog_real.evalAdvancedQuery
#students_folder = context.portal_url.getPortalObject().campus.students

request = context.REQUEST
session = request.SESSION
response = request.RESPONSE
setheader = request.RESPONSE.setHeader
logger.info('start')
count = 0
not_count = 0
found = 0
commit_after = 1000
#certificates = context.getCertificatesDict()
certificates = context.portal_catalog.search({'meta_type': "Certificate"})
logger.info("found %d certificates" % len(certificates))
cat = {'cos': 'basic',
       'cos_cest': 'cest',
       'cos_sandwich': 'sandwich',
       }
to_change = {}
for cert in certificates:
    found = []
    for v in ('cos','cos_cest','cos_sandwich'):
        voc = getattr(context.portal_vocabularies,v)
        if cert.id in voc.keys():
            found += v,
    if found:
        d = {}
        d['application_category'] = cat[found[0]]
        d['cert_doc'] = cert.getObject().getContent()
        to_change[cert.id] = d
        logger.info("%s in %s" % (cert.id,','.join(found)))
        count += 1
    else:
        not_count += 1
        logger.info("%s not in vocabularies" % (cert.id,))
for cert,d in to_change.items():
    d['cert_doc'].edit(mapping = d)
    logger.info("set application_category of %s to %s" %
               (cert,d['application_category']))
logger.info("%d cert codes found, %d not found in vocabularies" % (count,not_count))
