Beispiel #1
0
def reportperks(id):
    '''report perk from database'''
    try:
        res = perks.report_perk(id)
        return jsonify(res)

    except (OSError, IOError) as excp:
        return jsonify({'error': excp.message})
Beispiel #2
0
def redeem():
    '''
    redeem specified perk(
    id (int) - perk id
    )
    '''
    try:
        payload = json.loads(request.data)
        pbody = perks.report_perk(payload['id'])
        perk = pbody['bodies'][0]
        id = perk['id']

        if id in user.custom_data['perks']:
            return jsonify({'error': 'perk already redeemed and active'})
        if user.custom_data['points'] < perk['cost']:
            return jsonify({'error': 'insufficient funds'})
        if utils.str_to_dt(utils.current_time()) > perk['expires']:
            return jsonify({'error': 'perk has expired'})

        user.custom_data['points'] -= perk['cost']
        user.custom_data['perks'][id] = {
            'name': perk['name'],
            'description': perk['description'],
            'cost': perk['cost'],
            'code': perk['code'],
            'expires': str(perk['expires']),
            'active': True
        }
        perks.edit_perk(**{'code': utils.gen_perk_code(), 'id': id})
        return jsonify({'status': 'perk redeemed successfully'})

    except (OSError, IOError) as excp:
        return jsonify({'error': excp.message})

    finally:
        user.custom_data.save()