1 | :mod:`waeup.sirp.university.facultycontainer` -- Faculty Containers |
---|
2 | ******************************************************************* |
---|
3 | |
---|
4 | .. module:: waeup.sirp.university.facultycontainer |
---|
5 | |
---|
6 | Components that represent faculty containers. |
---|
7 | |
---|
8 | :Test-Layer: unit |
---|
9 | |
---|
10 | Before we can create faculty containers, we have to grok the |
---|
11 | :mod:`waeup.sirp` package. This happens automatically in real-world |
---|
12 | use: |
---|
13 | |
---|
14 | >>> import grok |
---|
15 | >>> grok.testing.grok('waeup.sirp') |
---|
16 | |
---|
17 | |
---|
18 | Content 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 | |
---|
45 | >>> from waeup.sirp.interfaces import IFacultyContainer |
---|
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 | |
---|
84 | Utilities |
---|
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 | |
---|
121 | Examples |
---|
122 | ======== |
---|
123 | |
---|
124 | We can easily create `FacultyContainers`: |
---|
125 | |
---|
126 | >>> from waeup.sirp.university.facultycontainer import FacultyContainer |
---|
127 | >>> mycontainer = FacultyContainer() |
---|
128 | |
---|
129 | Faculty containers provide `IFacultyContainer`: |
---|
130 | |
---|
131 | >>> from waeup.sirp.interfaces import IFacultyContainer |
---|
132 | >>> IFacultyContainer.providedBy(mycontainer) |
---|
133 | True |
---|
134 | |
---|
135 | Another way to get a faculty container -- without importing the class |
---|
136 | -- is via factories. We registered a factory for faculty containers |
---|
137 | under the name ``waeup.facultycontainer``: |
---|
138 | |
---|
139 | >>> from zope.component import createObject |
---|
140 | >>> createObject(u'waeup.FacultyContainer') |
---|
141 | <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...> |
---|
142 | |
---|
143 | This way we get a thing that implements IFacultyContainer without |
---|
144 | imports or similar. |
---|
145 | |
---|
146 | We can be sure, that the full interface is supported by the |
---|
147 | FacultyContainer class:: |
---|
148 | |
---|
149 | >>> from zope.interface.verify import verifyClass |
---|
150 | >>> verifyClass(IFacultyContainer, FacultyContainer) |
---|
151 | True |
---|
152 | |
---|
153 | |
---|
154 | Storing things in faculty containers |
---|
155 | ==================================== |
---|
156 | |
---|
157 | We can, of course, store things in a faculty container. But when we |
---|
158 | really 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 | |
---|
165 | Okay, so we have to get a faculty first:: |
---|
166 | |
---|
167 | >>> from waeup.sirp.university.faculty import Faculty |
---|
168 | >>> myfaculty = Faculty() |
---|
169 | |
---|
170 | We can add this faculty to our container:: |
---|
171 | |
---|
172 | >>> mycontainer.addFaculty(myfaculty) |
---|
173 | |
---|
174 | We get back the key, under which the faculty was stored. It will be |
---|
175 | some string, but there is no guarantee at all, how this key looks |
---|
176 | like. It might be a string of integers, a name or whatever; you cannot |
---|
177 | know before. |
---|
178 | |
---|