#67 Made endpoint to update assignment fields
This commit was merged in pull request #71.
This commit is contained in:
@@ -106,14 +106,6 @@ class Assignment(db.Model):
|
||||
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:
|
||||
|
||||
@@ -245,6 +245,7 @@ def get_assignment(id):
|
||||
|
||||
@bp.route("/assignment/<int:id>", methods=["DELETE"])
|
||||
@login_required
|
||||
@instructor_required
|
||||
def delete_assignment(id):
|
||||
a = Assignment.query.get(id)
|
||||
if not a:
|
||||
@@ -254,3 +255,21 @@ def delete_assignment(id):
|
||||
db.session.commit()
|
||||
return jsonify(a.to_dict())
|
||||
|
||||
@bp.route("/assignment/<int:id>", methods=["PUT"])
|
||||
@login_required
|
||||
@instructor_required
|
||||
def update_assignment(id):
|
||||
a = Assignment.query.get(id)
|
||||
if not a:
|
||||
return error_response(400, f"Assignment with id {id} does not exist")
|
||||
|
||||
data = request.get_json()
|
||||
expected_fields = ["name", "description", "due_date"]
|
||||
for d in data:
|
||||
if d not in expected_fields:
|
||||
return error_response(400, f"Field {d} was not expected")
|
||||
|
||||
a.from_dict(data)
|
||||
db.session.commit()
|
||||
return jsonify(a.to_dict())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user