added back user registration endpoint

deb
androiddrew 7 years ago
parent 922f8c7d52
commit e3e4ba7ace

@ -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)
]

@ -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()

Loading…
Cancel
Save