From b6056e6125da46cb1829319903113f9c4ad618e0 Mon Sep 17 00:00:00 2001 From: SamR1 Date: Sun, 17 Dec 2017 09:16:08 +0100 Subject: [PATCH] API: minor fixes & db update --- mpwo_api/mpwo_api/__init__.py | 2 +- mpwo_api/mpwo_api/tests/test_auth.py | 3 ++- mpwo_api/mpwo_api/tests/test_users.py | 2 +- mpwo_api/mpwo_api/tests/utils.py | 2 +- mpwo_api/mpwo_api/users/auth.py | 4 +++- mpwo_api/mpwo_api/users/models.py | 1 + mpwo_api/server.py | 13 +++---------- 7 files changed, 12 insertions(+), 15 deletions(-) diff --git a/mpwo_api/mpwo_api/__init__.py b/mpwo_api/mpwo_api/__init__.py index 60435e28..7cb2f79c 100644 --- a/mpwo_api/mpwo_api/__init__.py +++ b/mpwo_api/mpwo_api/__init__.py @@ -27,7 +27,7 @@ app.register_blueprint(users_blueprint, url_prefix='/api') app.register_blueprint(auth_blueprint, url_prefix='/api') if app.debug: - logging.getLogger('sqlalchemy').setLevel(logging.INFO) + logging.getLogger('sqlalchemy').setLevel(logging.WARNING) logging.getLogger('sqlalchemy' ).handlers = logging.getLogger('werkzeug').handlers logging.getLogger('sqlalchemy.orm').setLevel(logging.WARNING) diff --git a/mpwo_api/mpwo_api/tests/test_auth.py b/mpwo_api/mpwo_api/tests/test_auth.py index 96c36bba..d6c0b097 100644 --- a/mpwo_api/mpwo_api/tests/test_auth.py +++ b/mpwo_api/mpwo_api/tests/test_auth.py @@ -39,7 +39,8 @@ class TestAuthBlueprint(BaseTestCase): ) data = json.loads(response.data.decode()) 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.assertEqual(response.status_code, 400) diff --git a/mpwo_api/mpwo_api/tests/test_users.py b/mpwo_api/mpwo_api/tests/test_users.py index 3f12f829..a77440b4 100644 --- a/mpwo_api/mpwo_api/tests/test_users.py +++ b/mpwo_api/mpwo_api/tests/test_users.py @@ -1,8 +1,8 @@ import json 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.users.models import User class TestUserService(BaseTestCase): diff --git a/mpwo_api/mpwo_api/tests/utils.py b/mpwo_api/mpwo_api/tests/utils.py index b3b2c49e..9acdd6fc 100644 --- a/mpwo_api/mpwo_api/tests/utils.py +++ b/mpwo_api/mpwo_api/tests/utils.py @@ -1,5 +1,5 @@ -from mpwo_api.users.models import User from mpwo_api import db +from mpwo_api.users.models import User def add_user(username, email, password): diff --git a/mpwo_api/mpwo_api/users/auth.py b/mpwo_api/mpwo_api/users/auth.py index 4c2b6b65..bf69d588 100644 --- a/mpwo_api/mpwo_api/users/auth.py +++ b/mpwo_api/mpwo_api/users/auth.py @@ -1,7 +1,7 @@ from flask import Blueprint, jsonify, request from sqlalchemy import exc, or_ -from mpwo_api import bcrypt, db +from mpwo_api import appLog, bcrypt, db from .models import User @@ -51,6 +51,7 @@ def register_user(): # handler errors except (exc.IntegrityError, exc.OperationalError, ValueError) as e: db.session.rollback() + appLog.error(e) response_object = { 'status': 'error', 'message': 'Invalid payload.' @@ -91,6 +92,7 @@ def login_user(): # handler errors except (exc.IntegrityError, exc.OperationalError, ValueError) as e: db.session.rollback() + appLog.error(e) response_object = { 'status': 'error', 'message': 'Try again' diff --git a/mpwo_api/mpwo_api/users/models.py b/mpwo_api/mpwo_api/users/models.py index 2f613da5..0805aab3 100644 --- a/mpwo_api/mpwo_api/users/models.py +++ b/mpwo_api/mpwo_api/users/models.py @@ -13,6 +13,7 @@ class User(db.Model): email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) created_at = db.Column(db.DateTime, nullable=False) + admin = db.Column(db.Boolean, default=False, nullable=False) def __repr__(self): return '' % self.username diff --git a/mpwo_api/server.py b/mpwo_api/server.py index 7c47836a..5cfbb796 100644 --- a/mpwo_api/server.py +++ b/mpwo_api/server.py @@ -16,16 +16,9 @@ def recreate_db(): @app.cli.command() def seed_db(): """Seeds the database.""" - db.session.add(User( - username='test', - email='test@test.com', - password='test' - )) - db.session.add(User( - username='test2', - email='test2@test.com', - password='test2' - )) + admin = User(username='admin', email='admin@example.com', password='admin') + admin.admin = True + db.session.add(admin) db.session.commit()