Пример #1
0
def test_patron_types_subscription(patron_type_youngsters_sion,
                                   patron_type_adults_martigny,
                                   patron_type_grown_sion, patron_sion):
    """Test subscription behavior for patron_types."""
    assert patron_type_youngsters_sion.is_subscription_required

    # A patron_type with a subscription amount equal to 0 doesn't
    # require a subscription
    patron_type_youngsters_sion['subscription_amount'] = 0
    assert not patron_type_youngsters_sion.is_subscription_required

    del (patron_type_youngsters_sion['subscription_amount'])
    assert not patron_type_youngsters_sion.is_subscription_required

    # Test the 'get_yearly_subscription_patron_types' function.
    assert len(list(PatronType.get_yearly_subscription_patron_types())) == 2

    # Test 'get_linked_patrons' functions.
    assert len(list(patron_type_grown_sion.get_linked_patron())) == 1
    assert len(list(patron_type_youngsters_sion.get_linked_patron())) == 0
    patron_sion['patron']['type']['$ref'] = get_ref_for_pid(
        'ptty', patron_type_youngsters_sion.pid)
    patron_sion.update(patron_sion, dbcommit=True)
    patron_sion.reindex()
    assert len(list(patron_type_grown_sion.get_linked_patron())) == 0
    assert len(list(patron_type_youngsters_sion.get_linked_patron())) == 1
    patron_sion['patron']['type']['$ref'] = get_ref_for_pid(
        'ptty', patron_type_grown_sion.pid)
    patron_sion.update(patron_sion, dbcommit=True)
    patron_sion.reindex()
Пример #2
0
def check_patron_types_and_add_subscriptions():
    """Check patron types with subscription amount and add subscriptions.

    Search for patron_type requiring a subscription. For each patron_type
    search about patron linked to it and without valid subscription. For
    each of these patrons, create a new subscription if needed.
    """
    # Note this function should never doing anything because never any patron
    # linked to these patron types shouldn't have no subscription. This is
    # because, a listener creating an active subscription is linked to signal
    # create/update for any patron. In addition, the method
    # `clean_obsolete_subscriptions`, at the last loop instruction will update
    # the patron ; this update will trigger the same listener and so create a
    # new active subscription is necessary.
    for ptty in PatronType.get_yearly_subscription_patron_types():
        patron_no_subsc = Patron.get_patrons_without_subscription(ptty.pid)
        for patron in patron_no_subsc:
            msg = 'Add a subscription for patron#{ptrn} ... it shouldn\'t ' \
                  'happen !!'.format(ptrn=patron.pid)
            current_app.logger.error(msg)
            start_date = datetime.now()
            end_date = add_years(start_date, 1)
            patron.add_subscription(ptty, start_date, end_date)