add rooms post route

This commit is contained in:
Sorrel Bri 2019-10-08 22:53:37 -07:00
parent 34182fdc23
commit 8cd0856728
4 changed files with 55 additions and 10 deletions

View file

@ -1,12 +1,18 @@
from flask import Blueprint, request, jsonify, session from flask import Blueprint, request, jsonify, session
import os
import jwt
def jwt_required(): def jwt_required():
def decorator(func): def decorator(func):
def authorized(*args, **kwargs): def authorized(*args, **kwargs):
auth_header = request.headers.get('Authorization') or None auth_header = request.headers.get('Authorization') or None
print(User.decode_auth_token(auth_header.split(" ")[1]))
if auth_header: if auth_header:
# check secret on auth header auth_token = auth_header.split(" ")[1]
return func(*args, **kwargs) if jwt.decode(auth_token, os.environ.get('SECRET_KEY')):
return func(*args, **kwargs)
else:
abort(401)
else: else:
abort(401) abort(401)
return authorized return authorized

View file

@ -1,6 +1,7 @@
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 flask import Blueprint, request, jsonify, session
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')
@ -15,5 +16,28 @@ def get_rooms():
# protected route # protected route
@api_rooms.route('/', methods=['POST']) @api_rooms.route('/', methods=['POST'])
@jwt_required()
def post_room(): def post_room():
pass data = request.get_json()
try:
room = GameRoom(
name = data['name'],
description = data['description'],
private = data['private'],
language = data['language']
)
db.session.add(room)
db.session.commit()
response = {
'status': 'success',
'message': 'Succesfully registered.',
'gameRoom': room.id
}
return jsonify(response), 201
except Exception as e:
print(e.__dict__)
response = {
'status': 'fail',
'message': 'There was an error. Please try again.'
}
return jsonify(response), 401

View file

@ -1,8 +1,14 @@
from app import db, ma from app import db, ma
# TODO User >---< GameRoom
import enum import enum
game_rooms_users = db.Table('game_rooms_users',
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)
)
class Languages(enum.Enum): class Languages(enum.Enum):
EN: "English" EN = "English"
class GameRoom(db.Model): class GameRoom(db.Model):
__tablename__ = "game_rooms" __tablename__ = "game_rooms"
@ -14,5 +20,16 @@ class GameRoom(db.Model):
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) language = db.Column(db.Enum(Languages), nullable=False)
def __init__(self): # ! Foreign Keys
pass users = db.relationship(
'User',
secondary=game_rooms_users,
lazy='subquery',
backref=db.backref('game_rooms', lazy=True)
)
def __init__(self, name, description, private=False, language=Languages.EN):
self.name = name
self.description = description
self.private = private
self.language = language

View file

@ -46,7 +46,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" UR = "Unknown Rank"
class User(db.Model): class User(db.Model):
__tablename__ = "users" __tablename__ = "users"
@ -61,9 +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.K1, admin=False): def __init__(self, username, email, password, rank=Ranks.UR, admin=False):
print(rank)
print(Ranks)
self.username = username self.username = username
self.email = email self.email = email
self.password = bcrypt.generate_password_hash( self.password = bcrypt.generate_password_hash(