diff options
| author | jc0b <j@jc0b.computer> | 2020-06-26 12:29:06 +0200 |
|---|---|---|
| committer | jc0b <j@jc0b.computer> | 2020-06-26 12:29:06 +0200 |
| commit | 6f51282cd7c3945ddd0fac68407a7a7be57aa2ba (patch) | |
| tree | a03a8395e78b2419582074766d70a6a34c8885c4 /opendc/api/v2/simulations/simulationId/topologies/endpoint.py | |
| parent | c15448fb6f3aacc8939f2714fc20304dde98cd28 (diff) | |
Reworking topologies POST
Diffstat (limited to 'opendc/api/v2/simulations/simulationId/topologies/endpoint.py')
| -rw-r--r-- | opendc/api/v2/simulations/simulationId/topologies/endpoint.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py index 4104c4dd..ee60e5b4 100644 --- a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py @@ -1,33 +1,39 @@ from datetime import datetime +from opendc.models.simulation import Simulation from opendc.models.topology import Topology from opendc.util import exceptions from opendc.util.rest import Response -from opendc.models.user import User from opendc.util.database import Database def POST(request): - """Add a new Topology and return it""" + """Add a new Topology to the specified simulation and return it""" # Make sure required parameters are there try: - request.check_required_parameters(body={'topology': {'name': 'string'}}) + request.check_required_parameters(path={'simulationId': 'string'}, body={'topology': {'name': 'string'}}) except exceptions.ParameterError as e: return Response(400, str(e)) - # Instantiate a Topology object from our request, and add some metadata + simulation = Simulation.from_id(request.params_path['simulationId']) + validation_error = simulation.validate() + if validation_error is not None: + return validation_error + + access_error = simulation.validate_user_access(request.google_id, False) + if access_error is not None: + return access_error topology = Topology({'name': request.params_body['topology']['name']}) topology.set_property('datetimeCreated', Database.datetime_to_string(datetime.now())) topology.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) topology.insert() + simulation.obj['topologyIds'].append(topology.obj['_id']) + simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) + simulation.update() # Instantiate the user from the request, and add this topology object to their authorizations - user = User.from_google_id(request.google_id) - user.obj['authorizations'].append({'topologyId': topology.obj['_id'], 'authorizationLevel': 'OWN'}) - user.update() - return Response(200, 'Successfully inserted topology.', topology.obj) |
