def editperk(): '''edit perk from database''' try: payload = json.loads(request.data) perks.edit_perk(**payload) return jsonify({'status': 'perk edited'}) except (OSError, IOError) as excp: return jsonify({'error': excp.message})
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()