Merge pull request #11 from sorrelbri/sj-gameroom-endpoints
create room
This commit is contained in:
commit
254655eed4
6 changed files with 88 additions and 10 deletions
|
@ -1,13 +1,15 @@
|
||||||
from flask import Blueprint, request, jsonify, session
|
from flask import Blueprint, request, jsonify, session, abort
|
||||||
import os
|
import os
|
||||||
import jwt
|
import jwt
|
||||||
|
|
||||||
def jwt_required():
|
def jwt_required():
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
def authorized(*args, **kwargs):
|
def authorized(*args, **kwargs):
|
||||||
|
print(request.headers)
|
||||||
auth_header = request.headers.get('Authorization') or None
|
auth_header = request.headers.get('Authorization') or None
|
||||||
if auth_header:
|
if auth_header:
|
||||||
auth_token = auth_header.split(" ")[1]
|
auth_token = auth_header.split(" ")[1]
|
||||||
|
print(auth_token)
|
||||||
if jwt.decode(auth_token, os.environ.get('SECRET_KEY')):
|
if jwt.decode(auth_token, os.environ.get('SECRET_KEY')):
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
from flask import Blueprint, request, jsonify, session
|
||||||
from models.User import User, user_schema, users_schema
|
from models.User import User, user_schema, users_schema
|
||||||
from models.GameRoom import GameRoom
|
from models.GameRoom import GameRoom
|
||||||
from flask import Blueprint, request, jsonify, session
|
from database import db
|
||||||
from ..decorators import jwt_required
|
from ..decorators import jwt_required
|
||||||
|
|
||||||
api_rooms = Blueprint('api_rooms', __name__, url_prefix='/api/rooms')
|
api_rooms = Blueprint('api_rooms', __name__, url_prefix='/api/rooms')
|
||||||
|
@ -18,13 +19,16 @@ def get_rooms():
|
||||||
@api_rooms.route('/', methods=['POST'])
|
@api_rooms.route('/', methods=['POST'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def post_room():
|
def post_room():
|
||||||
|
print('Hey it\'s a POST request')
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
print(data)
|
||||||
try:
|
try:
|
||||||
room = GameRoom(
|
room = GameRoom(
|
||||||
name = data['name'],
|
name = data['name'],
|
||||||
description = data['description'],
|
description = data['description'],
|
||||||
private = data['private'],
|
# TODO add support for private rooms and multiple languages
|
||||||
language = data['language']
|
# private = data['private'],
|
||||||
|
# language = data['language']
|
||||||
)
|
)
|
||||||
db.session.add(room)
|
db.session.add(room)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
30
migrations/versions/bafcadd8390e_.py
Normal file
30
migrations/versions/bafcadd8390e_.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
"""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 ###
|
35
migrations/versions/e5b4175be075_.py
Normal file
35
migrations/versions/e5b4175be075_.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
"""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 ###
|
|
@ -1,14 +1,19 @@
|
||||||
from app import db, ma
|
from app import db, ma
|
||||||
# TODO User >---< GameRoom
|
# TODO User >---< GameRoom
|
||||||
import enum
|
|
||||||
|
|
||||||
|
# ! Game Room >-< Users join table
|
||||||
game_rooms_users = db.Table('game_rooms_users',
|
game_rooms_users = db.Table('game_rooms_users',
|
||||||
db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
|
db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
|
||||||
db.Column('game_rooms_id', db.Integer, db.ForeignKey('game_rooms.id'), primary_key=True)
|
db.Column('game_rooms_id', db.Integer, db.ForeignKey('game_rooms.id'), primary_key=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
class Languages(enum.Enum):
|
class RoomLanguages(db.Model):
|
||||||
EN = "English"
|
__tablename__ = "room_languages"
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
|
name = db.Column(db.String(40), nullable=False)
|
||||||
|
iso = db.Column(db.String(2), nullable=False)
|
||||||
|
|
||||||
|
|
||||||
class GameRoom(db.Model):
|
class GameRoom(db.Model):
|
||||||
__tablename__ = "game_rooms"
|
__tablename__ = "game_rooms"
|
||||||
|
@ -18,7 +23,6 @@ class GameRoom(db.Model):
|
||||||
name = db.Column(db.String(40), nullable=False)
|
name = db.Column(db.String(40), nullable=False)
|
||||||
description = db.Column(db.String(200), nullable=False)
|
description = db.Column(db.String(200), nullable=False)
|
||||||
private = db.Column(db.Boolean(), nullable=False, default=False)
|
private = db.Column(db.Boolean(), nullable=False, default=False)
|
||||||
language = db.Column(db.Enum(Languages), nullable=False)
|
|
||||||
|
|
||||||
# ! Foreign Keys
|
# ! Foreign Keys
|
||||||
users = db.relationship(
|
users = db.relationship(
|
||||||
|
@ -28,7 +32,10 @@ class GameRoom(db.Model):
|
||||||
backref=db.backref('game_rooms', lazy=True)
|
backref=db.backref('game_rooms', lazy=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, name, description, private=False, language=Languages.EN):
|
language = db.Column(db.Integer, db.ForeignKey("room_languages.id"), nullable=False)
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, name, description, private=False, language=1):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
self.private = private
|
self.private = private
|
||||||
|
|
|
@ -61,7 +61,7 @@ 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, username, email, password, rank=Ranks.UR, admin=False):
|
def __init__(self, username, email, password, rank=Ranks.K1, admin=False):
|
||||||
self.username = username
|
self.username = username
|
||||||
self.email = email
|
self.email = email
|
||||||
self.password = bcrypt.generate_password_hash(
|
self.password = bcrypt.generate_password_hash(
|
||||||
|
|
Loading…
Reference in a new issue