#5-create-course #30
@@ -37,11 +37,24 @@ class User(UserMixin, db.Model):
|
|||||||
if new_user and "password" in data:
|
if new_user and "password" in data:
|
||||||
self.set_password(data["password"])
|
self.set_password(data["password"])
|
||||||
|
|
||||||
|
|
||||||
class Course(db.Model):
|
class Course(db.Model):
|
||||||
id = sa.Column(sa.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
name = sa.Column(sa.String(128), index=True)
|
name = sa.Column(sa.String(128), index=True)
|
||||||
description = sa.Column(sa.Text, index=True)
|
description = sa.Column(sa.Text, index=True)
|
||||||
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
|
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
|
||||||
created_at = sa.Column(sa.DateTime, default=datetime.now())
|
created_at = sa.Column(sa.DateTime)
|
||||||
|
|
||||||
|
def from_dict(self, data):
|
||||||
|
for field in ["name", "description", "instructor"]:
|
||||||
|
if field in data:
|
||||||
|
setattr(self, field, data[field])
|
||||||
|
|
||||||
|
if not self.created_at:
|
||||||
|
self.created_at = datetime.now()
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
d = {}
|
||||||
|
for f in ["id", "name", "description", "instructor", "created_at"]:
|
||||||
|
d[f] = getattr(self, f)
|
||||||
|
return d
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from app.errors import error_response
|
|||||||
from flask_login import current_user
|
from flask_login import current_user
|
||||||
|
|
||||||
from app import login, db
|
from app import login, db
|
||||||
from app.models import User
|
from app.models import Course, User
|
||||||
|
|
||||||
|
|
||||||
@login.user_loader
|
@login.user_loader
|
||||||
@@ -13,6 +13,13 @@ def load_user(user_id):
|
|||||||
return User.query.get(user_id)
|
return User.query.get(user_id)
|
||||||
|
|
||||||
|
|
||||||
|
def check_data(data, required_fields):
|
||||||
|
for f in required_fields:
|
||||||
|
if f not in data:
|
||||||
|
return f
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/login", methods=["POST"])
|
@bp.route("/login", methods=["POST"])
|
||||||
def login_route():
|
def login_route():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
@@ -46,10 +53,8 @@ def logout_route():
|
|||||||
@bp.route("/register", methods=["POST"])
|
@bp.route("/register", methods=["POST"])
|
||||||
def register():
|
def register():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
||||||
required_fields = ["role", "username", "email", "password", "password2"]
|
required_fields = ["role", "username", "email", "password", "password2"]
|
||||||
for f in required_fields:
|
if f := check_data(data, required_fields):
|
||||||
if f not in data:
|
|
||||||
return error_response(400, f"Must supply {f}")
|
return error_response(400, f"Must supply {f}")
|
||||||
|
|
||||||
if User.query.filter_by(username=data["username"]).first():
|
if User.query.filter_by(username=data["username"]).first():
|
||||||
@@ -65,3 +70,28 @@ def register():
|
|||||||
|
|
||||||
resp = jsonify(u.to_dict())
|
resp = jsonify(u.to_dict())
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/course", methods=["POST"])
|
||||||
|
def create_course():
|
||||||
|
data = request.get_json()
|
||||||
|
|
||||||
|
required_fields = ["name", "description", "instructor"]
|
||||||
|
|
||||||
|
if f := check_data(data, required_fields):
|
||||||
|
return error_response(400, f"Must supply {f}")
|
||||||
|
|
||||||
|
u = User.query.get(data["instructor"])
|
||||||
|
if not u:
|
||||||
|
return error_response(400, f"User with id {data['instructor']} does not exist")
|
||||||
|
|
||||||
|
if u.role != "instructor":
|
||||||
|
return error_response(400, "User is not instructor")
|
||||||
|
|
||||||
|
c = Course()
|
||||||
|
c.from_dict(data)
|
||||||
|
db.session.add(c)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return jsonify(c.to_dict())
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""empty message
|
"""Create course model
|
||||||
|
|
||||||
Revision ID: 471b4225837e
|
Revision ID: 471b4225837e
|
||||||
Revises: 8e48199f1417
|
Revises: 8e48199f1417
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""empty message
|
"""Create user model
|
||||||
|
|
||||||
Revision ID: 7736bc740f9b
|
Revision ID: 7736bc740f9b
|
||||||
Revises:
|
Revises:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""empty message
|
"""Add user role, remove about_me
|
||||||
|
|
||||||
Revision ID: 8e48199f1417
|
Revision ID: 8e48199f1417
|
||||||
Revises: 7736bc740f9b
|
Revises: 7736bc740f9b
|
||||||
|
|||||||
Reference in New Issue
Block a user