diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-06-30 14:07:51 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-08-24 19:42:29 +0200 |
| commit | a27598ee4755423ebd2f0ad8c505644d644cf2c8 (patch) | |
| tree | cdeaf6ca95069e50afa1d4be968be558899cba95 /web-server | |
| parent | e1b29eafbd0b6285b7bea2e24709c7622d41173d (diff) | |
Make accessing the ID easer
Diffstat (limited to 'web-server')
5 files changed, 12 insertions, 8 deletions
diff --git a/web-server/opendc/api/v2/simulations/endpoint.py b/web-server/opendc/api/v2/simulations/endpoint.py index b48ad71b..c978fad7 100644 --- a/web-server/opendc/api/v2/simulations/endpoint.py +++ b/web-server/opendc/api/v2/simulations/endpoint.py @@ -18,12 +18,12 @@ def POST(request): simulation = Simulation({'simulation': request.params_body['simulation']}) simulation.set_property('datetimeCreated', Database.datetime_to_string(datetime.now())) simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) - simulation.set_property('topologyIds', [topology.obj['_id']]) + simulation.set_property('topologyIds', [topology.get_id()]) simulation.set_property('experimentIds', []) simulation.insert() user = User.from_google_id(request.google_id) - user.obj['authorizations'].append({'simulationId': simulation.obj['_id'], 'authorizationLevel': 'OWN'}) + user.obj['authorizations'].append({'simulationId': simulation.get_id(), 'authorizationLevel': 'OWN'}) user.update() return Response(200, 'Successfully created simulation.', simulation.obj) diff --git a/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py index 637b8011..0d7c208d 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py +++ b/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py @@ -29,7 +29,7 @@ def POST(request): experiment.insert() - simulation.obj['experimentIds'].append(experiment.obj['_id']) + simulation.obj['experimentIds'].append(experiment.get_id()) simulation.update() return Response(200, 'Successfully added Experiment.', experiment.obj) 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 0ad7e4bf..952959ca 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ b/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py @@ -21,7 +21,7 @@ def POST(request): topology.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) topology.insert() - simulation.obj['topologyIds'].append(topology.obj['_id']) + simulation.obj['topologyIds'].append(topology.get_id()) simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) simulation.update() diff --git a/web-server/opendc/models/model.py b/web-server/opendc/models/model.py index 2b8eb4dc..1935638f 100644 --- a/web-server/opendc/models/model.py +++ b/web-server/opendc/models/model.py @@ -21,6 +21,10 @@ class Model: def __init__(self, obj): self.obj = obj + def get_id(self): + """Returns the ID of the enclosed object.""" + return self.obj['_id'] + def check_exists(self): """Raises an error if the enclosed object does not exist.""" if self.obj is None: @@ -36,8 +40,8 @@ class Model: def update(self): """Updates the enclosed object and updates the internal reference to the newly inserted object.""" - self.obj = DB.update(self.obj['_id'], self.obj, self.collection_name) + self.obj = DB.update(self.get_id(), self.obj, self.collection_name) def delete(self): """Deletes the enclosed object in the database.""" - DB.delete_one({'_id': self.obj['_id']}, self.collection_name) + DB.delete_one({'_id': self.get_id()}, self.collection_name) diff --git a/web-server/opendc/models/simulation.py b/web-server/opendc/models/simulation.py index bf19368c..dbe1e800 100644 --- a/web-server/opendc/models/simulation.py +++ b/web-server/opendc/models/simulation.py @@ -18,7 +18,7 @@ class Simulation(Model): """ user = User.from_google_id(google_id) authorizations = list( - filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations'])) + filter(lambda x: str(x['simulationId']) == str(self.get_id()), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): raise ClientError(Response(403, "Forbidden from retrieving simulation.")) @@ -26,6 +26,6 @@ class Simulation(Model): """Get all user IDs having access to this simulation.""" return [ user['_id'] for user in DB.fetch_all({'authorizations': { - 'simulationId': self.obj['_id'] + 'simulationId': self.get_id() }}, User.collection_name) ] |
