in microservices/classroom_shim/src/routes/lti_assignment.py [0:0]
def create_lti_assignment(input_lti_assignment: InputLTIAssignmentModel):
"""Create a LTI Assignment endpoint
Args:
input_lti_assignment (InputLTIAssignmentModel): input LTI Assignment to be inserted
Raises:
ResourceNotFoundException: If the Course Template does not exist.
Exception: 500 Internal Server Error if something went wrong
Returns:
CreateLTIAssignmentResponseModel: LTI Assignment Object,
NotFoundErrorResponseModel: if the Course template not found,
InternalServerErrorResponseModel:
if the LTI Assignment creation raises an exception
"""
try:
lti_assignment_dict = {**input_lti_assignment.dict()}
lti_assignment = LTIAssignment.from_dict(lti_assignment_dict)
lti_assignment.save()
lti_assignment_id = lti_assignment.id
course_work_type = lti_assignment_dict.get("course_work_type")
if course_work_type == "course_work":
coursework = {
"title": lti_assignment.lti_assignment_title,
"materials": [{
"link": {
"url":
f"{API_DOMAIN}/classroom-shim/api/v1/launch?lti_assignment_id={lti_assignment_id}"
}
},],
"workType": "ASSIGNMENT",
"state": "PUBLISHED"
}
lti_assignment_due_date = lti_assignment_dict.get("due_date")
if lti_assignment_due_date:
curr_utc_timestamp = datetime.datetime.utcnow()
lti_assignment_datetime = datetime.datetime.fromtimestamp(
lti_assignment_due_date.timestamp())
if lti_assignment_datetime < curr_utc_timestamp:
raise ValidationError(
f"Given due date - {lti_assignment_due_date} is in the past")
coursework["dueDate"] = {
"year": lti_assignment.due_date.year,
"month": lti_assignment.due_date.month,
"day": lti_assignment.due_date.day
}
coursework["dueTime"] = {
"hours": lti_assignment.due_date.hour,
"minutes": lti_assignment.due_date.minute
}
lti_assignment_max_points = lti_assignment_dict.get("max_points")
if lti_assignment_max_points:
if lti_assignment_max_points <= 0:
raise ValidationError(
f"Given max points - {lti_assignment_due_date} should be greater than zero"
)
coursework["maxPoints"] = lti_assignment_dict.get("max_points")
elif course_work_type == "course_work_material":
coursework_material = {
"title": lti_assignment.lti_assignment_title,
"materials": [{
"link": {
"url":
f"{API_DOMAIN}/classroom-shim/api/v1/launch?lti_assignment_id={lti_assignment_id}"
}
},],
"state": "PUBLISHED"
}
else:
raise ValidationError(
f"Provided course work type - '{course_work_type}' is not valid")
context_resp = get_context_details(lti_assignment.context_id)
course_id = context_resp["data"]["classroom_id"]
try:
if course_work_type == "course_work":
classroom_resp = classroom_crud.create_coursework(course_id, coursework)
elif course_work_type == "course_work_material":
classroom_resp = classroom_crud.create_coursework_material(
course_id, coursework_material)
except Exception as e:
LTIAssignment.delete_by_id(lti_assignment_id)
Logger.error(
f"Failed to create assignment (deleted assignment id - {lti_assignment_id}) due to error from classroom API with error - {e}"
)
raise Exception(
f"Internal error from classroom - {e} for assigment - {lti_assignment_id}"
) from e
lti_assignment.course_work_id = classroom_resp["id"]
lti_assignment.update()
lti_assignment_data = lti_assignment.to_dict()
return {"data": lti_assignment_data}
except ValidationError as e:
raise BadRequest(str(e)) from e
except ResourceNotFoundException as e:
Logger.error(e)
raise ResourceNotFound(str(e)) from e
except Exception as e:
Logger.error(e)
Logger.error(traceback.print_exc())
raise InternalServerError(str(e)) from e