def generateWorkflows(self): '''Generates the file that contains one function by workflow. Those functions are called by Plone for registering the workflows.''' workflows = '' for wfDescr in self.workflows: # Compute state names & info, transition names & infos, managed # permissions stateNames=','.join(['"%s"' % sn for sn in wfDescr.getStateNames()]) stateInfos = wfDescr.getStatesInfo(asDumpableCode=True) transitionNames = ','.join(['"%s"' % tn for tn in \ wfDescr.getTransitionNames()]) transitionInfos = wfDescr.getTransitionsInfo(asDumpableCode=True) managedPermissions = ','.join(['"%s"' % tn for tn in \ wfDescr.getManagedPermissions()]) wfName = WorkflowDescriptor.getWorkflowName(wfDescr.klass) workflows += '%s\ndef create_%s(self, id):\n ' \ 'stateNames = [%s]\n ' \ 'stateInfos = %s\n ' \ 'transitionNames = [%s]\n ' \ 'transitionInfos = %s\n ' \ 'managedPermissions = [%s]\n ' \ 'return WorkflowCreator("%s", DCWorkflowDefinition, ' \ 'stateNames, "%s", stateInfos, transitionNames, ' \ 'transitionInfos, managedPermissions, PROJECTNAME, ' \ 'ExternalMethod).run()\n' \ 'addWorkflowFactory(create_%s,\n id="%s",\n ' \ 'title="%s")\n\n' % (wfDescr.getScripts(), wfName, stateNames, stateInfos, transitionNames, transitionInfos, managedPermissions, wfName, wfDescr.getInitialStateName(), wfName, wfName, wfName) repls = self.repls.copy() repls['workflows'] = workflows self.copyFile('workflows.py', repls, destFolder='Extensions')
def generateWorkflow(self, wfDescr): '''This method does not generate the workflow definition, which is done in self.generateWorkflows. This method just creates the i18n labels related to the workflow described by p_wfDescr.''' k = wfDescr.klass print 'Generating %s.%s (gen-workflow)...' % (k.__module__, k.__name__) # Identify Plone workflow name wfName = WorkflowDescriptor.getWorkflowName(wfDescr.klass) # Add i18n messages for states and transitions for sName in wfDescr.getStateNames(): poMsg = PoMessage('%s_%s' % (wfName, sName), '', sName) poMsg.produceNiceDefault() self.labels.append(poMsg) for tName, tLabel in wfDescr.getTransitionNames(withLabels=True): poMsg = PoMessage('%s_%s' % (wfName, tName), '', tLabel) poMsg.produceNiceDefault() self.labels.append(poMsg) for transition in wfDescr.getTransitions(): if transition.notify: # Appy will send a mail when this transition is triggered. # So we need 2 i18n labels for every DC transition corresponding # to this Appy transition: one for the mail subject and one for # the mail body. tName = wfDescr.getNameOf(transition) # Appy name tNames = wfDescr.getTransitionNamesOf(tName, transition) # DC # name(s) for tn in tNames: subjectLabel = '%s_%s_mail_subject' % (wfName, tn) poMsg = PoMessage(subjectLabel, '', PoMessage.EMAIL_SUBJECT) self.labels.append(poMsg) bodyLabel = '%s_%s_mail_body' % (wfName, tn) poMsg = PoMessage(bodyLabel, '', PoMessage.EMAIL_BODY) self.labels.append(poMsg)
def generateConfig(self): repls = self.repls.copy() # Get some lists of classes classes = self.getClasses() classesWithCustom = self.getClasses(include='custom') classesButTool = self.getClasses(include='allButTool') classesAll = self.getClasses(include='all') # Compute imports imports = ['import %s' % self.applicationName] for classDescr in (classesWithCustom + self.workflows): theImport = 'import %s' % classDescr.klass.__module__ if theImport not in imports: imports.append(theImport) repls['imports'] = '\n'.join(imports) # Compute default add roles repls['defaultAddRoles'] = ','.join( ['"%s"' % r for r in self.config.defaultCreators]) # Compute list of add permissions addPermissions = '' for classDescr in classesButTool: addPermissions += ' "%s":"%s: Add %s",\n' % (classDescr.name, self.applicationName, classDescr.name) repls['addPermissions'] = addPermissions # Compute root classes repls['rootClasses'] = ','.join(["'%s'" % c.name \ for c in classesButTool if c.isRoot()]) # Compute list of class definitions repls['appClasses'] = ','.join(['%s.%s' % (c.klass.__module__, \ c.klass.__name__) for c in classes]) # Compute lists of class names repls['appClassNames'] = ','.join(['"%s"' % c.name \ for c in classes]) repls['allClassNames'] = ','.join(['"%s"' % c.name \ for c in classesButTool]) # Compute classes whose instances must not be catalogued. catalogMap = '' blackClasses = [self.tool.name] for blackClass in blackClasses: catalogMap += "catalogMap['%s'] = {}\n" % blackClass catalogMap += "catalogMap['%s']['black'] = " \ "['portal_catalog']\n" % blackClass repls['catalogMap'] = catalogMap # Compute workflows workflows = '' for classDescr in classesAll: if hasattr(classDescr.klass, 'workflow'): wfName = WorkflowDescriptor.getWorkflowName( classDescr.klass.workflow) workflows += '\n "%s":"%s",' % (classDescr.name, wfName) repls['workflows'] = workflows # Compute workflow instances initialisation wfInit = '' for workflowDescr in self.workflows: k = workflowDescr.klass className = '%s.%s' % (k.__module__, k.__name__) wfInit += 'wf = %s()\n' % className wfInit += 'wf._transitionsMapping = {}\n' for transition in workflowDescr.getTransitions(): tName = workflowDescr.getNameOf(transition) tNames = workflowDescr.getTransitionNamesOf(tName, transition) for trName in tNames: wfInit += 'wf._transitionsMapping["%s"] = wf.%s\n' % \ (trName, tName) # We need a new attribute that stores states in order wfInit += 'wf._states = []\n' for stateName in workflowDescr.getStateNames(ordered=True): wfInit += 'wf._states.append("%s")\n' % stateName wfInit += 'workflowInstances[%s] = wf\n' % className repls['workflowInstancesInit'] = wfInit # Compute the list of ordered attributes (forward and backward, # inherited included) for every Appy class. attributes = [] attributesDict = [] for classDescr in classesAll: titleFound = False attrs = [] attrNames = [] for name, appyType, klass in classDescr.getOrderedAppyAttributes(): attrs.append(self.getAppyTypePath(name, appyType, klass)) attrNames.append(name) if name == 'title': titleFound = True # Add the "title" mandatory field if not found if not titleFound: attrs.insert(0, 'copy.deepcopy(appy.gen.title)') attrNames.insert(0, 'title') # Any backward attributes to append? if classDescr.name in self.referers: for field, rel in self.referers[classDescr.name]: try: getattr(field.classDescr.klass, field.fieldName) klass = field.classDescr.klass except AttributeError: klass = field.classDescr.modelClass attrs.append(self.getAppyTypePath(field.fieldName, field.appyType, klass, isBack=True)) attrNames.append(field.appyType.back.attribute) attributes.append('"%s":[%s]' % (classDescr.name,','.join(attrs))) aDict = '' i = -1 for attr in attrs: i += 1 aDict += '"%s":attributes["%s"][%d],' % \ (attrNames[i], classDescr.name, i) attributesDict.append('"%s":{%s}' % (classDescr.name, aDict)) repls['attributes'] = ',\n '.join(attributes) repls['attributesDict'] = ',\n '.join(attributesDict) # Compute list of used roles for registering them if needed specificRoles = self.getAllUsedRoles(plone=False) repls['roles'] = ','.join(['"%s"' % r.name for r in specificRoles]) globalRoles = self.getAllUsedRoles(plone=False, local=False) repls['gRoles'] = ','.join(['"%s"' % r.name for r in globalRoles]) grantableRoles = self.getAllUsedRoles(local=False, grantable=True) repls['grRoles'] = ','.join(['"%s"' % r.name for r in grantableRoles]) # Generate configuration options repls['showPortlet'] = self.config.showPortlet repls['languages'] = ','.join('"%s"' % l for l in self.config.languages) repls['languageSelector'] = self.config.languageSelector repls['minimalistPlone'] = self.config.minimalistPlone repls['appFrontPage'] = bool(self.config.frontPage) repls['sourceLanguage'] = self.config.sourceLanguage self.copyFile('config.py', repls)