refactor database models

This commit is contained in:
Sorrel Bri 2019-10-10 14:57:29 -07:00
parent e6fa8a7e31
commit df3313d089
20 changed files with 190 additions and 333 deletions

View file

@ -1,6 +1,7 @@
from flask import Blueprint, request, jsonify, session
from .users.api_users import api_users
from .rooms.api_rooms import api_rooms
from .games.api_games import api_games
from auth.auth import auth
@ -9,6 +10,7 @@ api = Blueprint('api', __name__, url_prefix='/api')
def register_api_endpoints(app):
app.register_blueprint(api_users)
app.register_blueprint(api_rooms)
app.register_blueprint(api_games)
app.register_blueprint(api)
app.register_blueprint(auth)
return app

View file

@ -5,11 +5,9 @@ import jwt
def jwt_required():
def decorator(func):
def authorized(*args, **kwargs):
print(request.headers)
auth_header = request.headers.get('Authorization') or None
if auth_header:
auth_token = auth_header.split(" ")[1]
print(auth_token)
if jwt.decode(auth_token, os.environ.get('SECRET_KEY')):
return func(*args, **kwargs)
else:

53
api/games/api_games.py Normal file
View file

@ -0,0 +1,53 @@
from flask import Blueprint, request, jsonify, session
from models.User import User, user_schema, users_schema
from models.GameRoom import GameRoom, rooms_schema, room_schema
from models.Game import Game
from database import db
from ..decorators import jwt_required
import jwt
import os
import json
api_games = Blueprint('api_games', __name__, url_prefix='/api/games')
@api_games.route('/', methods=['POST'])
@jwt_required()
def post_game():
print('Hey it\'s a post request!')
data = request.get_json()
# TODO create decorator that returns user from header
auth_header = request.headers.get('Authorization')
user = jwt.decode(auth_header.split(" ")[1], os.environ.get('SECRET_KEY'))['user']
print(user)
print('data')
print(data)
user_id = json.loads(user)['id']
try:
game = Game(
name = data['name'],
description = data['description'],
board_size = data['boardSize'],
game_room = data['gameRoom'],
player_white = user_id
)
print(game)
db.session.add(game)
print('game added')
db.session.commit()
print('game')
print(game)
response = {
'status': 'success',
'message': 'Game created',
'game': game.id
}
return jsonify(response), 201
except Exception as e:
print('error')
print(e)
print(e.__dict__)
response = {
'status': 'fail',
'message': 'There was an error. Please try again.'
}
return jsonify(response), 401

View file

@ -1,7 +0,0 @@
from flask import Blueprint
game = Blueprint('games', __name__)
@game.route('/<int:game_id>')
def func():
pass

View file

@ -1,14 +1,18 @@
from flask import Blueprint, request, jsonify, session
from models.User import User, user_schema, users_schema
from models.GameRoom import GameRoom, rooms_schema, room_schema
from models.Game import Game, games_schema
from database import db
from ..decorators import jwt_required
api_rooms = Blueprint('api_rooms', __name__, url_prefix='/api/rooms')
@api_rooms.route('/<room_id>', methods=['GET'])
def get_room():
pass
def get_room(room_id):
print(room_id)
games = Game.query.filter_by(game_room=room_id).all()
response = games_schema.dumps(games)
return jsonify(response)
@api_rooms.route('/', methods=['GET'])
def get_rooms():
@ -40,6 +44,7 @@ def post_room():
}
return jsonify(response), 201
except Exception as e:
print(e)
print(e.__dict__)
response = {
'status': 'fail',

View file

@ -9,7 +9,7 @@ def api_get_users():
print('called one')
users = User.query.all()
response = users_schema.dumps(users)
return jsonify(response)
return jsonify(response), 200
@api_users.route('/users/account', methods=['GET'])

View file

@ -1,34 +0,0 @@
"""empty message
Revision ID: 02be4c8fbd69
Revises: 0e0f8ad1362d
Create Date: 2019-10-09 10:05:32.403967
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '02be4c8fbd69'
down_revision = '0e0f8ad1362d'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('game_rooms_users',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('game_rooms_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['game_rooms_id'], ['game_rooms.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id', 'game_rooms_id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('game_rooms_users')
# ### end Alembic commands ###

View file

@ -1,30 +0,0 @@
"""empty message
Revision ID: 0e0f8ad1362d
Revises: 50aab465cf44
Create Date: 2019-10-05 23:21:57.620808
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '0e0f8ad1362d'
down_revision = '50aab465cf44'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('username', sa.String(length=255), nullable=False))
op.create_unique_constraint(None, 'users', ['username'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'users', type_='unique')
op.drop_column('users', 'username')
# ### end Alembic commands ###

View file

@ -1,28 +0,0 @@
"""empty message
Revision ID: 16c70b11242e
Revises: 9519d28f13f0
Create Date: 2019-10-02 14:50:27.180692
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '16c70b11242e'
down_revision = '9519d28f13f0'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('moves', sa.Column('is_main', sa.Boolean(), nullable=False))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('moves', 'is_main')
# ### end Alembic commands ###

View file

@ -1,28 +0,0 @@
"""empty message
Revision ID: 50aab465cf44
Revises: bfa4b406e22f
Create Date: 2019-10-05 22:37:07.643522
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '50aab465cf44'
down_revision = 'bfa4b406e22f'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('password', sa.String(length=255), nullable=False))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'password')
# ### end Alembic commands ###

