source: products/quintagroup.pfg.captcha/branches/migration/quintagroup/pfg/captcha/setuphandlers.py @ 2733

Last change on this file since 2733 was 2733, checked in by mylan, 14 years ago

#222: Fix uninstallation 'old' packages when second run quintagroup.pfg.captcha installation

  • Property svn:eol-style set to native
File size: 1.4 KB
Line 
1import logging
2from zope.component import queryMultiAdapter
3
4logger = logging.getLogger('quintagroup.pfg.captcha')
5
6captcha_fields = []
7
8def migrateToPackage(context):
9    """Collect old Products.qPloneCaptchaFields fields (before types tool setup).
10    """
11    global captcha_fields
12    if context.readDataFile('quintagroup.pfg.captcha_default.txt') is None:
13        return
14
15    site = context.getSite()
16    plone_tools = queryMultiAdapter((site, site.REQUEST), name="plone_tools")
17    ptypes = plone_tools.types()
18    cftype = getattr(ptypes, 'CaptchaField', None)
19    if cftype and getattr(cftype, 'product', "") == "qPloneCaptchaField":
20        catalog = plone_tools.catalog()
21        captcha_fields = [(cf.id, cf.getObject().aq_parent) \
22                          for cf in catalog.search({'portal_type':'CaptchaField'})]
23        logger.info("Old Products.qPloneCaptchaField fields collected.")
24
25
26def afterTypesTool(context):
27    """ Replace old qPloneCaptchaField with new quintagroup.pfg.captcha fields
28    (after types tool setup).
29    """
30    global captcha_fields
31
32    if context.readDataFile('quintagroup.pfg.captcha_default.txt') is None:
33        return
34
35    while captcha_fields:
36        cf_id, form = captcha_fields.pop()
37        form.manage_delObjects(cf_id)
38        form.invokeFactory("CaptchaField", cf_id)
39        logger.info("Fixed CaptchaField in '%s'" % form.getId())
40    logger.info("Finish Captcha field fixing")
Note: See TracBrowser for help on using the repository browser.