Esempio n. 1
0
def get_user(id):
    if request.method == 'GET':
        data = User.query.get_or_404(id).to_dict()
        if data == {} or data == None:
            return not_found("User not found!")

        return jsonify(user=data),200
    elif request.method == 'DELETE':
        data = User.query.get_or_404(id).to_dict()
        if data == {} or data == None:
            return not_found("User not found!")

        db.session.remove(data)
        db.session.commit()

        return 200
Esempio n. 2
0
def update_lending(id: int):
    lending = Lending.query.filter_by(id=id).first()
    if lending is None:
        return not_found('empréstimo não encontrado')

    data = request.get_json() or {}

    error = Lending.check_data(data=data)
    if 'user_id' in data and data['user_id'] is not None and \
            User.query.get(data['user_id']) is None:
        error = 'usuário não existe'
    if 'thirdparty_id' in data and data['thirdparty_id'] is not None and \
            Thirdparty.query.get(data['thirdparty_id']) is None:
        error = 'terceiro não existe'
    if 'item_id' in data and data['item_id'] != lending.item_id and \
            data['item_id'] is not None:
        item = Item.query.get(data['item_id'])
        if item is None:
            error = 'item não existe'
        if not item.available:
            error = 'item não está disponível'
        else:
            item.available = False
    if error:
        return bad_request(error)

    lending.from_dict(data)
    try:
        db.session.commit()
    except Exception:
        return internal_server()

    return jsonify(lending.to_dict())
def api_get_user_followed(user_id):
    user = User.query.get(user_id)

    if not user:
        return not_found()

    return jsonify([user._asdict() for user in user.followed])
def update_reservation(id: int):
    reservation = Reservation.query.filter_by(id=id).first()
    if reservation is None:
        return not_found('reserva não encontrada')

    data = request.get_json() or {}

    error = Reservation.check_data(data=data)
    if 'user_id' in data and data['user_id'] is not None and \
            User.query.get(data['user_id']) is None:
        error = 'usuário não existe'
    if 'thirdparty_id' in data and data['thirdparty_id'] is not None and \
            Thirdparty.query.get(data['thirdparty_id']) is None:
        error = 'terceiro não existe'
    if Reservation.query.filter(
            Reservation.date_start >= data['date_start']).filter(
                Reservation.date_start <= data['date_end']).all(
                ) or Reservation.query.filter(
                    Reservation.date_end >= data['date_start']).filter(
                        Reservation.date_end <= data['date_end']).all():
        error = 'já existe um evento nesse período'

    if error:
        return bad_request(error)

    reservation.from_dict(data)
    try:
        db.session.commit()
    except Exception:
        return internal_server()

    return jsonify(reservation.to_dict())
Esempio n. 5
0
def update_item(id: int):
    item = Item.query.filter_by(id=id).first()
    if item is None:
        return not_found('item não encontrado')

    data = request.get_json() or {}

    error = Item.check_data(data=data)
    if 'registry' in data and data['registry'] is not None \
            and data['registry'] != item.registry and \
            Item.query.filter_by(registry=data['registry']).first() is not None:
        error = 'tombo já existe'
    if 'category_id' in data and data['category_id'] is not None and \
            Category.query.get(data['category_id']) is None:
        error = 'category_id não existe'
    if error:
        return bad_request(error)

    item.from_dict(data)
    try:
        db.session.commit()
    except Exception:
        return internal_server()

    return jsonify(item.to_dict())
Esempio n. 6
0
def refresh_captcha(hash):
    captcha = CaptchaStore.query.filter_by(hash=hash).first()
    if captcha is None:
        return not_found("captcha not found")
    captcha.remove()
    db.session.commit()
    return get_captcha()
Esempio n. 7
0
def remove_article(id):
    article = Article.query.filter_by(id=id).first()
    if article is None:
        return not_found("article not found")
    if not g.current_user.privilege > 0:
        return forbidden("Forbidden")
    article.remove()
    db.session.commit()
    return make_response("", 200)
