diff --git a/.gitignore b/.gitignore index 68efbfc..bbbf9b1 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,6 @@ docs/_build/ # Redis /dump.rdb + +# Project +config.py \ No newline at end of file diff --git a/cookie_api/auth.py b/cookie_api/auth.py index 7b900f3..94f16bc 100644 --- a/cookie_api/auth.py +++ b/cookie_api/auth.py @@ -4,6 +4,7 @@ from apistar import Component, Settings, http, Route, Include from apistar.backends.sqlalchemy_backend import Session from apistar_jwt.authentication import get_jwt from apistar_jwt.token import JWT +from sqlalchemy.exc import IntegrityError, InvalidRequestError from cookie_api.models import User @@ -46,8 +47,31 @@ def logout(): # TODO Add user registration -def register(): - pass +def register(settings: Settings, json_data: http.RequestData, session: Session): + user_id = json_data.get('email') + password = json_data.get('password') + + email_check = session.query(User).filter_by(email=user_id).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_id, password=password) + + session.add(user) + session.commit() + + # TODO Send off an email confirmation + headers = {} + message = { + 'status': 'success', + 'message': 'Please check your inbox and confirm your email' + } + return http.Response(message, status=200, headers=headers) # TODO Add user profile endpoint @@ -56,7 +80,7 @@ def user_profile(): # TODO Add email confirmation -def confirm(): +def confirm(settings: Settings, json_data: http.RequestData, session: Session): pass @@ -66,7 +90,8 @@ def reset(): routes = [ - Route('/login', 'POST', login) + Route('/login', 'POST', login), + Route('/register', 'POST', register) ] auth_routes = [Include('/auth', routes)] diff --git a/wsgi.py b/wsgi.py index 7a7bf74..d4f1906 100644 --- a/wsgi.py +++ b/wsgi.py @@ -5,53 +5,8 @@ except ImportError: from psycopg2cffi import compat compat.register() -import os -import sys -from cookie_api import JSONRenderer, Base, application_factory - -settings = { - 'DATABASE': { - 'URL': 'postgresql://apistar:local@localhost/apistar', - 'METADATA': Base.metadata - }, - 'RENDERERS': [JSONRenderer()], - 'JWT': { - 'SECRET': 'thisisasecret' - }, - 'LOGGING': { - "LOG_FILE": 'mylogfile.log', - "LEVEL": "DEBUG" - } -} - - -def prepare_import(path): - """Given a filename this will try to calculate the python path, add it - to the search path and return the actual module name that is expected. - """ - path = os.path.realpath(path) - - if os.path.splitext(path)[1] == '.py': - path = os.path.splitext(path)[0] - - if os.path.basename(path) == '__init__': - path = os.path.dirname(path) - - module_name = [] - - # move up until outside package structure (no __init__.py) - while True: - path, name = os.path.split(path) - module_name.append(name) - - if not os.path.exists(os.path.join(path, '__init__.py')): - break - - if sys.path[0] != path: - sys.path.insert(0, path) - - return '.'.join(module_name[::-1]) - +from cookie_api import application_factory +from config import settings app = application_factory(settings)