def update_lti_assignment()

in microservices/classroom_shim/src/routes/lti_assignment.py [0:0]


def update_lti_assignment(
    lti_assignment_id: str,
    update_lti_assignment_model: UpdateLTIAssignmentModel):
  """Update LTI Assignment API

    Args:
        update_lti_assignment_model (UpdateLTIAssignmentModel):
            Pydantic model object which contains update details
    Raises:
        InternalServerError: 500 Internal Server Error if something fails
        ResourceNotFoundException :
          404 if LTI Assignment not found
    Returns:
        UpdateLTIAssignment: Returns Updated LTI Assignment object
        InternalServerErrorResponseModel:
            If the LTI Assignment update raises an exception
    """
  try:
    lti_assignment_details = LTIAssignment.find_by_id(lti_assignment_id)
    update_lti_assignment_dict = {**update_lti_assignment_model.dict()}

    if not any(update_lti_assignment_dict.values()):
      raise ValidationError(
          "Invalid request please provide some data " +
          f"to update the LTI Assignment with id {lti_assignment_id}")

    context_resp = get_context_details(lti_assignment_details.context_id)
    course_id = context_resp["data"]["classroom_id"]

    coursework_body = {}
    update_mask_list = []

    course_work_type = lti_assignment_details.course_work_type

    if course_work_type == "course_work":
      if update_lti_assignment_dict.get(
          "lti_assignment_title"
      ) != lti_assignment_details.lti_assignment_title:
        update_mask_list.append("title")
        coursework_body["title"] = update_lti_assignment_dict[
            "lti_assignment_title"]

      if update_lti_assignment_dict.get(
          "max_points") != lti_assignment_details.max_points:
        update_mask_list.append("maxPoints")
        coursework_body["maxPoints"] = update_lti_assignment_dict["max_points"]

      if update_lti_assignment_dict.get(
          "due_date") != lti_assignment_details.due_date:
        update_mask_list.append("dueDate")
        update_mask_list.append("dueTime")
        coursework_body["dueDate"] = {
            "year": update_lti_assignment_dict["due_date"].year,
            "month": update_lti_assignment_dict["due_date"].month,
            "day": update_lti_assignment_dict["due_date"].day
        }
        coursework_body["dueTime"] = {
            "hours": update_lti_assignment_dict["due_date"].hour,
            "minutes": update_lti_assignment_dict["due_date"].minute
        }

    elif course_work_type == "course_work_material":
      if update_lti_assignment_dict.get("lti_assignment_title")!=\
        lti_assignment_details.lti_assignment_title:
        update_mask_list.append("title")
        coursework_body["title"] = update_lti_assignment_dict[
            "lti_assignment_title"]

    update_mask = ",".join(update_mask_list)

    if update_mask:
      try:
        if course_work_type == "course_work":
          classroom_crud.update_course_work(
              course_id, lti_assignment_details.course_work_id, update_mask,
              coursework_body)
        elif course_work_type == "course_work_material":
          classroom_crud.update_course_work_material(
              course_id, lti_assignment_details.course_work_id, update_mask,
              coursework_body)
      except Exception as e:
        Logger.error(
            f"Update coursework failed for assignment with id - {lti_assignment_id} due to error from classroom API with error - {e}"
        )
        raise Exception(
            f"Internal error from classroom - {e} for assignment - {lti_assignment_id}"
        ) from e

    for key in update_lti_assignment_dict:
      if update_lti_assignment_dict[key] is not None:
        setattr(lti_assignment_details, key, update_lti_assignment_dict[key])
    lti_assignment_details.update()
    lti_assignment_data = lti_assignment_details.to_dict()

    return {
        "message":
            f"Successfully updated the LTI Assignment with id {lti_assignment_id}",
        "data":
            lti_assignment_data
    }
  except ValidationError as e:
    Logger.error(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