routes

py

School

University of California, Los Angeles *

*We aren’t endorsed by this school

Course

32

Subject

Electrical Engineering

Date

Dec 6, 2023

Type

py

Pages

15

Report

Uploaded by JusticeLightningDog3

ffrom __main__ import app import sys, os from flask import request, jsonify #cs190b infrastructure and utilities from db import db_session, myname from models import * import utils DEBUG=True ###################################### @app.route('/') # the default route def hello(): ''' This route prints the welcome message using the myname variable passed in as an environment variable. ''' return f'Welcome to CS190B Sensors Hub from {myname}\n', 200 ###################################### @app.route('/api/getsensorbyname/', methods=['GET']) def get_sensor_by_name(): ''' Given a sensor_name, return the information about the sensor from the Sensors table in the DB Arguments: sensor_name Error cases: (1) missing argument (2) no sensors found in DB sensors table for that name Success cases: (1) sensor is found in DB sensors table. The sensor should be represented as a dictionary of key-value pairs with keys "id", "sensor name", "sensor type", and "sensor location", appended to the response value for the RESULT key. RESULT is a list consisting of sensor/sensors that follow the constraint. ''' response = {} status = 503 #unavailable if DEBUG: print("get_sensor_by_name GET") try: sname = request.args.get("sensor_name", None) if not sname: #invalid/missing message response["MESSAGE"] = f"ERROR: /api/getsensorbyname/ case 1: sensor_name argument is missing" status = 400 else: result = db_session.query(Sensors).filter( Sensors.sname == sname).all() #use all so we can iterate below if not result: #error from DB response["MESSAGE"] = f"ERROR: /api/getsensorbyname/ case 3: Error retrieving sensors for name {sname}" status = 400 else: response["RESULT"] = [] response["MESSAGE"] = f"SUCCESS: /api/getsensorbyname/ sensor data retrieved successfully" status = 200 for sensor in result: response["RESULT"].append({"id": sensor.sensor_id,
"sensor name": sensor.sname}) except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getsensorbyname/ {e}" print(response["MESSAGE"]) status = 500 # Return the response in json format with status code return jsonify(response), status ###################################### @app.route('/api/getsensors/', methods=['GET']) def get_sensors(): ''' Given a sensor_type, return the information on the 1+ from the Sensors table in the DB Arguments: sensor_type Error cases: (1) missing or invalid argument (2) no sensors found for the valid sensor type requested Success cases: (1) sensors of the specified type were found: RESULT key has a list of sensors of the type specified appended. RESULT is a list consisting of sensor/sensors that follow the constraint. Directions: Each sensor should be represented as a dictionary of key-value pairs with keys "id", "sensor name", "sensor type", and "sensor location". The values for these keys should be those stored in the sensors table in the database at the time of the request. Valid sensor types are "temperature", "gas", "humidity", and "air quality". "all" is also considered a valid sensor type which is meant to indicate that the request should return all entries in the sensors table. You will use the all and filter/all db_session query. ''' #TODO - replace the following 3 lines of code with the correct implementation response = {} status = 503 if DEBUG: print("get_sensors GET") try: sType = request.args.get("sensor_type", None) sDict = ["temperature", "gas", "humidity", "air quality"] if not sType: response["MESSAGE"] = f"ERROR: /api/getsensorbytype/ case 1: sensor_type argument is missing" status = 400 return jsonify(response), status if sType == "all": sensors = db_session.query(Sensors).all() else: sensors = db_session.query(Sensors).filter(Sensors.stype == sType).all() if not sensors: response["MESSAGE"] = f"ERROR: /api/getsensorbytype/ case 2: no sensors found for the valid sensor type requested" status = 400 else: sensor_data = [] for sensor in sensors: sensor_info = { "id": sensor.sensor_id,
"sensor name": sensor.sname, "sensor type": sensor.stype, "sensor location": sensor.sloc } sensor_data.append(sensor_info) response["MESSAGE"] = f"SUCCESS: /api/getsensors/ data retrieved successfully" status = 200 return jsonify(response), status except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getsensors/ {e}" print(response["MESSAGE"]) status = 500 return jsonify(response), status ###################################### @app.route('/api/getsensorbyid/', methods=['GET']) def get_sensor_by_id(): ''' Given a sensor_id, return the information about the sensor from the Sensors table in the DB GET /api/getsensorbyid/ Arguments: sensor_id Error cases: (1) missing sensor_id argument (2) argument is not compatible with integer type – note that utils.is_id_integer(id) checks this for you (3) no sensor found in DB sensors table for that id Success cases: (1) sensor is found in DB sensors table. RESULT is a list consisting of sensor/sensors that follow the constraint. Directions: The sensor should be represented as a dictionary of key-value pairs with keys "id", "sensor name", "sensor type", and "sensor location", appended to the response value for the RESULT key. You will use the get db_session query because sensor_id is the unique primary key of the sensors table. ''' response = {} status = 503 #unavailable #TODO - replace the following 3 lines of code with the correct implementation if DEBUG: print("get_sensor_by_id GET") try: sID = request.args.get("sensor_id", None) if not sID: response["MESSAGE"] = f"ERROR: /api/getsensorbytype/ case 1: sensor_type argument is missing" status = 400 return jsonify(response), status if not (utils.is_id_integer(sID)): esponse["MESSAGE"] = f"ERROR: /api/getsensorbyid/ case 2: argument is not compatible with integer type" status = 400 return jsonify(response), status else: sensors = db_session.query(Sensors).filter(Sensors.sensor_id == sID).all() if not sensors:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
response["MESSAGE"] = f"ERROR: /api/getsensorbyid/ case 3: no sensor found in DB sensors table for that id" status = 400 return jsonify(response), status else: sensor_data = [] for sensor in sensors: sensor_info = { "id": sensor.sensor_id, "sensor name": sensor.sname, "sensor type": sensor.stype, "sensor location": sensor.sloc } sensor_data.append(sensor_info) response["MESSAGE"] = f"SUCCESS: /api/getsensors/ data retrieved successfully" status = 200 return jsonify(response), status except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getsensors/ {e}" print(response["MESSAGE"]) status = 500 return jsonify(response), status ###################################### ###################################### @app.route('/api/getlocations/', methods=['GET']) def get_locations(): ''' Return the information about 1+ locations from the Locations table in the DB Arguments: (a) none (b) location_id (c) location (location_id is ignored if also passed in) (location is the string name) Error cases: (1) no locations found (2) location_id passed in does not have integer type -- note that utils.is_id_integer(id) checks this for you Success cases: (1) found locations in the DB locations table -- append the list of locations to the value of the RESULT key in the response. A list element should be represented as a dictionary of key-value pairs with keys "location_id" and "location" with their associated values from the DB table. ''' response = {} status = 503 #unavailable if DEBUG: print("get_locations GET") try: location = request.args.get("location", None) #string name if location: locations = db_session.query(Locations).filter( Locations.location == location).all() else: loc_id = request.args.get("location_id", None) if loc_id: if not utils.is_id_integer(location_id): response["MESSAGE"] = f"ERROR: /api/getlocations/ case 2: ID is
not a valid integer {loc_id}" status = 400 else: locations = db_session.query(Locations).filter( Locations.location_id == location_id).all() else: locations = db_session.query(Locations).all() if not locations: #error from DB response["MESSAGE"] = f"ERROR: /api/getlocations/ case 1: no locations found" status = 400 else: response["RESULT"] = [] response["MESSAGE"] = f"SUCCESS: /api/getlocations/ data retrieved successfully" status = 200 for loc in locations: response["RESULT"].append({"location_id": loc.location_id, "location": loc.location}) except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getlocations/ {e}" print(response["MESSAGE"]) status = 500 # Return the response in json format with status code return jsonify(response), status ###################################### @app.route('/api/getsensorsinlocations/', methods=['GET']) def get_sensors_in_locations(): ''' Return the information about 1+ sensors from the Sensors table in the DB based on location data passed in, if any Arguments: (a) none (b) sensor id only, (c) location id only (d) sensor id and location id Error cases: (1) no sensors found for arguments passed in Success cases: The success cases map to each of the argument options passed in (a-d above): (1) arguments=(a): Output an entry for all sensors (2) arguments=(b): Output an entry for the sensor id passed in (3) arguments=(c): Output an entry for all sensors that have the location id passed in (4) arguments=(d): Output an entry for the sensor and location id passed in. Directions: Each sensor should be represented as a dictionary of key-value pairs with keys "location_id", "sensor_id", "sensor_name", "sensor type", and "sensor location", appended to the response value for the RESULT key. Note that the sensor location is a string that is stored in the locations table in the database. ''' response = {} status = 503 #unavailable if DEBUG: print("get_sensors_in_locations GET") try: sID = request.args.get("sensor_id", None) locID = request.args.get("location_id", None)
filter_conditions = { (True, True): lambda x: x.filter(Sensors.sensor_id == sID, Sensors.sloc == locID), (True, False): lambda x: x.filter(Sensors.sensor_id == sID), (False, True): lambda x: x.filter(Sensors.sloc == locID), (False, False): lambda x: x, } sensors = filter_conditions[(bool(sID), bool(locID))] (db_session.query(Sensors)).all() if not sensors: response["MESSAGE"] = f"ERROR: /api/getsensorsinlocations/ case 1: no sensors found for arguments passed in" status = 400 return jsonify(response), status else: sensor_data = [] for sensor in sensors: loc = db_session.query(Locations).filter(Locations.location_id == sensor.sloc).one_or_none() sensor_info = { "location_id": sensor.sloc, "sensor_id": sensor.sensor_id, "sensor name": sensor.sname, "sensor type": sensor.stype, "sensor location": loc.location } sensor_data.append(sensor_info) response["MESSAGE"] = f"SUCCESS: /api/getsensorsinlocations/ data retrieved successfully" status = 200 return jsonify(response), status except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getsensorsinlocations/ {e}" print(response["MESSAGE"]) status = 500 return jsonify(response), status ###################################### @app.route('/api/getlogidsfortimestamp/', methods=['GET']) def get_log_ids_for_timestamp(): ''' Given a timestamp, return information about 1+ log entries from the Logging table in the DB Arguments: timestamp (format="2023-03-15T10:15:20" aka %Y-%m-%dT%H:%M:%S) Error cases: (1) timestamp key not passed in (2) timestamp in incorrect format -- note that this function checks it for you: utils.is_valid_timestamp(ts) (3) no log ids found for timestamp Success cases: list of log ids found (and returned) for timestampEach log id should be represented as a dictionary of key-value pairs with key "log_id" appended to the response value for the RESULT key. ''' response = {} status = 503 if DEBUG: print("get_log_ids_for_timestamp GET") try:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
ts = request.args.get("timestamp", None) if not ts: response["MESSAGE"] = f"ERROR: /api/getlogidsfortimestamp/ case 1: invalid argument passed in" status = 400 else: if not utils.is_valid_timestamp(ts): response["MESSAGE"] = f"ERROR: /api/getlogidsfortimestamp/ case 2: invalid timestamp passed in" status = 400 else: results = db_session.query(Logging).filter( Logging.ts == ts).all() if not results: response["MESSAGE"] = f"ERROR: /api/getlogidsfortimestamp/ case 3: no results found for timestamp {ts}" status = 400 else: response["RESULT"] = [] response["MESSAGE"] = f"SUCCESS: /api/getlogidsfortimestamp/ obtained list of logids for timestamp {ts} " status = 200 for res in results: response["RESULT"].append({"log_id": res.log_id}) except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getlogidsfortimestamp/ {e}" print(response["MESSAGE"]) status = 500 # Return the response in json format with status code return jsonify(response), status ###################################### @app.route('/api/getlogsbydate/', methods=["GET"]) def get_logs_by_date(): ''' Given start/end dates arguments, using keys "start" and "end", return the first 20 log elements within the date range ordered by timestamp. Arguments: (a) "start" date and "end" date (b) just "start" date (c) just "end" date (d) none Error cases: (1) start date is specified but is in valid or end date is specified and is invalid (2) not case 1 but no logs are found for the arguments passed in Success cases: (1) arguments=(a): return first 20 log entries with timestamps greater than or equal to start date, and smaller than end date. (ordered by timestamp) (2) arguments=(b): return first 20 log entries with a timestamp larger than or equal to start date (ordered by timestamp) (3) arguments=(c): return first 20 log entries with a timestamp smaller than end date (ordered by timestamp) (4) arguments=(d): return first 20 log entries (ordered by timestamp) Directions: - Valid dates have the format yyyy-mm-dd (util.is_valid_date(date) checks this for you). - Log entries should be appended as a list to the value of the RESULT key in the response, with keys log_id, sensor_id, timestamp, and value and the appropriate values for each from the logging DB table. '''
response = {} status = 503 #unava sDate = request.args.get("start", None) eDate = request.args.get("end", None) if sDate and not utils.is_valid_date(sDate): response["MESSAGE"] = "ERROR: /api/getlogsbydate/ Invalid start date" status = 400 elif eDate and not utils.is_valid_date(eDate): response["MESSAGE"] = "ERROR: /api/getlogsbydate/ Invalid end date" status = 400 else: try: if (sDate and eDate): logs = db_session.query(Logging).filter(Logging.ts >= sdate, Logging.ts < edate).order_by(Logging.ts).limit(20).all() elif sDate: logs = db_session.query(Logging).filter(Logging.ts >= sdate).order_by(Logging.ts).limit(20).all() elif eDate: logs = db_session.query(Logging).filter(Logging.ts < edate).order_by(Logging.ts).limit(20).all() else: logs = db_session.query(Logging).order_by(Logging.ts).limit(20).all() if logs: response["RESULT"] = [{"log_id": log.log_id, "sensor_id": log.sensor_id, "timestamp": log.ts, "value": log.value} for log in logs] response["MESSAGE"] = "SUCCESS: /api/getlogsbydate/ log data retrieved successfully" status = 200 for log in logs: response["RESULT"].append({ "log_id": log.log_id, "sensor_id": log.sensor_id, "timestamp": log.ts, "value": log.value}) except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getlogsbydate/ {e}" status = 500 return jsonify(response), status @app.route('/api/getlogbylogid/', methods=['GET']) def get_log_by_log_id(): ''' Given a log id, display the table data for the id Arguments: log_id Error cases: (1) log_id is missing (2) log_id is passed in but it is invalid (cannot be converted to an integer) -- note that utils.is_id_integer(id) checks this for you, (3) log_id is valid but not found in the logging DB table. Success cases: log entry with the log id specified is deleted (or was not found in the table). Note that there is no RESULT key in the response for this case. ''' response = {}
status = 503 if DEBUG: print("get_log_by_log_id GET") try: log_id = request.args.get("log_id", None) if not log_id: response["MESSAGE"] = f"ERROR: /api/getlogbylogid/ case 1: log_id parameter is missing" status = 400 elif not utils.is_id_integer(log_id): response["MESSAGE"] = f"ERROR: /api/getlogbylogid/ case 2: ID is not a valid integer {log_id}" status = 400 else: obj = db_session.query(Logging).filter( Logging.log_id == log_id).one() if not obj: response["MESSAGE"] = f"ERROR: /api/getlogbylogid/ case 3: log_id is valid ({log_id}) but not found in DB table" status = 400 else: response["RESULT"] = [] response["MESSAGE"] = f"SUCCESS: /api/getlogbylogid/ log entry found for ID {log_id}" response["RESULT"].append({"log_id": obj.log_id ,"sensor_id": obj.sensor_id , "timestamp": obj.ts, "value": obj.value}) status = 200 except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/getlogbylogid/ {e}" print(response["MESSAGE"]) status = 500 # Return the response in json format with status code return jsonify(response), status ###################################### @app.route('/api/addlocation/', methods=['POST']) def add_location(): ''' Given a location name, create and add a location object to the locations DB table Arguments: location_name Error cases: (1) missing location_name argument (2) unable to add location to the Locations DB table for some reason (give this a status of 500 since it is the same as an exception when adding) Success cases: (1) location with this location_name added to the locations table in the DB successfully -- Note that utils.add_slocs_to_db(location_name) performs the add for you. -- Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 if DEBUG: print("add_location POST") try: location_name = request.args.get("location", None) if not location_name: response["MESSAGE"] = f"ERROR: /api/addlocation/ case 1: location string name missing"
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
status = 400 else: loclist = [location_name] if not utils.add_slocs_to_db(loclist): response["MESSAGE"] = f"ERROR: /api/addlocation/ case 2: Problem adding location" #this is the same as an exception so give it status 500, no 400 status here status = 500 else: response["MESSAGE"] = f"SUCCESS: /api/addlocation/ Location {location_name} added successfully" status = 200 except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/addlocation {e}" print(response["MESSAGE"]) status = 500 # Return the response in json format with status code return jsonify(response), status ###################################### @app.route('/api/addsensor/', methods=['POST']) def add_sensor(): ''' Given a name, type, and location string, add a sensor to the sensors table Arguments: sensor name, sensor type, and location Error cases: (1) missing arguments (2) sensor already in the table (3) unable to add sensor to the sensors DB table for some reason (give this a status of 500 since it is the same as an exception when adding) Success cases: (1) sensor added to the sensors table in the DB successfully -- Note that utils.add_sensor_to_db(sensor_name,sensor_type,location) performs this add for you (but sends back a special return value when the sensor is already in the table). -- Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 #unavailable #TODO - replace the following 3 lines of code with the correct implementation if DEBUG: print("add_sensor POST") sname = request.args.get("sensor_name", None) stype = request.args.get("sensor_type", None) sloc = request.args.get("location", None) if sname and stype and sloc: added, dup = utils.add_sensor_to_db(sname, stype, sloc) if added: response["MESSAGE"] = f"SUCCESS: /api/addsensor/ sensor {sname} added successfully" status = 200 elif dup: response["MESSAGE"] = f"ERROR: /api/addsensor/ case 2: sensor already in table" status = 400 else: response["MESSAGE"] = f"ERROR: /api/addsensor/ case 3: unable to add
sensor to DB table for some reason" status = 500 else: response["MESSAGE"] = f"ERROR: /api/addsensor/ case 1: missing arguments" status = 500 return jsonify(response), status ###################################### @app.route('/api/addlogentry/', methods=['POST']) def add_log_entry(): ''' Given a timestamp, sensor ID, and value, add a log entry to the logging table Arguments: timestamp (format="2023-03-15T10:15:20" aka %Y-%m-%dT%H:%M:%S), sensor id, and value Error cases: (1) invalid or missing arguments (2) sensor ID cannot be converted to an integer -- Note that utils.is_id_integer(sensor_id) checks this for you, (3) the timestamp is invalid -- note that utils.is_valid_timestamp(ts) checks this for you, (4) the service is unable to add a log entry for some reason (utils.add_log_to_db returns False). -- Note that log entries with the same data are not duplicates because they are each given a unique log ID (and so they are different). Success case: the log entry is added to the logging DB table -- note that utils.add_log_to_db(sensor_id,ts,value) performs the add for you. Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 if DEBUG: print("add_log_entry POST") timeStamp = request.args.get("timestamp", None) sID = request.args.get("sensor_id", None) val = request.args.get("value", None) if timeStamp and sID and val: if not (utils.is_id_integer(sID)): response["MESSAGE"] = f"ERROR: /api/addlogentry/ case 2: sensor ID cannot be converted to an integer" status = 400 elif not (utils.is_valid_timestamp(timeStamp)): response["MESSAGE"] = f"ERROR: /api/addlogentry/ case 3: the timestamp is invalid" status = 400 else: added = utils.add_log_to_db(sID, timeStamp, val) if added: response["MESSAGE"] = f"SUCCESS: /api/addlogentry/ the log entry is added to the logging DB table" status = 200 else: response["MESSAGE"] = f"ERROR: /api/addlogentry/ case 4: the service is unable to add a log entry for some reason" status = 500 else: response["MESSAGE"] = f"ERROR: /api/addlogentry/ case 1: invalid or missing
arguments" status = 400 return jsonify(response), status ###################################### @app.route('/api/deletesensor/', methods=['DELETE']) def delete_sensor(): ''' DELETE /api/deletesensor/ - given a sensor name, type, and location id, delete the sensor from the sensors DB table Arguments: sensor name, sensor type, location id Error cases: (1) missing argument(s) Success cases: sensor is deleted (or was not found in the table). Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 #unavailable if DEBUG: print("delete_sensor DELETE") sname = request.args.get("sensor_name", None) sType = request.args.get("sensor_type", None) locID = request.args.get("location_id", None) if sname and sType and locID: try: item = db_session.query(Sensors).filter(Sensors.sname == sname, Sensors.stype == sType, Sensors.sloc == locID).one_or_none() if item: db_session.delete(item) db_session.commit() response["MESSAGE"] = f"SUCCESS: /api/deletesensor/ sensor is deleted" status = 200 except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/deletesensor/ {e}" print(response["MESSAGE"]) status = 500 else: response["MESSAGE"] = f"ERROR: /api/deletesensor/ case 1: missing argument(s)" status = 400 return jsonify(response), status ###################################### @app.route('/api/deletesensorbyid/', methods=['DELETE']) def delete_sensor_by_id(): ''' DELETE /api/deletesensorbyid/ - given a sensor id, delete the sensor from the sensors DB table Arguments: sensor id Error cases: (1) sensor id missing Success case: the sensor with sensor id specified is deleted (or was not found in the table). -- Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 #unavailable if DEBUG: print("delete_sensors_by_id DELETE") try:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
sid = request.args.get("sensor_id", None) if not sid: response["MESSAGE"] = "ERROR: /api/deletesensorbyid/ case 1: sensor_id parameter is missing" status = 400 else: obj = db_session.query(Sensors).filter( Sensors.sensor_id == sid).one_or_none() if obj: db_session.delete(obj) db_session.commit() response["MESSAGE"] = "SUCCESS: /api/deletesensorbyid/ log entry is deleted successfully" status = 200 except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/deletesensorsbyid/ {e}" print(response["MESSAGE"]) status = 500 # Return the response in json format with status code return jsonify(response), status ###################################### @app.route('/api/deletelocation/', methods=['DELETE']) def delete_location(): ''' Given a string location name, delete the location from the locations DB table Arguments: location Error cases: (1) location argument missing Success case: the location with the name specified is deleted (or was not found in the table). --Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 #unavailable if DEBUG: print("delete_location DELETE") try: location = request.args.get("location", None) if not location: response["MESSAGE"] = "ERROR: /api/deletelocation/ case 1: loctaion string name parameter is missing" status = 400 else: obj = db_session.query(Locations).filter( Locations.location == location).one_or_none() if obj: db_session.delete(obj) db_session.commit() response["MESSAGE"] = f"SUCCESS: /api/deletelocation/ location entry for {location} is deleted successfully" status = 200 except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/deletelocation/ {e}" print(response["MESSAGE"]) status = 500 # Return the response in json format with status code return jsonify(response), status ######################################
@app.route('/api/deletelogbylogid/', methods=['DELETE']) def delete_log_by_log_id(): ''' Given a log id, delete the log entry in the logging DB table Arguments: log_id Error cases: (1) log_id is missing (2) log_id is passed in but it is invalid (cannot be converted to an integer) -- note that utils.is_id_integer(id) checks this for you. Success cases: log entry with the log id specified is deleted (or was not found in the table). --Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 if DEBUG: print("delete_log_by_log_id DELETE") logID = request.args.get("log_id", None) try: if logID is None: response["MESSAGE"] = f"ERROR: /api/deletelogbylogid/ case 1: log_id is missing" status = 400 elif not utils.is_id_integer(logID): response["MESSAGE"] = f"ERROR: /api/deletelogbylogid/ case 2: log_id is passed in but it is invalid" status = 400 else: obj = db_session.query(Logging).filter(Logging.log_id == logID).one_or_none() if obj: db_session.delete(obj) db_session.commit() response["MESSAGE"] = f"SUCCESS: /api/deletelogbylogid/ log entry is deleted successfully" status = 200 except Exception as e: response["MESSAGE"] = f"EXCEPTION: /api/deletelogbylogid/ {e}" print(response["MESSAGE"]) status = 500 return jsonify(response), status ###################################### @app.route('/api/movesensorinlocation/', methods=['PUT']) def move_sensor_in_location(): ''' Given a sensor name, type, location id, and new location string name, update the location id of the sensor in the sensor DB table with the location id of the new location string name. The new location id is obtained from the locations DB table using the string name. Arguments: sensor_name, sensor_type, location_id, location_name (new location name) Error cases: (1) 1+ arguments is missing (2) the new location passed in is not found in the locations table (3) the new location string passed in has a location id that is the same as location id passed in (locations are the same), (4) the specified sensor is not found. Success cases: sensor is found and it is not already at the new location specified,
thus its location is updated. For this case, update the MESSAGE string to include the old location and new location. -- Note that there is no RESULT key in the response for this case. ''' response = {} status = 503 #unavailable #TODO - replace the following 3 lines of code with the correct implementation if DEBUG: print("move_sensor_in_location PUT")
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help