[4977] | 1 | :mod:`waeup.sirp.university.facultycontainer` -- Faculty Containers |
---|
| 2 | ******************************************************************* |
---|
[3923] | 3 | |
---|
[4977] | 4 | .. module:: waeup.sirp.university.facultycontainer |
---|
[3923] | 5 | |
---|
[4977] | 6 | Components that represent faculty containers. |
---|
| 7 | |
---|
[3923] | 8 | :Test-Layer: unit |
---|
| 9 | |
---|
[4977] | 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: |
---|
[3923] | 13 | |
---|
[4977] | 14 | >>> import grok |
---|
| 15 | >>> grok.testing.grok('waeup.sirp') |
---|
[3923] | 16 | |
---|
[4977] | 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 | |
---|
[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 | |
---|
| 84 | Utilities |
---|
| 85 | ========= |
---|
| 86 | |
---|
[5015] | 87 | :class:`AcademicsPlugin` |
---|
| 88 | ------------------------ |
---|
| 89 | |
---|
| 90 | .. class:: AcademicsPlugin() |
---|
| 91 | |
---|
| 92 | .. attribute:: grok.implements(IWAeUPSIRPPluggable) |
---|
| 93 | |
---|
| 94 | This plugin component tells under which name (``faculties``) an |
---|
| 95 | instance of academics stuff should be created in plain WAeUP SIRP |
---|
| 96 | instances. It also tells the factory name for FacultyContainer |
---|
| 97 | instances, which serve as root object for academics stuff in WAeUP |
---|
| 98 | SIRP apps. |
---|
| 99 | |
---|
| 100 | |
---|
[4977] | 101 | :class:`FacultyContainerFactory` |
---|
| 102 | -------------------------------- |
---|
| 103 | |
---|
| 104 | .. class:: FacultyContainerFactory() |
---|
| 105 | |
---|
| 106 | .. attribute:: grok.name(u'waeup.FacultyContainer') |
---|
| 107 | |
---|
| 108 | .. attribute:: grok.implements(IFactoryContainer) |
---|
| 109 | |
---|
| 110 | A named utility to deliver new instances of :class:`FacultyContainer` |
---|
| 111 | without the need to import the implementation before: |
---|
| 112 | |
---|
| 113 | >>> from zope.component import createObject |
---|
| 114 | >>> mycontainer = createObject(u'waeup.FacultyContainer') |
---|
| 115 | >>> mycontainer |
---|
| 116 | <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...> |
---|
| 117 | |
---|
| 118 | The factory complies with the specifications from the |
---|
| 119 | :class:`IFactory` insterface: |
---|
| 120 | |
---|
| 121 | >>> from zope.interface.verify import verifyClass |
---|
| 122 | >>> from zope.component.interfaces import IFactory |
---|
| 123 | >>> from waeup.sirp.university.facultycontainer import ( |
---|
| 124 | ... FacultyContainerFactory) |
---|
| 125 | >>> verifyClass(IFactory, FacultyContainerFactory) |
---|
| 126 | True |
---|
| 127 | |
---|
| 128 | This means also, that we can get the interfaces of the created |
---|
| 129 | object from the factory: |
---|
| 130 | |
---|
| 131 | >>> fac_container_factory = FacultyContainerFactory() |
---|
| 132 | >>> fac_container_factory.getInterfaces() |
---|
| 133 | <implementedBy waeup.sirp.university.facultycontainer.FacultyContainer> |
---|
| 134 | |
---|
| 135 | Examples |
---|
| 136 | ======== |
---|
| 137 | |
---|
| 138 | We can easily create `FacultyContainers`: |
---|
| 139 | |
---|
[4920] | 140 | >>> from waeup.sirp.university.facultycontainer import FacultyContainer |
---|
[3923] | 141 | >>> mycontainer = FacultyContainer() |
---|
| 142 | |
---|
[4080] | 143 | Faculty containers provide `IFacultyContainer`: |
---|
[3923] | 144 | |
---|
[5005] | 145 | >>> from waeup.sirp.university.interfaces import IFacultyContainer |
---|
[3923] | 146 | >>> IFacultyContainer.providedBy(mycontainer) |
---|
| 147 | True |
---|
| 148 | |
---|
[4080] | 149 | Another way to get a faculty container -- without importing the class |
---|
| 150 | -- is via factories. We registered a factory for faculty containers |
---|
[4977] | 151 | under the name ``waeup.facultycontainer``: |
---|
[3923] | 152 | |
---|
[4080] | 153 | >>> from zope.component import createObject |
---|
| 154 | >>> createObject(u'waeup.FacultyContainer') |
---|
[4920] | 155 | <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...> |
---|
[3923] | 156 | |
---|
| 157 | This way we get a thing that implements IFacultyContainer without |
---|
| 158 | imports or similar. |
---|
| 159 | |
---|
| 160 | We can be sure, that the full interface is supported by the |
---|
| 161 | FacultyContainer class:: |
---|
| 162 | |
---|
| 163 | >>> from zope.interface.verify import verifyClass |
---|
| 164 | >>> verifyClass(IFacultyContainer, FacultyContainer) |
---|
| 165 | True |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | Storing things in faculty containers |
---|
| 169 | ==================================== |
---|
| 170 | |
---|
| 171 | We can, of course, store things in a faculty container. But when we |
---|
| 172 | really store an object, then it must be a faculty:: |
---|
| 173 | |
---|
| 174 | >>> mycontainer.addFaculty(42) |
---|
| 175 | Traceback (most recent call last): |
---|
| 176 | ... |
---|
| 177 | TypeError: FacultyContainers contain only IFaculty instances |
---|
| 178 | |
---|
| 179 | Okay, so we have to get a faculty first:: |
---|
| 180 | |
---|
[4920] | 181 | >>> from waeup.sirp.university.faculty import Faculty |
---|
[3923] | 182 | >>> myfaculty = Faculty() |
---|
| 183 | |
---|
| 184 | We can add this faculty to our container:: |
---|
| 185 | |
---|
| 186 | >>> mycontainer.addFaculty(myfaculty) |
---|
| 187 | |
---|
[3924] | 188 | We get back the key, under which the faculty was stored. It will be |
---|
| 189 | some string, but there is no guarantee at all, how this key looks |
---|
| 190 | like. It might be a string of integers, a name or whatever; you cannot |
---|
| 191 | know before. |
---|
[4162] | 192 | |
---|