Ejemplo n.º 1
0
    def get(self, **kwargs):
        token_information = kwargs.get("token_information")
        if token_information is None:
            return json_response(
                status_code=500,
                message="Token information couldn't be verified",
                path=request.full_path,
                method=request.method,
            )

        username = token_information.get("username")
        payload = {
            "username": username,
            "contacts": db.child("users")
            .child(username)
            .child("contacts")
            .get()
            .val(),
        }

        return json_response(
            status_code=200,
            message="",
            path=request.full_path,
            method=request.method,
            payload=payload,
        )
Ejemplo n.º 2
0
    def delete(self, **kwargs):
        body = request.get_json()
        if body is None:
            return json_response(status_code=400,
                                 message="You must provide a json body")

        cpf = body.get("cpf", None)

        if cpf is None:
            return json_response(status_code=400,
                                 message="Field 'cpf' must not be empty")

        produtor = ProdutorRural.query.filter_by(cpf=cpf).first()
        if not produtor:
            return json_response(
                status_code=404,
                message=f"Produtor with cpf {cpf} was not found",
            )

        try:
            db.session.delete(produtor)
            db.session.commit()
        except Exception:
            return json_response(status_code=500, message="Could not delete")
        return json_response(200)
Ejemplo n.º 3
0
    def post(self):
        """Tries to get user's access token"""
        body = request.get_json()
        if body is None:
            return json_response(status_code=400,
                                 message="A JSON body must be provided")

        username = body.get("username", None)
        password = body.get("password", None)

        if username is None:
            return json_response(status_code=400,
                                 message="Field 'username' must not be empty")
        if password is None:
            return json_response(status_code=400,
                                 message="Field 'password' must not be empty")

        try:
            token = generate_token(username=username, password=password)
        except (InvalidUserError, IncorrectPasswordError):
            return json_response(
                # ! Don't say to hackers if it is the username that doesn't
                # ! exists or if the password is incorrect.
                status_code=400,
                message="Invalid username or password",
            )
        else:
            return json_response(
                message="Token successful generated",
                payload={"access_token": token},
            )
Ejemplo n.º 4
0
    def inner(*args, **kwargs):
        token = request.args.get("access_token", None)
        if not token:
            body = request.get_json()
            if body is not None:
                token = body.get("access_token", None)
        if not token:
            return json_response(
                status_code=401,
                message="An access_token parameter must be provided",
            )

        try:
            token_information = jwt.decode(
                token, getenv("SECRET_KEY"), algorithms=["HS256"]
            )
        except jwt.ExpiredSignatureError:
            return json_response(
                status_code=401, message="Expired access_token"
            )
        except jwt.InvalidTokenError:
            return json_response(
                status_code=403, message="Invalid access_token"
            )
        except Exception:
            return json_response(
                status_code=500, message="Error processing access_token"
            )

        return func(*args, **kwargs, token_information=token_information)
Ejemplo n.º 5
0
def log_responses(query):
    if request.method == 'POST':
        jdata = request.get_json(force=True)
        store_values(query, jdata)
        rsp = utils.json_response({'hello': 'world'})
        return rsp
    elif request.method == 'GET':
        data = queries.get_config(query)
        rsp = utils.json_response(data)
        return rsp
    else:
        rsp = Response(status=400)
        return rsp
Ejemplo n.º 6
0
    def post(self, **kwargs):
        token_information = kwargs.get("token_information")
        if token_information is None:
            return json_response(
                status_code=500,
                message="Token information couldn't be verified",
                path=request.full_path,
                method=request.method,
            )

        body = request.get_json()
        if body is None:
            return json_response(
                message="A JSON body must be provided",
                status_code=400,
                path=request.full_path,
                method=request.method,
            )

        username = token_information.get("username")
        contact_name = body.get("contact_name", None)
        if (
            db.child("users")
            .child(username)
            .child("contacts")
            .child(contact_name)
            .get()
            .each()
            is not None
        ):
            return json_response(
                message=f"Contact {contact_name} already exists",
                status_code=400,
                path=request.full_path,
                method=request.method,
            )

        emails = body.get("emails", None)
        if type(emails) == str:
            emails = [emails]
        db.child("users").child(username).child("contacts").child(
            contact_name
        ).set(emails)

        return json_response(
            status_code=201,
            message="",
            path=request.full_path,
            method=request.method,
        )
