Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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')
Ejemplo n.º 4
0
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')
Ejemplo n.º 5
0
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')
Ejemplo n.º 6
0
def get_pita():
    """
    Retrieves the Pita associated with the current account, if any.
    """

    # TODO: This should not be a POST endpoint. We can change that later
    # though.

    if not g.authorized:
        return access_denied()

    existing_pita = Pita.get_by_account(g.account.aid)
    if not existing_pita:
        return jsonify(status="ok", has_pita=False)

    pita_dict = vars(existing_pita)
    pita_dict['has_pita'] = True
    pita_dict['status'] = 'ok'
    return jsonify(pita_dict)