Esempio n. 8
0
def get_sensor_log(sensor_id):
    sensor = Sensor.get_by_id(sensor_id)
    if sensor is None:
        return not_found("Sensor not found")
    rows = LogRow.get_by_sensor_id(sensor_id)
    res = []
    for row in rows:
        res.append(row.to_json())
    return jsonify(res), 200
Esempio n. 9
0
def validate_captcha(hash, text):
    captcha = CaptchaStore.query.filter_by(hash=hash).first()
    if captcha is None:
        return not_found("captcha not found")
    captcha.decided = True
    db.session.commit()
    if captcha.value == text:
        return make_response("", 200)
    else:
        return make_response("", 400)
Esempio n. 10
0
def delete_item(id: int):
    item = Item.query.filter_by(id=id).first()
    if item is None:
        return not_found('item não encontrado')

    try:
        db.session.delete(item)
        db.session.commit()
    except Exception:
        return internal_server()

    return '', 204
Esempio n. 11
0
def delete_lending(id: int):
    lending = Lending.query.filter_by(id=id).first()
    if lending is None:
        return not_found('empréstimo não encontrado')

    try:
        db.session.delete(lending)
        db.session.commit()
    except Exception:
        return internal_server()

    return '', 204
Esempio n. 12
0
def delete_user(id):
    user = User.query.filter_by(id=id).first()
    if user is None:
        return not_found('usuário não encontrado')

    try:
        db.session.delete(user)
        db.session.commit()
    except Exception:
        return internal_server()

    return '', 204
def delete_reservation(id: int):
    reservation = Reservation.query.filter_by(id=id).first()
    if reservation is None:
        return not_found('reserva não encontrada')

    try:
        db.session.delete(reservation)
        db.session.commit()
    except Exception:
        return internal_server()

    return '', 204
Esempio n. 14
0
def get_shortcode(shortcode):
    shorten_url = ShortenURL.query.filter_by(shortcode=shortcode).first()
    if shorten_url:
        shorten_url.redirect_count += 1
        shorten_url.last_seen_date = datetime.utcnow()
        db.session.commit()
        redirection_url = shorten_url.url
        # in case we added url without http:// or https://. required for correct redirection
        if redirection_url.find("http://") != 0 and redirection_url.find("https://") != 0:
            redirection_url = "http://" + redirection_url
        return redirect(redirection_url, code=302)
    return not_found("The shortcode cannot be found in the system")
Esempio n. 15
0
def delete_category(id: int):
    category = Category.query.filter_by(id=id).first()
    if category is None:
        return not_found('categoria não encontrada')

    try:
        db.session.delete(category)
        db.session.commit()
    except Exception:
        return internal_server()

    return '', 204
def delete_thirdparty(id: int):
    thirdparty = Thirdparty.query.filter_by(id=id).first()
    if thirdparty is None:
        return not_found('terceiro não encontrado')

    try:
        db.session.delete(thirdparty)
        db.session.commit()
    except Exception:
        return internal_server()

    return '', 204
Esempio n. 17
0
def update_sensor(sensor_id):
    sensor = Sensor.get_by_id(sensor_id)
    if sensor is None:
        return not_found("Sensor not found")
    name_change = next(c for c in request.get_json() if {
        "op": "replace",
        "path": "/name"
    }.items() <= c.items())
    if name_change is not None and "value" in name_change:
        sensor.name = name_change["value"]
        db.session.commit()
        return jsonify(sensor.to_json()), 200
    return bad_request("Not able to parse any patch changes")
Esempio n. 18
0
def get_users():
    users = User.query.all()
    if users == None:
        return not_found("Users not found!")
    user_list = []
    for user in users:
        user_list.append({
            "id": user.id,
            "name": user.name,
            "email": user.email
        })

    return jsonify(users=user_list), 200
