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 | Tools |
---|
78 | ===== |
---|
79 | |
---|
80 | :func:`search_invalidated` |
---|
81 | -------------------------- |
---|
82 | |
---|
83 | .. function:: search_invalidated(batch_prefix, batch_num) |
---|
84 | |
---|
85 | Get set of accesscode catalog entries, where the accesscode was |
---|
86 | already invalidated and that belong to the batch given by the |
---|
87 | arguments. |
---|
88 | |
---|
89 | Note, that the set returned for efficiency reasons returns a set of |
---|
90 | catalog entries (int id mappings) and not the access-code objects |
---|
91 | themselves. |
---|
92 | |
---|
93 | :func:`invalidated_num` |
---|
94 | ----------------------- |
---|
95 | |
---|
96 | .. function:: invalidated_num(batch_prefix, batch_num) |
---|
97 | |
---|
98 | Get number of accesscodes found by `search_invalidated` for the |
---|
99 | batch specified by arguments. |
---|
100 | |
---|
101 | Examples |
---|
102 | ======== |
---|
103 | |
---|
104 | We register some AccessCodes and search them afterwards. |
---|
105 | |
---|
106 | >>> from datetime import datetime |
---|
107 | >>> from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch |
---|
108 | >>> batch = AccessCodeBatch( |
---|
109 | ... datetime(2009, 12, 23), 'Fred','APP', 12.12, 3, num=10) |
---|
110 | >>> u['accesscodes'].addBatch(batch) |
---|
111 | |
---|
112 | >>> from zope.site.hooks import setSite |
---|
113 | >>> setSite(u) |
---|
114 | |
---|
115 | We use the :class:`waeup.sirp.catalog.WAeUPQuery` object defined in |
---|
116 | :mod:`waeup.sirp.catalog`: |
---|
117 | |
---|
118 | >>> from hurry.query.interfaces import IQuery |
---|
119 | >>> from zope.component import getUtility |
---|
120 | >>> q = getUtility(IQuery) |
---|
121 | >>> q |
---|
122 | <waeup.sirp.catalog.WAeUPQuery object at 0x...> |
---|
123 | |
---|
124 | >>> from hurry.query import Text |
---|
125 | >>> r1 = q.searchResults( |
---|
126 | ... Text(('accesscode_catalog', 'text'), 'APP*')) |
---|
127 | >>> list(r1) |
---|
128 | [<...AccessCode object at 0x...>, |
---|
129 | <...AccessCode object at 0x...>, |
---|
130 | <...AccessCode object at 0x...] |
---|
131 | |
---|
132 | We can lookup catalog entries refering to invalidated accesscodes: |
---|
133 | |
---|
134 | >>> from waeup.sirp.accesscodes.catalog import search_invalidated |
---|
135 | >>> r2 = search_invalidated(batch.prefix, batch.num) |
---|
136 | >>> list(r2) |
---|
137 | [] |
---|
138 | |
---|
139 | And also lookup the number of entries: |
---|
140 | |
---|
141 | >>> from waeup.sirp.accesscodes.catalog import invalidated_num |
---|
142 | >>> invalidated_num(batch.prefix, batch.num) |
---|
143 | 0 |
---|
144 | |
---|
145 | When we invalidate an accesscode, we will get different results: |
---|
146 | |
---|
147 | >>> import grok |
---|
148 | >>> ac0 = list(r1)[0] |
---|
149 | >>> ac0.invalidation_date = datetime.now() |
---|
150 | |
---|
151 | We have to fire an ObjectModifiedEvent in order to inform the |
---|
152 | component architechture about the changed status of the object: |
---|
153 | |
---|
154 | >>> grok.notify(grok.ObjectModifiedEvent(ac0)) |
---|
155 | |
---|
156 | >>> invalidated_num(batch.prefix, batch.num) |
---|
157 | 1 |
---|