def checkup(): '''earn two more points for staying on campus''' try: coords = json.loads(request.data) if utils.check_distance(**coords)['distance'] > .378788: # .378788 miles = 2000 feet return jsonify({'error':\ ''' You are not on campus. You will be checked out if you don't return in five minutes. '''}) if not user.custom_data['checked_in']: return jsonify({'error': 'you are not checked in'}) check_res = utils.check_points(user) if isinstance(check_res, dict): return jsonify(check_res) #if utils.check_wifi(): user.custom_data['last_point'] = utils.current_time() user.custom_data['points'] += 2 name = user.given_name return jsonify({'status': '%s just earned two more points' % name}) # else: # return jsonify({'error': 'not logged into school wifi'}) except (OSError, IOError) as excp: return jsonify({'error': excp.message}) finally: user.custom_data.save()
def checkin(): '''checkin to campus''' try: cust = user.custom_data coords = json.loads(request.data) if utils.check_distance(**coords)['distance'] > .378788: # .378788 miles = 2000 feet return jsonify({'error': 'you are not on campus'}) if cust['checked_in']: return jsonify({'error': 'you are already checked in'}) else: check_res = utils.check_points(user) if isinstance(check_res, dict): return jsonify(check_res) # if utils.check_wifi(): user.custom_data['checked_in'] = True user.custom_data['points'] += 10 user.custom_data['last_point'] = utils.current_time() return jsonify({'status': 'you are now checked into campus'}) # return jsonify({'error': 'not logged into school wifi'}) except (OSError, IOError) as excp: return jsonify({'error': excp.message}) finally: user.custom_data.save()
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()
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})
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()