def upload_new(cls, registry: Registry, local_quiz, args): canvas = registry.get_service(args.service, "canvas") quiz_data = cls._make_canvas_upload(registry, local_quiz, args) created_quiz = canvas.api.post('quizzes/', data=quiz_data) if 'errors' in created_quiz: pprint(created_quiz['errors']) raise WaltzException("Error loading data, see above.") print("Created quiz", local_quiz['title'], "on canvas") # Create the groups group_name_to_id = {} for group in local_quiz['groups'].values(): group_data = QuizGroup._make_canvas_upload(registry, group, args) created_group = canvas.api.post( 'quizzes/{quiz_id}/groups'.format(quiz_id=created_quiz['id']), data=group_data) created_group = created_group['quiz_groups'][ 0] # acbart: Weird response type # acbart: Okay because names are strings and IDs are ints group_name_to_id[created_group['name']] = created_group['id'] group_name_to_id[created_group['id']] = created_group['id'] if local_quiz['groups']: print("Created quiz", local_quiz['title'], "groups on canvas") # Create the questions for question in local_quiz['questions']: if 'quiz_group_id' in question and question[ 'quiz_group_id'] is not None: question['quiz_group_id'] = group_name_to_id[ question['quiz_group_id']] question_data = QuizQuestion._make_canvas_upload( registry, question, args) created_question = canvas.api.post( 'quizzes/{quiz_id}/questions'.format( quiz_id=created_quiz['id']), data=question_data) print("Created quiz", local_quiz['title'], "questions on canvas")
def upload_edit(cls, registry: Registry, old_quiz, new_quiz, args): canvas = registry.get_service(args.service, "canvas") quiz_id = old_quiz['id'] # Edit the quiz on canvas quiz_data = cls._make_canvas_upload(registry, new_quiz, args) canvas.api.put('quizzes/{quiz_id}'.format(quiz_id=quiz_id), data=quiz_data) print("Updated quiz", old_quiz['title'], "on canvas") # Make a map of the old groups' names/ids to the groups old_group_map = {} for group in old_quiz['groups'].values(): old_group_map[group['name']] = group old_group_map[group['id']] = group # Update groups with the same name and create new ones used_groups = {} for group in new_quiz['groups'].values(): group_data = QuizGroup._make_canvas_upload(registry, group, args) if group['name'] in old_group_map: canvas_group = old_group_map[group['name']] canvas_group = canvas.api.put( 'quizzes/{quiz_id}/groups/{group_id}'.format( quiz_id=quiz_id, group_id=canvas_group['id']), data=group_data) else: canvas_group = canvas.api.post( 'quizzes/{quiz_id}/groups'.format(quiz_id=quiz_id), data=group_data) canvas_group = canvas_group['quiz_groups'][ 0] # acbart: Weird response type used_groups[canvas_group['name']] = canvas_group used_groups[canvas_group['id']] = canvas_group if new_quiz['groups']: print("Updated quiz", old_quiz['title'], "groups on canvas") # Delete any groups that no longer have a reference for old_group in old_quiz['groups'].values(): if old_group['id'] not in used_groups: canvas.api.delete('quizzes/{quiz_id}/groups/{group_id}'.format( quiz_id=quiz_id, group_id=old_group['id'])) print("Deleted question group", old_group['name'], " (ID: {})".format(old_group['id'])) # Push all the questions name_map = {q['question_name']: q for q in old_quiz['questions']} used_questions = {} for new_question in new_quiz['questions']: if new_question.get('quiz_group_id') is not None: new_question['quiz_group_id'] = used_groups[ new_question['quiz_group_id']]['id'] question_data = QuizQuestion._make_canvas_upload( registry, new_question, args) if new_question['question_name'] in name_map: canvas_question = name_map[new_question['question_name']] canvas_question = canvas.api.put( 'quizzes/{quiz_id}/questions/{question_id}'.format( quiz_id=quiz_id, question_id=canvas_question['id']), data=question_data) else: canvas_question = canvas.api.post( 'quizzes/{quiz_id}/questions'.format(quiz_id=quiz_id), data=question_data) used_questions[canvas_question['id']] = canvas_question print("Updated quiz", old_quiz['title'], "questions on canvas") # Delete any old questions for question in old_quiz['questions']: if question['id'] not in used_questions: canvas.api.delete( 'quizzes/{quiz_id}/questions/{question_id}'.format( quiz_id=quiz_id, question_id=question['id'])) print("Deleted question", question.get('name', "NO NAME"), " (ID: {})".format(question['id']))