[7195] | 1 | ## $Id: accesscode.py 8194 2012-04-17 11:40:06Z henrik $ |
---|
| 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 | ## |
---|
[5068] | 18 | """Components to handle access codes. |
---|
[6495] | 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. |
---|
[5068] | 26 | """ |
---|
[5110] | 27 | import csv |
---|
[5068] | 28 | import grok |
---|
[5110] | 29 | import os |
---|
[5118] | 30 | from datetime import datetime |
---|
[6432] | 31 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
[5068] | 32 | from random import SystemRandom as random |
---|
[8182] | 33 | from zope.component import getUtility |
---|
| 34 | from waeup.kofa.interfaces import IKofaUtils, IKofaPluggable, IObjectHistory |
---|
[8186] | 35 | from waeup.kofa.utils.helpers import now |
---|
[7811] | 36 | from waeup.kofa.utils.logger import Logger |
---|
| 37 | from waeup.kofa.accesscodes.interfaces import ( |
---|
[5079] | 38 | IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer |
---|
| 39 | ) |
---|
[7811] | 40 | from waeup.kofa.accesscodes.workflow import DISABLED, USED, ac_states_dict |
---|
[5068] | 41 | |
---|
[6359] | 42 | class AccessCode(grok.Model): |
---|
[6495] | 43 | """An access code (aka PIN). |
---|
[6499] | 44 | |
---|
| 45 | Implements |
---|
[7811] | 46 | :class:`waeup.kofa.accesscodes.interfaces.IAccessCode`. :class:`AccessCode` |
---|
[6499] | 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 |
---|
[7811] | 72 | :mod:`waeup.kofa.accesscodes.workflow`. This also means that |
---|
[6499] | 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 |
---|
[7811] | 77 | :mod:`waeup.kofa.accesscodes.workflow` for details. |
---|
[6499] | 78 | |
---|
[6495] | 79 | """ |
---|
[5068] | 80 | grok.implements(IAccessCode) |
---|
| 81 | |
---|
[6386] | 82 | def __init__(self, batch_serial, random_num): |
---|
[6623] | 83 | super(AccessCode, self).__init__() |
---|
[5068] | 84 | self.batch_serial = batch_serial |
---|
| 85 | self.random_num = random_num |
---|
[6927] | 86 | self.owner = None |
---|
[6359] | 87 | IWorkflowInfo(self).fireTransition('init') |
---|
[5068] | 88 | |
---|
| 89 | @property |
---|
| 90 | def representation(self): |
---|
[6499] | 91 | """A string representation of the :class:`AccessCode`. |
---|
| 92 | |
---|
| 93 | It has format ``APP-XXX-YYYYYYYYYY`` as described above. |
---|
| 94 | """ |
---|
[5068] | 95 | return '%s-%s-%s' % ( |
---|
| 96 | self.batch_prefix, self.batch_num, self.random_num) |
---|
| 97 | |
---|
[5079] | 98 | @property |
---|
| 99 | def batch(self): |
---|
[6499] | 100 | """The batch this :class:`AccessCode` is contained. |
---|
| 101 | """ |
---|
[5079] | 102 | return getattr(self, '__parent__', None) |
---|
[5086] | 103 | |
---|
[5079] | 104 | @property |
---|
| 105 | def batch_prefix(self): |
---|
[6499] | 106 | """The prefix of the batch this :class:`AccessCode` belongs to. |
---|
| 107 | """ |
---|
[5079] | 108 | if self.batch is None: |
---|
| 109 | return '' |
---|
| 110 | return self.batch.prefix |
---|
[5086] | 111 | |
---|
[5079] | 112 | @property |
---|
| 113 | def batch_num(self): |
---|
[6499] | 114 | """The number of the batch this :class:`AccessCode` belongs to. A |
---|
| 115 | read-only attribute. |
---|
| 116 | """ |
---|
[5079] | 117 | if self.batch is None: |
---|
| 118 | return '' |
---|
| 119 | return self.batch.num |
---|
[5068] | 120 | |
---|
[5118] | 121 | @property |
---|
| 122 | def cost(self): |
---|
[6499] | 123 | """A float representing the price or ``None``. A read-only attribute. |
---|
| 124 | """ |
---|
[5118] | 125 | if self.batch is None: |
---|
| 126 | return None |
---|
| 127 | return self.batch.cost |
---|
| 128 | |
---|
[6413] | 129 | @property |
---|
[6470] | 130 | def state(self): |
---|
[6499] | 131 | """The workflow state. A read-only attribute. |
---|
| 132 | """ |
---|
[6450] | 133 | return IWorkflowState(self).getState() |
---|
| 134 | |
---|
| 135 | @property |
---|
[7689] | 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 |
---|
[6423] | 142 | def history(self): |
---|
[7811] | 143 | """A :class:`waeup.kofa.objecthistory.ObjectHistory` instance. |
---|
[6495] | 144 | """ |
---|
[6423] | 145 | history = IObjectHistory(self) |
---|
[6453] | 146 | return '||'.join(history.messages) |
---|
[6423] | 147 | |
---|
[6417] | 148 | class AccessCodeBatch(grok.Container): |
---|
[5068] | 149 | """A batch of access codes. |
---|
| 150 | """ |
---|
| 151 | grok.implements(IAccessCodeBatch) |
---|
| 152 | |
---|
[5086] | 153 | def __init__(self, creation_date, creator, batch_prefix, cost, |
---|
| 154 | entry_num, num=None): |
---|
[5079] | 155 | super(AccessCodeBatch, self).__init__() |
---|
[5068] | 156 | self.creation_date = creation_date |
---|
| 157 | self.creator = creator |
---|
[5116] | 158 | self.prefix = batch_prefix.upper() |
---|
[5068] | 159 | self.cost = cost |
---|
| 160 | self.entry_num = entry_num |
---|
[5086] | 161 | self.num = num |
---|
[6425] | 162 | self.used_num = 0 |
---|
[5149] | 163 | self.disabled_num = 0 |
---|
[5079] | 164 | |
---|
[5118] | 165 | def _createEntries(self): |
---|
[5079] | 166 | """Create the entries for this batch. |
---|
| 167 | """ |
---|
[6936] | 168 | for num, pin in enumerate(self.getNewRandomNum(num=self.entry_num)): |
---|
[5121] | 169 | self.addAccessCode(num, pin) |
---|
[5112] | 170 | return |
---|
[5118] | 171 | |
---|
[6936] | 172 | def getNewRandomNum(self, num=1): |
---|
[5112] | 173 | """Create a set of ``num`` random numbers of 10 digits each. |
---|
[5086] | 174 | |
---|
[5068] | 175 | The number is returned as string. |
---|
| 176 | """ |
---|
[5118] | 177 | curr = 1 |
---|
| 178 | while curr <= num: |
---|
[5112] | 179 | pin = '' |
---|
[5068] | 180 | for x in range(10): |
---|
[5112] | 181 | pin += str(random().randint(0, 9)) |
---|
[6417] | 182 | if not '%s-%s-%s' % (self.prefix, self.num, pin) in self.keys(): |
---|
[5127] | 183 | curr += 1 |
---|
| 184 | yield pin |
---|
| 185 | # PIN already in use |
---|
[5073] | 186 | |
---|
[5118] | 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 | """ |
---|
[6417] | 200 | return self[ac_id] |
---|
[5118] | 201 | |
---|
[6936] | 202 | def addAccessCode(self, num, pin, owner=None): |
---|
[5121] | 203 | """Add an access-code. |
---|
| 204 | """ |
---|
| 205 | ac = AccessCode(num, pin) |
---|
[6936] | 206 | if owner: |
---|
| 207 | ac.owner = owner |
---|
[5121] | 208 | ac.__parent__ = self |
---|
[6417] | 209 | self[ac.representation] = ac |
---|
[5121] | 210 | return |
---|
| 211 | |
---|
[5110] | 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') |
---|
[5118] | 222 | ac_storage = self._getStoragePath() |
---|
[5110] | 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]) |
---|
[5118] | 230 | |
---|
[6424] | 231 | for value in sorted(self.values(), |
---|
| 232 | cmp=lambda x, y: cmp( |
---|
| 233 | x.batch_serial, y.batch_serial)): |
---|
[5110] | 234 | writer.writerow( |
---|
| 235 | [str(value.batch_serial), str(value.representation)] |
---|
| 236 | ) |
---|
[5118] | 237 | site = grok.getSite() |
---|
[5112] | 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) |
---|
[5110] | 243 | return os.path.basename(csv_path) |
---|
| 244 | |
---|
[5118] | 245 | def archive(self): |
---|
| 246 | """Create a CSV file for archive. |
---|
| 247 | """ |
---|
| 248 | ac_storage = self._getStoragePath() |
---|
[8183] | 249 | tz = getUtility(IKofaUtils).tzinfo |
---|
[8186] | 250 | dt_now = now(tz) |
---|
| 251 | timestamp = dt_now.strftime('%Y_%m_%d_%H_%M_%S') |
---|
[5118] | 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) |
---|
[6470] | 257 | writer.writerow(['prefix', 'serial', 'ac', 'state', 'history']) |
---|
[5118] | 258 | writer.writerow([self.prefix, '%0.2f' % self.cost, str(self.num), |
---|
| 259 | str(self.entry_num)]) |
---|
[6424] | 260 | for value in sorted( |
---|
| 261 | self.values(), |
---|
| 262 | cmp = lambda x, y: cmp(x.batch_serial, y.batch_serial) |
---|
| 263 | ): |
---|
[5118] | 264 | writer.writerow([ |
---|
| 265 | self.prefix, value.batch_serial, value.representation, |
---|
[6470] | 266 | value.state, value.history |
---|
[5118] | 267 | ]) |
---|
| 268 | return os.path.basename(csv_path) |
---|
| 269 | |
---|
[6417] | 270 | @grok.subscribe(IAccessCodeBatch, grok.IObjectAddedEvent) |
---|
[6418] | 271 | def handle_batch_added(batch, event): |
---|
[6417] | 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 | |
---|
[7747] | 282 | class AccessCodeBatchContainer(grok.Container, Logger): |
---|
[5079] | 283 | grok.implements(IAccessCodeBatchContainer) |
---|
[5073] | 284 | |
---|
[5132] | 285 | def _getStoragePath(self): |
---|
| 286 | """Get the directory, where batch import files are stored. |
---|
[6542] | 287 | |
---|
| 288 | If the path does not exist yet, it is created. The path is |
---|
| 289 | normally ``accesscodes/imports`` below the datacenter storage |
---|
[7811] | 290 | path (see :data:`waeup.kofa.accesscodes.Datacenter.storage`). |
---|
[5132] | 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') |
---|
[6542] | 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) |
---|
[5132] | 300 | return import_path |
---|
| 301 | |
---|
[5079] | 302 | def addBatch(self, batch): |
---|
[6542] | 303 | """Add an already created `batch`. |
---|
[5086] | 304 | """ |
---|
| 305 | batch.num = self.getNum(batch.prefix) |
---|
| 306 | key = "%s-%s" % (batch.prefix, batch.num) |
---|
[5079] | 307 | self[key] = batch |
---|
[5086] | 308 | self._p_changed = True |
---|
[5079] | 309 | |
---|
[6417] | 310 | def createBatch(self, creation_date, creator, prefix, cost, |
---|
[5127] | 311 | entry_num): |
---|
| 312 | """Create and add a batch. |
---|
| 313 | """ |
---|
[6417] | 314 | batch_num = self.getNum(prefix) |
---|
| 315 | batch = AccessCodeBatch(creation_date, creator, prefix, |
---|
[5127] | 316 | cost, entry_num, num=batch_num) |
---|
| 317 | self.addBatch(batch) |
---|
| 318 | return batch |
---|
[6124] | 319 | |
---|
[5086] | 320 | def getNum(self, prefix): |
---|
| 321 | """Get next unused num for given prefix. |
---|
| 322 | """ |
---|
[6932] | 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 |
---|
[5116] | 330 | while self.get('%s-%s' % (prefix, num), None) is not None: |
---|
[5086] | 331 | num += 1 |
---|
| 332 | return num |
---|
[5095] | 333 | |
---|
[5132] | 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 |
---|
[6124] | 340 | |
---|
[6449] | 341 | # This is temporary reimport solution. Access codes will be imported |
---|
[6470] | 342 | # with state initialized no matter if they have been used before. |
---|
[5132] | 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() |
---|
[6449] | 352 | cost = float(entry['serial']) |
---|
[5132] | 353 | num = int(entry['ac']) |
---|
[6449] | 354 | prefix = entry['prefix'] |
---|
| 355 | batch_name = '%s-%s' % (prefix, num) |
---|
[5132] | 356 | if batch_name in self.keys(): |
---|
| 357 | raise KeyError('Batch already exists: %s' % batch_name) |
---|
| 358 | batch = AccessCodeBatch( |
---|
[8194] | 359 | datetime.utcnow(), creator, prefix, cost, 0, num=num) |
---|
[5132] | 360 | num_entries = 0 |
---|
[6417] | 361 | self[batch_name] = batch |
---|
[5132] | 362 | for row in reader: |
---|
| 363 | pin = row['ac'] |
---|
| 364 | serial = int(row['serial']) |
---|
| 365 | rand_num = pin.rsplit('-', 1)[-1] |
---|
| 366 | batch.addAccessCode(serial, rand_num) |
---|
| 367 | num_entries += 1 |
---|
| 368 | batch.entry_num = num_entries |
---|
[6417] | 369 | |
---|
[5132] | 370 | batch.createCSVLogFile() |
---|
| 371 | return |
---|
[5149] | 372 | |
---|
[5153] | 373 | def getAccessCode(self, ac_id): |
---|
| 374 | """Get the AccessCode with ID ``ac_id`` or ``KeyError``. |
---|
| 375 | """ |
---|
| 376 | for batchname in self.keys(): |
---|
| 377 | batch = self[batchname] |
---|
| 378 | try: |
---|
| 379 | return batch.getAccessCode(ac_id) |
---|
| 380 | except KeyError: |
---|
| 381 | continue |
---|
| 382 | return None |
---|
[6124] | 383 | |
---|
[6458] | 384 | def disable(self, ac_id, comment=None): |
---|
[5153] | 385 | """Disable the AC with ID ``ac_id``. |
---|
| 386 | |
---|
| 387 | ``user_id`` is the user ID of the user triggering the |
---|
| 388 | process. Already disabled ACs are left untouched. |
---|
| 389 | """ |
---|
| 390 | ac = self.getAccessCode(ac_id) |
---|
| 391 | if ac is None: |
---|
| 392 | return |
---|
[6458] | 393 | disable_accesscode(ac_id, comment) |
---|
[5153] | 394 | return |
---|
| 395 | |
---|
[6458] | 396 | def enable(self, ac_id, comment=None): |
---|
[5153] | 397 | """(Re-)enable the AC with ID ``ac_id``. |
---|
| 398 | |
---|
| 399 | This leaves the given AC in state ``unused``. Already enabled |
---|
| 400 | ACs are left untouched. |
---|
| 401 | """ |
---|
| 402 | ac = self.getAccessCode(ac_id) |
---|
| 403 | if ac is None: |
---|
| 404 | return |
---|
[6458] | 405 | reenable_accesscode(ac_id, comment) |
---|
[5153] | 406 | return |
---|
| 407 | |
---|
[7811] | 408 | logger_name = 'waeup.kofa.${sitename}.accesscodes' |
---|
[7747] | 409 | logger_filename = 'accesscodes.log' |
---|
| 410 | |
---|
| 411 | def logger_info(self, ob_class, comment=None): |
---|
| 412 | """Get the logger's info method. |
---|
| 413 | """ |
---|
| 414 | self.logger.info('%s - %s' % ( |
---|
| 415 | ob_class, comment)) |
---|
| 416 | return |
---|
| 417 | |
---|
[5073] | 418 | class AccessCodePlugin(grok.GlobalUtility): |
---|
| 419 | grok.name('accesscodes') |
---|
[7819] | 420 | grok.implements(IKofaPluggable) |
---|
[5073] | 421 | |
---|
| 422 | def setup(self, site, name, logger): |
---|
[6932] | 423 | basecontainer = AccessCodeBatchContainer() |
---|
| 424 | site['accesscodes'] = basecontainer |
---|
[5079] | 425 | logger.info('Installed container for access code batches.') |
---|
[6933] | 426 | # Create empty school fee, clearance and hostel application AC |
---|
| 427 | # batches during initialization of university instance. |
---|
[6932] | 428 | cost = 0.0 |
---|
| 429 | creator = 'system' |
---|
| 430 | entry_num = 0 |
---|
[8194] | 431 | creation_date = datetime.utcnow() |
---|
[6932] | 432 | basecontainer.createBatch(creation_date, creator, |
---|
[6933] | 433 | 'SFE', cost, entry_num) |
---|
[6932] | 434 | basecontainer.createBatch(creation_date, creator, |
---|
[6933] | 435 | 'CLR', cost, entry_num) |
---|
[6932] | 436 | basecontainer.createBatch(creation_date, creator, |
---|
[6933] | 437 | 'HOS', cost, entry_num) |
---|
| 438 | logger.info('Installed empty SFE, CLR and HOS access code batches.') |
---|
[5079] | 439 | return |
---|
[5073] | 440 | |
---|
| 441 | def update(self, site, name, logger): |
---|
[5107] | 442 | if not 'accesscodes' in site.keys(): |
---|
| 443 | logger.info('Updating site at %s. Installing access codes.' % ( |
---|
| 444 | site,)) |
---|
| 445 | self.setup(site, name, logger) |
---|
| 446 | else: |
---|
| 447 | logger.info( |
---|
| 448 | 'AccessCodePlugin: Updating site at %s: Nothing to do.' % ( |
---|
| 449 | site, )) |
---|
| 450 | return |
---|
[5447] | 451 | |
---|
| 452 | def get_access_code(access_code): |
---|
| 453 | """Get an access code instance. |
---|
| 454 | |
---|
| 455 | An access code here is a string like ``PUDE-1-1234567890``. |
---|
[6124] | 456 | |
---|
[5447] | 457 | Returns ``None`` if the given code cannot be found. |
---|
| 458 | |
---|
| 459 | This is a convenicence function that is faster than looking up a |
---|
| 460 | batch container for the approriate access code. |
---|
| 461 | """ |
---|
| 462 | site = grok.getSite() |
---|
| 463 | if not isinstance(access_code, basestring): |
---|
| 464 | return None |
---|
| 465 | try: |
---|
| 466 | batch_id, ac_id = access_code.rsplit('-', 1) |
---|
| 467 | except: |
---|
| 468 | return None |
---|
[5467] | 469 | batch = site['accesscodes'].get(batch_id, None) |
---|
| 470 | if batch is None: |
---|
[5447] | 471 | return None |
---|
| 472 | try: |
---|
| 473 | code = batch.getAccessCode(access_code) |
---|
| 474 | except KeyError: |
---|
| 475 | return None |
---|
| 476 | return code |
---|
[6359] | 477 | |
---|
[6927] | 478 | def fire_transition(access_code, arg, toward=False, comment=None, owner=None): |
---|
[6408] | 479 | """Fire workflow transition for access code. |
---|
| 480 | |
---|
| 481 | The access code instance is looked up via `access_code` (a string |
---|
| 482 | like ``APP-1-12345678``). |
---|
| 483 | |
---|
| 484 | `arg` tells what kind of transition to trigger. This will be a |
---|
| 485 | transition id like ``'use'`` or ``'init'``, or some transition |
---|
[7811] | 486 | target like :data:`waeup.kofa.accesscodes.workflow.INITIALIZED`. |
---|
[6408] | 487 | |
---|
| 488 | If `toward` is ``False`` (the default) you have to pass a |
---|
| 489 | transition id as `arg`, otherwise you must give a transition |
---|
| 490 | target. |
---|
| 491 | |
---|
[6420] | 492 | If `comment` is specified (default is ``None``) the given string |
---|
| 493 | will be passed along as transition comment. It will appear in the |
---|
| 494 | history of the changed access code. You can use this to place |
---|
| 495 | remarks like for which object the access code was used or similar. |
---|
| 496 | |
---|
[6927] | 497 | If `owner` is specified, the owner attribute of the access code is checked. |
---|
| 498 | If the owner is different :func:`fire_transition` fails and returns False. |
---|
| 499 | |
---|
[6408] | 500 | :func:`fire_transition` might raise exceptions depending on the |
---|
| 501 | reason why the requested transition cannot be performed. |
---|
| 502 | |
---|
| 503 | The following exceptions can occur during processing: |
---|
| 504 | |
---|
| 505 | :exc:`KeyError`: |
---|
| 506 | signals not existent access code, batch or site. |
---|
| 507 | |
---|
| 508 | :exc:`ValueError`: |
---|
| 509 | signals illegal format of `access_code` string. The regular format is |
---|
| 510 | ``APP-N-XXXXXXXX``. |
---|
| 511 | |
---|
| 512 | :exc:`hurry.workflow.interfaces.InvalidTransitionError`: |
---|
| 513 | the transition requested cannot be performed because the workflow |
---|
| 514 | rules forbid it. |
---|
| 515 | |
---|
| 516 | :exc:`Unauthorized`: |
---|
| 517 | the current user is not allowed to perform the requested transition. |
---|
| 518 | |
---|
| 519 | """ |
---|
[6374] | 520 | try: |
---|
[6408] | 521 | batch_id, ac_id = access_code.rsplit('-', 1) |
---|
| 522 | except ValueError: |
---|
[6588] | 523 | raise ValueError( |
---|
| 524 | 'Invalid access code format: %s (use: APP-N-XXXXXXXX)' % ( |
---|
| 525 | access_code,)) |
---|
[6408] | 526 | try: |
---|
| 527 | ac = grok.getSite()['accesscodes'][batch_id].getAccessCode(access_code) |
---|
[6588] | 528 | except TypeError: |
---|
| 529 | raise KeyError( |
---|
| 530 | 'No site available for looking up accesscodes') |
---|
[6927] | 531 | if owner: |
---|
| 532 | ac_owner = getattr(ac, 'owner', None) |
---|
| 533 | if ac_owner and ac_owner != owner: |
---|
| 534 | return False |
---|
[6408] | 535 | info = IWorkflowInfo(ac) |
---|
| 536 | if toward: |
---|
[6420] | 537 | info.fireTransitionToward(arg, comment=comment) |
---|
[6408] | 538 | else: |
---|
[6420] | 539 | info.fireTransition(arg, comment=comment) |
---|
[6374] | 540 | return True |
---|
| 541 | |
---|
[6927] | 542 | def invalidate_accesscode(access_code, comment=None, owner=None): |
---|
[6374] | 543 | """Invalidate AccessCode denoted by string ``access_code``. |
---|
| 544 | |
---|
| 545 | Fires an appropriate transition to perform the task. |
---|
| 546 | |
---|
[6420] | 547 | `comment` is a string that will appear in the access code |
---|
| 548 | history. |
---|
| 549 | |
---|
[6408] | 550 | See :func:`fire_transition` for possible exceptions and their |
---|
| 551 | meanings. |
---|
[6374] | 552 | """ |
---|
[6588] | 553 | try: |
---|
[6927] | 554 | return fire_transition(access_code, 'use', comment=comment, owner=owner) |
---|
[6588] | 555 | except: |
---|
| 556 | return False |
---|
[6359] | 557 | |
---|
[6420] | 558 | def disable_accesscode(access_code, comment=None): |
---|
[6374] | 559 | """Disable AccessCode denoted by string ``access_code``. |
---|
| 560 | |
---|
| 561 | Fires an appropriate transition to perform the task. |
---|
| 562 | |
---|
[6420] | 563 | `comment` is a string that will appear in the access code |
---|
| 564 | history. |
---|
| 565 | |
---|
[6408] | 566 | See :func:`fire_transition` for possible exceptions and their |
---|
| 567 | meanings. |
---|
[6374] | 568 | """ |
---|
[6420] | 569 | return fire_transition( |
---|
| 570 | access_code, DISABLED, toward=True, comment=comment) |
---|
[6359] | 571 | |
---|
[6420] | 572 | def reenable_accesscode(access_code, comment=None): |
---|
[6374] | 573 | """Reenable AccessCode denoted by string ``access_code``. |
---|
| 574 | |
---|
| 575 | Fires an appropriate transition to perform the task. |
---|
| 576 | |
---|
[6420] | 577 | `comment` is a string that will appear in the access code |
---|
| 578 | history. |
---|
| 579 | |
---|
[6408] | 580 | See :func:`fire_transition` for possible exceptions and their |
---|
| 581 | meanings. |
---|
[6374] | 582 | """ |
---|
[6420] | 583 | return fire_transition(access_code, 'reenable', comment=comment) |
---|
[6936] | 584 | |
---|
[6937] | 585 | def create_accesscode(batch_prefix, batch_num, owner): |
---|
[6936] | 586 | """ |
---|
| 587 | """ |
---|
| 588 | batch_id = '%s-%s' % (batch_prefix, batch_num) |
---|
| 589 | try: |
---|
| 590 | batch = grok.getSite()['accesscodes'][batch_id] |
---|
| 591 | except KeyError: |
---|
[6939] | 592 | return None, u'No AC batch available.' |
---|
[6936] | 593 | rand_num = list(batch.getNewRandomNum())[0] |
---|
| 594 | num = len(batch) + 1 |
---|
| 595 | batch.addAccessCode(num, rand_num, owner) |
---|
[6945] | 596 | batch.entry_num += 1 |
---|
[6936] | 597 | pin = u'%s-%s-%s' % (batch_prefix,batch_num,rand_num) |
---|
[7811] | 598 | return pin, None |
---|