From e3e4ba7acecb0f57f80d10b2063e49a6fb821b61 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Mon, 30 Apr 2018 13:56:24 -0400 Subject: [PATCH] added back user registration endpoint --- cookie_api/auth.py | 58 +++++++++++++++++++++----------------------- cookie_api/schema.py | 14 +++++++++++ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/cookie_api/auth.py b/cookie_api/auth.py index 75c22b4..dde68b9 100644 --- a/cookie_api/auth.py +++ b/cookie_api/auth.py @@ -9,12 +9,10 @@ from sqlalchemy.orm import Session from sqlalchemy.orm.exc import NoResultFound from cookie_api.models import User +from cookie_api.schema import UserExportSchema, UserCreateSchema from cookie_api.util import ExtJSONResponse -# from cookie_api.schema import - - def login(json_data: http.RequestData, session: Session, jwt: JWT): user_id = json_data.get('email') password = json_data.get('password') @@ -55,42 +53,42 @@ def logout(): pass -# TODO Add user registration -# def register(user_rep: UserCreateSchema, session: Session, mail: Mail): -def register(): - # email_check = session.query(User).filter_by(email=user_rep['email']).one_or_none() - # - # if email_check is not None: - # message = { - # 'status': 'error', - # 'message': 'user email address is already in use' - # } - # return http.Response(message, status=400) - # - # user = User(email=user_rep['email'], password=user_rep['password']) - # - # session.add(user) - # session.commit() - # +# TODO Add email confirmation to registration +# def register(user_data: UserCreateSchema, session: Session, mail: Mail): +def register(user_data: UserCreateSchema, session: Session) -> UserExportSchema: + email_check = session.query(User).filter_by(email=user_data['email']).one_or_none() + + if email_check is not None: + error = { + 'status': 'error', + 'message': 'user email address is already in use' + } + return ExtJSONResponse(error, 400) + + user = User(email=user_data['email'], password=user_data['password']) + + session.add(user) + session.commit() + # msg = Message("Thank you for registering please confirm your email", recipients=[user_rep['email']]) # mail.send(msg) - # + # headers = {} - # message = { - # 'status': 'success', - # 'message': 'Please check your inbox and confirm your email' - # } - # return http.Response(message, status=201, headers=headers) - pass + message = { + 'status': 'success', + 'message': 'Please check your inbox and confirm your email', + 'data': UserExportSchema(user) + } + return ExtJSONResponse(message, 201) -def user_profile(user: JWTUser, session: Session): # -> UserSchema +def user_profile(user: JWTUser, session: Session) -> UserExportSchema: try: user = session.query(User).filter_by(id=user.id).one() except NoResultFound as e: error = {'message': str(e)} return ExtJSONResponse(error, 400) - return ExtJSONResponse(user.to_dict()) + return ExtJSONResponse(UserExportSchema(user)) # TODO Add email confirmation @@ -105,7 +103,7 @@ def reset(): routes = [ Route('/login', 'POST', login), - # Route('/register', 'POST', register), + Route('/register', 'POST', register), Route('/status', 'GET', user_profile) ] diff --git a/cookie_api/schema.py b/cookie_api/schema.py index ee93d8e..d141c36 100644 --- a/cookie_api/schema.py +++ b/cookie_api/schema.py @@ -11,3 +11,17 @@ class CookieSchema(types.Type): sku = validators.String(max_length=55) qoh = validators.Integer(minimum=0) unit_cost = Decimal(minimum=0.0) + + +class UserCreateSchema(types.Type): + email = validators.String(max_length=255) + password = validators.String(max_length=255) + + +class UserExportSchema(types.Type): + id = validators.Integer() + created_date = validators.DateTime(allow_null=True) + modified_date = validators.DateTime(allow_null=True) + email = validators.String() + admin = validators.Boolean() + confirmed = validators.Boolean()