diff options
Diffstat (limited to 'web-server')
| -rw-r--r-- | web-server/main.py | 17 | ||||
| -rw-r--r-- | web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py | 7 | ||||
| -rw-r--r-- | web-server/opendc/api/v2/topologies/topologyId/endpoint.py | 6 | ||||
| -rw-r--r-- | web-server/opendc/models/experiment.py | 2 | ||||
| -rw-r--r-- | web-server/opendc/models/model.py | 11 | ||||
| -rw-r--r-- | web-server/opendc/models/simulation.py | 4 |
6 files changed, 23 insertions, 24 deletions
diff --git a/web-server/main.py b/web-server/main.py index 412bf3f9..7f499b34 100644 --- a/web-server/main.py +++ b/web-server/main.py @@ -26,10 +26,11 @@ else: # Set up database if not testing if not TEST_MODE: - database.DB.initialize_database(user=os.environ['OPENDC_DB_USERNAME'], - password=os.environ['OPENDC_DB_PASSWORD'], - database=os.environ['OPENDC_DB'], - host='localhost') + database.DB.initialize_database( + user=os.environ['OPENDC_DB_USERNAME'], + password=os.environ['OPENDC_DB_PASSWORD'], + database=os.environ['OPENDC_DB'], + host=os.environ['OPENDC_DB_HOST'] if 'OPENDC_DB_HOST' in os.environ else 'localhost') # Set up the core app FLASK_CORE_APP = Flask(__name__, static_url_path='', static_folder=STATIC_ROOT) @@ -83,8 +84,8 @@ def sign_in(): data = {'isNewUser': user.obj is None} - if user is not None: - data['userId'] = user.id + if user.obj is not None: + data['userId'] = user.get_id() return jsonify(**data) @@ -151,10 +152,10 @@ def receive_message(message): """"Receive a SocketIO request""" (req, res) = _process_message(message) - print(f'Socket:\t{req.method} to `/{req.path}` resulted in {res.status["code"]}: {res.status["description"]}') + print(f'Socket: {req.method} to `/{req.path}` resulted in {res.status["code"]}: {res.status["description"]}') sys.stdout.flush() - flask_socketio.emit('res', res.to_JSON(), json=True) + flask_socketio.emit('response', res.to_JSON(), json=True) def _process_message(message): diff --git a/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py index ecf80353..09c84019 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ b/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py @@ -9,12 +9,7 @@ from opendc.util.database import Database def POST(request): """Add a new Topology to the specified simulation and return it""" - request.check_required_parameters(path={'simulationId': 'string'}, - body={ - 'topology': { - 'name': 'string' - } - }) + request.check_required_parameters(path={'simulationId': 'string'}, body={'topology': {'name': 'string'}}) simulation = Simulation.from_id(request.params_path['simulationId']) diff --git a/web-server/opendc/api/v2/topologies/topologyId/endpoint.py b/web-server/opendc/api/v2/topologies/topologyId/endpoint.py index a4f71ed6..84f20903 100644 --- a/web-server/opendc/api/v2/topologies/topologyId/endpoint.py +++ b/web-server/opendc/api/v2/topologies/topologyId/endpoint.py @@ -9,7 +9,7 @@ from opendc.util.rest import Response def GET(request): """Get this Topology.""" - request.check_required_parameters(path={'topologyId': 'int'}) + request.check_required_parameters(path={'topologyId': 'string'}) topology = Topology.from_id(request.params_path['topologyId']) @@ -21,7 +21,7 @@ def GET(request): def PUT(request): """Update this topology""" - request.check_required_parameters(path={'topologyId': 'int'}, body={'topology': {'name': 'string', 'rooms': {}}}) + request.check_required_parameters(path={'topologyId': 'string'}, body={'topology': {'name': 'string', 'rooms': {}}}) topology = Topology.from_id(request.params_path['topologyId']) topology.check_exists() @@ -38,7 +38,7 @@ def PUT(request): def DELETE(request): """Delete this topology""" - request.check_required_parameters(path={'topologyId': 'int'}) + request.check_required_parameters(path={'topologyId': 'string'}) topology = Topology.from_id(request.params_path['topologyId']) diff --git a/web-server/opendc/models/experiment.py b/web-server/opendc/models/experiment.py index ac606d64..36b5d415 100644 --- a/web-server/opendc/models/experiment.py +++ b/web-server/opendc/models/experiment.py @@ -21,4 +21,4 @@ class Experiment(Model): authorizations = list( filter(lambda x: str(x['simulationId']) == str(self.obj['simulationId']), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): - raise ClientError(Response(403, "Forbidden from retrieving/editing experiment.")) + raise ClientError(Response(403, 'Forbidden from retrieving/editing experiment.')) diff --git a/web-server/opendc/models/model.py b/web-server/opendc/models/model.py index 1935638f..f42134bf 100644 --- a/web-server/opendc/models/model.py +++ b/web-server/opendc/models/model.py @@ -1,3 +1,5 @@ +from uuid import uuid4 + from opendc.util.database import DB from opendc.util.exceptions import ClientError from opendc.util.rest import Response @@ -23,7 +25,7 @@ class Model: def get_id(self): """Returns the ID of the enclosed object.""" - return self.obj['_id'] + return str(self.obj['_id']) def check_exists(self): """Raises an error if the enclosed object does not exist.""" @@ -35,12 +37,13 @@ class Model: self.obj[key] = value def insert(self): - """Inserts the enclosed object and updates the internal reference to the newly inserted object.""" - self.obj = DB.insert(self.obj, self.collection_name) + """Inserts the enclosed object and generates a UUID for it.""" + self.obj['_id'] = str(uuid4()) + DB.insert(self.obj, self.collection_name) def update(self): """Updates the enclosed object and updates the internal reference to the newly inserted object.""" - self.obj = DB.update(self.get_id(), self.obj, self.collection_name) + DB.update(self.get_id(), self.obj, self.collection_name) def delete(self): """Deletes the enclosed object in the database.""" diff --git a/web-server/opendc/models/simulation.py b/web-server/opendc/models/simulation.py index 86aa4726..9a2770cf 100644 --- a/web-server/opendc/models/simulation.py +++ b/web-server/opendc/models/simulation.py @@ -25,7 +25,7 @@ class Simulation(Model): def get_all_authorizations(self): """Get all user IDs having access to this simulation.""" return [ - user['_id'] for user in DB.fetch_all({'authorizations': { - 'simulationId': self.get_id() + str(user['_id']) for user in DB.fetch_all({'authorizations': { + 'simulationId': self.obj['_id'] }}, User.collection_name) ] |
