예제 #1
0
def useperk():
    '''use perk, deactivating it for further use'''
    try:
        payload = json.loads(request.data)
        id = str(payload['id'])
        cust = user.custom_data
        if id not in cust['perks']:
            return jsonify({'error': 'you do not have this perk'})

        perk = cust['perks'][id]
        now = utils.str_to_dt(utils.current_time())
        exp = utils.str_to_dt(perk['expires'])
        if now > exp:
            perk['active'] = False
            return jsonify({'error': 'your perk has expired.'})

        name = user.full_name
        pname = perk['name']
        if not perk['active']:
            return jsonify({'error': 'this perk has been used or expired'})

        user.custom_data['perks'][id]['active'] = False
        return jsonify({'status': '%s just used %s' % (name, pname)})

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

    finally:
        user.custom_data.save()
예제 #2
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})
예제 #3
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()