diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-06-23 18:08:28 +0200 |
|---|---|---|
| committer | Georgios Andreadis <info@gandreadis.com> | 2020-06-23 18:08:28 +0200 |
| commit | 565ede0dc50c3b2df09c066ea3a28a4901cce547 (patch) | |
| tree | e81a1bbed43d2eaf7e2be0bc61007963f807b62d /opendc/api/v2/users/endpoint.py | |
| parent | 6f41be7d9c244b67bfa5ff72f1e90d18fa45b590 (diff) | |
Add DB handlers and rename to v2
Diffstat (limited to 'opendc/api/v2/users/endpoint.py')
| -rw-r--r-- | opendc/api/v2/users/endpoint.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/opendc/api/v2/users/endpoint.py b/opendc/api/v2/users/endpoint.py new file mode 100644 index 00000000..abd54f27 --- /dev/null +++ b/opendc/api/v2/users/endpoint.py @@ -0,0 +1,82 @@ +from opendc.models.user import User +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Search for a User using their email address.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + query={ + 'email': 'string' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate and read a User from the database + + user = User.from_email(request.params_query['email']) + + # Make sure this User exists in the database + + if not user.exists(): + return Response(404, '{} not found'.format(user)) + + # Return this User + + return Response( + 200, + 'Successfully retrieved {}.'.format(user), + user.to_JSON() + ) + + +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 + + request.params_body['user']['googleId'] = request.google_id + user = User.from_JSON(request.params_body['user']) + + # Make sure a User with this Google ID does not already exist + + 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: + return Response(403, 'Forbidden from creating this User.') + + # Insert the User + + user.insert() + + # Return a JSON representation of the User + + return Response( + 200, + 'Successfully created {}'.format(user), + user.to_JSON() + ) |
