[4920] | 1 | :mod:`waeup.sirp.university.coursecontainer` -- Course containers |
---|
| 2 | ***************************************************************** |
---|
[4261] | 3 | |
---|
[4920] | 4 | .. module:: waeup.sirp.university.coursecontainer |
---|
[4364] | 5 | |
---|
[4261] | 6 | Containers for courses. |
---|
| 7 | |
---|
| 8 | :Test-Layer: unit |
---|
| 9 | |
---|
[4364] | 10 | Because this module makes use of components registered with the Zope |
---|
| 11 | Component Architecture (ZCA), we first have to grok the `waeup` |
---|
| 12 | package if we want to deploy it. This happens automatically in |
---|
| 13 | real-world use: |
---|
| 14 | |
---|
| 15 | >>> import grok |
---|
| 16 | >>> grok.testing.grok('waeup') |
---|
| 17 | |
---|
| 18 | Content Classes (models and containers) |
---|
| 19 | ======================================= |
---|
| 20 | |
---|
| 21 | :class:`CourseContainer` |
---|
| 22 | ------------------------ |
---|
| 23 | |
---|
| 24 | .. class:: CourseContainer() |
---|
| 25 | |
---|
| 26 | Create a course container instance. |
---|
| 27 | |
---|
| 28 | .. attribute:: grok.implements(ICourseContainer) |
---|
| 29 | |
---|
| 30 | .. attribute:: grok.require('waeup.manageUniversity') |
---|
| 31 | |
---|
| 32 | :class:`CourseContainer` instances are instances of |
---|
| 33 | :class:`grok.Container` that hold and manage instances of |
---|
[4920] | 34 | :class:`waeup.sirp.university.course.Course`. |
---|
[4364] | 35 | |
---|
[4920] | 36 | >>> from waeup.sirp.university.coursecontainer import CourseContainer |
---|
[4364] | 37 | >>> mycontainer = CourseContainer() |
---|
| 38 | >>> mycontainer |
---|
[4920] | 39 | <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...> |
---|
[4364] | 40 | |
---|
| 41 | Course containers provide `ICourseContainer`: |
---|
| 42 | |
---|
[5005] | 43 | >>> from waeup.sirp.university.interfaces import ICourseContainer |
---|
[4364] | 44 | >>> ICourseContainer.providedBy(mycontainer) |
---|
| 45 | True |
---|
| 46 | |
---|
| 47 | We can be sure, that the full interface is supported by the |
---|
| 48 | CourseContainer class: |
---|
| 49 | |
---|
| 50 | >>> from zope.interface.verify import verifyClass |
---|
| 51 | >>> verifyClass(ICourseContainer, CourseContainer) |
---|
| 52 | True |
---|
| 53 | |
---|
| 54 | As normal ``grok.Container``, :class:`CourseContainer` instances |
---|
| 55 | can be used like dicts: |
---|
| 56 | |
---|
| 57 | >>> list(mycontainer.keys()) |
---|
| 58 | [] |
---|
| 59 | |
---|
| 60 | .. method:: addCourse(course) |
---|
| 61 | |
---|
| 62 | Add a course. |
---|
| 63 | |
---|
| 64 | *course* must be an instance of |
---|
[4920] | 65 | :class:`waeup.sirp.university.course.Course`: |
---|
[4364] | 66 | |
---|
[4920] | 67 | >>> from waeup.sirp.university.coursecontainer import CourseContainer |
---|
[4364] | 68 | >>> mycontainer = CourseContainer() |
---|
| 69 | >>> mycontainer.addCourse('blah') |
---|
| 70 | Traceback (most recent call last): |
---|
| 71 | ... |
---|
| 72 | TypeError: CourseContainers contain only ICourse instances |
---|
| 73 | |
---|
| 74 | The key of added items is the ``code`` attribute: |
---|
| 75 | |
---|
[4920] | 76 | >>> from waeup.sirp.university.course import Course |
---|
[4364] | 77 | >>> mycourse = Course(code='COURSE1') |
---|
| 78 | >>> mycontainer.addCourse(mycourse) |
---|
| 79 | >>> list(mycontainer.keys()) |
---|
| 80 | [u'COURSE1'] |
---|
| 81 | |
---|
| 82 | .. method:: clear() |
---|
| 83 | |
---|
| 84 | Remove all contained courses: |
---|
| 85 | |
---|
| 86 | >>> len(mycontainer.items()) |
---|
| 87 | 1 |
---|
| 88 | |
---|
| 89 | >>> mycontainer.clear() |
---|
| 90 | >>> len(mycontainer.items()) |
---|
| 91 | 0 |
---|
| 92 | |
---|
| 93 | Utilities |
---|
| 94 | ========= |
---|
| 95 | |
---|
| 96 | :class:`CourseContainerFactory` |
---|
| 97 | ------------------------------- |
---|
| 98 | |
---|
| 99 | .. class:: CourseContainerFactory() |
---|
| 100 | |
---|
| 101 | .. attribute:: grok.name(u'waeup.CourseContainer') |
---|
| 102 | |
---|
| 103 | .. attribute:: grok.implements(IFactory) |
---|
| 104 | |
---|
| 105 | A named utility to deliver new instances of :class:`CourseContainer` |
---|
| 106 | without the need to import the implementation before: |
---|
| 107 | |
---|
| 108 | >>> from zope.component import createObject |
---|
| 109 | >>> mycoursecontainer = createObject(u'waeup.CourseContainer') |
---|
| 110 | >>> mycoursecontainer |
---|
[4920] | 111 | <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...> |
---|
[4364] | 112 | |
---|
| 113 | The factory complies with the specifications from the |
---|
| 114 | :class:`IFactory` insterface: |
---|
| 115 | |
---|
| 116 | >>> from zope.interface.verify import verifyClass |
---|
| 117 | >>> from zope.component.interfaces import IFactory |
---|
[4920] | 118 | >>> from waeup.sirp.university.coursecontainer import CourseContainerFactory |
---|
[4364] | 119 | >>> verifyClass(IFactory, CourseContainerFactory) |
---|
| 120 | True |
---|
| 121 | |
---|
| 122 | This means also, that we can get the interfaces of the created |
---|
| 123 | object from the factory: |
---|
| 124 | |
---|
| 125 | >>> coursecontainer_factory = CourseContainerFactory() |
---|
| 126 | >>> coursecontainer_factory.getInterfaces() |
---|
[4920] | 127 | <implementedBy waeup.sirp.university.coursecontainer.CourseContainer> |
---|
[4364] | 128 | |
---|
| 129 | |
---|
| 130 | Examples |
---|
| 131 | ======== |
---|
| 132 | |
---|
[4261] | 133 | Getting a course container |
---|
[4364] | 134 | -------------------------- |
---|
[4261] | 135 | |
---|
| 136 | We can easily create `CourseContainers`: |
---|
| 137 | |
---|
[4920] | 138 | >>> from waeup.sirp.university.coursecontainer import CourseContainer |
---|
[4261] | 139 | >>> mycontainer = CourseContainer() |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | Another way to get a course container -- without importing the class |
---|
| 143 | -- is via factories. We registered a factory for course containers |
---|
[4364] | 144 | under the name ``waeup.CourseContainer``. Now we can ask for an object |
---|
| 145 | by calling the appropriate factory: |
---|
[4261] | 146 | |
---|
| 147 | >>> from zope.component import createObject |
---|
| 148 | >>> createObject(u'waeup.CourseContainer') |
---|
[4920] | 149 | <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...> |
---|
[4261] | 150 | |
---|
| 151 | This way we get a thing that implements ICourseContainer without |
---|
| 152 | imports or similar. |
---|
| 153 | |
---|