summaryrefslogtreecommitdiff
path: root/opendc/api/v2/simulations/simulationId
diff options
context:
space:
mode:
Diffstat (limited to 'opendc/api/v2/simulations/simulationId')
-rw-r--r--opendc/api/v2/simulations/simulationId/endpoint.py54
1 files changed, 12 insertions, 42 deletions
diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py
index b08cf8be..8d29202d 100644
--- a/opendc/api/v2/simulations/simulationId/endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/endpoint.py
@@ -10,19 +10,12 @@ from opendc.util.rest import Response
def GET(request):
"""Get this Simulation."""
- try:
- request.check_required_parameters(path={'simulationId': 'string'})
- except exceptions.ParameterError as e:
- return Response(400, str(e))
+ request.check_required_parameters(path={'simulationId': 'string'})
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
+ simulation.check_exists()
+ simulation.check_user_access(request.google_id, False)
return Response(200, 'Successfully retrieved simulation', simulation.obj)
@@ -30,20 +23,12 @@ def GET(request):
def PUT(request):
"""Update a simulation's name."""
- try:
- request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'})
- except exceptions.ParameterError as e:
- return Response(400, str(e))
+ request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'})
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, True)
- if access_error is not None:
- return access_error
+ simulation.check_exists()
+ simulation.check_user_access(request.google_id, True)
simulation.set_property('name', request.params_body['simulation']['name'])
simulation.set_property('datetime_last_edited', Database.datetime_to_string(datetime.now()))
@@ -55,30 +40,15 @@ def PUT(request):
def DELETE(request):
"""Delete this Simulation."""
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(path={'simulationId': 'string'})
-
- except exceptions.ParameterError as e:
- return Response(400, str(e))
-
- # Instantiate a Simulation and make sure it exists
+ request.check_required_parameters(path={'simulationId': 'string'})
- simulation = Simulation.from_primary_key((request.params_path['simulationId'], ))
-
- if not simulation.exists():
- return Response(404, '{} not found.'.format(simulation))
-
- # Make sure this User is allowed to delete this Simulation
+ simulation = Simulation.from_id(request.params_path['simulationId'])
- if not simulation.google_id_has_at_least(request.google_id, 'OWN'):
- return Response(403, 'Forbidden from deleting {}.'.format(simulation))
+ simulation.check_exists()
+ simulation.check_user_access(request.google_id, True)
- # Delete this Simulation from the database
+ # FIXME cascading
simulation.delete()
- # Return this Simulation
-
- return Response(200, 'Successfully deleted {}.'.format(simulation), simulation.to_JSON())
+ return Response(200, f'Successfully deleted simulation.', simulation.obj)