Changeset 6374 for main


Ignore:
Timestamp:
15 Jun 2011, 23:45:04 (14 years ago)
Author:
uli
Message:

Improve accesscode triggers.

Location:
main/waeup.sirp/trunk/src/waeup/sirp/accesscodes
Files:
3 edited

Legend:

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

    r6359 r6374  
    11# Make this a package.
    22from waeup.sirp.accesscodes.accesscodes import (
    3     get_access_code, invalidate_accesscode)
     3    get_access_code, invalidate_accesscode, disable_accesscode,
     4    reenable_accesscode)
    45
    56# Public API of this submodule
     
    78    get_access_code,
    89    invalidate_accesscode,
     10    disable_accesscode,
     11    reenable_accesscode,
    912    ]
    1013
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscodes.py

    r6359 r6374  
    66from BTrees.OIBTree import OIBTree
    77from datetime import datetime
    8 from hurry.workflow.interfaces import IWorkflowInfo
     8from hurry.workflow.interfaces import IWorkflowInfo, InvalidTransitionError
    99from random import SystemRandom as random
    1010from waeup.sirp.interfaces import IWAeUPSIRPPluggable
     
    1212    IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer
    1313    )
    14 from waeup.sirp.accesscodes.workflow import DISABLED
     14from waeup.sirp.accesscodes.workflow import DISABLED, INITIALIZED
    1515
    1616class ManageACBatches(grok.Permission):
     
    448448    return code
    449449
     450def _fire_transition(info, arg, toward=False):
     451    try:
     452        if toward:
     453            info.fireTransitionToward(arg)
     454        else:
     455            info.fireTransition(arg)
     456    except InvalidTransitionError:
     457        return False
     458    return True
     459
    450460def invalidate_accesscode(access_code):
     461    """Invalidate AccessCode denoted by string ``access_code``.
     462
     463    The access code that belongs to the passed string must exist.
     464
     465    Fires an appropriate transition to perform the task.
     466
     467    Returns ``True`` if the ac was invalidated, ``False`` else.
     468    """
    451469    ac = get_access_code(access_code)
    452470    info = IWorkflowInfo(ac)
    453     info.fireTransition('use')
    454     return
     471    return _fire_transition(info, 'use')
    455472
    456473def disable_accesscode(access_code):
     474    """Disable AccessCode denoted by string ``access_code``.
     475
     476    The access code that belongs to the passed string must exist.
     477
     478    Fires an appropriate transition to perform the task.
     479
     480    Returns ``True`` if the ac was disabled, ``False`` else.
     481    """
    457482    ac = get_access_code(access_code)
    458483    info = IWorkflowInfo(ac)
    459     info.fireTransitionToward(DISABLED)
    460     return
     484    return _fire_transition(info, DISABLED, toward=True)
    461485
    462486def reenable_accesscode(access_code):
     487    """Reenable AccessCode denoted by string ``access_code``.
     488
     489    The access code that belongs to the passed string must exist.
     490
     491    Fires an appropriate transition to perform the task.
     492
     493    Returns ``True`` if the ac was reenabled, ``False`` else.
     494    """
    463495    ac = get_access_code(access_code)
    464496    info = IWorkflowInfo(ac)
    465     info.fireTransition('reenable')
    466     return
     497    return _fire_transition(info, 'reenable')
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/tests/test_accesscodes.py

    r6359 r6374  
    2929from waeup.sirp.accesscodes.accesscodes import (
    3030    AccessCodeBatch, get_access_code, invalidate_accesscode,
    31     disable_accesscode)
     31    disable_accesscode, reenable_accesscode)
    3232
    3333class AccessCodeHelpersTests(FunctionalTestCase):
     
    8989    def test_invalidate_accesscode(self):
    9090        assert self.ac1._invalidation_date is None
     91        result = invalidate_accesscode('APP-1-11111111')
     92        assert self.ac1._invalidation_date is not None
     93        assert result is True
     94
     95    def test_invalidate_used_accesscode(self):
    9196        invalidate_accesscode('APP-1-11111111')
    92         assert self.ac1._invalidation_date is not None
     97        inv_date = self.ac1._invalidation_date
     98        result = invalidate_accesscode('APP-1-11111111')
     99        assert result is False
     100        assert self.ac1._invalidation_date == inv_date
     101
     102    def test_invalidate_disabled_accesscode(self):
     103        # disabled acs cannot be invalidated.
     104        disable_accesscode('APP-1-11111111')
     105        result = invalidate_accesscode('APP-1-11111111')
     106        assert result is False
     107        assert self.ac1._disabled is True
    93108
    94109    def test_disable_accesscode_unused(self):
     
    102117        disable_accesscode('APP-1-11111111')
    103118        assert self.ac1._disabled is True
     119
     120    def test_reenable_accesscode(self):
     121        disable_accesscode('APP-1-11111111')
     122        result = reenable_accesscode('APP-1-11111111')
     123        assert result is True
     124        assert self.ac1._disabled is False
     125
     126    def test_reenable_accesscode_enabled(self):
     127        result = reenable_accesscode('APP-1-11111111')
     128        assert result is False
     129        assert self.ac1._disabled is False
Note: See TracChangeset for help on using the changeset viewer.