[ Pobierz całość w formacie PDF ]
.Access control is controlling who can create new instances of your ZClass.As you saw earlier inthe Chapter, instances are created by Factories.Factories are associated with permissions.In the case of theZoo Exhibit, the Add Zoo Exhibits permission controls the ability to create Zoo Exhibit instances.Normally only Managers will have the Add Zoo Exhibits permission, so only Managers will be able to createnew Zoo Exhibits.However, like all Zope permissions, you can change which roles have this permissions indifferent locations of your site.It's important to realize that this permission is controlled separately from theEdit Zoo Exhibits permission.This makes it possible to allow some people such as Caretakers to change, butnot create Zoo Exhibits.Providing Context-Sensitive Help for your ZClassOn the View screen of your ZClass, you can see that each view can be associated with a Help Topic.Thisallows you to provide a link to a different help topics depending on which view the user is looking at.Forexample, let's create a Help Topic for the Edit view of the ZooExhibit ZClass.First, you need to create an actual help topic object.This is done by going to the ZooExhibit Product whichcontains the ZooExhibit ZClass, and clicking on the Help folder.The icon should look like a folder with ablue question mark on it.Inside this special folder, pull down the add list and select Help Topic.Give this topic the id"ExhibitEditHelp" and the title "Help for Editing Exhibits" and click Add.Now you will see the Help folder contains a new help topic object called ExhibitEditHelp.You can click onthis object and edit it, it works just like a DTML Document.In this document, you should place the helpinformation you want to show to your users:Help!To edit an exhibit, click on either the animal,description, or caretakers boxes to editthem.Now that you have created the help topic, you need to associate with the Edit view of your ZClass.To dothis, select the ZooExhibit ZClass and click on the Views tab.At the right, in the same row as the Edit view isdefined, pull down the help select box and select ExhibitEditHelp and click Change.Now go to one of yourZooExhibit instances, the Edit view now has a *Help!* button that you can click to look at your Help Topicfor this view.In the next section, you'll see how ZClasses can be cobined with standard Python classes to extend theirfunctionality into raw Python.Controlling Access to instances of Your ZClass 223IntroductionUsing Python Base ClassesZClasses give you a web managable interface to design new kinds of objects in Zope.In the beginning of thischapter, we showed you how you can select from a list of base classes to subclass your ZClass from.Most ofthese base classes are actually written in Python, and in this section you'll see how you can take your ownPython classes and include them in that list so that your ZClasses can extend their methods.Writing Python base classes is easy, but it involves a few installation details.To create a Python base classyou need access to the filesystem.Create a directory inside your lib/python/Products directory namedAnimalBase.In this directory create a file named Animal.py with these contents:class Animal:"""A base class for Animals"""_hungry=0def eat(self, food, servings=1):"""Eat food"""self._hungry=0def sleep(self):"""Sleep"""self._hungry=1def hungry(self):"""Is the Animal hungry?"""return self._hungryThis class defines a couple related methods and one default attribute.Notice that like External Methods, themethods of this class can access private attributes.Next you need to register your base class with Zope.Create an __init__.py file in the AnimalBase directorywith these contents:from Animal import Animaldef initialize(context):"""Register base class"""context.registerBaseClass(Animal)Now you need to restart Zope in order for it find out about your base class.After Zope restarts you can verifythat your base class has been registered in a couple different ways.First go to the Products Folder in theControl Panel and look for an AnimalBase package.You should see a closed box product.If you see brokenbox, it means that there is something wrong with your AnimalBase product.Using Python Base Classes 224IntroductionClick on the Traceback view to see a Python traceback showing you what problem Zope ran into trying toregister your base class.Once you resolve any problems that your base class might have you'll need to restartZope again.Continue this process until Zope successfully loads your product.Now you can create a newZClass and you should see AnimalBase:Animal as a choice in the base classes selection field.To test your new base class create a ZClass that inherits from AnimalBase:Animal.Embellish you animalhowever you wish.Create a DTML Method named care with these contents:I am hungryI am not hungryNow create an instance of your animal class and test out its care method.The care method lets you feed youranimal and give it sleep by calling methods defined in its Python base class.Also notice how after feedingyour animal is not hungry, but if you give it a nap it wakes up hungry.As you can see, creating your own Products and ZClasses is an involved process, but simple to understandonce you grasp the basics
[ Pobierz całość w formacie PDF ]