def update(user, homework_id, markdown, new_hw_name, start, end, problem_ids, scoreboard_status): homework = engine.Homework.objects.get(id=homework_id) course = engine.Course.objects.get(id=homework.course_id) # check user is teacher or ta if perm(course, user) <= 1: raise PermissionError('user is not tacher or ta') course_id = course.id students = course.student_nicknames # check the new_name hasn't been use in this course if new_hw_name is not None: result = engine.Homework.objects(course_id=str(course_id), homework_name=new_hw_name) if len(result) != 0: raise engine.NotUniqueError('homework exist') else: homework.homework_name = new_hw_name homework.save() # update fields if start is not None: homework.duration.start = datetime.fromtimestamp(start) if end is not None: homework.duration.end = datetime.fromtimestamp(end) if scoreboard_status is not None: homework.scoreboard_status = scoreboard_status drop_ids = set(homework.problem_ids) - set(problem_ids) new_ids = set(problem_ids) - set(homework.problem_ids) # add for pid in new_ids: if pid not in homework.problem_ids: problem = Problem(problem_id=pid).obj if problem is None: continue homework.problem_ids.append(pid) problem.homeworks.append(homework) problem.save() for key in students: homework.student_status[key][str(pid)] = { 'score': 0, 'problemStatus': 1, 'submissionIds': [] } # delete for pid in drop_ids: problem = Problem(problem_id=pid).obj if problem is None: continue homework.problem_ids.remove(pid) problem.homeworks.remove(homework) problem.save() for status in homework.student_status.values(): del status[str(pid)] if markdown is not None: homework.markdown = markdown homework.save() return homework
def add_user_in_contest(user, contest_id): #get course of this contest contest = engine.Contest.objects(id=contest_id).first() db_user = engine.User.objects.get(username=user.username) course_of_contest = engine.Course.objects(id=contest.course_id).first() if contest is None: raise DoesNotExist if course_of_contest is None: raise CourseNotExist #check if user is student in the contest's course role = perm(course_of_contest, user) if role < 1: raise UserIsNotInCourse #check if user already is in this contest if user.contest is not None: raise ExistError #add this user in contest user_ids = {} user_problems = {} if contest.problem_ids is not None: for problem_id in contest.problem_ids: user_problems[str(problem_id)] = { "score": 0, "problemStatus": 1, "submissonIds": [] } contest.participants[user.username] = user_problems db_user.contest = contest contest.save() db_user.save()
def delete(user, course_name, contest_name): course = engine.Course.objects(course_name=course_name).first() if course is None: raise CourseNotExist contest = engine.Contest.objects(name=contest_name, course_id=str(course.id)).first() if contest is None: raise DoesNotExist # verify user's roles(teacher/admin) role = perm(course, user) if role < 2: raise AuthorityError if contest is None: raise DoesNotExist for problem_id in contest.problem_ids: problem = Problem(problem_id=problem_id).obj if problem is not None: problem.contests.remove(contest) problem.save() contest.delete() course.save() return contest
def add_contest(user, course_name, contest_name, problem_ids, scoreboard_status, contest_mode, start=None, end=None): #check the contest name won't repeat course = engine.Course.objects(course_name=course_name).first() if course is None: raise CourseNotExist for x in course.contests: if x.name == contest_name: raise NotUniqueError # verify user's roles(teacher/admin) role = perm(course, user) if role < 2: raise AuthorityError students = course.student_nicknames contest = engine.Contest(name=contest_name, course_id=str(course.id), problem_ids=problem_ids) if start: contest.duration.start = datetime.fromtimestamp(start) if end: contest.duration.end = datetime.fromtimestamp(end) contest.contest_mode = 0 if contest_mode is None else contest_mode contest.scoreboard_status = 0 if scoreboard_status is None else scoreboard_status if problem_ids is not None: for problem_id in problem_ids: problem = Problem(problem_id=problem_id).obj if problem is None: raise ProblemNotExist problem.contests.append(contest) problem.save() # get contestId then store in the correspond course contest.save() print(contest.id) course.contests.append(contest.id) course.save() return contest
def delete_problems(user, homework_id): homework = engine.Homework.objects.get(id=homework_id) if homework is None: raise engine.DoesNotExist('homework not exist') course = engine.Course.objects.get(id=homework.course_id) # check user is teacher or ta if perm(course, user) <= 1: raise PermissionError('user is not teacher or ta') for pid in homework.problem_ids: problem = Problem(problem_id=pid).obj if problem is None: continue problem.homeworks.remove(homework) problem.save() homework.delete() course.save() return homework
def get_single_contest(user, id): contest = engine.Contest.objects(id=id).first() if contest is None: raise DoesNotExist course = engine.Course.objects(id=contest.course_id).first() if course is None: raise CourseNotExist data = { "name": contest.name, "start": contest.duration.start, "end": contest.duration.end, "contestMode": contest.contest_mode, "scoreboard_status": contest.scoreboard_status, "courseName": course.course_name } role = perm(course, user) if role > 1 or datetime.now() > contest.duration.start: data["problemIds"] = contest.problem_ids if contest.scoreboard_status == 0: data["participants"] = contest.participants return data
def add_hw( user, course_name, hw_name, problem_ids=[], markdown='', scoreboard_status=0, start=None, end=None, ): # query db check the hw doesn't exist course = Course(course_name).obj if course is None: raise engine.DoesNotExist('course not exist') course_id = course.id students = course.student_nicknames homework = engine.Homework.objects( course_id=str(course_id), homework_name=hw_name, ) # check user is teacher or ta if perm(course, user) <= 1: raise PermissionError('user is not teacher or ta') if len(homework) != 0: raise engine.NotUniqueError('homework exist') homework = engine.Homework( homework_name=hw_name, course_id=str(course_id), problem_ids=problem_ids, scoreboard_status=scoreboard_status, markdown=markdown, ) if start: homework.duration.start = datetime.fromtimestamp(start) if end: homework.duration.end = datetime.fromtimestamp(end) problems = [] for problem_id in problem_ids: problem = Problem(problem_id).obj if problem is None: raise engine.DoesNotExist(f'problem {problem_id} not found!') problems.append(problem) homework.save() # init student status user_ids = {} user_problems = {} for problem in problems: user_problems[str(problem.problem_id)] = { 'score': 0, 'problemStatus': None, 'submissionIds': [] } problem.homeworks.append(homework) problem.save() for key in students: user_ids[key] = user_problems homework.student_status = user_ids homework.save() # get homeworkId then store in the correspond course course.homeworks.append(homework.id) course.save() return homework
def update(user, course_name, contest_name, new_contest_name, start, end, problem_ids, scoreboard_status, contest_mode): course = engine.Course.objects(course_name=course_name).first() if course is None: raise CourseNotExist # verify user's roles(teacher/admin) role = perm(course, user) if role < 2: raise AuthorityError students = course.student_nicknames contest = engine.Contest.objects.get(name=contest_name, course_id=str(course.id)) if contest is None: raise DoesNotExist # check the new_name hasn't been use in this course if new_contest_name is not None: result = engine.Contest.objects(name=new_contest_name) if len(result) != 0: raise NotUniqueError else: contest.name = new_contest_name # update fields if start is not None: contest.duration.start = datetime.fromtimestamp(start) if end is not None: contest.duration.end = datetime.fromtimestamp(end) if scoreboard_status is not None: contest.scoreboard_status = scoreboard_status if contest_mode is not None: contest.contest_mode = contest_mode # if problemid exist then delete ,else add it in list user_problems = {} user_ids = {} # 傳進來的problem_ids應該只有要新增/刪除的 if problem_ids is not None: for id in problem_ids: if (id in contest.problem_ids): contest.problem_ids.remove(id) for userId in contest.participants: contest.participants[userId].pop(id) problem = Problem(problem_id=id).obj if problem is None: raise ProblemNotExist problem.contests.remove(contest) problem.save() else: contest.problem_ids.append(id) for key in contest.participants: contest.participants[key][id] = { "score": 0, "problemStatus": 1, "submissonIds": [] } problem = Problem(problem_id=id).obj if problem is None: raise ProblemNotExist problem.contests.append(contest) problem.save() contest.save() return contest