示例#1
0
def postperk():
    '''post perk to database'''
    try:
        payload = json.loads(request.data)
        dtl = utils.int_to_days(payload.pop('dtl'))
        now = utils.str_to_dt(utils.current_time())
        payload['expires'] = now + dtl
        payload['code'] = utils.gen_perk_code()
        perks.post_perk(**payload)
        return jsonify({'status': 'perk posted'})
    except (OSError, IOError) as excp:
        return jsonify({'error': excp.message})
示例#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()