def removefile(db, course_, full_path, dry_run): cid = canvas_id.CanvasID(full_path, course_.canvas_id) cid.find_id(db) if cid.canvas_id: r = helpers.delete(FILE_PATH.format(cid.canvas_id), dry_run=dry_run) if r.get('upload_status') == 'success': cid.remove(db)
def remove(self, db, course_, dry_run): course_id = course_.canvas_id cid = canvas_id.CanvasID(self.filename, course_id) cid.find_id(db) if not cid.canvas_id: print(f"failed to delete {self} from course {course_}") print("No canvas information found for the given component. If the" " component still exists in Canvas we may have lost track " "of it so you will have to manually delete it. Sorry!") return # TODO: confirm they want to delete it? path = self.format_update_path(db, course_id, cid.canvas_id) resp = helpers.delete(path, dry_run=dry_run) err = False if "errors" in resp: print(f"canvas failed to delete the component {self}") for error in resp['errors']: if "does not exist" in error['message']: print("But that's ok because you were probably just " "removing the local canvas info for it.") else: print("CANVAS ERROR:", error['message']) err = True if err: print("local remove action aborted") print( "Canvas may or may not have successfully deleted the component" ) return if dry_run: print(f"DRYRUN - deleting the canvas relationship for {self}") else: cid.remove(db)
def postprocess(self, db, course_, dry_run): course_id = course_.canvas_id cid = canvas_id.CanvasID(self.filename, course_id) cid.find_id(db) if not cid.canvas_id: print(f"failed to add QuizQuestions to {self}, we don't " "have a canvas id for it") print("make sure the quiz has first been pushed to Canvas") return # delete any existing questions on the quiz # we'll use the local file as the source of truth and always update # canvas to match it quiz_path = QUIZ_PATH.format(course_id, cid.canvas_id) questions_path = quiz_path + "/questions" quiz_questions = helpers.get(questions_path, dry_run) for question in quiz_questions: if 'id' in question: path = questions_path + "/{}".format(question['id']) helpers.delete(path) # prepare actual QuizQuestion objects to be pushed to canvas questions = build_questions(self.quiz_questions) # push the questions for question in questions: print(f"\tpushing {question} to Quiz {self}") question.push(db, course_, dry_run, parent_component=self) # once I push the questions, canvas doesn't seem to update the # quiz's points possible until I save the entire quiz again... # https://community.canvaslms.com/t5/Question-Forum/Saving-Quizzes-w-API/td-p/226406 # turns out that canvas won't do this if the quiz is unpublished when # you create the questions. so I'm hackily unpublishing and then # publishing (if the user wants to publish it) if self.remember_published: helpers.put(quiz_path, {"quiz": {"published": True}})