1 | ## $Id: accesscode.py 8186 2012-04-17 00:31:10Z uli $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Components to handle access codes. |
---|
19 | |
---|
20 | Access codes (aka PINs) in waeup sites are organized in batches. That |
---|
21 | means a certain accesscode must be part of a batch. As a site (or |
---|
22 | university) can hold an arbitrary number of batches, we also provide a |
---|
23 | batch container. Each university has one batch container that holds |
---|
24 | all access code batches of which each one can hold several thousands |
---|
25 | of access codes. |
---|
26 | """ |
---|
27 | import csv |
---|
28 | import grok |
---|
29 | import os |
---|
30 | from datetime import datetime |
---|
31 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
32 | from random import SystemRandom as random |
---|
33 | from zope.component import getUtility |
---|
34 | from waeup.kofa.interfaces import IKofaUtils, IKofaPluggable, IObjectHistory |
---|
35 | from waeup.kofa.utils.helpers import now |
---|
36 | from waeup.kofa.utils.logger import Logger |
---|
37 | from waeup.kofa.accesscodes.interfaces import ( |
---|
38 | IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer |
---|
39 | ) |
---|
40 | from waeup.kofa.accesscodes.workflow import DISABLED, USED, ac_states_dict |
---|
41 | |
---|
42 | class AccessCode(grok.Model): |
---|
43 | """An access code (aka PIN). |
---|
44 | |
---|
45 | Implements |
---|
46 | :class:`waeup.kofa.accesscodes.interfaces.IAccessCode`. :class:`AccessCode` |
---|
47 | instances are normally part of an :class:`AccessCodeBatch` so |
---|
48 | their representation (or code) is built with the containing batch |
---|
49 | involved. |
---|
50 | |
---|
51 | `batch_serial` |
---|
52 | the serial number of the new :class:`AccessCode` inside its batch. |
---|
53 | |
---|
54 | `random_num` |
---|
55 | a 10-digit number representing the main part of the code. |
---|
56 | |
---|
57 | :class:`AccessCode` instances normally have a representation (or |
---|
58 | code) like |
---|
59 | |
---|
60 | ``APP-XXX-YYYYYYYYYY`` |
---|
61 | |
---|
62 | where ``APP`` is the prefix of the containing batch, ``XXX`` is |
---|
63 | the batch number and ``YYYYYYYYYY`` is the real code. The complete |
---|
64 | PIN is portal-wide unique. |
---|
65 | |
---|
66 | Access code instances are far more than simple strings. They have |
---|
67 | a state, a history (so that all changes can be tracked) and a |
---|
68 | cost (given as a float number). |
---|
69 | |
---|
70 | The state of an access code is something like 'used', 'disabled', |
---|
71 | etc. and determined by the workflow defined in |
---|
72 | :mod:`waeup.kofa.accesscodes.workflow`. This also means that |
---|
73 | instead of setting the status of an access code directly (you |
---|
74 | can't do that easily, and yes, that's intentionally), you have to |
---|
75 | trigger a transition (that might fail, if the transition is not |
---|
76 | allowed in terms of logic or permissions). See |
---|
77 | :mod:`waeup.kofa.accesscodes.workflow` for details. |
---|
78 | |
---|
79 | """ |
---|
80 | grok.implements(IAccessCode) |
---|
81 | |
---|
82 | def __init__(self, batch_serial, random_num): |
---|
83 | super(AccessCode, self).__init__() |
---|
84 | self.batch_serial = batch_serial |
---|
85 | self.random_num = random_num |
---|
86 | self.owner = None |
---|
87 | IWorkflowInfo(self).fireTransition('init') |
---|
88 | |
---|
89 | @property |
---|
90 | def representation(self): |
---|
91 | """A string representation of the :class:`AccessCode`. |
---|
92 | |
---|
93 | It has format ``APP-XXX-YYYYYYYYYY`` as described above. |
---|
94 | """ |
---|
95 | return '%s-%s-%s' % ( |
---|
96 | self.batch_prefix, self.batch_num, self.random_num) |
---|
97 | |
---|
98 | @property |
---|
99 | def batch(self): |
---|
100 | """The batch this :class:`AccessCode` is contained. |
---|
101 | """ |
---|
102 | return getattr(self, '__parent__', None) |
---|
103 | |
---|
104 | @property |
---|
105 | def batch_prefix(self): |
---|
106 | """The prefix of the batch this :class:`AccessCode` belongs to. |
---|
107 | """ |
---|
108 | if self.batch is None: |
---|
109 | return '' |
---|
110 | return self.batch.prefix |
---|
111 | |
---|
112 | @property |
---|
113 | def batch_num(self): |
---|
114 | """The number of the batch this :class:`AccessCode` belongs to. A |
---|
115 | read-only attribute. |
---|
116 | """ |
---|
117 | if self.batch is None: |
---|
118 | return '' |
---|
119 | return self.batch.num |
---|
120 | |
---|
121 | @property |
---|
122 | def cost(self): |
---|
123 | """A float representing the price or ``None``. A read-only attribute. |
---|
124 | """ |
---|
125 | if self.batch is None: |
---|
126 | return None |
---|
127 | return self.batch.cost |
---|
128 | |
---|
129 | @property |
---|
130 | def state(self): |
---|
131 | """The workflow state. A read-only attribute. |
---|
132 | """ |
---|
133 | return IWorkflowState(self).getState() |
---|
134 | |
---|
135 | @property |
---|
136 | def translated_state(self): |
---|
137 | """The translated workflow state. A read-only attribute. |
---|
138 | """ |
---|
139 | return ac_states_dict[self.state] |
---|
140 | |
---|
141 | @property |
---|
142 | def history(self): |
---|
143 | """A :class:`waeup.kofa.objecthistory.ObjectHistory` instance. |
---|
144 | """ |
---|
145 | history = IObjectHistory(self) |
---|
146 | return '||'.join(history.messages) |
---|
147 | |
---|
148 | class AccessCodeBatch(grok.Container): |
---|
149 | """A batch of access codes. |
---|
150 | """ |
---|
151 | grok.implements(IAccessCodeBatch) |
---|
152 | |
---|
153 | def __init__(self, creation_date, creator, batch_prefix, cost, |
---|
154 | entry_num, num=None): |
---|
155 | super(AccessCodeBatch, self).__init__() |
---|
156 | self.creation_date = creation_date |
---|
157 | self.creator = creator |
---|
158 | self.prefix = batch_prefix.upper() |
---|
159 | self.cost = cost |
---|
160 | self.entry_num = entry_num |
---|
161 | self.num = num |
---|
162 | self.used_num = 0 |
---|
163 | self.disabled_num = 0 |
---|
164 | |
---|
165 | def _createEntries(self): |
---|
166 | """Create the entries for this batch. |
---|
167 | """ |
---|
168 | for num, pin in enumerate(self.getNewRandomNum(num=self.entry_num)): |
---|
169 | self.addAccessCode(num, pin) |
---|
170 | return |
---|
171 | |
---|
172 | def getNewRandomNum(self, num=1): |
---|
173 | """Create a set of ``num`` random numbers of 10 digits each. |
---|
174 | |
---|
175 | The number is returned as string. |
---|
176 | """ |
---|
177 | curr = 1 |
---|
178 | while curr <= num: |
---|
179 | pin = '' |
---|
180 | for x in range(10): |
---|
181 | pin += str(random().randint(0, 9)) |
---|
182 | if not '%s-%s-%s' % (self.prefix, self.num, pin) in self.keys(): |
---|
183 | curr += 1 |
---|
184 | yield pin |
---|
185 | # PIN already in use |
---|
186 | |
---|
187 | def _getStoragePath(self): |
---|
188 | """Get the directory, where we store all batch-related CSV files. |
---|
189 | """ |
---|
190 | site = grok.getSite() |
---|
191 | storagepath = site['datacenter'].storage |
---|
192 | ac_storage = os.path.join(storagepath, 'accesscodes') |
---|
193 | if not os.path.exists(ac_storage): |
---|
194 | os.mkdir(ac_storage) |
---|
195 | return ac_storage |
---|
196 | |
---|
197 | def getAccessCode(self, ac_id): |
---|
198 | """Get the AccessCode with ID ``ac_id`` or ``KeyError``. |
---|
199 | """ |
---|
200 | return self[ac_id] |
---|
201 | |
---|
202 | def addAccessCode(self, num, pin, owner=None): |
---|
203 | """Add an access-code. |
---|
204 | """ |
---|
205 | ac = AccessCode(num, pin) |
---|
206 | if owner: |
---|
207 | ac.owner = owner |
---|
208 | ac.__parent__ = self |
---|
209 | self[ac.representation] = ac |
---|
210 | return |
---|
211 | |
---|
212 | def createCSVLogFile(self): |
---|
213 | """Create a CSV file with data in batch. |
---|
214 | |
---|
215 | Data will not contain invalidation date nor student ids. File |
---|
216 | will be created in ``accesscodes`` subdir of data center |
---|
217 | storage path. |
---|
218 | |
---|
219 | Returns name of created file. |
---|
220 | """ |
---|
221 | date = self.creation_date.strftime('%Y_%m_%d_%H_%M_%S') |
---|
222 | ac_storage = self._getStoragePath() |
---|
223 | csv_path = os.path.join( |
---|
224 | ac_storage, '%s-%s-%s-%s.csv' % ( |
---|
225 | self.prefix, self.num, date, self.creator) |
---|
226 | ) |
---|
227 | writer = csv.writer(open(csv_path, 'w'), quoting=csv.QUOTE_ALL) |
---|
228 | writer.writerow(['serial', 'ac', 'cost']) |
---|
229 | writer.writerow([self.prefix, str(self.num), "%0.2f" % self.cost]) |
---|
230 | |
---|
231 | for value in sorted(self.values(), |
---|
232 | cmp=lambda x, y: cmp( |
---|
233 | x.batch_serial, y.batch_serial)): |
---|
234 | writer.writerow( |
---|
235 | [str(value.batch_serial), str(value.representation)] |
---|
236 | ) |
---|
237 | site = grok.getSite() |
---|
238 | logger = site.logger |
---|
239 | logger.info( |
---|
240 | "Created batch %s-%s" % (self.prefix, self.num)) |
---|
241 | logger.info( |
---|
242 | "Written batch CSV to %s" % csv_path) |
---|
243 | return os.path.basename(csv_path) |
---|
244 | |
---|
245 | def archive(self): |
---|
246 | """Create a CSV file for archive. |
---|
247 | """ |
---|
248 | ac_storage = self._getStoragePath() |
---|
249 | tz = getUtility(IKofaUtils).tzinfo |
---|
250 | dt_now = now(tz) |
---|
251 | timestamp = dt_now.strftime('%Y_%m_%d_%H_%M_%S') |
---|
252 | csv_path = os.path.join( |
---|
253 | ac_storage, '%s-%s_archive-%s-%s.csv' % ( |
---|
254 | self.prefix, self.num, timestamp, self.creator) |
---|
255 | ) |
---|
256 | writer = csv.writer(open(csv_path, 'w'), quoting=csv.QUOTE_ALL) |
---|
257 | writer.writerow(['prefix', 'serial', 'ac', 'state', 'history']) |
---|
258 | writer.writerow([self.prefix, '%0.2f' % self.cost, str(self.num), |
---|
259 | str(self.entry_num)]) |
---|
260 | for value in sorted( |
---|
261 | self.values(), |
---|
262 | cmp = lambda x, y: cmp(x.batch_serial, y.batch_serial) |
---|
263 | ): |
---|
264 | writer.writerow([ |
---|
265 | self.prefix, value.batch_serial, value.representation, |
---|
266 | value.state, value.history |
---|
267 | ]) |
---|
268 | return os.path.basename(csv_path) |
---|
269 | |
---|
270 | @grok.subscribe(IAccessCodeBatch, grok.IObjectAddedEvent) |
---|
271 | def handle_batch_added(batch, event): |
---|
272 | # A (maybe dirty?) workaround to make batch containers work |
---|
273 | # without self-maintained acids: as batches should contain their |
---|
274 | # set of data immediately after creation, but we cannot add |
---|
275 | # subobjects as long as the batch was not added already to the |
---|
276 | # ZODB, we trigger the item creation for the time after the batch |
---|
277 | # was added to the ZODB. |
---|
278 | batch._createEntries() |
---|
279 | return |
---|
280 | |
---|
281 | |
---|
282 | class AccessCodeBatchContainer(grok.Container, Logger): |
---|
283 | grok.implements(IAccessCodeBatchContainer) |
---|
284 | |
---|
285 | def _getStoragePath(self): |
---|
286 | """Get the directory, where batch import files are stored. |
---|
287 | |
---|
288 | If the path does not exist yet, it is created. The path is |
---|
289 | normally ``accesscodes/imports`` below the datacenter storage |
---|
290 | path (see :data:`waeup.kofa.accesscodes.Datacenter.storage`). |
---|
291 | """ |
---|
292 | site = grok.getSite() |
---|
293 | storagepath = site['datacenter'].storage |
---|
294 | ac_storage = os.path.join(storagepath, 'accesscodes') |
---|
295 | import_path = os.path.join(ac_storage, 'imports') |
---|
296 | for path in [ac_storage, import_path]: |
---|
297 | if not os.path.exists(path): |
---|
298 | os.mkdir(path) |
---|
299 | site.logger.info('created path %s' % path) |
---|
300 | return import_path |
---|
301 | |
---|
302 | def addBatch(self, batch): |
---|
303 | """Add an already created `batch`. |
---|
304 | """ |
---|
305 | batch.num = self.getNum(batch.prefix) |
---|
306 | key = "%s-%s" % (batch.prefix, batch.num) |
---|
307 | self[key] = batch |
---|
308 | self._p_changed = True |
---|
309 | |
---|
310 | def createBatch(self, creation_date, creator, prefix, cost, |
---|
311 | entry_num): |
---|
312 | """Create and add a batch. |
---|
313 | """ |
---|
314 | batch_num = self.getNum(prefix) |
---|
315 | batch = AccessCodeBatch(creation_date, creator, prefix, |
---|
316 | cost, entry_num, num=batch_num) |
---|
317 | self.addBatch(batch) |
---|
318 | return batch |
---|
319 | |
---|
320 | def getNum(self, prefix): |
---|
321 | """Get next unused num for given prefix. |
---|
322 | """ |
---|
323 | # School fee, clearance and hostel application batches start with 0. |
---|
324 | # These batches are being emptily created during initialization of the |
---|
325 | # university instance. |
---|
326 | if prefix in ('CLR', 'SFE', 'HOS'): |
---|
327 | num = 0 |
---|
328 | else: |
---|
329 | num = 1 |
---|
330 | while self.get('%s-%s' % (prefix, num), None) is not None: |
---|
331 | num += 1 |
---|
332 | return num |
---|
333 | |
---|
334 | def getImportFiles(self): |
---|
335 | """Return a generator with basenames of available import files. |
---|
336 | """ |
---|
337 | path = self._getStoragePath() |
---|
338 | for filename in sorted(os.listdir(path)): |
---|
339 | yield filename |
---|
340 | |
---|
341 | # This is temporary reimport solution. Access codes will be imported |
---|
342 | # with state initialized no matter if they have been used before. |
---|
343 | def reimport(self, filename, creator=u'UNKNOWN'): |
---|
344 | """Reimport a batch given in CSV file. |
---|
345 | |
---|
346 | CSV file must be of format as generated by createCSVLogFile |
---|
347 | method. |
---|
348 | """ |
---|
349 | path = os.path.join(self._getStoragePath(), filename) |
---|
350 | reader = csv.DictReader(open(path, 'rb'), quoting=csv.QUOTE_ALL) |
---|
351 | entry = reader.next() |
---|
352 | cost = float(entry['serial']) |
---|
353 | num = int(entry['ac']) |
---|
354 | prefix = entry['prefix'] |
---|
355 | batch_name = '%s-%s' % (prefix, num) |
---|
356 | if batch_name in self.keys(): |
---|
357 | raise KeyError('Batch already exists: %s' % batch_name) |
---|
358 | tz = getUtility(IKofaUtils).tzinfo |
---|
359 | batch = AccessCodeBatch( |
---|
360 | datetime.now(tz), creator, prefix, cost, 0, num=num) |
---|
361 | num_entries = 0 |
---|
362 | self[batch_name] = batch |
---|
363 | for row in reader: |
---|
364 | pin = row['ac'] |
---|
365 | serial = int(row['serial']) |
---|
366 | rand_num = pin.rsplit('-', 1)[-1] |
---|
367 | batch.addAccessCode(serial, rand_num) |
---|
368 | num_entries += 1 |
---|
369 | batch.entry_num = num_entries |
---|
370 | |
---|
371 | batch.createCSVLogFile() |
---|
372 | return |
---|
373 | |
---|
374 | def getAccessCode(self, ac_id): |
---|
375 | """Get the AccessCode with ID ``ac_id`` or ``KeyError``. |
---|
376 | """ |
---|
377 | for batchname in self.keys(): |
---|
378 | batch = self[batchname] |
---|
379 | try: |
---|
380 | return batch.getAccessCode(ac_id) |
---|
381 | except KeyError: |
---|
382 | continue |
---|
383 | return None |
---|
384 | |
---|
385 | def disable(self, ac_id, comment=None): |
---|
386 | """Disable the AC with ID ``ac_id``. |
---|
387 | |
---|
388 | ``user_id`` is the user ID of the user triggering the |
---|
389 | process. Already disabled ACs are left untouched. |
---|
390 | """ |
---|
391 | ac = self.getAccessCode(ac_id) |
---|
392 | if ac is None: |
---|
393 | return |
---|
394 | disable_accesscode(ac_id, comment) |
---|
395 | return |
---|
396 | |
---|
397 | def enable(self, ac_id, comment=None): |
---|
398 | """(Re-)enable the AC with ID ``ac_id``. |
---|
399 | |
---|
400 | This leaves the given AC in state ``unused``. Already enabled |
---|
401 | ACs are left untouched. |
---|
402 | """ |
---|
403 | ac = self.getAccessCode(ac_id) |
---|
404 | if ac is None: |
---|
405 | return |
---|
406 | reenable_accesscode(ac_id, comment) |
---|
407 | return |
---|
408 | |
---|
409 | logger_name = 'waeup.kofa.${sitename}.accesscodes' |
---|
410 | logger_filename = 'accesscodes.log' |
---|
411 | |
---|
412 | def logger_info(self, ob_class, comment=None): |
---|
413 | """Get the logger's info method. |
---|
414 | """ |
---|
415 | self.logger.info('%s - %s' % ( |
---|
416 | ob_class, comment)) |
---|
417 | return |
---|
418 | |
---|
419 | class AccessCodePlugin(grok.GlobalUtility): |
---|
420 | grok.name('accesscodes') |
---|
421 | grok.implements(IKofaPluggable) |
---|
422 | |
---|
423 | def setup(self, site, name, logger): |
---|
424 | basecontainer = AccessCodeBatchContainer() |
---|
425 | site['accesscodes'] = basecontainer |
---|
426 | logger.info('Installed container for access code batches.') |
---|
427 | # Create empty school fee, clearance and hostel application AC |
---|
428 | # batches during initialization of university instance. |
---|
429 | cost = 0.0 |
---|
430 | creator = 'system' |
---|
431 | entry_num = 0 |
---|
432 | tz = getUtility(IKofaUtils).tzinfo |
---|
433 | creation_date = datetime.now(tz) |
---|
434 | basecontainer.createBatch(creation_date, creator, |
---|
435 | 'SFE', cost, entry_num) |
---|
436 | basecontainer.createBatch(creation_date, creator, |
---|
437 | 'CLR', cost, entry_num) |
---|
438 | basecontainer.createBatch(creation_date, creator, |
---|
439 | 'HOS', cost, entry_num) |
---|
440 | logger.info('Installed empty SFE, CLR and HOS access code batches.') |
---|
441 | return |
---|
442 | |
---|
443 | def update(self, site, name, logger): |
---|
444 | if not 'accesscodes' in site.keys(): |
---|
445 | logger.info('Updating site at %s. Installing access codes.' % ( |
---|
446 | site,)) |
---|
447 | self.setup(site, name, logger) |
---|
448 | else: |
---|
449 | logger.info( |
---|
450 | 'AccessCodePlugin: Updating site at %s: Nothing to do.' % ( |
---|
451 | site, )) |
---|
452 | return |
---|
453 | |
---|
454 | def get_access_code(access_code): |
---|
455 | """Get an access code instance. |
---|
456 | |
---|
457 | An access code here is a string like ``PUDE-1-1234567890``. |
---|
458 | |
---|
459 | Returns ``None`` if the given code cannot be found. |
---|
460 | |
---|
461 | This is a convenicence function that is faster than looking up a |
---|
462 | batch container for the approriate access code. |
---|
463 | """ |
---|
464 | site = grok.getSite() |
---|
465 | if not isinstance(access_code, basestring): |
---|
466 | return None |
---|
467 | try: |
---|
468 | batch_id, ac_id = access_code.rsplit('-', 1) |
---|
469 | except: |
---|
470 | return None |
---|
471 | batch = site['accesscodes'].get(batch_id, None) |
---|
472 | if batch is None: |
---|
473 | return None |
---|
474 | try: |
---|
475 | code = batch.getAccessCode(access_code) |
---|
476 | except KeyError: |
---|
477 | return None |
---|
478 | return code |
---|
479 | |
---|
480 | def fire_transition(access_code, arg, toward=False, comment=None, owner=None): |
---|
481 | """Fire workflow transition for access code. |
---|
482 | |
---|
483 | The access code instance is looked up via `access_code` (a string |
---|
484 | like ``APP-1-12345678``). |
---|
485 | |
---|
486 | `arg` tells what kind of transition to trigger. This will be a |
---|
487 | transition id like ``'use'`` or ``'init'``, or some transition |
---|
488 | target like :data:`waeup.kofa.accesscodes.workflow.INITIALIZED`. |
---|
489 | |
---|
490 | If `toward` is ``False`` (the default) you have to pass a |
---|
491 | transition id as `arg`, otherwise you must give a transition |
---|
492 | target. |
---|
493 | |
---|
494 | If `comment` is specified (default is ``None``) the given string |
---|
495 | will be passed along as transition comment. It will appear in the |
---|
496 | history of the changed access code. You can use this to place |
---|
497 | remarks like for which object the access code was used or similar. |
---|
498 | |
---|
499 | If `owner` is specified, the owner attribute of the access code is checked. |
---|
500 | If the owner is different :func:`fire_transition` fails and returns False. |
---|
501 | |
---|
502 | :func:`fire_transition` might raise exceptions depending on the |
---|
503 | reason why the requested transition cannot be performed. |
---|
504 | |
---|
505 | The following exceptions can occur during processing: |
---|
506 | |
---|
507 | :exc:`KeyError`: |
---|
508 | signals not existent access code, batch or site. |
---|
509 | |
---|
510 | :exc:`ValueError`: |
---|
511 | signals illegal format of `access_code` string. The regular format is |
---|
512 | ``APP-N-XXXXXXXX``. |
---|
513 | |
---|
514 | :exc:`hurry.workflow.interfaces.InvalidTransitionError`: |
---|
515 | the transition requested cannot be performed because the workflow |
---|
516 | rules forbid it. |
---|
517 | |
---|
518 | :exc:`Unauthorized`: |
---|
519 | the current user is not allowed to perform the requested transition. |
---|
520 | |
---|
521 | """ |
---|
522 | try: |
---|
523 | batch_id, ac_id = access_code.rsplit('-', 1) |
---|
524 | except ValueError: |
---|
525 | raise ValueError( |
---|
526 | 'Invalid access code format: %s (use: APP-N-XXXXXXXX)' % ( |
---|
527 | access_code,)) |
---|
528 | try: |
---|
529 | ac = grok.getSite()['accesscodes'][batch_id].getAccessCode(access_code) |
---|
530 | except TypeError: |
---|
531 | raise KeyError( |
---|
532 | 'No site available for looking up accesscodes') |
---|
533 | if owner: |
---|
534 | ac_owner = getattr(ac, 'owner', None) |
---|
535 | if ac_owner and ac_owner != owner: |
---|
536 | return False |
---|
537 | info = IWorkflowInfo(ac) |
---|
538 | if toward: |
---|
539 | info.fireTransitionToward(arg, comment=comment) |
---|
540 | else: |
---|
541 | info.fireTransition(arg, comment=comment) |
---|
542 | return True |
---|
543 | |
---|
544 | def invalidate_accesscode(access_code, comment=None, owner=None): |
---|
545 | """Invalidate AccessCode denoted by string ``access_code``. |
---|
546 | |
---|
547 | Fires an appropriate transition to perform the task. |
---|
548 | |
---|
549 | `comment` is a string that will appear in the access code |
---|
550 | history. |
---|
551 | |
---|
552 | See :func:`fire_transition` for possible exceptions and their |
---|
553 | meanings. |
---|
554 | """ |
---|
555 | try: |
---|
556 | return fire_transition(access_code, 'use', comment=comment, owner=owner) |
---|
557 | except: |
---|
558 | return False |
---|
559 | |
---|
560 | def disable_accesscode(access_code, comment=None): |
---|
561 | """Disable AccessCode denoted by string ``access_code``. |
---|
562 | |
---|
563 | Fires an appropriate transition to perform the task. |
---|
564 | |
---|
565 | `comment` is a string that will appear in the access code |
---|
566 | history. |
---|
567 | |
---|
568 | See :func:`fire_transition` for possible exceptions and their |
---|
569 | meanings. |
---|
570 | """ |
---|
571 | return fire_transition( |
---|
572 | access_code, DISABLED, toward=True, comment=comment) |
---|
573 | |
---|
574 | def reenable_accesscode(access_code, comment=None): |
---|
575 | """Reenable AccessCode denoted by string ``access_code``. |
---|
576 | |
---|
577 | Fires an appropriate transition to perform the task. |
---|
578 | |
---|
579 | `comment` is a string that will appear in the access code |
---|
580 | history. |
---|
581 | |
---|
582 | See :func:`fire_transition` for possible exceptions and their |
---|
583 | meanings. |
---|
584 | """ |
---|
585 | return fire_transition(access_code, 'reenable', comment=comment) |
---|
586 | |
---|
587 | def create_accesscode(batch_prefix, batch_num, owner): |
---|
588 | """ |
---|
589 | """ |
---|
590 | batch_id = '%s-%s' % (batch_prefix, batch_num) |
---|
591 | try: |
---|
592 | batch = grok.getSite()['accesscodes'][batch_id] |
---|
593 | except KeyError: |
---|
594 | return None, u'No AC batch available.' |
---|
595 | rand_num = list(batch.getNewRandomNum())[0] |
---|
596 | num = len(batch) + 1 |
---|
597 | batch.addAccessCode(num, rand_num, owner) |
---|
598 | batch.entry_num += 1 |
---|
599 | pin = u'%s-%s-%s' % (batch_prefix,batch_num,rand_num) |
---|
600 | return pin, None |
---|