source: products/ploneorg.kudobounty/trunk/ploneorg/kudobounty/setuphandlers.py @ 3129

Last change on this file since 3129 was 3129, checked in by mylan, 10 years ago

Initial import of the package

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1from ploneorg.kudobounty.config import FORM_ID
2from ploneorg.kudobounty.config import TOPIC_ID
3from ploneorg.kudobounty.config import SUBMISSION_CONTAINER_ID
4
5from Products.CMFCore.utils import getToolByName
6from Products.GenericSetup.context import TarballExportContext, TarballImportContext
7from Products.GenericSetup.interfaces import IFilesystemExporter, IFilesystemImporter
8
9CREATE_SCRIPT_BODY = """
10portal = context.portal_url.getPortalObject()
11formProcessor = portal.restrictedTraverse('@@processBountyForm')
12formProcessor()
13return {}
14"""
15
16def createTopic(container, logger):
17    """
18    Located in: /bounty-mission/index_html
19
20    Item Type = ["Bounty Program Submission",]
21    state = ['published',]
22
23    sort_on = "creation"   
24    """
25    theCollection = getattr(container, TOPIC_ID, None)
26    ptype = getattr(theCollection, 'portal_type', None)
27    if not ptype == "Topic":
28        container.invokeFactory(id=TOPIC_ID, type_name="Topic")
29        theCollection = getattr(container, TOPIC_ID)
30        theCollection.unmarkCreationFlag()
31        theCriteria = theCollection.addCriterion('Type','ATPortalTypeCriterion')
32        theCriteria.setValue(["Bounty Program Submission",])
33        theCriteria = theCollection.addCriterion('review_state','ATSelectionCriterion')
34        theCriteria.setValue("published")
35        theCriteria = theCollection.addCriterion('created','ATSortCriterion') 
36        logger.info("To '%s' added collection, which grab all " \
37                    "'Bounty Program Submission' objects" % \
38                    '/'.join(theCriteria.getPhysicalPath()))
39    else:
40        logger.info("To '%s' collection already present in the portal" \
41                    % '/'.join(theCollection.getPhysicalPath())) 
42
43
44def createPFGForm(context, container, logger):
45    """
46    """
47    form = getattr(container, FORM_ID, None)
48    wftool = getToolByName(container, "portal_workflow")
49
50    if form is not None:
51        # Delete form if it exist
52        container.manage_delObjects(ids = FORM_ID)
53
54    # Create new form object
55    container.invokeFactory(id=FORM_ID, type_name="FormFolder",
56                           title="Form of Bounty Program")
57    form = getattr(container, FORM_ID)
58    logger.info("To '%s' added Form Folder" % \
59                '/'.join(form.getPhysicalPath()))
60    # cleanup form and import data from the archive
61    form.manage_delObjects(ids=form.objectIds())
62    pfg_data = context.readDataFile("pfg_data.tar.gz")
63    ctx = TarballImportContext(form, pfg_data)
64    IFilesystemImporter(form).import_(ctx, 'structure', True)
65    logger.info("Successfully imported PFG from data from archive into the form")
66    # Fix importing PFG via GS bug
67    #   - it adds extra indentation, wchich breaks the script.
68    create_bounty_script = form["create-bounty-submission"]
69    create_bounty_script.setScriptBody(CREATE_SCRIPT_BODY)
70    # Update form
71    form.update(**{"actionAdapter":["create-bounty-submission",],})
72    form.unmarkCreationFlag()
73    form.reindexObject()
74    # Publish the form
75    if wftool.getInfoFor(form, 'review_state') != 'published':
76        wftool.doActionFor(form, 'publish')
77        logger.info("'%s' PFG form successfully published" % form.Title())
78    else:
79        logger.info("'%s' PFG form already in 'published' state" % form.Title())
80
81
82
83def createStructure(context, logger):
84    site = context.getSite()
85
86    subcontainer = getattr(site, SUBMISSION_CONTAINER_ID, None)
87    if subcontainer is None:
88        site.invokeFactory("Folder", SUBMISSION_CONTAINER_ID)
89        subcontainer = getattr(site, SUBMISSION_CONTAINER_ID)
90        subcontainer.update(title="Bounty Submissions container")
91        logger.info("Successfully crated '%s' submissions container" \
92                    "in the portal" % SUBMISSION_CONTAINER_ID)
93    else:
94        logger.info("To '%s' container already present in the portal" \
95                    % '/'.join(subcontainer.getPhysicalPath())) 
96
97    createTopic(subcontainer, logger)
98    createPFGForm(context, subcontainer, logger)
99       
100
101
102def importVarious(context):
103    """ Various import steps
104    """
105    if context.readDataFile('ploneorg_kudobounty.txt') is None:
106        return
107    logger = context.getLogger("ploneorg.kudobounty")
108   
109    createStructure(context, logger)
Note: See TracBrowser for help on using the repository browser.