Customizing generated code
Although ArchGenXML builds all the necessary boilerplate to create Plone Archetypes-based content types, there are some pieces of code that we can't specify anywhere in a UML model. Fortunately, we can modify the automatically generated code in different places so that ArchGenXML won't overwrite it in future builds.
Typical examples of code customization are setting the order of the widgets in the edition form and defining the body of complicated methods.
Getting ready
Every time we customize ArchGenXML-generated code, we must do it inside the special comment blocks placed everywhere in the source files like the following:
##code-section class-header #fill in your manual code here ##/code-section class-header ##code-section module-footer #fill in your manual code here ##/code-section module-footer
How to do it…
We will first define the order for the widgets when displayed in standard edit form and view page.
- Go to the
models/poxContentTypes/content
folder inside your ArchGenXML installation directory and openXNewsItem.py
file in your favorite code editor. - Find the following comment block placed just after the definition of
XNewsItem
schema:##code-section after-schema #fill in your manual code here ##/code-section after-schema
- Add the following lines to alter the order of the widgets in the resulting schema:
##code-section after-schema #fill in your manual code here XNewsItem_schema.moveField('lead', after='title') XNewsItem_schema.moveField('country', before='text') XNewsItem_schema.moveField('relatedItems', pos='bottom') ##/code-section after-schema
Look at the highlighted lines:
- We get
lead
field and put it next to thetitle
. - Then we get
country
field and place it before the bodytext
field. - Finally, we get
relatedItems
field and send it to the bottom of the schema.
We have only one method in our XNewsItem
class: countryVocabulary
. For teaching purposes, we have defined its body as a line of code returning just three countries (check Creating a model). However, we are now going to add more options. We could actually get and return a list of countries from somewhere, like a special tool in Plone or even a Web service. Instead, we are just going to increase the list with a static one. This is the way ArchGenXML lets us define the body of complicated methods.
We must first remove the body of the method in the UML model:
- Select the
countryVocabulary
method in the operations panel (the third one in theXNewsItem
class). - Open the Tagged Values tab in the bottom-right panel.
- Remove the
code
tagged value and its associated value. Place the cursor in the corresponding row and press the trash icon. Be sure to remove not the contents but the whole row to prevent ArchGenXML parsing errors. - Open
XNewsItem.py
file again (insidemodels/poxContentTypes/content
folder) and locate thecountryVocabulary
definition line. - You'll see the old three-country list. Remove it and modify the code as follows:
security.declarePublic('countryVocabulary') def countryVocabulary(self): """ Returns a list of country codes and their names for the "country" field """ return (\ ('AQ', 'Antartiqa'),\ ('AR', 'Argentina'),\ ('BS', 'Bahamas'),\ ('BR', 'Brazil'),\ ('CM', 'Cameroon'),\ ('CL', 'Chile')\ )
How it works…
ArchGenXML is so great that if there's no way to turn UML into code, it lets us insert our own code in places that won't be modified in subsequent builds.
In the procedure above, we covered two typical examples of code customization.
In Steps 1 to 3, we moved the fields we used in Creating a model to be placed exactly where we wanted.
Although we can set widgets order via tagged values, we wanted to show how to do this for other Archetypes content types that might be created without ArchGenXML.
Note
To learn how to use move:after
, move:before
, move:bottom
, move:top
, and move:pos
tagged values, check the documentation at http://plone.org/documentation/manual/archgenxml2/reference/tagged-values.
As you may have guessed, moveField
is a schema method aimed to place the fields in the order we want. If we don't use this method, fields' widgets will be placed in the order of declaration in the source file. In our case, due to the way that ArchGenXML generates the final schema, the News Items
inherited fields are placed first followed by the fields we added manually in the UML model.
In Steps 4 to 8, we modified the body of a method with a more complicated functionality. If ArchGenXML finds a piece of code for a method, it won't try to write its body even if there's a code
tagged value defined in the UML model. However, it's a good practice to keep things clean and clear.
Because we have placed these two changes in special parts of the source files (Step 3 above), ArchGenXML won't touch them in next runs.
See also
- Creating a model