in microservices/lms/src/services/section_service.py [0:0]
def copy_course_background_task_alpha(
course_template_details,
sections_details,
cohort_details,
lms_job_id,
message=""):
"""
This function is background function for alpha copy course
"""
lms_job = LmsJob.find_by_id(lms_job_id)
logs = lms_job.logs
try:
# Create a new course
original_courseworks = classroom_crud.get_coursework_list(
course_template_details.classroom_id)
original_coursework_materials = classroom_crud.get_coursework_material_list(
course_template_details.classroom_id)
Logger.info(message)
Logger.info(f"Origial coursework list \
{len(original_courseworks)}")
Logger.info(f"Original coursework Material list\
{len(original_coursework_materials)}")
logs["info"].append(f"Original Courseworks {len(original_courseworks)}")
logs["info"].append(f"Original Coursework Materials \
{len(original_coursework_materials)}")
# Call classroom copy course API in Alpha version
copied_course = classroom_crud.copy_classroom_course(course_template_details.classroom_id,
course_template_details.name)
classroom_id = copied_course["id"]
logs["info"].append(f"Classroom copy course API competed {classroom_id}")
lms_job.classroom_id = copied_course["id"]
lms_job.start_time = datetime.datetime.utcnow()
lms_job.status = "running"
lms_job.update()
# Create section with the required fields
section = Section()
section.name = course_template_details.name
section.section = sections_details.name
section.description = sections_details.description
section.max_students = sections_details.max_students
# Reference document can be get using get() method
section.course_template = course_template_details
section.cohort = cohort_details
section.classroom_id = copied_course["id"]
section.classroom_code = copied_course["enrollmentCode"]
section.classroom_url = copied_course["alternateLink"]
section.enrolled_students_count = 0
section.status = "PROVISIONING"
section_id = section.save().id
lms_job.section_id = section_id
lms_job.update()
lms_job.logs = logs
lms_job.update()
course_template_id = course_template_details.id
# Call check_copy_course function to verify all courseworks and coursework\
# material is copied
error_flag = check_copy_course_alpha(original_courseworks,
original_coursework_materials,
copied_course,lms_job_id, section_id,
course_template_id)
# Calling classroom Update course API to update section name and
# description
try:
copied_course = classroom_crud.update_course(classroom_id,
sections_details.name,
sections_details.description)
except Exception as error:
error = traceback.format_exc().replace("\n", " ")
Logger.error(error)
error_flag = True
lms_job = LmsJob.find_by_id(lms_job_id)
logs= lms_job.logs
logs["errors"].append("Classroom course update failed")
logs["errors"].append(error)
lms_job.logs = logs
lms_job.update()
# Calling Classroom registrations API to send classroom event noifications
try:
classroom_crud.enable_notifications(copied_course["id"], "COURSE_WORK_CHANGES")
classroom_crud.enable_notifications(copied_course["id"],
"COURSE_ROSTER_CHANGES")
except Exception as error:
error = traceback.format_exc().replace("\n", " ")
Logger.error(error)
error_flag = True
lms_job = LmsJob.find_by_id(lms_job_id)
logs= lms_job.logs
logs["errors"].append("Enable notification for classroom failed")
logs["errors"].append(error)
lms_job.logs = logs
lms_job.update()
# add instructional designer
list_course_template_enrollment_mapping = CourseTemplateEnrollmentMapping\
.fetch_all_by_course_template(course_template_details.key)
if list_course_template_enrollment_mapping:
for course_template_mapping in list_course_template_enrollment_mapping:
try:
add_instructional_designer_into_section(section,
course_template_mapping)
except Exception as error:
error = traceback.format_exc().replace("\n", " ")
Logger.error(f"Create teacher failed for \
for {course_template_details.instructional_designer}")
Logger.error(error)
rows=[{
"sectionId":section_id,\
"courseId":copied_course["id"],\
"classroomUrl":copied_course["alternateLink"],\
"name":sections_details.name,\
"description":sections_details.description,\
"cohortId":cohort_details.id,\
"courseTemplateId":course_template_details.id,\
"status":section.status,\
"enrollmentStatus": section.enrollment_status,
"maxStudents": section.max_students,
"timestamp":datetime.datetime.utcnow()
}]
insert_rows_to_bq(
rows=rows,
dataset=BQ_DATASET,
table_name=BQ_TABLE_DICT["BQ_COLL_SECTION_TABLE"])
if error_flag:
section.status = "FAILED_TO_PROVISION"
else:
section.status = "ACTIVE"
section.update()
lms_job = LmsJob.find_by_id(lms_job_id)
logs = lms_job.logs
logs["info"].append(
f"Background Task Completed for section Creation for cohort\
{cohort_details.id}")
logs["info"].append(f"Section Details are section id {section_id},\
classroom id {classroom_id}")
if error_flag:
lms_job.status = "failed"
else:
lms_job.status = "success"
Logger.info( f"Background Task Completed for section Creation for cohort\
{cohort_details.id}")
Logger.info(f"Section Details are section id {section_id},\
classroom id {classroom_id} {lms_job.id}")
lms_job.logs = logs
lms_job.end_time = datetime.datetime.utcnow()
lms_job.update()
return True
except Exception as e:
error = traceback.format_exc().replace("\n", " ")
Logger.error(error)
Logger.error(e)
logs["errors"].append(str(e))
lms_job.logs = logs
lms_job.end_time = datetime.datetime.utcnow()
lms_job.status = "failed"
lms_job.update()
raise InternalServerError(str(e)) from e