source: main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscode.py @ 7737

Last change on this file since 7737 was 7689, checked in by Henrik Bettermann, 13 years ago

Translate accesscode workflow.

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