source: main/waeup.kofa/trunk/src/waeup/kofa/university/coursescontainer.txt @ 7835

Last change on this file since 7835 was 7819, checked in by Henrik Bettermann, 13 years ago

KOFA -> Kofa

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