[5068] | 1 | """Components to handle access codes. |
---|
| 2 | """ |
---|
[5110] | 3 | import csv |
---|
[5068] | 4 | import grok |
---|
[5110] | 5 | import os |
---|
[5118] | 6 | from datetime import datetime |
---|
[6432] | 7 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
[5068] | 8 | from random import SystemRandom as random |
---|
[6423] | 9 | from waeup.sirp.interfaces import IWAeUPSIRPPluggable, IObjectHistory |
---|
[5079] | 10 | from waeup.sirp.accesscodes.interfaces import ( |
---|
| 11 | IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer |
---|
| 12 | ) |
---|
[6413] | 13 | from waeup.sirp.accesscodes.workflow import DISABLED, USED |
---|
[5068] | 14 | |
---|
[5102] | 15 | class ManageACBatches(grok.Permission): |
---|
| 16 | grok.name('waeup.manageACBatches') |
---|
| 17 | |
---|
[6359] | 18 | class AccessCode(grok.Model): |
---|
[5068] | 19 | grok.implements(IAccessCode) |
---|
| 20 | |
---|
[6386] | 21 | def __init__(self, batch_serial, random_num): |
---|
[5068] | 22 | self.batch_serial = batch_serial |
---|
| 23 | self.random_num = random_num |
---|
[6359] | 24 | IWorkflowInfo(self).fireTransition('init') |
---|
[5068] | 25 | |
---|
| 26 | @property |
---|
| 27 | def representation(self): |
---|
| 28 | return '%s-%s-%s' % ( |
---|
| 29 | self.batch_prefix, self.batch_num, self.random_num) |
---|
| 30 | |
---|
[5079] | 31 | @property |
---|
| 32 | def batch(self): |
---|
| 33 | return getattr(self, '__parent__', None) |
---|
[5086] | 34 | |
---|
[5079] | 35 | @property |
---|
| 36 | def batch_prefix(self): |
---|
| 37 | if self.batch is None: |
---|
| 38 | return '' |
---|
| 39 | return self.batch.prefix |
---|
[5086] | 40 | |
---|
[5079] | 41 | @property |
---|
| 42 | def batch_num(self): |
---|
| 43 | if self.batch is None: |
---|
| 44 | return '' |
---|
| 45 | return self.batch.num |
---|
[5068] | 46 | |
---|
[5118] | 47 | @property |
---|
| 48 | def cost(self): |
---|
| 49 | if self.batch is None: |
---|
| 50 | return None |
---|
| 51 | return self.batch.cost |
---|
| 52 | |
---|
[6413] | 53 | @property |
---|
[6450] | 54 | def status(self): |
---|
| 55 | return IWorkflowState(self).getState() |
---|
| 56 | |
---|
| 57 | @property |
---|
[6423] | 58 | def history(self): |
---|
| 59 | history = IObjectHistory(self) |
---|
[6453] | 60 | return '||'.join(history.messages) |
---|
[6423] | 61 | |
---|
[6417] | 62 | class AccessCodeBatch(grok.Container): |
---|
[5068] | 63 | """A batch of access codes. |
---|
| 64 | """ |
---|
| 65 | grok.implements(IAccessCodeBatch) |
---|
| 66 | |
---|
[5086] | 67 | def __init__(self, creation_date, creator, batch_prefix, cost, |
---|
| 68 | entry_num, num=None): |
---|
[5079] | 69 | super(AccessCodeBatch, self).__init__() |
---|
[5068] | 70 | self.creation_date = creation_date |
---|
| 71 | self.creator = creator |
---|
[5116] | 72 | self.prefix = batch_prefix.upper() |
---|
[5068] | 73 | self.cost = cost |
---|
| 74 | self.entry_num = entry_num |
---|
[5086] | 75 | self.num = num |
---|
[6425] | 76 | self.used_num = 0 |
---|
[5149] | 77 | self.disabled_num = 0 |
---|
[5079] | 78 | |
---|
[5118] | 79 | def _createEntries(self): |
---|
[5079] | 80 | """Create the entries for this batch. |
---|
| 81 | """ |
---|
[5118] | 82 | for num, pin in enumerate(self._getNewRandomNum(num=self.entry_num)): |
---|
[5121] | 83 | self.addAccessCode(num, pin) |
---|
[5112] | 84 | return |
---|
[5118] | 85 | |
---|
| 86 | def _getNewRandomNum(self, num=1): |
---|
[5112] | 87 | """Create a set of ``num`` random numbers of 10 digits each. |
---|
[5086] | 88 | |
---|
[5068] | 89 | The number is returned as string. |
---|
| 90 | """ |
---|
[5118] | 91 | curr = 1 |
---|
| 92 | while curr <= num: |
---|
[5112] | 93 | pin = '' |
---|
[5068] | 94 | for x in range(10): |
---|
[5112] | 95 | pin += str(random().randint(0, 9)) |
---|
[6417] | 96 | if not '%s-%s-%s' % (self.prefix, self.num, pin) in self.keys(): |
---|
[5127] | 97 | curr += 1 |
---|
| 98 | yield pin |
---|
| 99 | # PIN already in use |
---|
[5073] | 100 | |
---|
[5118] | 101 | def _getStoragePath(self): |
---|
| 102 | """Get the directory, where we store all batch-related CSV files. |
---|
| 103 | """ |
---|
| 104 | site = grok.getSite() |
---|
| 105 | storagepath = site['datacenter'].storage |
---|
| 106 | ac_storage = os.path.join(storagepath, 'accesscodes') |
---|
| 107 | if not os.path.exists(ac_storage): |
---|
| 108 | os.mkdir(ac_storage) |
---|
| 109 | return ac_storage |
---|
| 110 | |
---|
| 111 | def getAccessCode(self, ac_id): |
---|
| 112 | """Get the AccessCode with ID ``ac_id`` or ``KeyError``. |
---|
| 113 | """ |
---|
[6417] | 114 | return self[ac_id] |
---|
[5118] | 115 | |
---|
[5121] | 116 | def addAccessCode(self, num, pin): |
---|
| 117 | """Add an access-code. |
---|
| 118 | """ |
---|
| 119 | ac = AccessCode(num, pin) |
---|
| 120 | ac.__parent__ = self |
---|
[6417] | 121 | self[ac.representation] = ac |
---|
[5121] | 122 | return |
---|
| 123 | |
---|
[5110] | 124 | def createCSVLogFile(self): |
---|
| 125 | """Create a CSV file with data in batch. |
---|
| 126 | |
---|
| 127 | Data will not contain invalidation date nor student ids. File |
---|
| 128 | will be created in ``accesscodes`` subdir of data center |
---|
| 129 | storage path. |
---|
| 130 | |
---|
| 131 | Returns name of created file. |
---|
| 132 | """ |
---|
| 133 | date = self.creation_date.strftime('%Y_%m_%d_%H_%M_%S') |
---|
[5118] | 134 | ac_storage = self._getStoragePath() |
---|
[5110] | 135 | csv_path = os.path.join( |
---|
| 136 | ac_storage, '%s-%s-%s-%s.csv' % ( |
---|
| 137 | self.prefix, self.num, date, self.creator) |
---|
| 138 | ) |
---|
| 139 | writer = csv.writer(open(csv_path, 'w'), quoting=csv.QUOTE_ALL) |
---|
| 140 | writer.writerow(['serial', 'ac', 'cost']) |
---|
| 141 | writer.writerow([self.prefix, str(self.num), "%0.2f" % self.cost]) |
---|
[5118] | 142 | |
---|
[6424] | 143 | for value in sorted(self.values(), |
---|
| 144 | cmp=lambda x, y: cmp( |
---|
| 145 | x.batch_serial, y.batch_serial)): |
---|
[5110] | 146 | writer.writerow( |
---|
| 147 | [str(value.batch_serial), str(value.representation)] |
---|
| 148 | ) |
---|
[5118] | 149 | site = grok.getSite() |
---|
[5112] | 150 | logger = site.logger |
---|
| 151 | logger.info( |
---|
| 152 | "Created batch %s-%s" % (self.prefix, self.num)) |
---|
| 153 | logger.info( |
---|
| 154 | "Written batch CSV to %s" % csv_path) |
---|
[5110] | 155 | return os.path.basename(csv_path) |
---|
| 156 | |
---|
[5118] | 157 | def archive(self): |
---|
| 158 | """Create a CSV file for archive. |
---|
| 159 | """ |
---|
| 160 | ac_storage = self._getStoragePath() |
---|
| 161 | now = datetime.now() |
---|
| 162 | timestamp = now.strftime('%Y_%m_%d_%H_%M_%S') |
---|
| 163 | csv_path = os.path.join( |
---|
| 164 | ac_storage, '%s-%s_archive-%s-%s.csv' % ( |
---|
| 165 | self.prefix, self.num, timestamp, self.creator) |
---|
| 166 | ) |
---|
| 167 | writer = csv.writer(open(csv_path, 'w'), quoting=csv.QUOTE_ALL) |
---|
[6452] | 168 | writer.writerow(['prefix', 'serial', 'ac', 'status', 'history']) |
---|
[5118] | 169 | writer.writerow([self.prefix, '%0.2f' % self.cost, str(self.num), |
---|
| 170 | str(self.entry_num)]) |
---|
[6424] | 171 | for value in sorted( |
---|
| 172 | self.values(), |
---|
| 173 | cmp = lambda x, y: cmp(x.batch_serial, y.batch_serial) |
---|
| 174 | ): |
---|
[5118] | 175 | writer.writerow([ |
---|
| 176 | self.prefix, value.batch_serial, value.representation, |
---|
[6452] | 177 | value.status, value.history |
---|
[5118] | 178 | ]) |
---|
| 179 | return os.path.basename(csv_path) |
---|
| 180 | |
---|
[5149] | 181 | def search(self, searchterm, searchtype): |
---|
| 182 | if searchtype == 'serial': |
---|
| 183 | if len(self._entries) < searchterm + 1: |
---|
| 184 | return [] |
---|
| 185 | return [self._entries[searchterm]] |
---|
| 186 | if searchtype == 'pin': |
---|
| 187 | try: |
---|
| 188 | entry = self.getAccessCode(searchterm) |
---|
| 189 | return [entry] |
---|
| 190 | except KeyError: |
---|
| 191 | return [] |
---|
| 192 | |
---|
[6417] | 193 | @grok.subscribe(IAccessCodeBatch, grok.IObjectAddedEvent) |
---|
[6418] | 194 | def handle_batch_added(batch, event): |
---|
[6417] | 195 | # A (maybe dirty?) workaround to make batch containers work |
---|
| 196 | # without self-maintained acids: as batches should contain their |
---|
| 197 | # set of data immediately after creation, but we cannot add |
---|
| 198 | # subobjects as long as the batch was not added already to the |
---|
| 199 | # ZODB, we trigger the item creation for the time after the batch |
---|
| 200 | # was added to the ZODB. |
---|
| 201 | batch._createEntries() |
---|
| 202 | return |
---|
| 203 | |
---|
| 204 | |
---|
[5079] | 205 | class AccessCodeBatchContainer(grok.Container): |
---|
| 206 | grok.implements(IAccessCodeBatchContainer) |
---|
[5073] | 207 | |
---|
[5132] | 208 | def _getStoragePath(self): |
---|
| 209 | """Get the directory, where batch import files are stored. |
---|
| 210 | """ |
---|
| 211 | site = grok.getSite() |
---|
| 212 | storagepath = site['datacenter'].storage |
---|
| 213 | ac_storage = os.path.join(storagepath, 'accesscodes') |
---|
| 214 | import_path = os.path.join(ac_storage, 'imports') |
---|
| 215 | if not os.path.exists(import_path): |
---|
| 216 | os.mkdir(import_path) |
---|
| 217 | return import_path |
---|
| 218 | |
---|
[5079] | 219 | def addBatch(self, batch): |
---|
[5086] | 220 | """Add a batch. |
---|
| 221 | """ |
---|
| 222 | batch.num = self.getNum(batch.prefix) |
---|
| 223 | key = "%s-%s" % (batch.prefix, batch.num) |
---|
[5079] | 224 | self[key] = batch |
---|
[5086] | 225 | self._p_changed = True |
---|
[5079] | 226 | |
---|
[6417] | 227 | def createBatch(self, creation_date, creator, prefix, cost, |
---|
[5127] | 228 | entry_num): |
---|
| 229 | """Create and add a batch. |
---|
| 230 | """ |
---|
[6417] | 231 | batch_num = self.getNum(prefix) |
---|
| 232 | batch = AccessCodeBatch(creation_date, creator, prefix, |
---|
[5127] | 233 | cost, entry_num, num=batch_num) |
---|
| 234 | self.addBatch(batch) |
---|
| 235 | return batch |
---|
[6124] | 236 | |
---|
[5086] | 237 | def getNum(self, prefix): |
---|
| 238 | """Get next unused num for given prefix. |
---|
| 239 | """ |
---|
| 240 | num = 1 |
---|
[5116] | 241 | while self.get('%s-%s' % (prefix, num), None) is not None: |
---|
[5086] | 242 | num += 1 |
---|
| 243 | return num |
---|
[5095] | 244 | |
---|
[5132] | 245 | def getImportFiles(self): |
---|
| 246 | """Return a generator with basenames of available import files. |
---|
| 247 | """ |
---|
| 248 | path = self._getStoragePath() |
---|
| 249 | for filename in sorted(os.listdir(path)): |
---|
| 250 | yield filename |
---|
[6124] | 251 | |
---|
[6449] | 252 | # This is temporary reimport solution. Access codes will be imported |
---|
| 253 | # with status initialized no matter if they have been used before. |
---|
[5132] | 254 | def reimport(self, filename, creator=u'UNKNOWN'): |
---|
| 255 | """Reimport a batch given in CSV file. |
---|
| 256 | |
---|
| 257 | CSV file must be of format as generated by createCSVLogFile |
---|
| 258 | method. |
---|
| 259 | """ |
---|
| 260 | path = os.path.join(self._getStoragePath(), filename) |
---|
| 261 | reader = csv.DictReader(open(path, 'rb'), quoting=csv.QUOTE_ALL) |
---|
| 262 | entry = reader.next() |
---|
[6449] | 263 | cost = float(entry['serial']) |
---|
[5132] | 264 | num = int(entry['ac']) |
---|
[6449] | 265 | prefix = entry['prefix'] |
---|
| 266 | batch_name = '%s-%s' % (prefix, num) |
---|
[5132] | 267 | if batch_name in self.keys(): |
---|
| 268 | raise KeyError('Batch already exists: %s' % batch_name) |
---|
| 269 | batch = AccessCodeBatch( |
---|
[6449] | 270 | datetime.now(), creator, prefix, cost, 0, num=num) |
---|
[5132] | 271 | num_entries = 0 |
---|
[6417] | 272 | self[batch_name] = batch |
---|
[5132] | 273 | for row in reader: |
---|
| 274 | pin = row['ac'] |
---|
| 275 | serial = int(row['serial']) |
---|
| 276 | rand_num = pin.rsplit('-', 1)[-1] |
---|
| 277 | batch.addAccessCode(serial, rand_num) |
---|
| 278 | num_entries += 1 |
---|
| 279 | batch.entry_num = num_entries |
---|
[6417] | 280 | |
---|
[5132] | 281 | batch.createCSVLogFile() |
---|
| 282 | return |
---|
[5149] | 283 | |
---|
[5153] | 284 | def getAccessCode(self, ac_id): |
---|
| 285 | """Get the AccessCode with ID ``ac_id`` or ``KeyError``. |
---|
| 286 | """ |
---|
| 287 | for batchname in self.keys(): |
---|
| 288 | batch = self[batchname] |
---|
| 289 | try: |
---|
| 290 | return batch.getAccessCode(ac_id) |
---|
| 291 | except KeyError: |
---|
| 292 | continue |
---|
| 293 | return None |
---|
[6124] | 294 | |
---|
[6458] | 295 | def disable(self, ac_id, comment=None): |
---|
[5153] | 296 | """Disable the AC with ID ``ac_id``. |
---|
| 297 | |
---|
| 298 | ``user_id`` is the user ID of the user triggering the |
---|
| 299 | process. Already disabled ACs are left untouched. |
---|
| 300 | """ |
---|
| 301 | ac = self.getAccessCode(ac_id) |
---|
| 302 | if ac is None: |
---|
| 303 | return |
---|
[6458] | 304 | disable_accesscode(ac_id, comment) |
---|
[5153] | 305 | return |
---|
| 306 | |
---|
[6458] | 307 | def enable(self, ac_id, comment=None): |
---|
[5153] | 308 | """(Re-)enable the AC with ID ``ac_id``. |
---|
| 309 | |
---|
| 310 | This leaves the given AC in state ``unused``. Already enabled |
---|
| 311 | ACs are left untouched. |
---|
| 312 | """ |
---|
| 313 | ac = self.getAccessCode(ac_id) |
---|
| 314 | if ac is None: |
---|
| 315 | return |
---|
[6458] | 316 | reenable_accesscode(ac_id, comment) |
---|
[5153] | 317 | return |
---|
| 318 | |
---|
[5073] | 319 | class AccessCodePlugin(grok.GlobalUtility): |
---|
| 320 | grok.name('accesscodes') |
---|
| 321 | grok.implements(IWAeUPSIRPPluggable) |
---|
| 322 | |
---|
| 323 | def setup(self, site, name, logger): |
---|
[5079] | 324 | site['accesscodes'] = AccessCodeBatchContainer() |
---|
| 325 | logger.info('Installed container for access code batches.') |
---|
| 326 | return |
---|
[5073] | 327 | |
---|
| 328 | def update(self, site, name, logger): |
---|
[5107] | 329 | if not 'accesscodes' in site.keys(): |
---|
| 330 | logger.info('Updating site at %s. Installing access codes.' % ( |
---|
| 331 | site,)) |
---|
| 332 | self.setup(site, name, logger) |
---|
| 333 | else: |
---|
| 334 | logger.info( |
---|
| 335 | 'AccessCodePlugin: Updating site at %s: Nothing to do.' % ( |
---|
| 336 | site, )) |
---|
| 337 | return |
---|
[5447] | 338 | |
---|
| 339 | def get_access_code(access_code): |
---|
| 340 | """Get an access code instance. |
---|
| 341 | |
---|
| 342 | An access code here is a string like ``PUDE-1-1234567890``. |
---|
[6124] | 343 | |
---|
[5447] | 344 | Returns ``None`` if the given code cannot be found. |
---|
| 345 | |
---|
| 346 | This is a convenicence function that is faster than looking up a |
---|
| 347 | batch container for the approriate access code. |
---|
| 348 | """ |
---|
| 349 | site = grok.getSite() |
---|
| 350 | if not isinstance(access_code, basestring): |
---|
| 351 | return None |
---|
| 352 | try: |
---|
| 353 | batch_id, ac_id = access_code.rsplit('-', 1) |
---|
| 354 | except: |
---|
| 355 | return None |
---|
[5467] | 356 | batch = site['accesscodes'].get(batch_id, None) |
---|
| 357 | if batch is None: |
---|
[5447] | 358 | return None |
---|
| 359 | try: |
---|
| 360 | code = batch.getAccessCode(access_code) |
---|
| 361 | except KeyError: |
---|
| 362 | return None |
---|
| 363 | return code |
---|
[6359] | 364 | |
---|
[6420] | 365 | def fire_transition(access_code, arg, toward=False, comment=None): |
---|
[6408] | 366 | """Fire workflow transition for access code. |
---|
| 367 | |
---|
| 368 | The access code instance is looked up via `access_code` (a string |
---|
| 369 | like ``APP-1-12345678``). |
---|
| 370 | |
---|
| 371 | `arg` tells what kind of transition to trigger. This will be a |
---|
| 372 | transition id like ``'use'`` or ``'init'``, or some transition |
---|
| 373 | target like :var:`waeup.sirp.accesscodes.workflow.INITIALIZED`. |
---|
| 374 | |
---|
| 375 | If `toward` is ``False`` (the default) you have to pass a |
---|
| 376 | transition id as `arg`, otherwise you must give a transition |
---|
| 377 | target. |
---|
| 378 | |
---|
[6420] | 379 | If `comment` is specified (default is ``None``) the given string |
---|
| 380 | will be passed along as transition comment. It will appear in the |
---|
| 381 | history of the changed access code. You can use this to place |
---|
| 382 | remarks like for which object the access code was used or similar. |
---|
| 383 | |
---|
[6408] | 384 | :func:`fire_transition` might raise exceptions depending on the |
---|
| 385 | reason why the requested transition cannot be performed. |
---|
| 386 | |
---|
| 387 | The following exceptions can occur during processing: |
---|
| 388 | |
---|
| 389 | :exc:`KeyError`: |
---|
| 390 | signals not existent access code, batch or site. |
---|
| 391 | |
---|
| 392 | :exc:`ValueError`: |
---|
| 393 | signals illegal format of `access_code` string. The regular format is |
---|
| 394 | ``APP-N-XXXXXXXX``. |
---|
| 395 | |
---|
| 396 | :exc:`hurry.workflow.interfaces.InvalidTransitionError`: |
---|
| 397 | the transition requested cannot be performed because the workflow |
---|
| 398 | rules forbid it. |
---|
| 399 | |
---|
| 400 | :exc:`Unauthorized`: |
---|
| 401 | the current user is not allowed to perform the requested transition. |
---|
| 402 | |
---|
| 403 | """ |
---|
[6374] | 404 | try: |
---|
[6408] | 405 | batch_id, ac_id = access_code.rsplit('-', 1) |
---|
| 406 | except ValueError: |
---|
| 407 | raise ValueError( |
---|
| 408 | 'Invalid access code format: %s (use: APP-N-XXXXXXXX)' % ( |
---|
| 409 | access_code,)) |
---|
| 410 | try: |
---|
| 411 | ac = grok.getSite()['accesscodes'][batch_id].getAccessCode(access_code) |
---|
| 412 | except TypeError: |
---|
| 413 | raise KeyError( |
---|
| 414 | 'No site available for looking up accesscodes') |
---|
| 415 | info = IWorkflowInfo(ac) |
---|
| 416 | if toward: |
---|
[6420] | 417 | info.fireTransitionToward(arg, comment=comment) |
---|
[6408] | 418 | else: |
---|
[6420] | 419 | info.fireTransition(arg, comment=comment) |
---|
[6374] | 420 | return True |
---|
| 421 | |
---|
[6420] | 422 | def invalidate_accesscode(access_code, comment=None): |
---|
[6374] | 423 | """Invalidate AccessCode denoted by string ``access_code``. |
---|
| 424 | |
---|
| 425 | Fires an appropriate transition to perform the task. |
---|
| 426 | |
---|
[6420] | 427 | `comment` is a string that will appear in the access code |
---|
| 428 | history. |
---|
| 429 | |
---|
[6408] | 430 | See :func:`fire_transition` for possible exceptions and their |
---|
| 431 | meanings. |
---|
[6374] | 432 | """ |
---|
[6420] | 433 | return fire_transition(access_code, 'use', comment=comment) |
---|
[6359] | 434 | |
---|
[6420] | 435 | def disable_accesscode(access_code, comment=None): |
---|
[6374] | 436 | """Disable AccessCode denoted by string ``access_code``. |
---|
| 437 | |
---|
| 438 | Fires an appropriate transition to perform the task. |
---|
| 439 | |
---|
[6420] | 440 | `comment` is a string that will appear in the access code |
---|
| 441 | history. |
---|
| 442 | |
---|
[6408] | 443 | See :func:`fire_transition` for possible exceptions and their |
---|
| 444 | meanings. |
---|
[6374] | 445 | """ |
---|
[6420] | 446 | return fire_transition( |
---|
| 447 | access_code, DISABLED, toward=True, comment=comment) |
---|
[6359] | 448 | |
---|
[6420] | 449 | def reenable_accesscode(access_code, comment=None): |
---|
[6374] | 450 | """Reenable AccessCode denoted by string ``access_code``. |
---|
| 451 | |
---|
| 452 | Fires an appropriate transition to perform the task. |
---|
| 453 | |
---|
[6420] | 454 | `comment` is a string that will appear in the access code |
---|
| 455 | history. |
---|
| 456 | |
---|
[6408] | 457 | See :func:`fire_transition` for possible exceptions and their |
---|
| 458 | meanings. |
---|
[6374] | 459 | """ |
---|
[6420] | 460 | return fire_transition(access_code, 'reenable', comment=comment) |
---|