def update(course=False): """ Update the data of courses and or exercises from server. """ if course: with Spinner.context(msg="Updated course metadata.", waitmsg="Updating course metadata."): for course in api.get_courses(): old = None try: old = Course.get(Course.tid == course["id"]) except peewee.DoesNotExist: old = None if old: old.details_url = course["details_url"] old.save() continue Course.create(tid=course["id"], name=course["name"], details_url=course["details_url"]) else: selected = Course.get_selected() # with Spinner.context(msg="Updated exercise metadata.", # waitmsg="Updating exercise metadata."): print("Updating exercise data.") for exercise in api.get_exercises(selected): old = None try: old = Exercise.byid(exercise["id"]) except peewee.DoesNotExist: old = None if old is not None: old.name = exercise["name"] old.course = selected.id old.is_attempted = exercise["attempted"] old.is_completed = exercise["completed"] old.deadline = exercise.get("deadline") old.is_downloaded = os.path.isdir(old.path()) old.return_url = exercise["return_url"] old.zip_url = exercise["zip_url"] old.submissions_url = exercise["exercise_submissions_url"] old.save() download_exercise(old, update=True) else: ex = Exercise.create(tid=exercise["id"], name=exercise["name"], course=selected.id, is_attempted=exercise["attempted"], is_completed=exercise["completed"], deadline=exercise.get("deadline"), return_url=exercise["return_url"], zip_url=exercise["zip_url"], submissions_url=exercise[("exercise_" "submissions_" "url")]) ex.is_downloaded = os.path.isdir(ex.path()) ex.save()
def select(course=False, tid=None, auto=False): """ Select a course or an exercise. """ if course: update(course=True) course = None try: course = Course.get_selected() except NoCourseSelected: pass ret = {} if not tid: ret = Menu.launch("Select a course", Course.select().execute(), course) else: ret["item"] = Course.get(Course.tid == tid) if "item" in ret: ret["item"].set_select() update() if ret["item"].path == "": select_a_path(auto=auto) # Selects the first exercise in this course skip() return else: print("You can select the course with `tmc select --course`") return else: selected = None try: selected = Exercise.get_selected() except NoExerciseSelected: pass ret = {} if not tid: ret = Menu.launch("Select an exercise", Course.get_selected().exercises, selected) else: ret["item"] = Exercise.byid(tid) if "item" in ret: ret["item"].set_select() print("Selected {}".format(ret["item"]))
def inner(*args, **kwargs): course = Course.get_selected() return func(course, *args, **kwargs)