示例#1
0
def add_sensors_to_db():
    for sensor in SENSORS:
        s = Sensor()
        s.from_dict(sensor)
        db.session.add(s)
        db.session.commit()
        sensor["id"] = s.id
示例#2
0
def create_sensor():
    data = request.get_json() or {}
    if "name" not in data:
        return bad_request("must include name field")
    if "categories" not in data:
        return bad_request("must include categories field")
    if not Category.are_valid_categories(data["categories"]):
        return bad_request("invalid categor(y)/(ies) in included categories")
    if Sensor.query.filter_by(name=data["name"]).first():
        return bad_request("please use a different name")
    sensor = Sensor()
    sensor.from_dict(data)
    db.session.add(sensor)
    db.session.commit()
    response = jsonify(sensor.to_dict())
    response.status_code = 201
    response.headers["Location"] = url_for("api.get_sensor",
                                           sensor_id=sensor.id)
    return response