def extra_push(self, course, resource_id): quiz_id = resource_id.canvas_id # Get all the questions old information questions = get('quizzes/{qid}/questions/'.format(qid=quiz_id), course=course.course_name, all=True) if 'errors' in questions: raise WaltzException("Errors in Canvas data: " + repr(questions)) # Push all the groups group_ids = { question['quiz_group_id'] for question in questions if question['quiz_group_id'] is not None } groups = [ get('quizzes/{qid}/groups/{gid}'.format(qid=quiz_id, gid=gid), course=course.course_name) for gid in group_ids ] group_map = {group['name']: group['id'] for group in groups} for group in self.groups: json_data = group.to_json(course, resource_id) group.push(course, quiz_id, json_data, group_map) # Push all the questions name_map = {q['question_name']: q['id'] for q in questions} for question in self.questions: if question.quiz_group_id is not None: question.quiz_group_id = group_map[question.quiz_group_id] json_data = question.to_json(course, resource_id) question.push(course, quiz_id, name_map, json_data) if question.question_name in name_map: del name_map[question.question_name] # Delete any old questions for leftover_name, leftover_id in name_map.items(): deleted = delete('quizzes/{qid}/questions/{question_id}'.format( qid=quiz_id, question_id=leftover_id), course=course.course_name) print("Deleted", leftover_name, deleted) # Reorder questions as needed questions = get('quizzes/{qid}/questions/'.format(qid=quiz_id), course=course.course_name, all=True) return # TODO: Figure out how to get around the fact that Canvas doesn't # allow you to download an ordering, so uploading an ordering is irrelevant. name_map = {q['question_name']: q['id'] for q in questions} payload = {'order': []} for question in self.questions: payload['order'].append({ 'type': 'question', 'id': str(name_map[question.question_name]) }) print( post('quizzes/{}/reorder'.format(quiz_id), json=payload, course=course.course_name))
def get_resource_on_canvas(cls, course, resource_name): data = get('{}/{}'.format(cls.canvas_name, resource_name), course=course.course_name) print(resource_name) if 'errors' in data: raise WaltzNoResourceFound("Errors in Canvas data: " + repr(data)) return data
def _lookup_quiz_id(cls, quiz_id, group_id, course): if (quiz_id, group_id) in cls.QUESTION_GROUP_CACHE_ID: return cls.QUESTION_GROUP_CACHE_ID[(quiz_id, group_id)] group = get('quizzes/{qid}/groups/{gid}'.format(qid=quiz_id, gid=group_id), course=course.course_name) cls.QUESTION_GROUP_CACHE_ID[id] = group return group
def extra_pull(cls, course, resource_id): if resource_id.canvas_data is True: return quiz_id = resource_id.canvas_id questions = get('quizzes/{qid}/questions/'.format(qid=quiz_id), course=course.course_name, all=True) resource_id.canvas_data['questions'] = questions group_ids = { question['quiz_group_id'] for question in questions if question['quiz_group_id'] is not None } groups = [ get('quizzes/{qid}/groups/{gid}'.format(qid=quiz_id, gid=gid), course=course.course_name) for gid in group_ids ] resource_id.canvas_data['groups'] = groups
def find_resource_on_canvas(cls, course, resource_name): results = get(cls.canvas_name, params={"search_term": resource_name}, course=course.course_name, all=True) if 'errors' in results: raise WaltzException("Errors in Canvas data: " + repr(results)) return results
def identify_resource_by_name(self, resource_name): for category in ['assignments', 'pages']: results = get(category, all=True, course=self.course_name, data={'search_term': resource_name}) if len(results) > 1: raise WaltzException("Too many results for: " + resource_name) elif results: return category, results[0] else: raise WaltzException("No results for: " + resource_name)
def from_json(cls, course, json_data): questions = get('quizzes/{qid}/questions'.format(qid=json_data['id']), course=course.course_name, all=True) group_ids = {question['quiz_group_id'] for question in questions} groups = [ QuizGroup.from_json( course, get('quizzes/{qid}/groups/{gid}'.format(qid=json_data['id'], gid=gid), course=course.course_name)) for gid in group_ids if gid is not None ] group_map = {group.id: group.name for group in groups} questions = [ QuizQuestion.from_json(course, question, group_map) for question in sorted(questions, key=sort_quiz_question) ] return cls(**json_data, questions=questions, course=course, groups=groups)
def from_json(cls, course, json_data): if 'body' not in json_data: data = get('{}/{}'.format(cls.canvas_name, json_data['url']), course=course.course_name) json_data['body'] = data['body'] return cls(**json_data, course=course)