Esempio n. 19
0
def add_sensor_log(sensor_id):
    sensor = Sensor.get_by_id(sensor_id)
    if sensor is None:
        return not_found("Sensor not found")
    log_request = request.get_json()
    if "timestamp" not in log_request:
        return bad_request("timestamp attribute missing")
    if "message" not in log_request:
        return bad_request("message attribute missing")
    if len(log_request["message"]) > 256:
        return bad_request("message attribute too long, max is 256")
    LogRow.create(
        sensor_id=sensor_id,
        timestamp=datetime.utcfromtimestamp(log_request["timestamp"]),
        message=log_request["message"],
    )
    return jsonify({"message": "thanks"}), 201
Esempio n. 20
0
def update_user(id):
    user = User.query.filter_by(id=id).first()
    if user is None:
        return not_found('usuário não encontrado')
    data = request.get_json() or {}

    error = User.check_data(data)
    if 'email' in data and data['email'] != user.email and \
            User.query.filter_by(email=data['email']).first() is not None:
        error = 'email já existe'
    if error:
        return bad_request(error)

    user.from_dict(data)
    try:
        db.session.commit()
    except Exception:
        return internal_server()

    return jsonify(user.to_dict())
Esempio n. 21
0
def update_category(id: int):
    category = Category.query.filter_by(id=id).first()
    if category is None:
        return not_found('categoria não encontrada')

    data = request.get_json() or {}

    error = Category.check_data(data=data)
    if 'name' in data and data['name'] != category.name and \
            Category.query.filter_by(name=data['name']).first() is not None:
        error = 'nome já existe'
    if error:
        return bad_request(error)

    category.from_dict(data)
    try:
        db.session.commit()
    except Exception:
        return internal_server()

    return jsonify(category.to_dict())
def update_thirdparty(id: int):
    thirdparty = Thirdparty.query.filter_by(id=id).first()
    if thirdparty is None:
        return not_found('terceiro não encontrado')

    data = request.get_json() or {}

    error = Thirdparty.check_data(data=data)
    if 'email' in data and data['email'] != thirdparty.email and \
            Thirdparty.query.filter_by(email=data['email']).first() is not None:
        error = 'email já existe'
    if error:
        return bad_request(error)

    thirdparty.from_dict(data)
    try:
        db.session.commit()
    except Exception:
        return internal_server()

    return jsonify(thirdparty.to_dict())
def api_update_user(user_id):

    body = request.json
    user = User.query.get(user_id)

    if not user:
        return not_found()

    if 'first_name' in body:
        user.first_name = body['first_name']

    if 'last_name' in body:
        user.last_name = body['last_name']

    if 'email' in body:
        user.email = body['email']

    if 'about_me' in body:
        user.about_me = body['about_me']

    db.session.add(user)
    db.session.commit()

    return jsonify(user._asdict())
def get_thirdparty(id: int):
    thirdparty = Thirdparty.query.filter_by(id=id).first()
    if thirdparty is None:
        return not_found('terceiro não encontrado')
    return jsonify(thirdparty.to_dict())
Esempio n. 25
0
def get_lending(id: int):
    lending = Lending.query.filter_by(id=id).first()
    if lending is None:
        return not_found('empréstimo não encontrado')
    return jsonify(lending.to_dict())
Esempio n. 26
0
def get_item(id: int):
    item = Item.query.filter_by(id=id).first()
    if item is None:
        return not_found('item não encontrado')
    return jsonify(item.to_dict())
Esempio n. 27
0
def get_reading(sensor_id, reading_id):
    sensor = Sensor.query.get_or_404(sensor_id)
    data = sensor.readings.filter_by(id=reading_id).first()
    if not data:
        return not_found("requested reading has not been created")
    return jsonify(data.to_dict())
Esempio n. 28
0
def not_found_handler(e):
    return not_found('Resource not found')
def get_reservation(id: int):
    reservation = Reservation.query.filter_by(id=id).first()
    if reservation is None:
        return not_found('reserva não encontrada')
    return jsonify(reservation.to_dict())
Esempio n. 30
0
def get_sensor(sensor_id):
    sensor = Sensor.get_by_id(sensor_id)
    if sensor is None:
        return not_found("Sensor not found")
    return jsonify(sensor.to_json()), 200