Skip to content

Commit

Permalink
added pagination to student call
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonmyers committed Sep 30, 2020
1 parent 78ba418 commit 9094451
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
27 changes: 17 additions & 10 deletions extend_quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,24 @@ def dl_students(course_id, AUTH_HEADER, API_URL):
None (creates csv from data)
'''
student_list = _get_students(course_id, AUTH_HEADER, API_URL)

#print(student_list)
# Default for extra_time and extra_attempts is null as requested
df = pd.DataFrame(columns=['name','SIS_id','canvas_id','extra_time','extra_attempts'])
for student in student_list:
df = df.append({
'name':student['name'],
'SIS_id': student['sis_user_id'],
'canvas_id': student['id'],
'extra_time': None,
'extra_attempts': None
}, ignore_index=True)
df = pd.DataFrame(student_list)
df = df[['name','sis_user_id','id']]
df['extra_time'] = ''
df['extra_attempts'] =''
df = df.rename(columns={"sis_user_id": "SIS_id", "id":"canvas_id"})
df = df[['name','SIS_id','canvas_id','extra_time','extra_attempts']]
#print(df)
# df = pd.DataFrame(columns=['name','SIS_id','canvas_id','extra_time','extra_attempts'])
# for student in student_list:
# df = df.append({
# 'name':student['name'],
# 'SIS_id': student['sis_user_id'],
# 'canvas_id': student['id'],
# 'extra_time': None,
# 'extra_attempts': None
# }, ignore_index=True)
path = os.path.join(INPUT, 'input-all_course_students.csv')
df.to_csv(path, index=False)

Expand Down
18 changes: 15 additions & 3 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,20 @@ def _get_quiz(course_obj, quiz_id):
except Exception as qe:
print_error(f'ERROR: Could not find quiz [ID: {quiz_id}]. Please check course id.')
raise

def _paginate_list(sub_list, auth_header):
json_list = pd.read_json(sub_list.text)

while sub_list.links['current']['url'] != sub_list.links['last']['url']:
sub_list = requests.get(sub_list.links['next']['url'],
headers=auth_header)
admin_sub_table = pd.read_json(sub_list.text)
json_list= pd.concat([json_list, admin_sub_table], sort=True)
json_list=json_list.reset_index(drop=True)

return json_list

def _get_students(course_id, auth_header, API_URL):
def _get_students(course_id, AUTH_HEADER, API_URL):
'''
Function gets a list of students for a course
Parameters:
Expand All @@ -80,8 +91,9 @@ def _get_students(course_id, auth_header, API_URL):
# Above code doesn't return student SIS ID, forced to use request lib temporarily
try:
url = "{}api/v1/courses/{}/users".format(API_URL, course_id)
student_list = requests.get(url, headers=auth_header, params={'enrollment_type[]':'student'})
student_list = json.loads(student_list.text)
student_list = requests.get(url, headers=AUTH_HEADER, params={'enrollment_type[]':'student', 'per_page':50})
student_list = _paginate_list(student_list, AUTH_HEADER)
#student_list = json.loads(student_list.text)
return(student_list)
except Exception as se:
# print(str(se))
Expand Down

0 comments on commit 9094451

Please sign in to comment.