Пример #1
0
def save_posto():
    """
    Processa o POST para salvar um novo registro de posto
    """
    # Le o parametro api_key, enviado no POST
    api_key = request.forms.api_key
    if not api_key:
        return abort(code=401, text="Unauthorized. API Key is mandatory")

    # Busca um usuario pela chave da API passado no POST
    usuario = Usuario.get(Usuario.api_key == api_key)
    if not usuario:
        return abort(code=401, text="Unauthorized. Invalid API Key")

    # Le o parametro nome, enviado no POST
    nome = request.forms.name
    if not nome:
        return abort(code=400, text="Bad Request. Field \"name\" is required")

    # Le o parametro latitude, enviado no POST
    latitude = request.forms.latitude
    if not latitude:
        return abort(code=400,
                     text="Bad Request. Field \"latitude\" is required")

    # Le o parametro longitude, enviado no POST
    longitude = request.forms.longitude
    if not longitude:
        return abort(code=400,
                     text="Bad Request. Field \"longitude\" is required")

    posto = Posto(nome=nome, latitude=latitude, longitude=longitude)
    posto.save()

    last_id = get_last_id(table='posto')

    response.set_header('Content-Type', 'text/plain')
    response.status = 201
    response.body = "/postos/%d" % last_id

    return response
Пример #2
0
def save_user():
    """
    Processa o POST para salvar um novo registro de usuario
    """
    # Le o parametro nome, enviado no POST
    nome = request.forms.name
    if not nome:
        return abort(code=400, text="Bad Request. Field \"name\" is required")

    # Le o parametro latitude, enviado no POST
    email = request.forms.email
    if not email:
        return abort(code=400, text="Bad Request. Field \"email\" is required")
    #Verifica se consegue encontrar um outro usuario usando o email fornecido como parametro
    try:
        usuario = Usuario.get(Usuario.email == email)
        if usuario:
            return abort(
                code=409,
                text="Conflict. Email already in use by another account")
    except:
        # O email nao esta vinculado a nenhum usuario. Isso e bom!
        pass
    # Le o parametro longitude, enviado no POST
    password = request.forms.password
    if not password:
        return abort(code=400,
                     text="Bad Request. Field \"password\" is required")

    usuario = Usuario(nome=nome, email=email, senha=password)
    usuario.save()

    last_id = get_last_id(table='usuario')

    api_response = APIResponse(code=201, message="Created", data=usuario)
    response.set_header('Content-Type', 'application/json')
    response.status = 201
    response.body = api_response.to_json()

    return response
Пример #3
0
def save_posto():
    """
    Processa o POST para salvar um novo registro de posto
    """
    # Le o parametro api_key, enviado no POST
    api_key = request.forms.api_key
    if not api_key:
        return abort(code=401, text="Unauthorized. API Key is mandatory")

    # Busca um usuario pela chave da API passado no POST
    usuario = Usuario.get(Usuario.api_key == api_key)
    if not usuario:
        return abort(code=401, text="Unauthorized. Invalid API Key")

    # Le o parametro nome, enviado no POST
    nome = request.forms.name
    if not nome:
        return abort(code=400, text="Bad Request. Field \"name\" is required")

    # Le o parametro latitude, enviado no POST
    latitude = request.forms.latitude
    if not latitude:
        return abort(code=400, text="Bad Request. Field \"latitude\" is required")

    # Le o parametro longitude, enviado no POST
    longitude = request.forms.longitude
    if not longitude:
        return abort(code=400, text="Bad Request. Field \"longitude\" is required")

    posto = Posto(nome=nome, latitude=latitude, longitude=longitude)
    posto.save()

    last_id = get_last_id(table='posto')

    response.set_header('Content-Type', 'text/plain')
    response.status = 201
    response.body = "/postos/%d"%last_id

    return response
Пример #4
0
def save_user():
    """
    Processa o POST para salvar um novo registro de usuario
    """
    # Le o parametro nome, enviado no POST
    nome = request.forms.name
    if not nome:
        return abort(code=400, text="Bad Request. Field \"name\" is required")

    # Le o parametro latitude, enviado no POST
    email = request.forms.email
    if not email:
        return abort(code=400, text="Bad Request. Field \"email\" is required")
    #Verifica se consegue encontrar um outro usuario usando o email fornecido como parametro
    try:
        usuario = Usuario.get(Usuario.email == email)
        if usuario:
            return abort(code=409, text="Conflict. Email already in use by another account")
    except:
        # O email nao esta vinculado a nenhum usuario. Isso e bom!
        pass
    # Le o parametro longitude, enviado no POST
    password = request.forms.password
    if not password:
        return abort(code=400, text="Bad Request. Field \"password\" is required")

    usuario = Usuario(nome=nome, email=email, senha=password)
    usuario.save()

    last_id = get_last_id(table='usuario')

    api_response = APIResponse(code=201, message="Created", data=usuario)
    response.set_header('Content-Type', 'application/json')
    response.status = 201
    response.body = api_response.to_json()

    return response