#10 Made endpoint to create a new assignment
This commit is contained in:
@@ -105,3 +105,26 @@ class Assignment(db.Model):
|
||||
description = sa.Column(sa.Text, index=True)
|
||||
due_date = sa.Column(sa.DateTime)
|
||||
created_at = sa.Column(sa.DateTime)
|
||||
|
||||
def from_dict(self, data) -> None:
|
||||
for field in ["name", "course_id", "description", "due_date"]:
|
||||
if field in data:
|
||||
setattr(self, field, data[field])
|
||||
|
||||
if not self.created_at:
|
||||
self.created_at = datetime.now()
|
||||
|
||||
def from_dict(self, data) -> None:
|
||||
for field in ["name", "course_id", "description", "due_date"]:
|
||||
if field in data:
|
||||
setattr(self, field, data[field])
|
||||
|
||||
if not self.created_at:
|
||||
self.created_at = datetime.now()
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
d = {}
|
||||
for f in ["id", "name", "course_id", "description", "due_date", "created_at"]:
|
||||
d[f] = getattr(self, f)
|
||||
|
||||
return d
|
||||
|
||||
@@ -6,7 +6,7 @@ from app.errors import error_response
|
||||
from flask_login import current_user
|
||||
|
||||
from app import login, db
|
||||
from app.models import Course, User
|
||||
from app.models import Course, User, Assignment
|
||||
|
||||
|
||||
@login.user_loader
|
||||
@@ -125,6 +125,7 @@ def get_courses(id):
|
||||
resp = jsonify(d)
|
||||
return resp
|
||||
|
||||
|
||||
@bp.route("/course/<int:id>/students", methods=["GET"])
|
||||
@login_required
|
||||
def get_students_in_course(id):
|
||||
@@ -138,6 +139,7 @@ def get_students_in_course(id):
|
||||
resp["students"].append(s.to_dict())
|
||||
return jsonify(resp)
|
||||
|
||||
|
||||
@bp.route("/course/<int:id>", methods=["GET"])
|
||||
@login_required
|
||||
def get_course(id):
|
||||
@@ -147,6 +149,7 @@ def get_course(id):
|
||||
resp = jsonify(c.to_dict())
|
||||
return resp
|
||||
|
||||
|
||||
@bp.route("/user/<int:uid>/enroll/<int:cid>", methods=["POST", "DELETE"])
|
||||
@login_required
|
||||
@instructor_required
|
||||
@@ -172,6 +175,7 @@ def enroll_student(uid, cid):
|
||||
resp = {"user": u.to_dict(), "course": c.to_dict()}
|
||||
return jsonify(resp)
|
||||
|
||||
|
||||
@bp.route("/user/<string:username>/enroll/<int:cid>", methods=["POST"])
|
||||
@login_required
|
||||
@instructor_required
|
||||
@@ -197,3 +201,22 @@ def enroll_student_by_username(username, cid):
|
||||
resp = {"user": u.to_dict(), "course": c.to_dict()}
|
||||
return jsonify(resp)
|
||||
|
||||
|
||||
@bp.route("/assignment", methods=["POST"])
|
||||
@login_required
|
||||
@instructor_required
|
||||
def create_assignment():
|
||||
data = request.get_json()
|
||||
required_fields = ["name", "description", "course_id", "due_date"]
|
||||
if f := check_data(data, required_fields):
|
||||
return error_response(400, f"Must supply {f}")
|
||||
|
||||
c = Course.query.get(data["course_id"])
|
||||
if not c:
|
||||
return error_response(400, f"Course with id {data['course_id']} does not exist")
|
||||
|
||||
a = Assignment()
|
||||
a.from_dict(data)
|
||||
db.session.add(a)
|
||||
db.session.commit()
|
||||
return jsonify(a.to_dict())
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
"""Added due date to assignment model
|
||||
|
||||
Revision ID: 1e88e783d238
|
||||
Revises: cab0d39ef662
|
||||
Create Date: 2023-04-13 18:33:37.785568
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '1e88e783d238'
|
||||
down_revision = 'cab0d39ef662'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('assignment', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('due_date', sa.DateTime(), nullable=True))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('assignment', schema=None) as batch_op:
|
||||
batch_op.drop_column('due_date')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user