Last active
August 29, 2015 13:56
-
-
Save rickcnagy/9101813 to your computer and use it in GitHub Desktop.
Upload gradebook data downloaded via download_gradebook_data.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python | |
''' | |
Upload gradebook data downloaded via download_gradebook_data.py | |
''' | |
from tqdm import * | |
import qs | |
import api_logging | |
import data_migration | |
# skips to section id to resume section id, leave blank to not skip | |
old_section_start_id = '' | |
has_seen_start_id = False | |
def main(): | |
data_migration.create_file(prefix="upload", existing_file=True) | |
api_logging.basicConfig(log_filename=data_migration.get_filename()) | |
data_migration.check() | |
assignments = data_migration.load("download") | |
posted_assignment_ids = [] | |
posted_grade_count = 0 | |
for i in trange(0, len(assignments), desc='POST', leave=True): | |
old_section_id = assignments.keys()[i] | |
if skip(old_section_id): continue | |
# new_section_id = qs.get_matching_section(old_section_id) | |
new_section_id = old_section_id | |
if not new_section_id: continue | |
for original_assignment in assignments[old_section_id]: | |
# post assignment to new section | |
posted_assignment = qs.post_assignment(new_section_id, original_assignment) | |
assignment_id = posted_assignment['id'] | |
posted_assignment_ids.append(original_assignment['assignmentId']) | |
# post grades to newly posted assignment | |
grades = valid_grades(original_assignment['grades']) | |
qs.post_grades(new_section_id, assignment_id, grades) | |
posted_grade_count += len(grades) | |
api_logging.info( | |
"Complete. {} errors, {} successeful assignments, {} successful grades." | |
"".format(qs.get_error_count(), len(posted_assignment_ids), posted_grade_count), | |
{}, cc_print=True) | |
data_migration.save(posted_assignment_ids) | |
# check that the grade has marks and isn't empty | |
def valid_grades(raw_grades): | |
grades = [] | |
for grade in raw_grades: | |
if 'marks' in grade.keys(): | |
try: | |
float(grade['marks']) | |
grades.append(grade) | |
except(ValueError): | |
pass | |
return grades | |
def skip(old_section_id): | |
global has_seen_start_id | |
# don't skip if id is blank | |
if not old_section_start_id: return False | |
if (not has_seen_start_id) and (old_section_id != old_section_start_id): | |
return True | |
else: | |
return False | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment