def delete(self, name): """deletes a location by name""" location = LocationModel.find_location_by_name(name) if location: location.delete_from_db() return {"message": "Location deleted"} return {"message": "Location not found"}
def post(self, name): """adds a new location""" data = Location.parser.parse_args() if LocationModel.find_location_by_name(name): return {"message": "A location with name '{}' already exists. Please try to use another name".format(name)}, 400 location = LocationModel(name, **data) try: location.save_to_db() except: return {"message": "An error ocurred inserting the location."}, 500 return {"location_result": location.json()}, 201
def post(self, building, level, pos_x, pos_y): """adds a new nav point""" data = NavNode.parser.parse_args() try: filters = { 'building': building, 'level': int(level), 'pos_x': int(pos_x), 'pos_y': int(pos_y) } except: return { "message": "Please input valid number for level, pos_x, and pos_y." }, 400 if NavNodeModel.find_nav_node_by_pos(**filters): return { "message": "A navigation point with building {}, level {}, x {}, y {} already exists." .format(building, str(level), str(pos_x), str(pos_y)) }, 400 fixed_data = { 'building': building, 'level': int(level), 'pos_x': int(pos_x), 'pos_y': int(pos_y), 'default_exit_direction': data['default_exit_direction'], 'special_type': data['special_type'], 'location_id': None } if data['location_name']: found_location = LocationModel.find_location_by_name( data['location_name']) if found_location: fixed_data['location_id'] = found_location.id else: return { "message": "No location with a location name {} found.".format( data['location_name']) }, 400 nav_node = NavNodeModel(**fixed_data) try: nav_node.save_to_db() if fixed_data['location_id']: found_location.activate_armap() except: return {"message": "An error ocurred inserting the nav node."}, 500 return {"nav_node_result": nav_node.json()}, 201
def get(self, name): """displays a location's details""" location = LocationModel.find_location_by_name(name) if location: return {"location_result": location.json()}, 200 return {"message": "Location not found"}, 404