API: minor fixes & db update

This commit is contained in:
SamR1 2017-12-17 09:16:08 +01:00
parent 844457b33c
commit b6056e6125
7 changed files with 12 additions and 15 deletions

View File

@ -27,7 +27,7 @@ app.register_blueprint(users_blueprint, url_prefix='/api')
app.register_blueprint(auth_blueprint, url_prefix='/api') app.register_blueprint(auth_blueprint, url_prefix='/api')
if app.debug: if app.debug:
logging.getLogger('sqlalchemy').setLevel(logging.INFO) logging.getLogger('sqlalchemy').setLevel(logging.WARNING)
logging.getLogger('sqlalchemy' logging.getLogger('sqlalchemy'
).handlers = logging.getLogger('werkzeug').handlers ).handlers = logging.getLogger('werkzeug').handlers
logging.getLogger('sqlalchemy.orm').setLevel(logging.WARNING) logging.getLogger('sqlalchemy.orm').setLevel(logging.WARNING)

View File

@ -39,7 +39,8 @@ class TestAuthBlueprint(BaseTestCase):
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
self.assertTrue(data['status'] == 'error') self.assertTrue(data['status'] == 'error')
self.assertTrue(data['message'] == 'Sorry. That user already exists.') self.assertTrue(
data['message'] == 'Sorry. That user already exists.')
self.assertTrue(response.content_type == 'application/json') self.assertTrue(response.content_type == 'application/json')
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)

View File

@ -1,8 +1,8 @@
import json import json
from mpwo_api.tests.base import BaseTestCase from mpwo_api.tests.base import BaseTestCase
from mpwo_api.users.models import User
from mpwo_api.tests.utils import add_user from mpwo_api.tests.utils import add_user
from mpwo_api.users.models import User
class TestUserService(BaseTestCase): class TestUserService(BaseTestCase):

View File

@ -1,5 +1,5 @@
from mpwo_api.users.models import User
from mpwo_api import db from mpwo_api import db
from mpwo_api.users.models import User
def add_user(username, email, password): def add_user(username, email, password):

View File

@ -1,7 +1,7 @@
from flask import Blueprint, jsonify, request from flask import Blueprint, jsonify, request
from sqlalchemy import exc, or_ from sqlalchemy import exc, or_
from mpwo_api import bcrypt, db from mpwo_api import appLog, bcrypt, db
from .models import User from .models import User
@ -51,6 +51,7 @@ def register_user():
# handler errors # handler errors
except (exc.IntegrityError, exc.OperationalError, ValueError) as e: except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
db.session.rollback() db.session.rollback()
appLog.error(e)
response_object = { response_object = {
'status': 'error', 'status': 'error',
'message': 'Invalid payload.' 'message': 'Invalid payload.'
@ -91,6 +92,7 @@ def login_user():
# handler errors # handler errors
except (exc.IntegrityError, exc.OperationalError, ValueError) as e: except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
db.session.rollback() db.session.rollback()
appLog.error(e)
response_object = { response_object = {
'status': 'error', 'status': 'error',
'message': 'Try again' 'message': 'Try again'

View File

@ -13,6 +13,7 @@ class User(db.Model):
email = db.Column(db.String(120), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(255), nullable=False) password = db.Column(db.String(255), nullable=False)
created_at = db.Column(db.DateTime, nullable=False) created_at = db.Column(db.DateTime, nullable=False)
admin = db.Column(db.Boolean, default=False, nullable=False)
def __repr__(self): def __repr__(self):
return '<User %r>' % self.username return '<User %r>' % self.username

View File

@ -16,16 +16,9 @@ def recreate_db():
@app.cli.command() @app.cli.command()
def seed_db(): def seed_db():
"""Seeds the database.""" """Seeds the database."""
db.session.add(User( admin = User(username='admin', email='admin@example.com', password='admin')
username='test', admin.admin = True
email='test@test.com', db.session.add(admin)
password='test'
))
db.session.add(User(
username='test2',
email='test2@test.com',
password='test2'
))
db.session.commit() db.session.commit()