source: main/waeup.sirp/trunk/src/waeup/sirp/university/facultycontainer.txt @ 5006

Last change on this file since 5006 was 5005, checked in by uli, 15 years ago

Fix references to academics stuff interfaces. This is the first step to make academics stuff pluggable.

File size: 5.4 KB
RevLine 
[4977]1:mod:`waeup.sirp.university.facultycontainer` -- Faculty Containers
2*******************************************************************
[3923]3
[4977]4.. module:: waeup.sirp.university.facultycontainer
[3923]5
[4977]6Components that represent faculty containers.
7
[3923]8:Test-Layer: unit
9
[4977]10Before we can create faculty containers, we have to grok the
11:mod:`waeup.sirp` package. This happens automatically in real-world
12use:
[3923]13
[4977]14    >>> import grok
15    >>> grok.testing.grok('waeup.sirp')
[3923]16
[4977]17
18Content Classes (models and containers)
19=======================================
20
21:class:`FacultyContainer`
22-------------------------
23
24.. class:: FacultyContainer()
25
26   Create a faculty container:
27
28     >>> from waeup.sirp.university.facultycontainer import FacultyContainer
29     >>> mycontainer = FacultyContainer()
30     >>> mycontainer
31     <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...>
32
33   Another way to create :class:`FacultyContainer` instances is by asking
34   for a factory called ``waeup.FacultyContainer``. This way we can create a
35   faculty without importing a class:
36
37     >>> from zope.component import createObject
38     >>> mycontainer = createObject(u'waeup.FacultyContainer')
39     >>> mycontainer
40     <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...>
41
42   :class:`FacultyContainer` instances have the attributes required by the
43   `IFacultyContainer` interface:
44
[5005]45     >>> from waeup.sirp.university.interfaces import IFacultyContainer
[4977]46     >>> IFacultyContainer.providedBy(mycontainer)
47     True
48
49     >>> from zope.interface.verify import verifyObject
50     >>> verifyObject(IFacultyContainer, mycontainer)
51     True
52
53   .. method:: addFaculty(faculty)
54
55      Add a faculty into this container. Added objects are checked and
56      only `IFaculty` objects accepted:
57
58        >>> mycontainer.addFaculty(object())
59        Traceback (most recent call last):
60        ...
61        TypeError: FacultyContainers contain only IFaculty instances
62
63        >>> list(mycontainer.values())
64        []
65
66      Regular faculties are accepted:
67
68        >>> from waeup.sirp.university.faculty import Faculty
69        >>> mycontainer.addFaculty(Faculty(title='Physics',
70        ...                                code='FP'))
71
72        >>> list(mycontainer.items())
73        [(u'FP', <waeup.sirp.university.faculty.Faculty object at 0x...>)]
74
75   .. method:: clear()
76
77      Remove all departments from this faculty:
78
79        >>> mycontainer.clear()
80        >>> list(mycontainer.items())
81        []
82
83
84Utilities
85=========
86
87:class:`FacultyContainerFactory`
88--------------------------------
89
90.. class:: FacultyContainerFactory()
91
92   .. attribute:: grok.name(u'waeup.FacultyContainer')
93
94   .. attribute:: grok.implements(IFactoryContainer)
95
96   A named utility to deliver new instances of :class:`FacultyContainer`
97   without the need to import the implementation before:
98
99     >>> from zope.component import createObject
100     >>> mycontainer = createObject(u'waeup.FacultyContainer')
101     >>> mycontainer
102     <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...>
103
104   The factory complies with the specifications from the
105   :class:`IFactory` insterface:
106
107     >>> from zope.interface.verify import verifyClass
108     >>> from zope.component.interfaces import IFactory
109     >>> from waeup.sirp.university.facultycontainer import (
110     ...   FacultyContainerFactory)
111     >>> verifyClass(IFactory, FacultyContainerFactory)
112     True
113
114   This means also, that we can get the interfaces of the created
115   object from the factory:
116
117     >>> fac_container_factory = FacultyContainerFactory()
118     >>> fac_container_factory.getInterfaces()
119     <implementedBy waeup.sirp.university.facultycontainer.FacultyContainer>
120
121Examples
122========
123
124We can easily create `FacultyContainers`:
125
[4920]126    >>> from waeup.sirp.university.facultycontainer import FacultyContainer
[3923]127    >>> mycontainer = FacultyContainer()
128
[4080]129Faculty containers provide `IFacultyContainer`:
[3923]130
[5005]131    >>> from waeup.sirp.university.interfaces import IFacultyContainer
[3923]132    >>> IFacultyContainer.providedBy(mycontainer)
133    True
134
[4080]135Another way to get a faculty container -- without importing the class
136-- is via factories. We registered a factory for faculty containers
[4977]137under the name ``waeup.facultycontainer``:
[3923]138
[4080]139    >>> from zope.component import createObject
140    >>> createObject(u'waeup.FacultyContainer')
[4920]141    <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...>
[3923]142
143This way we get a thing that implements IFacultyContainer without
144imports or similar.
145
146We can be sure, that the full interface is supported by the
147FacultyContainer class::
148
149    >>> from zope.interface.verify import verifyClass
150    >>> verifyClass(IFacultyContainer, FacultyContainer)
151    True
152
153
154Storing things in faculty containers
155====================================
156
157We can, of course, store things in a faculty container. But when we
158really store an object, then it must be a faculty::
159
160    >>> mycontainer.addFaculty(42)
161    Traceback (most recent call last):
162    ...
163    TypeError: FacultyContainers contain only IFaculty instances
164
165Okay, so we have to get a faculty first::
166
[4920]167    >>> from waeup.sirp.university.faculty import Faculty
[3923]168    >>> myfaculty = Faculty()
169
170We can add this faculty to our container::
171
172    >>> mycontainer.addFaculty(myfaculty)
173
[3924]174We get back the key, under which the faculty was stored. It will be
175some string, but there is no guarantee at all, how this key looks
176like. It might be a string of integers, a name or whatever; you cannot
177know before.
[4162]178
Note: See TracBrowser for help on using the repository browser.