#70 Add endpoint to create content
This commit is contained in:
@@ -77,6 +77,7 @@ class Course(db.Model):
|
|||||||
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
|
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
|
||||||
created_at = sa.Column(sa.DateTime)
|
created_at = sa.Column(sa.DateTime)
|
||||||
assignments = db.relationship("Assignment", backref="course", lazy="dynamic")
|
assignments = db.relationship("Assignment", backref="course", lazy="dynamic")
|
||||||
|
content = db.relationship("Content", backref="course", lazy="dynamic")
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<Course {self.course_code}>"
|
return f"<Course {self.course_code}>"
|
||||||
@@ -130,7 +131,7 @@ class Content(db.Model):
|
|||||||
created_at = sa.Column(sa.DateTime)
|
created_at = sa.Column(sa.DateTime)
|
||||||
|
|
||||||
def from_dict(self, data) -> None:
|
def from_dict(self, data) -> None:
|
||||||
for field in ["name", "body"]:
|
for field in ["name", "body", "course_id"]:
|
||||||
if field in data:
|
if field in data:
|
||||||
setattr(self, field, data[field])
|
setattr(self, field, data[field])
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,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 Course, User, Assignment
|
from app.models import Content, Course, User, Assignment
|
||||||
|
|
||||||
|
|
||||||
@login.user_loader
|
@login.user_loader
|
||||||
@@ -31,6 +31,16 @@ def instructor_required(func):
|
|||||||
return dec
|
return dec
|
||||||
|
|
||||||
|
|
||||||
|
def admin_required(func):
|
||||||
|
@wraps(func)
|
||||||
|
def dec(*args, **kwargs):
|
||||||
|
if current_user.role != "admin":
|
||||||
|
return error_response(400, "User is not an admin!")
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return dec
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/login", methods=["POST"])
|
@bp.route("/login", methods=["POST"])
|
||||||
def login_route():
|
def login_route():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
@@ -139,6 +149,7 @@ def get_students_in_course(id):
|
|||||||
resp["students"].append(s.to_dict())
|
resp["students"].append(s.to_dict())
|
||||||
return jsonify(resp)
|
return jsonify(resp)
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/course/<int:id>/assignments", methods=["GET"])
|
@bp.route("/course/<int:id>/assignments", methods=["GET"])
|
||||||
@login_required
|
@login_required
|
||||||
def get_assignments_in_course(id):
|
def get_assignments_in_course(id):
|
||||||
@@ -234,6 +245,7 @@ def create_assignment():
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
return jsonify(a.to_dict())
|
return jsonify(a.to_dict())
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/assignment/<int:id>", methods=["GET"])
|
@bp.route("/assignment/<int:id>", methods=["GET"])
|
||||||
@login_required
|
@login_required
|
||||||
def get_assignment(id):
|
def get_assignment(id):
|
||||||
@@ -243,6 +255,7 @@ def get_assignment(id):
|
|||||||
|
|
||||||
return jsonify(a.to_dict())
|
return jsonify(a.to_dict())
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/assignment/<int:id>", methods=["DELETE"])
|
@bp.route("/assignment/<int:id>", methods=["DELETE"])
|
||||||
@login_required
|
@login_required
|
||||||
@instructor_required
|
@instructor_required
|
||||||
@@ -255,6 +268,7 @@ def delete_assignment(id):
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
return jsonify(a.to_dict())
|
return jsonify(a.to_dict())
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/assignment/<int:id>", methods=["PUT"])
|
@bp.route("/assignment/<int:id>", methods=["PUT"])
|
||||||
@login_required
|
@login_required
|
||||||
@instructor_required
|
@instructor_required
|
||||||
@@ -273,3 +287,19 @@ def update_assignment(id):
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
return jsonify(a.to_dict())
|
return jsonify(a.to_dict())
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/content", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
@instructor_required
|
||||||
|
def create_content():
|
||||||
|
data = request.get_json()
|
||||||
|
required_fields = ["name", "body", "course_id"]
|
||||||
|
if f := check_data(data, required_fields):
|
||||||
|
return error_response(400, f"Must supply {f}")
|
||||||
|
|
||||||
|
c = Content()
|
||||||
|
c.from_dict(data)
|
||||||
|
db.session.add(c)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return jsonify(c.to_dict())
|
||||||
|
|||||||
Reference in New Issue
Block a user