View file

@ -1,8 +1,8 @@
"""empty message
Revision ID: 9519d28f13f0
Revision ID: acabfc6d158d
Revises:
Create Date: 2019-10-02 13:54:17.877590
Create Date: 2019-10-10 14:25:27.198731
"""
from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '9519d28f13f0'
revision = 'acabfc6d158d'
down_revision = None
branch_labels = None
depends_on = None
@ -18,12 +18,10 @@ depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('game_rooms',
op.create_table('room_languages',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=40), nullable=False),
sa.Column('description', sa.String(length=200), nullable=False),
sa.Column('private', sa.Boolean(), nullable=False),
sa.Column('language', sa.Enum(name='languages'), nullable=False),
sa.Column('iso', sa.String(length=2), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('time_settings',
@ -38,15 +36,33 @@ def upgrade():
)
op.create_table('users',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('username', sa.String(length=255), autoincrement=True, nullable=False),
sa.Column('email', sa.String(length=255), nullable=False),
sa.Column('password', sa.String(length=255), nullable=False),
sa.Column('registered_on', sa.DateTime(), nullable=False),
sa.Column('admin', sa.Boolean(), nullable=False),
sa.Column('rank', sa.Enum('D7', 'D6', 'D5', 'D4', 'D3', 'D2', 'D1', 'K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8', 'K9', 'K10', 'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20', 'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', name='ranks'), nullable=True),
sa.Column('rank', sa.Enum('D7', 'D6', 'D5', 'D4', 'D3', 'D2', 'D1', 'K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8', 'K9', 'K10', 'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20', 'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', 'UR', name='ranks'), nullable=True),
sa.Column('elo', sa.Integer(), nullable=True),
sa.Column('rank_certainty', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email')
sa.UniqueConstraint('email'),
sa.UniqueConstraint('username')
)
op.create_table('game_rooms',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=40), nullable=False),
sa.Column('description', sa.String(length=200), nullable=False),
sa.Column('private', sa.Boolean(), nullable=False),
sa.Column('language', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['language'], ['room_languages.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('game_rooms_users',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('game_rooms_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['game_rooms_id'], ['game_rooms.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id', 'game_rooms_id')
)
op.create_table('games',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
@ -81,6 +97,8 @@ def upgrade():
sa.Column('x_point', sa.Integer(), nullable=True),
sa.Column('y_point', sa.Integer(), nullable=True),
sa.Column('move_number', sa.Integer(), nullable=True),
sa.Column('is_pass', sa.Boolean(), nullable=False),
sa.Column('is_main', sa.Boolean(), nullable=False),
sa.Column('game', sa.Integer(), nullable=False),
sa.Column('preceding_move', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['game'], ['games.id'], ),
@ -103,7 +121,9 @@ def downgrade():
op.drop_table('messages')
op.drop_table('moves')
op.drop_table('games')
op.drop_table('game_rooms_users')
op.drop_table('game_rooms')
op.drop_table('users')
op.drop_table('time_settings')
op.drop_table('game_rooms')
op.drop_table('room_languages')
# ### end Alembic commands ###

View file

@ -1,30 +0,0 @@
"""empty message
Revision ID: bafcadd8390e
Revises: e5b4175be075
Create Date: 2019-10-09 15:34:31.194998
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'bafcadd8390e'
down_revision = 'e5b4175be075'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('game_rooms', sa.Column('language', sa.Integer(), nullable=False))
op.create_foreign_key(None, 'game_rooms', 'room_languages', ['language'], ['id'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'game_rooms', type_='foreignkey')
op.drop_column('game_rooms', 'language')
# ### end Alembic commands ###

View file

@ -1,28 +0,0 @@
"""empty message
Revision ID: bdc3cd5d7499
Revises: 16c70b11242e
Create Date: 2019-10-05 22:34:04.358841
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'bdc3cd5d7499'
down_revision = '16c70b11242e'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('moves', sa.Column('is_pass', sa.Boolean(), nullable=False))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('moves', 'is_pass')
# ### end Alembic commands ###

View file

@ -1,28 +0,0 @@
"""empty message
Revision ID: bfa4b406e22f
Revises: bdc3cd5d7499
Create Date: 2019-10-05 22:36:45.420054
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = 'bfa4b406e22f'
down_revision = 'bdc3cd5d7499'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'password')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('password', postgresql.TIMESTAMP(), autoincrement=False, nullable=False))
# ### end Alembic commands ###

View file

@ -1,35 +0,0 @@
"""empty message
Revision ID: e5b4175be075
Revises: 02be4c8fbd69
Create Date: 2019-10-09 15:34:06.312258
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = 'e5b4175be075'
down_revision = '02be4c8fbd69'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('room_languages',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=40), nullable=False),
sa.Column('iso', sa.String(length=2), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.drop_column('game_rooms', 'language')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('game_rooms', sa.Column('language', postgresql.ENUM(name='languages'), autoincrement=False, nullable=False))
op.drop_table('room_languages')
# ### end Alembic commands ###

View file

@ -1,5 +1,12 @@
from app import db, ma
from marshmallow import fields
import enum
from models.User import user_schema
class Game(db.Model):
__tablename__ = "games"
__table_args__ = {'extend_existing': True}
class Players(enum.Enum):
BLACK = "The player taking black stones"
@ -13,10 +20,6 @@ class WinType(enum.Enum):
TIME = "The game ended in loss by time out"
VOID = "The game was suspended"
class Game(db.Model):
__tablename__ = "games"
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
date = db.Column(db.DateTime())
komi = db.Column(db.Numeric(2,1), nullable=False)
@ -40,5 +43,23 @@ class Game(db.Model):
player_black = db.Column(db.Integer, db.ForeignKey("users.id"))
player_white = db.Column(db.Integer, db.ForeignKey("users.id"))
def __init__(self):
pass
def __init__(self, name, description, board_size, game_room, player_white, komi=0.5, handicap=0, time_settings=1):
self.name = name
self.description = description
self.board_size = board_size
self.game_room = game_room
self.player_white = player_white
self.komi = komi
self.handicap = handicap
self.time_settings = time_settings
print('did it')
class GameSchema(ma.ModelSchema):
id = fields.Int()
name = fields.Str()
description = fields.Str()
boardSize = fields.Int()
player = fields.Nested(user_schema)
game_schema = GameSchema()
games_schema = GameSchema(many=True)

View file

@ -1,14 +1,15 @@
from app import db, ma
import enum
class Players(enum.Enum):
BLACK = "The player taking black stones"
WHITE = "The player taking white stones"
class Message(db.Model):
__tablename__ = "messages"
__table_args__ = {'extend_existing': True}
class Players(enum.Enum):
BLACK = "The player taking black stones"
WHITE = "The player taking white stones"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
date = db.Column(db.DateTime(), nullable=False)
content = db.Column(db.String(200), nullable=False)

View file

@ -1,14 +1,15 @@
from app import db, ma
import enum
class Players(enum.Enum):
BLACK = "The player taking black stones"
WHITE = "The player taking white stones"
class Move(db.Model):
__tablename__ = "moves"
__table_args__ = {'extend_existing': True}
class Players(enum.Enum):
BLACK = "The player taking black stones"
WHITE = "The player taking white stones"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
player = db.Column(db.Enum(Players))
x_point = db.Column(db.Integer)

View file

@ -1,15 +1,17 @@
from app import db, ma
import enum
class TimeSettings(db.Model):
__tablename__ = "time_settings"
__table_args__ = {'extend_existing': True}
class TimeTypes(enum.Enum):
BYOYOMI = "Counting by time period"
ABSOLUTE = "One period to use time"
HOURGLASS = "Absolute time for both players"
NONE = "Untimed"
class TimeSettings(db.Model):
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
main_time = db.Column(db.Enum(TimeTypes), nullable=False)
time_period = db.Column(db.Integer) # number of periods
@ -19,5 +21,6 @@ class TimeSettings(db.Model):
overtime_length = db.Column(db.Integer) # seconds
def __init__(self):
pass
def __init__(self, main_time=TimeTypes.NONE, overtime=TimeTypes.NONE):
self.main_time = main_time
self.overtime = overtime

View file

@ -8,6 +8,10 @@ import json
import jwt
import os
class User(db.Model):
__tablename__ = "users"
class Ranks(enum.Enum): # with minimal Elo rating
D7 = "Seven Dan" # Elo 2700+
D6 = "Six Dan"
@ -48,9 +52,6 @@ class Ranks(enum.Enum): # with minimal Elo rating
K30 = "Thirty Kyu" # Elo -900
UR = "Unknown Rank"
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(255), unique=True, nullable=False, autoincrement=True)
email = db.Column(db.String(255), unique=True, nullable=False)