def new_account(): """ Endpoint for creating new accounts when the app is installed. Returns the account id and the account's secret key. """ if not request.form.get('uuid'): return api_error('must provide a device uuid') uuid = request.form['uuid'].strip() name = request.form['name'].strip() if 'name' in request.form else None email = request.form['email'].strip() if 'email' in request.form else None phone = request.form['phone'].strip() if 'phone' in request.form else None if phone == '': phone = None if Account.uuid_used(uuid): return user_error('an account already exists for this device.') if phone and Account.phone_used(phone): return user_error('phone number already in use') if email and Account.email_used(email): return user_error('email already in use') new_account = Account.new(uuid, name, phone, email) if not new_account: return api_error('unable to create new account') ret = {'aid': new_account.aid, 'key': new_account.key} return jsonify(**ret)
def nearby_accounts(): """ Retrieves information about nearby accounts, including their pitas. """ if not g.authorized: return access_denied() if request.form.get('latitude') and request.form.get('longitude'): # The latitude and longitude parameters were provided, so # we should update the account's current location. g.account.update_location(request.form.get('latitude'), request.form.get('longitude'), None) if not g.account.loc or not g.account.loc_time: return api_error('there is no location for the current account') cur_time = datetime.datetime.now() if g.account.loc_time + LOCATION_CUTOFF_TIME < cur_time: # The most recent location we have for this account is # too old to use for finding nearby accounts. return api_error('the current account location is too stale for that') nearby_accounts = get_nearby_accounts() # TODO: Figure out the format in which we want to send back nearby # accounts and pitas. We need to be careful to not give too much # information. output = [] for acc in nearby_accounts: if acc['aid'] != g.account.aid: pita = Pita.get_by_account(acc['aid']) acc_output = dict() acc_output['aid'] = acc['aid'] # acc_output['dist'] = acc['dist_meters'] acc_output['proximity'] = 'close' if acc['dist_meters'] < 100: acc_output['proximity'] = 'very close' if pita: acc_output['pita_name'] = pita.name acc_output['pid'] = pita.pid output.append(acc_output) current_app.logger.debug(output) return jsonify(nearby_accounts=output)
def record_pita_hatch(): """ Endpoint used to record that a user has hatched their Pita. """ if not g.authorized: return access_denied() pita = Pita.get_by_account(g.account.aid) if not pita: return api_error('That account doesn\'t have a pita.') if pita.state != 'egg': return api_error('The pita is not in egg form.') pita.set_state('alive') PitaEvent.record_event(pita, 'born') return jsonify(status='ok')
def record_pita_disown(): """ Endpoint used to record that a Pita has been disowned. """ if not g.authorized: return access_denied() pita = Pita.get_by_account(g.account.aid) if not pita: return api_error('That account doesn\'t have a pita.') if pita.state != 'alive' and pita.state != 'egg': return api_error('The pita is not alive.') pita.set_state('disowned') PitaEvent.record_event(pita, 'disowned') return jsonify(status='ok')
def save_location(): """ Endpoint for recording a location data point for an account. """ if not g.authorized: return access_denied() if 'latitude' not in request.form or 'longitude' not in request.form: return api_error('latitude and longitude required') if request.form['latitude'] == '' or request.form['longitude'] == '': return api_error('latitude and longitude required') time = request.form['time'] if 'time' in request.form else None # If this is the most recent location for the account, update the # account's current location. if not time or not g.account.loc or time > g.account.loc_time: g.account.update_location(request.form['latitude'], request.form['longitude'], time) return jsonify(status='ok')
def random_pita(): """ An endpoint primarily used for testing. It creates a random pita and prints the serialized pita to the user. """ if not g.authorized: return access_denied() existing_pita = Pita.get_by_account(g.account.aid) if existing_pita: return api_error('That account already has a pita.') random_pita = Pita.create_random_pita(g.account.aid) return jsonify(vars(random_pita))