What do we have to change to add the functionality to the Display
class to pop up a list of items when the user clicks on a related tag?
Display
class are served by its index()
method, we will have to apply some changes there. index()
method both displays forms and processes the results when the submit button is pressed, so we have to look at both the aspects of the edit and add functionality. Browse
instance and will therefore be passed a related argument. This argument must be passed along with the contents of the form when the submit button is clicked in order to associate it with the item that initiated this edit action.These issues require that we apply a few changes to the index()
method.
The first thing we have to do is add a related
parameter to the index()
method. This parameter may hold the name of the entity and the ID of the specific related instance separated by a comma:
Chapter9/display.py
@cherrypy.expose def index(self,id=None,_=None,add=None,edit=None,related=None,** kw):
When processing the information passed to the index()
method of the Display
class, the portion dealing with the results of an add action has to act on the information in the related
parameter:
Chapter9/display.py
if not related is None and related != '':
r=related.split(',')
re=e.relclass[r[0]](id=int(r[1]))
e.add(re)
If the method was called as the result of clicking the add button in a list of items, the related
parameter will be non empty and we split it on the comma to retrieve the name of the entity and its ID.
The name of the entity is used to retrieve its class that was added to the relclass
dictionary when the relation was defined and this class' constructor is called with the ID of the instance to create an object (highlighted). The relation between the item we are currently editing and the related item is subsequently established by the add()
method.
Likewise, the portion responsible for delivering the add or edit form in the first place must include a hidden<input>
element that holds the contents of the related parameter passed to it when the user clicked the add button in a page delivered by a Browse
instance:
Chapter9/display.py
submitbutton='<input type="hidden" name="related" value="%s"><input type="submit" name="add" value="Add">'%related
18.224.56.29