Merge pull request #2 from sorrelbri/sj-api-routes

sj api routes
This commit is contained in:
sorrelbri 2019-10-04 15:03:09 -07:00 committed by GitHub
commit 63fc6b5c50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 152 additions and 34 deletions

14
api/api.py Normal file
View file

@ -0,0 +1,14 @@
from flask import Blueprint, request, jsonify, session
from .users.user_endpoint import UserEndpoint
api = Blueprint('api', __name__, url_prefix='/api')
@api.route('/home', methods=['GET'])
def api_home():
response = {"message": "home page"}
return jsonify(response)
@api.route('/users')
def api_users():
return jsonify(UserEndpoint.users())

0
api/users/__init__.py Normal file
View file

View file

@ -0,0 +1,5 @@
class UserEndpoint(object):
def users():
response = {"message": "users page"}
return response

29
app.py
View file

@ -14,17 +14,15 @@ from flask_cors import CORS
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
# base directory
basedir = os.path.abspath(os.path.dirname(__file__))
# dev database
DATABASE = 'postgresql://localhost/browser-go'
# config database # config database
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE app_settings = os.getenv(
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 'APP_SETTINGS',
'config.DevelopmentConfig'
)
#init bcrypt app.config.from_object(app_settings)
# init bcrypt
bcrypt = Bcrypt(app) bcrypt = Bcrypt(app)
# init database # init database
@ -34,12 +32,7 @@ db = SQLAlchemy(app)
ma = Marshmallow(app) ma = Marshmallow(app)
# init all db models # init all db models
from .models.User import User import models
from .models.GameRoom import GameRoom
from .models.TimeSettings import TimeSettings
from .models.Game import Game
from .models.Move import Move
from .models.Message import Message
migrate = Migrate(app, db) migrate = Migrate(app, db)
@ -54,5 +47,11 @@ PORT = 8000
def hello_world(): def hello_world():
return 'Hello World' return 'Hello World'
# Blue prints
from api.api import api
app.register_blueprint(api)
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=DEBUG, port=PORT) app.run(debug=DEBUG, port=PORT)

32
config.py Normal file
View file

@ -0,0 +1,32 @@
import os
# local db
DATABASE = 'postgresql://localhost/browser-go'
class BaseConfig:
"""Base configuration."""
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG = False
BCRYPT_LOG_ROUNDS = 13
SQLALCHEMY_TRACK_MODIFICATIONS = False
class DevelopmentConfig(BaseConfig):
"""Development configuration."""
DEBUG = True
BCRYPT_LOG_ROUNDS = 4
SQLALCHEMY_DATABASE_URI = DATABASE
class TestingConfig(BaseConfig):
"""Testing configuration."""
DEBUG = True
TESTING = True
BCRYPT_LOG_ROUNDS = 4
SQLALCHEMY_DATABASE_URI = DATABASE
PRESERVE_CONTEXT_ON_EXCEPTION = False
class ProductionConfig(BaseConfig):
"""Production configuration."""
SECRET_KEY = ''
DEBUG = False
SQLALCHEMY_DATABASE_URI = 'postgresql:///'

39
manage.py Normal file
View file

