Ignore:
Timestamp:
16 Jun 2011, 15:10:06 (13 years ago)
Author:
uli
Message:

Make fire_transition signal problems via exceptions.

Add docs.

Shorten convenience functions to reduce repetitve code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscodes.py

    r6374 r6379  
    448448    return code
    449449
    450 def _fire_transition(info, arg, toward=False):
     450def fire_transition(access_code, arg, toward=False):
     451    """Fire workflow transition for access code.
     452
     453    The access code instance is looked up via `access_code` (a string
     454    like ``APP-1-12345678``).
     455
     456    `arg` tells what kind of transition to trigger. This will be a
     457    transition id like ``'use'`` or ``'init'``, or some transition
     458    target like :var:`waeup.sirp.accesscodes.workflow.INITIALIZED`.
     459
     460    If `toward` is ``False`` (the default) you have to pass a
     461    transition id as `arg`, otherwise you must give a transition
     462    target.
     463
     464    :func:`fire_transition` might raise exceptions depending on the
     465    reason why the requested transition cannot be performed.
     466
     467    The following exceptions can occur during processing:
     468
     469    :exc:`KeyError`:
     470      signals not existent access code, batch or site.
     471
     472    :exc:`ValueError`:
     473      signals illegal format of `access_code` string. The regular format is
     474      ``APP-N-XXXXXXXX``.
     475
     476    :exc:`hurry.workflow.interfaces.InvalidTransitionError`:
     477      the transition requested cannot be performed because the workflow
     478      rules forbid it.
     479
     480    :exc:`Unauthorized`:
     481      the current user is not allowed to perform the requested transition.
     482
     483    """
    451484    try:
    452         if toward:
    453             info.fireTransitionToward(arg)
    454         else:
    455             info.fireTransition(arg)
    456     except InvalidTransitionError:
    457         return False
     485        batch_id, ac_id = access_code.rsplit('-', 1)
     486    except ValueError:
     487        raise ValueError(
     488            'Invalid access code format: %s (use: APP-N-XXXXXXXX)' % (
     489                access_code,))
     490    try:
     491        ac = grok.getSite()['accesscodes'][batch_id].getAccessCode(access_code)
     492    except TypeError:
     493        raise KeyError(
     494            'No site available for looking up accesscodes')
     495    info = IWorkflowInfo(ac)
     496    if toward:
     497        info.fireTransitionToward(arg)
     498    else:
     499        info.fireTransition(arg)
    458500    return True
    459501
     
    461503    """Invalidate AccessCode denoted by string ``access_code``.
    462504
    463     The access code that belongs to the passed string must exist.
    464 
    465505    Fires an appropriate transition to perform the task.
    466506
    467     Returns ``True`` if the ac was invalidated, ``False`` else.
     507    See :func:`fire_transition` for possible exceptions and their
     508    meanings.
    468509    """
    469     ac = get_access_code(access_code)
    470     info = IWorkflowInfo(ac)
    471     return _fire_transition(info, 'use')
     510    return fire_transition(access_code, 'use')
    472511
    473512def disable_accesscode(access_code):
    474513    """Disable AccessCode denoted by string ``access_code``.
    475514
    476     The access code that belongs to the passed string must exist.
    477 
    478515    Fires an appropriate transition to perform the task.
    479516
    480     Returns ``True`` if the ac was disabled, ``False`` else.
     517    See :func:`fire_transition` for possible exceptions and their
     518    meanings.
    481519    """
    482     ac = get_access_code(access_code)
    483     info = IWorkflowInfo(ac)
    484     return _fire_transition(info, DISABLED, toward=True)
     520    return fire_transition(access_code, DISABLED, toward=True)
    485521
    486522def reenable_accesscode(access_code):
    487523    """Reenable AccessCode denoted by string ``access_code``.
    488524
    489     The access code that belongs to the passed string must exist.
    490 
    491525    Fires an appropriate transition to perform the task.
    492526
    493     Returns ``True`` if the ac was reenabled, ``False`` else.
     527    See :func:`fire_transition` for possible exceptions and their
     528    meanings.
    494529    """
    495     ac = get_access_code(access_code)
    496     info = IWorkflowInfo(ac)
    497     return _fire_transition(info, 'reenable')
     530    return fire_transition(access_code, 'reenable')
Note: See TracChangeset for help on using the changeset viewer.