2009-06-29 07:06:01 -05:00
|
|
|
'''This file contains the main Generator class used for generating a
|
|
|
|
Plone 2.5-compliant product.'''
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
import os, os.path, re, sys
|
|
|
|
import appy.gen
|
|
|
|
from appy.gen import *
|
|
|
|
from appy.gen.po import PoMessage, PoFile, PoParser
|
|
|
|
from appy.gen.generator import Generator as AbstractGenerator
|
2010-09-02 09:16:08 -05:00
|
|
|
from appy.gen.utils import getClassName
|
2010-11-10 08:15:00 -06:00
|
|
|
from descriptors import ClassDescriptor, WorkflowDescriptor, \
|
2011-01-14 02:06:25 -06:00
|
|
|
ToolClassDescriptor, UserClassDescriptor, \
|
|
|
|
TranslationClassDescriptor
|
|
|
|
from model import ModelClass, User, Tool, Translation
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
# Common methods that need to be defined on every Archetype class --------------
|
|
|
|
COMMON_METHODS = '''
|
|
|
|
def getTool(self): return self.%s
|
|
|
|
def getProductConfig(self): return Products.%s.config
|
2009-11-20 13:17:06 -06:00
|
|
|
def skynView(self):
|
2011-01-28 07:36:30 -06:00
|
|
|
"""Redirects to skyn/view. Transfers the status message if any."""
|
|
|
|
rq = self.REQUEST
|
|
|
|
msg = rq.get('portal_status_message', '')
|
|
|
|
if msg:
|
|
|
|
url = self.getUrl(portal_status_message=msg)
|
|
|
|
else:
|
|
|
|
url = self.getUrl()
|
|
|
|
return rq.RESPONSE.redirect(url)
|
2009-06-29 07:06:01 -05:00
|
|
|
'''
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Generator(AbstractGenerator):
|
|
|
|
'''This generator generates a Plone 2.5-compliant product from a given
|
|
|
|
appy application.'''
|
|
|
|
poExtensions = ('.po', '.pot')
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2010-10-14 07:43:56 -05:00
|
|
|
Tool._appy_clean()
|
2009-06-29 07:06:01 -05:00
|
|
|
AbstractGenerator.__init__(self, *args, **kwargs)
|
2010-08-05 11:23:17 -05:00
|
|
|
# Set our own Descriptor classes
|
|
|
|
self.descriptorClasses['class'] = ClassDescriptor
|
|
|
|
self.descriptorClasses['workflow'] = WorkflowDescriptor
|
2011-01-14 02:06:25 -06:00
|
|
|
# Create our own Tool, User and Translation instances
|
2010-08-05 11:23:17 -05:00
|
|
|
self.tool = ToolClassDescriptor(Tool, self)
|
2010-09-02 09:16:08 -05:00
|
|
|
self.user = UserClassDescriptor(User, self)
|
2011-01-14 02:06:25 -06:00
|
|
|
self.translation = TranslationClassDescriptor(Translation, self)
|
2009-06-29 07:06:01 -05:00
|
|
|
# i18n labels to generate
|
|
|
|
self.labels = [] # i18n labels
|
|
|
|
self.toolInstanceName = 'portal_%s' % self.applicationName.lower()
|
|
|
|
self.portletName = '%s_portlet' % self.applicationName.lower()
|
|
|
|
self.skinsFolder = 'skins/%s' % self.applicationName
|
|
|
|
# The following dict, pre-filled in the abstract generator, contains a
|
|
|
|
# series of replacements that need to be applied to file templates to
|
|
|
|
# generate files.
|
|
|
|
commonMethods = COMMON_METHODS % \
|
|
|
|
(self.toolInstanceName, self.applicationName)
|
|
|
|
self.repls.update(
|
2011-01-14 02:06:25 -06:00
|
|
|
{'toolInstanceName': self.toolInstanceName,
|
|
|
|
'commonMethods': commonMethods})
|
2009-06-29 07:06:01 -05:00
|
|
|
self.referers = {}
|
|
|
|
|
|
|
|
versionRex = re.compile('(.*?\s+build)\s+(\d+)')
|
|
|
|
def initialize(self):
|
|
|
|
# Determine version number of the Plone product
|
|
|
|
self.version = '0.1 build 1'
|
|
|
|
versionTxt = os.path.join(self.outputFolder, 'version.txt')
|
|
|
|
if os.path.exists(versionTxt):
|
|
|
|
f = file(versionTxt)
|
|
|
|
oldVersion = f.read().strip()
|
|
|
|
f.close()
|
|
|
|
res = self.versionRex.search(oldVersion)
|
|
|
|
self.version = res.group(1) + ' ' + str(int(res.group(2))+1)
|
|
|
|
# Existing i18n files
|
|
|
|
self.i18nFiles = {} #~{p_fileName: PoFile}~
|
|
|
|
# Retrieve existing i18n files if any
|
|
|
|
i18nFolder = os.path.join(self.outputFolder, 'i18n')
|
|
|
|
if os.path.exists(i18nFolder):
|
|
|
|
for fileName in os.listdir(i18nFolder):
|
|
|
|
name, ext = os.path.splitext(fileName)
|
|
|
|
if ext in self.poExtensions:
|
|
|
|
poParser = PoParser(os.path.join(i18nFolder, fileName))
|
|
|
|
self.i18nFiles[fileName] = poParser.parse()
|
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
# Some useful aliases
|
|
|
|
msg = PoMessage
|
|
|
|
app = self.applicationName
|
|
|
|
# Some global i18n messages
|
|
|
|
poMsg = msg(app, '', app); poMsg.produceNiceDefault()
|
|
|
|
self.labels += [poMsg,
|
2009-12-30 10:12:18 -06:00
|
|
|
msg('workflow_state', '', msg.WORKFLOW_STATE),
|
2010-08-05 11:23:17 -05:00
|
|
|
msg('appy_title', '', msg.APPY_TITLE),
|
2009-12-30 10:12:18 -06:00
|
|
|
msg('data_change', '', msg.DATA_CHANGE),
|
|
|
|
msg('modified_field', '', msg.MODIFIED_FIELD),
|
|
|
|
msg('previous_value', '', msg.PREVIOUS_VALUE),
|
|
|
|
msg('phase', '', msg.PHASE),
|
|
|
|
msg('root_type', '', msg.ROOT_TYPE),
|
|
|
|
msg('workflow_comment', '', msg.WORKFLOW_COMMENT),
|
|
|
|
msg('choose_a_value', '', msg.CHOOSE_A_VALUE),
|
|
|
|
msg('choose_a_doc', '', msg.CHOOSE_A_DOC),
|
|
|
|
msg('min_ref_violated', '', msg.MIN_REF_VIOLATED),
|
|
|
|
msg('max_ref_violated', '', msg.MAX_REF_VIOLATED),
|
|
|
|
msg('no_ref', '', msg.REF_NO),
|
|
|
|
msg('add_ref', '', msg.REF_ADD),
|
|
|
|
msg('ref_actions', '', msg.REF_ACTIONS),
|
|
|
|
msg('move_up', '', msg.REF_MOVE_UP),
|
|
|
|
msg('move_down', '', msg.REF_MOVE_DOWN),
|
|
|
|
msg('query_create', '', msg.QUERY_CREATE),
|
|
|
|
msg('query_import', '', msg.QUERY_IMPORT),
|
|
|
|
msg('query_no_result', '', msg.QUERY_NO_RESULT),
|
|
|
|
msg('query_consult_all', '', msg.QUERY_CONSULT_ALL),
|
|
|
|
msg('import_title', '', msg.IMPORT_TITLE),
|
|
|
|
msg('import_show_hide', '', msg.IMPORT_SHOW_HIDE),
|
|
|
|
msg('import_already', '', msg.IMPORT_ALREADY),
|
|
|
|
msg('import_many', '', msg.IMPORT_MANY),
|
|
|
|
msg('import_done', '', msg.IMPORT_DONE),
|
|
|
|
msg('search_title', '', msg.SEARCH_TITLE),
|
|
|
|
msg('search_button', '', msg.SEARCH_BUTTON),
|
|
|
|
msg('search_objects', '', msg.SEARCH_OBJECTS),
|
|
|
|
msg('search_results', '', msg.SEARCH_RESULTS),
|
|
|
|
msg('search_results_descr', '', ' '),
|
2010-01-06 11:36:16 -06:00
|
|
|
msg('search_new', '', msg.SEARCH_NEW),
|
2010-01-07 13:25:18 -06:00
|
|
|
msg('search_from', '', msg.SEARCH_FROM),
|
|
|
|
msg('search_to', '', msg.SEARCH_TO),
|
2010-03-19 07:13:36 -05:00
|
|
|
msg('search_or', '', msg.SEARCH_OR),
|
|
|
|
msg('search_and', '', msg.SEARCH_AND),
|
2009-12-30 10:12:18 -06:00
|
|
|
msg('ref_invalid_index', '', msg.REF_INVALID_INDEX),
|
2010-08-05 11:23:17 -05:00
|
|
|
msg('bad_long', '', msg.BAD_LONG),
|
2009-12-30 10:12:18 -06:00
|
|
|
msg('bad_float', '', msg.BAD_FLOAT),
|
2011-02-12 10:09:11 -06:00
|
|
|
msg('bad_date', '', msg.BAD_DATE),
|
2009-12-30 10:12:18 -06:00
|
|
|
msg('bad_email', '', msg.BAD_EMAIL),
|
|
|
|
msg('bad_url', '', msg.BAD_URL),
|
|
|
|
msg('bad_alphanumeric', '', msg.BAD_ALPHANUMERIC),
|
2010-08-05 11:23:17 -05:00
|
|
|
msg('bad_select_value', '', msg.BAD_SELECT_VALUE),
|
2009-12-30 10:12:18 -06:00
|
|
|
msg('select_delesect', '', msg.SELECT_DESELECT),
|
|
|
|
msg('no_elem_selected', '', msg.NO_SELECTION),
|
|
|
|
msg('delete_confirm', '', msg.DELETE_CONFIRM),
|
|
|
|
msg('delete_done', '', msg.DELETE_DONE),
|
|
|
|
msg('goto_first', '', msg.GOTO_FIRST),
|
|
|
|
msg('goto_previous', '', msg.GOTO_PREVIOUS),
|
|
|
|
msg('goto_next', '', msg.GOTO_NEXT),
|
|
|
|
msg('goto_last', '', msg.GOTO_LAST),
|
|
|
|
msg('goto_source', '', msg.GOTO_SOURCE),
|
2010-01-06 11:36:16 -06:00
|
|
|
msg('whatever', '', msg.WHATEVER),
|
2010-03-29 13:44:28 -05:00
|
|
|
msg('yes', '', msg.YES),
|
|
|
|
msg('no', '', msg.NO),
|
2010-08-05 11:23:17 -05:00
|
|
|
msg('field_required', '', msg.FIELD_REQUIRED),
|
2010-11-16 08:32:47 -06:00
|
|
|
msg('field_invalid', '', msg.FIELD_INVALID),
|
2010-08-05 11:23:17 -05:00
|
|
|
msg('file_required', '', msg.FILE_REQUIRED),
|
|
|
|
msg('image_required', '', msg.IMAGE_REQUIRED),
|
2011-01-28 07:36:30 -06:00
|
|
|
msg('odt', '', msg.FORMAT_ODT),
|
|
|
|
msg('pdf', '', msg.FORMAT_PDF),
|
|
|
|
msg('doc', '', msg.FORMAT_DOC),
|
|
|
|
msg('rtf', '', msg.FORMAT_RTF),
|
2009-06-29 07:06:01 -05:00
|
|
|
]
|
2010-08-27 01:59:53 -05:00
|
|
|
# Create a label for every role added by this application
|
2010-09-02 09:16:08 -05:00
|
|
|
for role in self.getAllUsedRoles():
|
|
|
|
self.labels.append(msg('role_%s' % role.name,'', role.name,
|
|
|
|
niceDefault=True))
|
2009-06-29 07:06:01 -05:00
|
|
|
# Create basic files (config.py, Install.py, etc)
|
|
|
|
self.generateTool()
|
|
|
|
self.generateInit()
|
|
|
|
self.generateWorkflows()
|
2009-11-11 13:22:13 -06:00
|
|
|
self.generateTests()
|
2010-08-12 04:56:42 -05:00
|
|
|
if self.config.frontPage:
|
|
|
|
self.generateFrontPage()
|
2010-09-02 09:16:08 -05:00
|
|
|
self.copyFile('Install.py', self.repls, destFolder='Extensions')
|
2011-01-28 07:36:30 -06:00
|
|
|
self.generateConfigureZcml()
|
2009-06-29 07:06:01 -05:00
|
|
|
self.copyFile('import_steps.xml', self.repls,
|
|
|
|
destFolder='profiles/default')
|
|
|
|
self.copyFile('ProfileInit.py', self.repls, destFolder='profiles',
|
|
|
|
destName='__init__.py')
|
2009-10-18 07:52:27 -05:00
|
|
|
self.copyFile('Portlet.pt', self.repls,
|
|
|
|
destName='%s.pt' % self.portletName, destFolder=self.skinsFolder)
|
2009-06-29 07:06:01 -05:00
|
|
|
self.copyFile('tool.gif', {})
|
2009-11-06 04:33:56 -06:00
|
|
|
self.copyFile('Styles.css.dtml',self.repls, destFolder=self.skinsFolder,
|
2009-06-29 07:06:01 -05:00
|
|
|
destName = '%s.css.dtml' % self.applicationName)
|
2009-11-06 04:33:56 -06:00
|
|
|
self.copyFile('IEFixes.css.dtml',self.repls,destFolder=self.skinsFolder)
|
2009-09-18 07:42:31 -05:00
|
|
|
if self.config.minimalistPlone:
|
|
|
|
self.copyFile('colophon.pt', self.repls,destFolder=self.skinsFolder)
|
|
|
|
self.copyFile('footer.pt', self.repls, destFolder=self.skinsFolder)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Create version.txt
|
|
|
|
f = open(os.path.join(self.outputFolder, 'version.txt'), 'w')
|
|
|
|
f.write(self.version)
|
|
|
|
f.close()
|
2009-11-11 13:22:13 -06:00
|
|
|
# Make Extensions and tests Python packages
|
|
|
|
for moduleFolder in ('Extensions', 'tests'):
|
2009-06-29 07:06:01 -05:00
|
|
|
initFile = '%s/%s/__init__.py' % (self.outputFolder, moduleFolder)
|
|
|
|
if not os.path.isfile(initFile):
|
|
|
|
f = open(initFile, 'w')
|
|
|
|
f.write('')
|
|
|
|
f.close()
|
|
|
|
# Decline i18n labels into versions for child classes
|
|
|
|
for classDescr in self.classes:
|
|
|
|
for poMsg in classDescr.labelsToPropagate:
|
|
|
|
for childDescr in classDescr.getChildren():
|
|
|
|
childMsg = poMsg.clone(classDescr.name, childDescr.name)
|
|
|
|
if childMsg not in self.labels:
|
|
|
|
self.labels.append(childMsg)
|
|
|
|
# Generate i18n pot file
|
|
|
|
potFileName = '%s.pot' % self.applicationName
|
|
|
|
if self.i18nFiles.has_key(potFileName):
|
|
|
|
potFile = self.i18nFiles[potFileName]
|
|
|
|
else:
|
|
|
|
fullName = os.path.join(self.outputFolder, 'i18n/%s' % potFileName)
|
|
|
|
potFile = PoFile(fullName)
|
|
|
|
self.i18nFiles[potFileName] = potFile
|
2011-01-14 02:06:25 -06:00
|
|
|
# We update the POT file with our list of automatically managed labels.
|
2009-06-29 07:06:01 -05:00
|
|
|
removedLabels = potFile.update(self.labels, self.options.i18nClean,
|
|
|
|
not self.options.i18nSort)
|
|
|
|
if removedLabels:
|
|
|
|
print 'Warning: %d messages were removed from translation ' \
|
|
|
|
'files: %s' % (len(removedLabels), str(removedLabels))
|
2011-01-14 02:06:25 -06:00
|
|
|
# Before generating the POT file, we still need to add one label for
|
|
|
|
# every page for the Translation class. We've not done it yet because
|
|
|
|
# the number of pages depends on the total number of labels in the POT
|
|
|
|
# file.
|
|
|
|
pageLabels = []
|
|
|
|
nbOfPages = int(len(potFile.messages)/self.config.translationsPerPage)+1
|
|
|
|
for i in range(nbOfPages):
|
|
|
|
msgId = '%s_page_%d' % (self.translation.name, i+2)
|
|
|
|
pageLabels.append(msg(msgId, '', 'Page %d' % (i+2)))
|
|
|
|
potFile.update(pageLabels, keepExistingOrder=False)
|
2009-06-29 07:06:01 -05:00
|
|
|
potFile.generate()
|
|
|
|
# Generate i18n po files
|
|
|
|
for language in self.config.languages:
|
|
|
|
# I must generate (or update) a po file for the language(s)
|
|
|
|
# specified in the configuration.
|
|
|
|
poFileName = potFile.getPoFileName(language)
|
|
|
|
if self.i18nFiles.has_key(poFileName):
|
|
|
|
poFile = self.i18nFiles[poFileName]
|
|
|
|
else:
|
|
|
|
fullName = os.path.join(self.outputFolder,
|
|
|
|
'i18n/%s' % poFileName)
|
|
|
|
poFile = PoFile(fullName)
|
|
|
|
self.i18nFiles[poFileName] = poFile
|
|
|
|
poFile.update(potFile.messages, self.options.i18nClean,
|
|
|
|
not self.options.i18nSort)
|
|
|
|
poFile.generate()
|
2011-01-14 02:06:25 -06:00
|
|
|
# Generate corresponding fields on the Translation class
|
|
|
|
page = 'main'
|
|
|
|
i = 0
|
|
|
|
for message in potFile.messages:
|
|
|
|
i += 1
|
|
|
|
# A computed field is used for displaying the text to translate.
|
|
|
|
self.translation.addLabelField(message.id, page)
|
|
|
|
# A String field will hold the translation in itself.
|
|
|
|
self.translation.addMessageField(message.id, page, self.i18nFiles)
|
|
|
|
if (i % self.config.translationsPerPage) == 0:
|
|
|
|
# A new page must be defined.
|
|
|
|
if page == 'main':
|
|
|
|
page = '2'
|
|
|
|
else:
|
|
|
|
page = str(int(page)+1)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Generate i18n po files for other potential files
|
|
|
|
for poFile in self.i18nFiles.itervalues():
|
|
|
|
if not poFile.generated:
|
|
|
|
poFile.generate()
|
2011-01-14 02:06:25 -06:00
|
|
|
self.generateWrappers()
|
|
|
|
self.generateConfig()
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2010-09-02 09:16:08 -05:00
|
|
|
def getAllUsedRoles(self, plone=None, local=None, grantable=None):
|
2010-08-05 11:23:17 -05:00
|
|
|
'''Produces a list of all the roles used within all workflows and
|
2010-09-02 09:16:08 -05:00
|
|
|
classes defined in this application.
|
|
|
|
|
|
|
|
If p_plone is True, it keeps only Plone-standard roles; if p_plone
|
|
|
|
is False, it keeps only roles which are specific to this application;
|
|
|
|
if p_plone is None it has no effect (so it keeps both roles).
|
|
|
|
|
|
|
|
If p_local is True, it keeps only local roles (ie, roles that can
|
|
|
|
only be granted locally); if p_local is False, it keeps only "global"
|
|
|
|
roles; if p_local is None it has no effect (so it keeps both roles).
|
|
|
|
|
|
|
|
If p_grantable is True, it keeps only roles that the admin can
|
|
|
|
grant; if p_grantable is False, if keeps only ungrantable roles (ie
|
|
|
|
those that are implicitly granted by the system like role
|
|
|
|
"Authenticated"); if p_grantable is None it keeps both roles.'''
|
|
|
|
allRoles = {} # ~{s_roleName:Role_role}~
|
|
|
|
# Gather roles from workflow states and transitions
|
2009-06-29 07:06:01 -05:00
|
|
|
for wfDescr in self.workflows:
|
|
|
|
for attr in dir(wfDescr.klass):
|
|
|
|
attrValue = getattr(wfDescr.klass, attr)
|
|
|
|
if isinstance(attrValue, State) or \
|
|
|
|
isinstance(attrValue, Transition):
|
2010-09-02 09:16:08 -05:00
|
|
|
for role in attrValue.getUsedRoles():
|
|
|
|
if role.name not in allRoles:
|
|
|
|
allRoles[role.name] = role
|
|
|
|
# Gather roles from "creators" attributes from every class
|
2010-08-05 11:23:17 -05:00
|
|
|
for cDescr in self.getClasses(include='all'):
|
2010-09-02 09:16:08 -05:00
|
|
|
for role in cDescr.getCreators():
|
|
|
|
if role.name not in allRoles:
|
|
|
|
allRoles[role.name] = role
|
|
|
|
res = allRoles.values()
|
|
|
|
# Filter the result according to parameters
|
|
|
|
for p in ('plone', 'local', 'grantable'):
|
|
|
|
if eval(p) != None:
|
|
|
|
res = [r for r in res if eval('r.%s == %s' % (p, p))]
|
2009-06-29 07:06:01 -05:00
|
|
|
return res
|
|
|
|
|
|
|
|
def addReferer(self, fieldDescr, relationship):
|
|
|
|
'''p_fieldDescr is a Ref type definition. We will create in config.py a
|
|
|
|
dict that lists all back references, by type.'''
|
|
|
|
k = fieldDescr.appyType.klass
|
2010-09-02 09:16:08 -05:00
|
|
|
refClassName = getClassName(k, self.applicationName)
|
2009-06-29 07:06:01 -05:00
|
|
|
if not self.referers.has_key(refClassName):
|
|
|
|
self.referers[refClassName] = []
|
|
|
|
self.referers[refClassName].append( (fieldDescr, relationship))
|
|
|
|
|
2010-08-05 11:23:17 -05:00
|
|
|
def getAppyTypePath(self, name, appyType, klass, isBack=False):
|
|
|
|
'''Gets the path to the p_appyType when a direct reference to an
|
|
|
|
appyType must be generated in a Python file.'''
|
|
|
|
if issubclass(klass, ModelClass):
|
|
|
|
res = 'wraps.%s.%s' % (klass.__name__, name)
|
|
|
|
else:
|
|
|
|
res = '%s.%s.%s' % (klass.__module__, klass.__name__, name)
|
|
|
|
if isBack: res += '.back'
|
|
|
|
return res
|
|
|
|
|
2011-01-28 07:36:30 -06:00
|
|
|
def generateConfigureZcml(self):
|
|
|
|
'''Generates file configure.zcml.'''
|
|
|
|
repls = self.repls.copy()
|
|
|
|
# Note every class as "deprecated".
|
|
|
|
depr = ''
|
|
|
|
for klass in self.getClasses(include='all'):
|
|
|
|
depr += '<five:deprecatedManageAddDelete class=".%s.%s"/>\n' % \
|
|
|
|
(klass.name, klass.name)
|
|
|
|
repls['deprecated'] = depr
|
|
|
|
self.copyFile('configure.zcml', repls)
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def generateConfig(self):
|
2010-09-02 09:16:08 -05:00
|
|
|
repls = self.repls.copy()
|
2011-01-14 02:06:25 -06:00
|
|
|
# Get some lists of classes
|
|
|
|
classes = self.getClasses()
|
|
|
|
classesWithCustom = self.getClasses(include='custom')
|
|
|
|
classesButTool = self.getClasses(include='allButTool')
|
|
|
|
classesAll = self.getClasses(include='all')
|
2010-09-02 09:16:08 -05:00
|
|
|
# Compute imports
|
|
|
|
imports = ['import %s' % self.applicationName]
|
2011-01-14 02:06:25 -06:00
|
|
|
for classDescr in (classesWithCustom + self.workflows):
|
2010-09-02 09:16:08 -05:00
|
|
|
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 = ''
|
2011-01-14 02:06:25 -06:00
|
|
|
for classDescr in classesButTool:
|
2010-09-02 09:16:08 -05:00
|
|
|
addPermissions += ' "%s":"%s: Add %s",\n' % (classDescr.name,
|
|
|
|
self.applicationName, classDescr.name)
|
|
|
|
repls['addPermissions'] = addPermissions
|
|
|
|
# Compute root classes
|
2011-01-14 02:06:25 -06:00
|
|
|
repls['rootClasses'] = ','.join(["'%s'" % c.name \
|
|
|
|
for c in classesButTool if c.isRoot()])
|
2010-09-02 09:16:08 -05:00
|
|
|
# Compute list of class definitions
|
2011-01-14 02:06:25 -06:00
|
|
|
repls['appClasses'] = ','.join(['%s.%s' % (c.klass.__module__, \
|
|
|
|
c.klass.__name__) for c in classes])
|
2010-09-02 09:16:08 -05:00
|
|
|
# Compute lists of class names
|
2011-01-14 02:06:25 -06:00
|
|
|
repls['appClassNames'] = ','.join(['"%s"' % c.name \
|
|
|
|
for c in classes])
|
|
|
|
repls['allClassNames'] = ','.join(['"%s"' % c.name \
|
|
|
|
for c in classesButTool])
|
2010-09-02 09:16:08 -05:00
|
|
|
# Compute classes whose instances must not be catalogued.
|
|
|
|
catalogMap = ''
|
2011-01-14 02:06:25 -06:00
|
|
|
blackClasses = [self.tool.name]
|
2010-09-02 09:16:08 -05:00
|
|
|
for blackClass in blackClasses:
|
|
|
|
catalogMap += "catalogMap['%s'] = {}\n" % blackClass
|
|
|
|
catalogMap += "catalogMap['%s']['black'] = " \
|
|
|
|
"['portal_catalog']\n" % blackClass
|
|
|
|
repls['catalogMap'] = catalogMap
|
|
|
|
# Compute workflows
|
|
|
|
workflows = ''
|
2011-01-14 02:06:25 -06:00
|
|
|
for classDescr in classesAll:
|
2010-09-02 09:16:08 -05:00
|
|
|
if hasattr(classDescr.klass, 'workflow'):
|
|
|
|
wfName = WorkflowDescriptor.getWorkflowName(
|
|
|
|
classDescr.klass.workflow)
|
|
|
|
workflows += '\n "%s":"%s",' % (classDescr.name, wfName)
|
|
|
|
repls['workflows'] = workflows
|
2009-06-29 07:06:01 -05:00
|
|
|
# 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
|
2010-09-02 09:16:08 -05:00
|
|
|
repls['workflowInstancesInit'] = wfInit
|
2010-11-10 08:15:00 -06:00
|
|
|
# Compute the list of ordered attributes (forward and backward,
|
|
|
|
# inherited included) for every Appy class.
|
2010-08-05 11:23:17 -05:00
|
|
|
attributes = []
|
|
|
|
attributesDict = []
|
2011-01-14 02:06:25 -06:00
|
|
|
for classDescr in classesAll:
|
2010-08-05 11:23:17 -05:00
|
|
|
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))
|
2010-01-06 11:36:16 -06:00
|
|
|
repls['attributes'] = ',\n '.join(attributes)
|
2010-08-05 11:23:17 -05:00
|
|
|
repls['attributesDict'] = ',\n '.join(attributesDict)
|
2010-09-02 09:16:08 -05:00
|
|
|
# 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)
|
2011-01-14 02:06:25 -06:00
|
|
|
repls['sourceLanguage'] = self.config.sourceLanguage
|
2009-06-29 07:06:01 -05:00
|
|
|
self.copyFile('config.py', repls)
|
|
|
|
|
|
|
|
def generateInit(self):
|
|
|
|
# Compute imports
|
2010-08-05 11:23:17 -05:00
|
|
|
imports = []
|
|
|
|
classNames = []
|
|
|
|
for c in self.getClasses(include='all'):
|
2009-06-29 07:06:01 -05:00
|
|
|
importDef = ' import %s' % c.name
|
|
|
|
if importDef not in imports:
|
|
|
|
imports.append(importDef)
|
2010-08-05 11:23:17 -05:00
|
|
|
classNames.append("%s.%s" % (c.name, c.name))
|
2009-06-29 07:06:01 -05:00
|
|
|
repls = self.repls.copy()
|
|
|
|
repls['imports'] = '\n'.join(imports)
|
2010-08-05 11:23:17 -05:00
|
|
|
repls['classes'] = ','.join(classNames)
|
2009-12-03 09:45:05 -06:00
|
|
|
repls['totalNumberOfTests'] = self.totalNumberOfTests
|
2009-06-29 07:06:01 -05:00
|
|
|
self.copyFile('__init__.py', repls)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2010-10-14 07:43:56 -05:00
|
|
|
def generateWrapperProperty(self, name, type):
|
2010-08-05 11:23:17 -05:00
|
|
|
'''Generates the getter for attribute p_name.'''
|
|
|
|
res = ' def get_%s(self):\n ' % name
|
|
|
|
if name == 'title':
|
|
|
|
res += 'return self.o.Title()\n'
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
2010-10-14 07:43:56 -05:00
|
|
|
suffix = ''
|
|
|
|
if type == 'Ref': suffix = ', noListIfSingleObj=True'
|
|
|
|
res += 'return self.o.getAppyType("%s").getValue(self.o%s)\n' % \
|
|
|
|
(name, suffix)
|
2010-08-05 11:23:17 -05:00
|
|
|
res += ' %s = property(get_%s)\n\n' % (name, name)
|
2009-06-29 07:06:01 -05:00
|
|
|
return res
|
|
|
|
|
2010-08-05 11:23:17 -05:00
|
|
|
def getClasses(self, include=None):
|
|
|
|
'''Returns the descriptors for all the classes in the generated
|
2011-01-14 02:06:25 -06:00
|
|
|
gen-application. If p_include is:
|
|
|
|
* "all" it includes the descriptors for the config-related
|
|
|
|
classes (tool, user, translation)
|
|
|
|
* "allButTool" it includes the same descriptors, the tool excepted
|
|
|
|
* "custom" it includes descriptors for the config-related classes
|
|
|
|
for which the user has created a sub-class.'''
|
2010-08-05 11:23:17 -05:00
|
|
|
if not include: return self.classes
|
|
|
|
else:
|
|
|
|
res = self.classes[:]
|
2011-01-14 02:06:25 -06:00
|
|
|
configClasses = [self.tool, self.user, self.translation]
|
2010-08-05 11:23:17 -05:00
|
|
|
if include == 'all':
|
|
|
|
res += configClasses
|
2010-09-02 09:16:08 -05:00
|
|
|
elif include == 'allButTool':
|
|
|
|
res += configClasses[1:]
|
2010-08-05 11:23:17 -05:00
|
|
|
elif include == 'custom':
|
|
|
|
res += [c for c in configClasses if c.customized]
|
2011-01-14 02:06:25 -06:00
|
|
|
elif include == 'predefined':
|
|
|
|
res = configClasses
|
2010-08-05 11:23:17 -05:00
|
|
|
return res
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
def getClassesInOrder(self, allClasses):
|
|
|
|
'''When generating wrappers, classes mut be dumped in order (else, it
|
|
|
|
generates forward references in the Python file, that does not
|
|
|
|
compile).'''
|
|
|
|
res = [] # Appy class descriptors
|
|
|
|
resClasses = [] # Corresponding real Python classes
|
|
|
|
for classDescr in allClasses:
|
|
|
|
klass = classDescr.klass
|
|
|
|
if not klass.__bases__ or \
|
|
|
|
(klass.__bases__[0].__name__ == 'ModelClass'):
|
|
|
|
# This is a root class. We dump it at the begin of the file.
|
|
|
|
res.insert(0, classDescr)
|
|
|
|
resClasses.insert(0, klass)
|
|
|
|
else:
|
|
|
|
# If a child of this class is already present, we must insert
|
|
|
|
# this klass before it.
|
|
|
|
lowestChildIndex = sys.maxint
|
|
|
|
for resClass in resClasses:
|
|
|
|
if klass in resClass.__bases__:
|
|
|
|
lowestChildIndex = min(lowestChildIndex,
|
|
|
|
resClasses.index(resClass))
|
|
|
|
if lowestChildIndex != sys.maxint:
|
|
|
|
res.insert(lowestChildIndex, classDescr)
|
|
|
|
resClasses.insert(lowestChildIndex, klass)
|
|
|
|
else:
|
|
|
|
res.append(classDescr)
|
|
|
|
resClasses.append(klass)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def generateWrappers(self):
|
|
|
|
# We must generate imports and wrapper definitions
|
|
|
|
imports = []
|
|
|
|
wrappers = []
|
2010-08-05 11:23:17 -05:00
|
|
|
allClasses = self.getClasses(include='all')
|
2009-06-29 07:06:01 -05:00
|
|
|
for c in self.getClassesInOrder(allClasses):
|
2010-08-05 11:23:17 -05:00
|
|
|
if not c.predefined or c.customized:
|
2009-06-29 07:06:01 -05:00
|
|
|
moduleImport = 'import %s' % c.klass.__module__
|
|
|
|
if moduleImport not in imports:
|
|
|
|
imports.append(moduleImport)
|
|
|
|
# Determine parent wrapper and class
|
2010-08-05 11:23:17 -05:00
|
|
|
parentClasses = c.getParents(allClasses)
|
|
|
|
wrapperDef = 'class %s_Wrapper(%s):\n' % \
|
|
|
|
(c.name, ','.join(parentClasses))
|
2009-09-18 07:42:31 -05:00
|
|
|
wrapperDef += ' security = ClassSecurityInfo()\n'
|
2009-06-29 07:06:01 -05:00
|
|
|
titleFound = False
|
|
|
|
for attrName in c.orderedAttributes:
|
|
|
|
if attrName == 'title':
|
|
|
|
titleFound = True
|
2010-08-05 11:23:17 -05:00
|
|
|
try:
|
|
|
|
attrValue = getattr(c.klass, attrName)
|
|
|
|
except AttributeError:
|
|
|
|
attrValue = getattr(c.modelClass, attrName)
|
2009-06-29 07:06:01 -05:00
|
|
|
if isinstance(attrValue, Type):
|
2010-10-14 07:43:56 -05:00
|
|
|
wrapperDef += self.generateWrapperProperty(attrName,
|
|
|
|
attrValue.type)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Generate properties for back references
|
|
|
|
if self.referers.has_key(c.name):
|
|
|
|
for refDescr, rel in self.referers[c.name]:
|
|
|
|
attrName = refDescr.appyType.back.attribute
|
2010-10-14 07:43:56 -05:00
|
|
|
wrapperDef += self.generateWrapperProperty(attrName, 'Ref')
|
2009-06-29 07:06:01 -05:00
|
|
|
if not titleFound:
|
|
|
|
# Implicitly, the title will be added by Archetypes. So I need
|
|
|
|
# to define a property for it.
|
2010-10-14 07:43:56 -05:00
|
|
|
wrapperDef += self.generateWrapperProperty('title', 'String')
|
2010-08-05 11:23:17 -05:00
|
|
|
if c.customized:
|
2010-10-14 07:43:56 -05:00
|
|
|
# For custom tool, add a call to a method that allows to
|
|
|
|
# customize elements from the base class.
|
2009-09-18 07:42:31 -05:00
|
|
|
wrapperDef += " if hasattr(%s, 'update'):\n " \
|
2010-08-05 11:23:17 -05:00
|
|
|
"%s.update(%s)\n" % (parentClasses[1], parentClasses[1],
|
|
|
|
parentClasses[0])
|
2010-10-14 07:43:56 -05:00
|
|
|
# For custom tool, add security declaration that will allow to
|
|
|
|
# call their methods from ZPTs.
|
2010-08-05 11:23:17 -05:00
|
|
|
for parentClass in parentClasses:
|
|
|
|
wrapperDef += " for elem in dir(%s):\n " \
|
|
|
|
"if not elem.startswith('_'): security.declarePublic" \
|
|
|
|
"(elem)\n" % (parentClass)
|
2009-09-18 07:42:31 -05:00
|
|
|
# Register the class in Zope.
|
|
|
|
wrapperDef += 'InitializeClass(%s_Wrapper)\n' % c.name
|
2009-06-29 07:06:01 -05:00
|
|
|
wrappers.append(wrapperDef)
|
|
|
|
repls = self.repls.copy()
|
|
|
|
repls['imports'] = '\n'.join(imports)
|
|
|
|
repls['wrappers'] = '\n'.join(wrappers)
|
2011-01-14 02:06:25 -06:00
|
|
|
for klass in self.getClasses(include='predefined'):
|
|
|
|
modelClass = klass.modelClass
|
|
|
|
repls['%s' % modelClass.__name__] = modelClass._appy_getBody()
|
2009-06-29 07:06:01 -05:00
|
|
|
self.copyFile('appyWrappers.py', repls, destFolder='Extensions')
|
|
|
|
|
2009-11-11 13:22:13 -06:00
|
|
|
def generateTests(self):
|
|
|
|
'''Generates the file needed for executing tests.'''
|
|
|
|
repls = self.repls.copy()
|
|
|
|
modules = self.modulesWithTests
|
|
|
|
repls['imports'] = '\n'.join(['import %s' % m for m in modules])
|
|
|
|
repls['modulesWithTests'] = ','.join(modules)
|
|
|
|
self.copyFile('testAll.py', repls, destFolder='tests')
|
|
|
|
|
2010-08-12 04:56:42 -05:00
|
|
|
def generateFrontPage(self):
|
|
|
|
fp = self.config.frontPage
|
|
|
|
repls = self.repls.copy()
|
|
|
|
if fp == True:
|
|
|
|
# We need a front page, but no specific one has been given.
|
|
|
|
# So we will create a basic one that will simply display
|
|
|
|
# some translated text.
|
2010-09-02 09:16:08 -05:00
|
|
|
self.labels.append(PoMessage('front_page_text', '',
|
|
|
|
PoMessage.FRONT_PAGE_TEXT))
|
2010-08-12 04:56:42 -05:00
|
|
|
repls['pageContent'] = '<span tal:replace="structure python: ' \
|
|
|
|
'tool.translateWithMapping(\'front_page_text\')"/>'
|
|
|
|
else:
|
|
|
|
# The user has specified a macro to show. So in the generated front
|
|
|
|
# page, we will call this macro. The user will need to add itself
|
|
|
|
# a .pt file containing this macro in the skins folder of the
|
|
|
|
# generated Plone product.
|
|
|
|
page, macro = fp.split('/')
|
|
|
|
repls['pageContent'] = '<metal:call use-macro=' \
|
|
|
|
'"context/%s/macros/%s"/>' % (page, macro)
|
|
|
|
self.copyFile('frontPage.pt', repls, destFolder=self.skinsFolder,
|
|
|
|
destName='%sFrontPage.pt' % self.applicationName)
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def generateTool(self):
|
|
|
|
'''Generates the Plone tool that corresponds to this application.'''
|
|
|
|
Msg = PoMessage
|
2010-10-14 07:43:56 -05:00
|
|
|
# Create Tool-related i18n-related messages
|
2009-06-29 07:06:01 -05:00
|
|
|
self.labels += [
|
2011-01-14 02:06:25 -06:00
|
|
|
Msg(self.tool.name, '', Msg.CONFIG % self.applicationName),
|
|
|
|
Msg('%s_edit_descr' % self.tool.name, '', ' ')]
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
# Tune the Ref field between Tool and User
|
|
|
|
Tool.users.klass = User
|
|
|
|
if self.user.customized:
|
|
|
|
Tool.users.klass = self.user.klass
|
|
|
|
|
2011-01-14 02:06:25 -06:00
|
|
|
# Generate the Tool-related classes (User, Translation)
|
|
|
|
for klass in (self.user, self.translation):
|
|
|
|
klassType = klass.name[len(self.applicationName):]
|
|
|
|
klass.generateSchema()
|
|
|
|
self.labels += [ Msg(klass.name, '', klassType),
|
|
|
|
Msg('%s_edit_descr' % klass.name, '', ' '),
|
|
|
|
Msg('%s_plural' % klass.name,'', klass.name+'s')]
|
|
|
|
repls = self.repls.copy()
|
|
|
|
repls.update({'fields': klass.schema, 'methods': klass.methods,
|
|
|
|
'genClassName': klass.name, 'imports': '','baseMixin':'BaseMixin',
|
|
|
|
'baseSchema': 'BaseSchema', 'global_allow': 1,
|
|
|
|
'parents': 'BaseMixin, BaseContent', 'static': '',
|
|
|
|
'classDoc': 'User class for %s' % self.applicationName,
|
|
|
|
'implements': "(getattr(BaseContent,'__implements__',()),)",
|
|
|
|
'register': "registerType(%s, '%s')" % (klass.name,
|
|
|
|
self.applicationName)})
|
|
|
|
self.copyFile('Class.py', repls, destName='%s.py' % klass.name)
|
2010-12-06 04:11:40 -06:00
|
|
|
|
2010-10-14 07:43:56 -05:00
|
|
|
# Before generating the Tool class, finalize it with query result
|
2009-06-29 07:06:01 -05:00
|
|
|
# columns, with fields to propagate, workflow-related fields.
|
2010-12-06 04:11:40 -06:00
|
|
|
for classDescr in self.getClasses(include='allButTool'):
|
2010-10-14 07:43:56 -05:00
|
|
|
for fieldName, fieldType in classDescr.toolFieldsToPropagate:
|
2009-06-29 07:06:01 -05:00
|
|
|
for childDescr in classDescr.getChildren():
|
|
|
|
childFieldName = fieldName % childDescr.name
|
|
|
|
fieldType.group = childDescr.klass.__name__
|
2011-01-14 02:06:25 -06:00
|
|
|
self.tool.addField(childFieldName, fieldType)
|
2009-06-29 07:06:01 -05:00
|
|
|
if classDescr.isRoot():
|
2010-10-14 07:43:56 -05:00
|
|
|
# We must be able to configure query results from the tool.
|
2010-11-10 08:15:00 -06:00
|
|
|
self.tool.addQueryResultColumns(classDescr)
|
2010-01-06 11:36:16 -06:00
|
|
|
# Add the search-related fields.
|
2010-11-10 08:15:00 -06:00
|
|
|
self.tool.addSearchRelatedFields(classDescr)
|
2010-01-18 08:12:22 -06:00
|
|
|
importMean = classDescr.getCreateMean('Import')
|
|
|
|
if importMean:
|
2010-11-10 08:15:00 -06:00
|
|
|
self.tool.addImportRelatedFields(classDescr)
|
|
|
|
self.tool.addWorkflowFields(self.user)
|
2010-10-14 07:43:56 -05:00
|
|
|
self.tool.generateSchema()
|
|
|
|
|
|
|
|
# Generate the Tool class
|
2009-06-29 07:06:01 -05:00
|
|
|
repls = self.repls.copy()
|
2011-01-14 02:06:25 -06:00
|
|
|
repls.update({'fields': self.tool.schema, 'methods': self.tool.methods,
|
|
|
|
'genClassName': self.tool.name, 'imports':'', 'baseMixin':'ToolMixin',
|
|
|
|
'baseSchema': 'OrderedBaseFolderSchema', 'global_allow': 0,
|
|
|
|
'parents': 'ToolMixin, UniqueObject, OrderedBaseFolder',
|
|
|
|
'classDoc': 'Tool class for %s' % self.applicationName,
|
|
|
|
'implements': "(getattr(UniqueObject,'__implements__',()),) + " \
|
|
|
|
"(getattr(OrderedBaseFolder,'__implements__',()),)",
|
|
|
|
'register': "registerType(%s, '%s')" % (self.tool.name,
|
|
|
|
self.applicationName),
|
|
|
|
'static': "left_slots = ['here/portlet_prefs/macros/portlet']\n " \
|
|
|
|
"right_slots = []\n " \
|
|
|
|
"def __init__(self, id=None):\n " \
|
|
|
|
" OrderedBaseFolder.__init__(self, '%s')\n " \
|
|
|
|
" self.setTitle('%s')\n" % (self.toolInstanceName,
|
|
|
|
self.applicationName)})
|
|
|
|
self.copyFile('Class.py', repls, destName='%s.py' % self.tool.name)
|
2010-10-14 07:43:56 -05:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def generateClass(self, classDescr):
|
|
|
|
'''Is called each time an Appy class is found in the application, for
|
|
|
|
generating the corresponding Archetype class and schema.'''
|
|
|
|
k = classDescr.klass
|
|
|
|
print 'Generating %s.%s (gen-class)...' % (k.__module__, k.__name__)
|
|
|
|
if not classDescr.isAbstract():
|
2010-11-10 08:15:00 -06:00
|
|
|
self.tool.addWorkflowFields(classDescr)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Determine base archetypes schema and class
|
|
|
|
baseClass = 'BaseContent'
|
|
|
|
baseSchema = 'BaseSchema'
|
|
|
|
if classDescr.isFolder():
|
|
|
|
baseClass = 'OrderedBaseFolder'
|
|
|
|
baseSchema = 'OrderedBaseFolderSchema'
|
2010-11-30 10:41:18 -06:00
|
|
|
parents = ['BaseMixin', baseClass]
|
2009-06-29 07:06:01 -05:00
|
|
|
imports = []
|
|
|
|
implements = [baseClass]
|
|
|
|
for baseClass in classDescr.klass.__bases__:
|
|
|
|
if self.determineAppyType(baseClass) == 'class':
|
2010-09-02 09:16:08 -05:00
|
|
|
bcName = getClassName(baseClass)
|
2010-10-14 07:43:56 -05:00
|
|
|
parents.remove('BaseMixin')
|
2010-11-30 10:41:18 -06:00
|
|
|
parents.insert(0, bcName)
|
2009-06-29 07:06:01 -05:00
|
|
|
implements.append(bcName)
|
|
|
|
imports.append('from %s import %s' % (bcName, bcName))
|
|
|
|
baseSchema = '%s.schema' % bcName
|
|
|
|
break
|
|
|
|
parents = ','.join(parents)
|
|
|
|
implements = '+'.join(['(getattr(%s,"__implements__",()),)' % i \
|
|
|
|
for i in implements])
|
|
|
|
classDoc = classDescr.klass.__doc__
|
|
|
|
if not classDoc:
|
|
|
|
classDoc = 'Class generated with appy.gen.'
|
|
|
|
# If the class is abstract I will not register it
|
|
|
|
register = "registerType(%s, '%s')" % (classDescr.name,
|
|
|
|
self.applicationName)
|
|
|
|
if classDescr.isAbstract():
|
|
|
|
register = ''
|
|
|
|
repls = self.repls.copy()
|
2010-08-05 11:23:17 -05:00
|
|
|
classDescr.generateSchema()
|
2009-06-29 07:06:01 -05:00
|
|
|
repls.update({
|
|
|
|
'imports': '\n'.join(imports), 'parents': parents,
|
2011-01-14 02:06:25 -06:00
|
|
|
'className': classDescr.klass.__name__, 'global_allow': 1,
|
|
|
|
'genClassName': classDescr.name, 'baseMixin':'BaseMixin',
|
2009-06-29 07:06:01 -05:00
|
|
|
'classDoc': classDoc, 'applicationName': self.applicationName,
|
|
|
|
'fields': classDescr.schema, 'methods': classDescr.methods,
|
2011-01-14 02:06:25 -06:00
|
|
|
'implements': implements, 'baseSchema': baseSchema, 'static': '',
|
2009-06-29 07:06:01 -05:00
|
|
|
'register': register, 'toolInstanceName': self.toolInstanceName})
|
|
|
|
fileName = '%s.py' % classDescr.name
|
2009-11-03 08:02:18 -06:00
|
|
|
# Create i18n labels (class name, description and plural form)
|
2009-06-29 07:06:01 -05:00
|
|
|
poMsg = PoMessage(classDescr.name, '', classDescr.klass.__name__)
|
|
|
|
poMsg.produceNiceDefault()
|
|
|
|
self.labels.append(poMsg)
|
|
|
|
poMsgDescr = PoMessage('%s_edit_descr' % classDescr.name, '', ' ')
|
|
|
|
self.labels.append(poMsgDescr)
|
2009-11-03 08:02:18 -06:00
|
|
|
poMsgPl = PoMessage('%s_plural' % classDescr.name, '',
|
|
|
|
classDescr.klass.__name__+'s')
|
|
|
|
poMsgPl.produceNiceDefault()
|
|
|
|
self.labels.append(poMsgPl)
|
|
|
|
# Create i18n labels for searches
|
2009-10-30 15:31:39 -05:00
|
|
|
for search in classDescr.getSearches(classDescr.klass):
|
2009-11-06 04:33:56 -06:00
|
|
|
searchLabel = '%s_search_%s' % (classDescr.name, search.name)
|
|
|
|
labels = [searchLabel, '%s_descr' % searchLabel]
|
|
|
|
if search.group:
|
|
|
|
grpLabel = '%s_searchgroup_%s' % (classDescr.name, search.group)
|
|
|
|
labels += [grpLabel, '%s_descr' % grpLabel]
|
|
|
|
for label in labels:
|
2009-10-30 15:31:39 -05:00
|
|
|
default = ' '
|
2009-11-06 04:33:56 -06:00
|
|
|
if label == searchLabel: default = search.name
|
2009-10-30 15:31:39 -05:00
|
|
|
poMsg = PoMessage(label, '', default)
|
|
|
|
poMsg.produceNiceDefault()
|
2009-11-06 04:33:56 -06:00
|
|
|
if poMsg not in self.labels:
|
|
|
|
self.labels.append(poMsg)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Generate the resulting Archetypes class and schema.
|
2011-01-14 02:06:25 -06:00
|
|
|
self.copyFile('Class.py', repls, destName=fileName)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
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():
|
2011-01-28 19:18:14 -06:00
|
|
|
# Get the Appy transition name
|
|
|
|
tName = wfDescr.getNameOf(transition)
|
|
|
|
# Get the names of the corresponding DC transition(s)
|
|
|
|
tNames = wfDescr.getTransitionNamesOf(tName, transition)
|
|
|
|
if transition.confirm:
|
|
|
|
# We need to generate a label for the message that will be shown
|
|
|
|
# in the confirm popup.
|
|
|
|
for tn in tNames:
|
|
|
|
label = '%s_%s_confirm' % (wfName, tn)
|
|
|
|
poMsg = PoMessage(label, '', PoMessage.CONFIRM)
|
|
|
|
self.labels.append(poMsg)
|
2009-06-29 07:06:01 -05:00
|
|
|
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.
|
|
|
|
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)
|
|
|
|
# ------------------------------------------------------------------------------
|