1 | :mod:`waeup.sirp.accesscodes.catalog` -- Cataloging access-codes |
---|
2 | **************************************************************** |
---|
3 | |
---|
4 | .. module:: waeup.sirp.accesscodes.catalog |
---|
5 | |
---|
6 | Components that support cataloging and searching accesscodes inside a |
---|
7 | WAeUP SIRP site. |
---|
8 | |
---|
9 | :Test-Layer: functional |
---|
10 | |
---|
11 | .. contents:: |
---|
12 | |
---|
13 | |
---|
14 | The `catalog` module of `waeup.sirp.accesscodes` provides a catalog |
---|
15 | for search :class:`waeup.sirp.accesscode.interfaces.IAccessCode` |
---|
16 | objects. |
---|
17 | |
---|
18 | Components |
---|
19 | ========== |
---|
20 | |
---|
21 | :class:`AccessCodeCatalog` |
---|
22 | -------------------------- |
---|
23 | |
---|
24 | .. class:: AccessCodeCatalog |
---|
25 | |
---|
26 | .. attribute:: grok.site(waeup.sirp.interfaces.IUniversity) |
---|
27 | .. attribute:: grok.name('accesscode_catalog') |
---|
28 | .. attribute:: grok.context(waeup.sirp.accesscodes.interfaces.IAccessCode) |
---|
29 | |
---|
30 | A catalog is registered each time an IUniversity instance is created |
---|
31 | and stored in the ZODB: |
---|
32 | |
---|
33 | >>> from waeup.sirp.app import University |
---|
34 | >>> u = University() |
---|
35 | >>> getRootFolder()['u'] = u |
---|
36 | |
---|
37 | It is setup as a local utility named ``accesscode_catalog``. As a |
---|
38 | local utility it only catalogs objects of the 'current' site aka |
---|
39 | `University` object and does not interfere with catalogs for |
---|
40 | different sites. This means you can safely run different |
---|
41 | Universities in one instance which won't share their data. |
---|
42 | |
---|
43 | Furthermore AccessCodeCatalog objects catalog objects of type |
---|
44 | :class:`waeup.sirp.accesscodes.interfaces.IAccessCode`` only. |
---|
45 | |
---|
46 | >>> from zope.site.hooks import setSite |
---|
47 | >>> setSite(u) |
---|
48 | |
---|
49 | >>> from zope.component import getUtility |
---|
50 | >>> from zope.catalog.interfaces import ICatalog |
---|
51 | >>> catalog = getUtility(ICatalog, name=u'accesscode_catalog') |
---|
52 | >>> catalog |
---|
53 | <zope.catalog.catalog.Catalog object at 0x...> |
---|
54 | |
---|
55 | It provides several indexes: |
---|
56 | |
---|
57 | >>> from pprint import pprint |
---|
58 | >>> pprint(sorted(list(catalog.items()))) |
---|
59 | [(u'invalidated', <zope...FieldIndex object at 0x...>), |
---|
60 | (u'name', <zope...FieldIndex object at 0x...>), |
---|
61 | (u'student_id', <zope...FieldIndex object at 0x...>), |
---|
62 | (u'text', <zope...text.TextIndex object at 0x...>)] |
---|
63 | |
---|
64 | The ``invalidated`` index keeps references to the |
---|
65 | ``invalidation_date`` field of |
---|
66 | :class:`waeup.sirp.accesscodes.AccessCode` instances. This also |
---|
67 | means, that you have to look for ``datetime.datetime`` type values. |
---|
68 | |
---|
69 | The ``text`` and ``name`` indexes both store the ``representation`` |
---|
70 | field of AccessCode instances. While the first is a text index, it |
---|
71 | allows searching using wildcards. The latter is a field index used |
---|
72 | in internal search functions. |
---|
73 | |
---|
74 | ``student_id`` finally is an index to the search for student ids or |
---|
75 | similar when an access code gets invalidated. |
---|
76 | |
---|
77 | Examples |
---|
78 | ======== |
---|
79 | |
---|
80 | We register some AccessCodes and search them afterwards. |
---|
81 | |
---|
82 | >>> from datetime import datetime |
---|
83 | >>> from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch |
---|
84 | >>> batch = AccessCodeBatch( |
---|
85 | ... datetime(2009, 12, 23), 'Fred','APP', 12.12, 3, num=10) |
---|
86 | >>> u['accesscodes'].addBatch(batch) |
---|
87 | |
---|
88 | >>> from zope.site.hooks import setSite |
---|
89 | >>> setSite(u) |
---|
90 | |
---|
91 | We use the :class:`waeup.sirp.catalog.WAeUPQuery` object defined in |
---|
92 | :mod:`waeup.sirp.catalog`: |
---|
93 | |
---|
94 | >>> from hurry.query.interfaces import IQuery |
---|
95 | >>> from zope.component import getUtility |
---|
96 | >>> q = getUtility(IQuery) |
---|
97 | >>> q |
---|
98 | <waeup.sirp.catalog.WAeUPQuery object at 0x...> |
---|
99 | |
---|
100 | >>> from hurry.query import Text |
---|
101 | >>> r1 = q.searchResults( |
---|
102 | ... Text(('accesscode_catalog', 'text'), 'APP*')) |
---|
103 | >>> list(r1) |
---|
104 | [<...AccessCode object at 0x...>, |
---|
105 | <...AccessCode object at 0x...>, |
---|
106 | <...AccessCode object at 0x...] |
---|