@ -0,0 +1,39 @@
import os
import unittest
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app, db
migrate = Migrate(app, db)
manager = Manager(app)
# migrations
manager.add_command('db', MigrateCommand)
@manager.command
def test():
"""Runs the unit tests without test coverage."""
tests = unittest.TestLoader().discover('browser-go-api/tests', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
return 1
@manager.command
def create_db():
"""Creates the db tables."""
db.create_all()
@manager.command
def drop_db():
"""Drops the db tables."""
db.drop_all()
if __name__ == '__main__':
manager.run()

View file

@ -1 +1,6 @@
from app import db, ma from models.User import User
from models.GameRoom import GameRoom
from models.TimeSettings import TimeSettings
from models.Game import Game
from models.Move import Move
from models.Message import Message

View file

@ -1,4 +1,4 @@
from ..app import db, ma from app import db, ma
import enum import enum
class Players(enum.Enum): class Players(enum.Enum):

View file

@ -1,4 +1,4 @@
from ..app import db, ma from app import db, ma
import enum import enum
class Languages(enum.Enum): class Languages(enum.Enum):

View file

@ -1,4 +1,4 @@
from ..app import db, ma from app import db, ma
import enum import enum
class Players(enum.Enum): class Players(enum.Enum):

View file

@ -1,4 +1,4 @@
from ..app import db, ma from app import db, ma
import enum import enum
class Players(enum.Enum): class Players(enum.Enum):

View file

@ -1,4 +1,4 @@
from ..app import db, ma from app import db, ma
import enum import enum
class TimeTypes(enum.Enum): class TimeTypes(enum.Enum):

View file

@ -1,4 +1,5 @@
from ..app import db, ma, bcrypt from app import db, ma, bcrypt
import datetime
import enum import enum
class Ranks(enum.Enum): # with minimal Elo rating class Ranks(enum.Enum): # with minimal Elo rating
@ -39,6 +40,7 @@ class Ranks(enum.Enum): # with minimal Elo rating
K28 = "Twenty-Eight Kyu" K28 = "Twenty-Eight Kyu"
K29 = "Twenty-Nine Kyu" K29 = "Twenty-Nine Kyu"
K30 = "Thirty Kyu" # Elo -900 K30 = "Thirty Kyu" # Elo -900
RU = "Unknown Rank"
class User(db.Model): class User(db.Model):
__tablename__ = "users" __tablename__ = "users"
@ -52,7 +54,10 @@ class User(db.Model):
elo = db.Column(db.Integer) elo = db.Column(db.Integer)
rank_certainty = db.Column(db.Boolean, nullable=False, default=False) rank_certainty = db.Column(db.Boolean, nullable=False, default=False)
def __init__(self): def __init__(self, email, password, admin=False,):
self.email = email
self.password = bcrypt.generate_password_hash( self.password = bcrypt.generate_password_hash(
password, app.config.get('BCRYPT_LOG_ROUNDS') password, app.config.get('BCRYPT_LOG_ROUNDS')
).decode() ).decode()
self.registered_on = datetime.datetime.now()
self.admin = admin

View file

@ -8,7 +8,9 @@ Flask-Bcrypt==0.7.1
Flask-Cors==3.0.8 Flask-Cors==3.0.8
flask-marshmallow==0.10.1 flask-marshmallow==0.10.1
Flask-Migrate==2.5.2 Flask-Migrate==2.5.2
Flask-Script==2.0.6
Flask-SQLAlchemy==2.4.1 Flask-SQLAlchemy==2.4.1
Flask-Testing==0.7.1
isort==4.3.21 isort==4.3.21
itsdangerous==1.1.0 itsdangerous==1.1.0
Jinja2==2.10.1 Jinja2==2.10.1

0
tests/__init__.py Normal file
View file

14
tests/base.py Normal file
View file

@ -0,0 +1,14 @@
from flask_testing import TestCase
from ..app import app, db
class BaseTestCase(TestCase):
""" Base Tests """
def create_app(self):
pass
def setUp(self):
pass
def tearDown(self):
pass

14
tests/test_config.py Normal file
View file

@ -0,0 +1,14 @@
import unittest
from flask import current_app
from flask_testing import TestCase
from ..app import app
class TestDevelopmentConfig(TestCase):
def create_app(self):
app.config.from_object('browser-go-api.config.DevelopmentConfig')
return app
def test_app_is_development(self):
self.assertFalse(app.config['SECRET_KEY'] is 'my_precious')

View file

@ -1,11 +0,0 @@
from flask import Blueprint
user = Blueprint('users', __name__)
@user.route('/<int:user_id>')
def func():
pass
@user.route('/<str:user_username>')
def func():
pass