summaryrefslogtreecommitdiff
path: root/opendc/api/v2/users
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-24 10:05:22 +0200
committerGeorgios Andreadis <info@gandreadis.com>2020-06-24 10:05:22 +0200
commitc7f773b027019086153f0260b507c8fa173ee5e8 (patch)
tree982048b9a117f1c03abeb26ab84d70f4a82115f8 /opendc/api/v2/users
parentbae760a62fc6a480fbe615dff6a7de03c7fd6d1d (diff)
Fix API serialization issues
Diffstat (limited to 'opendc/api/v2/users')
-rw-r--r--opendc/api/v2/users/endpoint.py35
-rw-r--r--opendc/api/v2/users/userId/authorizations/endpoint.py2
-rw-r--r--opendc/api/v2/users/userId/endpoint.py6
3 files changed, 16 insertions, 27 deletions
diff --git a/opendc/api/v2/users/endpoint.py b/opendc/api/v2/users/endpoint.py
index 27c8a9ef..2712c625 100644
--- a/opendc/api/v2/users/endpoint.py
+++ b/opendc/api/v2/users/endpoint.py
@@ -1,6 +1,8 @@
+from werkzeug.exceptions import abort
+
from opendc.models.user import User
from opendc.util import exceptions
-from opendc.util.database import fetch_one
+from opendc.util.database import fetch_one, insert
from opendc.util.rest import Response
@@ -10,7 +12,7 @@ def GET(request):
try:
request.check_required_parameters(query={'email': 'string'})
except exceptions.ParameterError as e:
- return Response(400, e.message)
+ return Response(400, str(e))
user = fetch_one({'email': request.params_query['email']}, 'users')
@@ -23,34 +25,21 @@ def GET(request):
def POST(request):
"""Add a new User."""
- # Make sure required parameters are there
-
try:
request.check_required_parameters(body={'user': {'email': 'string'}})
-
except exceptions.ParameterError as e:
- return Response(400, e.message)
-
- # Instantiate a User
+ return Response(400, str(e))
request.params_body['user']['googleId'] = request.google_id
- user = User.from_JSON(request.params_body['user'])
+ user = request.params_body['user']
+ existing_user = fetch_one({'googleId': user['googleId']}, 'users')
- # Make sure a User with this Google ID does not already exist
+ if existing_user is not None:
+ return Response(409, '{} already exists.'.format(existing_user))
- if user.exists('google_id'):
- user = user.from_google_id(user.google_id)
- return Response(409, '{} already exists.'.format(user))
-
- # Make sure this User is authorized to create this User
-
- if not request.google_id == user.google_id:
+ if not request.google_id == user['googleId']:
return Response(403, 'Forbidden from creating this User.')
- # Insert the User
-
- user.insert()
-
- # Return a JSON representation of the User
+ user = insert(user, 'users')
- return Response(200, 'Successfully created {}'.format(user), user.to_JSON())
+ return Response(200, 'Successfully created {}'.format(user), user)
diff --git a/opendc/api/v2/users/userId/authorizations/endpoint.py b/opendc/api/v2/users/userId/authorizations/endpoint.py
index bb3e173c..161034e1 100644
--- a/opendc/api/v2/users/userId/authorizations/endpoint.py
+++ b/opendc/api/v2/users/userId/authorizations/endpoint.py
@@ -13,7 +13,7 @@ def GET(request):
request.check_required_parameters(path={'userId': 'int'})
except exceptions.ParameterError as e:
- return Response(400, e.message)
+ return Response(400, str(e))
# Instantiate a User and make sure they exist
diff --git a/opendc/api/v2/users/userId/endpoint.py b/opendc/api/v2/users/userId/endpoint.py
index 7f1ce84f..b7519973 100644
--- a/opendc/api/v2/users/userId/endpoint.py
+++ b/opendc/api/v2/users/userId/endpoint.py
@@ -12,7 +12,7 @@ def DELETE(request):
request.check_required_parameters(path={'userId': 'int'})
except exceptions.ParameterError as e:
- return Response(400, e.message)
+ return Response(400, str(e))
# Instantiate a User and make sure they exist
@@ -44,7 +44,7 @@ def GET(request):
request.check_required_parameters(path={'userId': 'int'})
except exceptions.ParameterError as e:
- return Response(400, e.message)
+ return Response(400, str(e))
# Instantiate a User and make sure they exist
@@ -75,7 +75,7 @@ def PUT(request):
path={'userId': 'int'})
except exceptions.ParameterError as e:
- return Response(400, e.message)
+ return Response(400, str(e))
# Instantiate a User and make sure they exist