Ejemplo n.º 7
0
    def post(self, **kwargs):
        body = request.get_json()
        if body is None:
            return json_response(status_code=400,
                                 message="You must provide a json body")

        nome = body.get("nome", None)
        email = body.get("email", None)
        cpf = body.get("cpf", None)

        if nome is None:
            return json_response(status_code=400,
                                 message="Field 'nome' must not be empty")
        if email is None:
            return json_response(status_code=400,
                                 message="Field 'email' must not be empty")
        if cpf is None:
            return json_response(status_code=400,
                                 message="Field 'cpf' must not be empty")

        try:
            produtor = ProdutorRural(nome=nome, email=email, cpf=cpf)
            db.session.add(produtor)
            db.session.commit()
        except IntegrityError:
            return json_response(status_code=400,
                                 message="CPF already registered")
        except Exception:
            return json_response(status_code=500, message="Could not create")

        return json_response(201)
Ejemplo n.º 8
0
 def get(self, **kwargs):
     return json_response(
         payload={
             "lavouras": [{
                 "latitude": lavoura.latitude,
                 "longitude": lavoura.longitude,
                 "tipo": lavoura.tipo,
             } for lavoura in Lavoura.query.all()]
         })
Ejemplo n.º 9
0
    def post(self):
        """Tries to create a new user"""
        body = request.get_json()
        if body is None:
            return json_response(
                message="A JSON body must be provided",
                status_code=400,
                path=request.full_path,
                method=request.method,
            )

        username = body.get("username", None)
        password = body.get("password", None)

        if username is None:
            return json_response(
                message="Field 'username' must not be empty",
                status_code=400,
                path=request.full_path,
                method=request.method,
            )
        if password is None:
            return json_response(
                message="Field 'password' must not be empty",
                status_code=400,
                path=request.full_path,
                method=request.method,
            )

        try:
            create_user(username=username, password=password)
            return json_response(
                message=f"Created user {username}",
                status_code=201,
                path=request.full_path,
                method=request.method,
            )
        except AlreadyRegisteredError:
            return json_response(
                message="Username already registered",
                status_code=400,
                path=request.full_path,
                method=request.method,
            )
Ejemplo n.º 10
0
    def post(self, **kwargs):
        body = request.get_json()
        if body is None:
            return json_response(status_code=400,
                                 message="You must provide a json body")

        username = body.get("username", None)
        password = body.get("password", None)
        name = body.get("name", None)

        if username is None:
            return json_response(status_code=400,
                                 message="Field 'username' must not be empty")
        if password is None:
            return json_response(status_code=400,
                                 message="Field 'password' must not be empty")

        try:
            user = create_user(username=username, password=password, name=name)
            payload = {
                "id": user.id,
                "username": user.username,
                "name": user.name,
            }
            return json_response(status_code=201, payload=payload)
        except AlreadyRegisteredError:
            return json_response(
                status_code=400,
                message=f"Username {username} is already registered",
            )
        except Exception:
            return json_response(status_code=500,
                                 message="Could not create user")
Ejemplo n.º 11
0
    def get(self, **kwargs):
        cpf = request.args.get("cpf", None)
        if cpf:
            produtores = ProdutorRural.query.filter(
                ProdutorRural.cpf.like("%" + cpf + "%")).all()
        else:
            produtores = ProdutorRural.query.all()

        return json_response(
            payload={
                "produtores": [{
                    "nome": produtor.nome,
                    "cpf": produtor.cpf,
                    "email": produtor.email,
                } for produtor in produtores]
            })
Ejemplo n.º 12
0
    def patch(self, **kwargs):
        body = request.get_json()
        if body is None:
            return json_response(status_code=400,
                                 message="You must provide a json body")

        novo_nome = body.get("novo_nome", None)
        novo_email = body.get("novo_email", None)
        novo_cpf = body.get("novo_cpf", None)
        cpf = body.get("cpf", None)

        if cpf is None:
            return json_response(status_code=400,
                                 message="Field 'cpf' must not be empty")

        if novo_nome is None and novo_email is None and novo_cpf is None:
            return json_response(
                status_code=400,
                message=("You must provide at least one of those fields:"
                         " 'novo_nome', 'novo_email', 'novo_cpf'"),
            )

        produtor = ProdutorRural.query.filter_by(cpf=cpf).first()

        if not produtor:
            return json_response(
                status_code=404,
                message=f"Produtor with cpf {cpf} was not found",
            )
        if novo_cpf:
            produtor.cpf = novo_cpf
        if novo_email:
            produtor.email = novo_email
        if novo_nome:
            produtor.nome = novo_nome

        try:
            db.session.add(produtor)
            db.session.commit()
        except IntegrityError:
            return json_response(status_code=400,
                                 message="CPF already registered")
        except Exception:
            return json_response(status_code=500, message="Could not update")

        return json_response(200)
Ejemplo n.º 13
0
 def method_not_allowed(e):
     return json_response(405)
Ejemplo n.º 14
0
 def bad_request(e):
     return json_response(400)