def enroll(request, token): """ Allows the user to enroll as a student or TA in a course using the token. """ course, privilege = _get_course_and_privilege_from_token(token) if course == None: raise http.Http404 existing_course_user = models.CourseUser.objects.filter(user=request.user, course=course) # User is already enroll for the course if existing_course_user.count() > 0: course_user = existing_course_user[0] if (course_user.privilege == models.CourseUser.STUDENT and privilege == models.CourseUser.STUDENT): return shortcuts.redirect('/welcome') elif (course_user.privilege == models.CourseUser.STUDENT and privilege == models.CourseUser.TA): # This means the user is being changed to TA privilege course_user.privilege = models.CourseUser.TA course_user.save() return shortcuts.redirect('/course/%d/roster/' % course.pk) else: # User already has an instructor/ TA privilege return shortcuts.redirect('/course/%d/roster/' % course.pk) # Enroll user for the course else: course_user = models.CourseUser(user=request.user, course=course, privilege=privilege) course_user.save() # If the user enrolled as a student, show the welcome page. For instructors # and TAs, we show the roster if privilege == models.CourseUser.STUDENT: return shortcuts.redirect('/welcome') else: return shortcuts.redirect('/course/%d/roster/' % course.pk)
def new_course(request): """ Allows the user to create a new course to grade. """ if request.method == 'POST': form = forms.CourseForm(request.POST) if form.is_valid(): # defer saving so we can add the token course = form.save(commit=False) course.student_enroll_token = utils.generate_random_string(10) course.ta_enroll_token = utils.generate_random_string(10) course.save() course_user = models.CourseUser(user=request.user, course=course, privilege=models.CourseUser.INSTRUCTOR) course_user.save() return shortcuts.redirect('/course/%d/roster/' % course.pk) else: form = forms.CourseForm() return helpers.render(request, 'new-course.epy', { 'title': 'New Course', 'new_course_form': form, })
def handle(self, *args, **options): # We are in debug mode, so the database can be deleted if options['delete'] and settings.DEBUG: os.system('python manage.py reset_db --noinput') os.system('python manage.py syncdb --all --noinput') elif options['delete']: responsibility = 'Catherine takes full responsibility for my mistakes' self.stdout.write('You are going to delete everything. I mean everything. FROM PRODUCTION.' + ' Chances are you will regret this. If you still want to go ahead please type:\n' + responsibility) text = raw_input('Enter in correct case: ') if text == responsibility: os.system('python manage.py reset_db --noinput') os.system('python manage.py syncdb --all --noinput') else: self.stdout.write('Incorrect text. Not deleting anything.') return superuser_data = json.load(open('scorystapp/fixtures/demo/json/superuser.json')) superuser = None if models.User.objects.filter(email=superuser_data['email']).count(): superuser = models.User.objects.filter(email=superuser_data['email'])[0] self.stdout.write('Super user from superuser.json already exists. Not recreating.') else: superuser = get_user_model().objects.create_superuser(superuser_data['email'], superuser_data['first_name'], superuser_data['last_name'], superuser_data['id'], superuser_data['password']) class_name = options['classname'] if options['classname'] else 'CS221' user = get_user_model().objects.create_user('*****@*****.**' % class_name.lower(), 'Demo', 'User','12345678', 'demo') course = models.Course(name=class_name, term=0) course.save() # Make the newly created user the instructor for this course course_user = models.CourseUser(user=user, course=course, privilege=models.CourseUser.INSTRUCTOR) course_user.save() # Make the super user an instructor as well course_user_super = models.CourseUser(user=superuser, course=course, privilege=models.CourseUser.INSTRUCTOR) course_user_super.save() questions = json.load(open('scorystapp/fixtures/demo/json/questions.json')) users = [] course_users = [] user_first_names = sorted(['John', 'Cynthia', 'Arushi', 'Chenyuan', 'Jose', 'Brett', 'Crystal', 'Jenny', 'Andy', 'Ben', 'George', 'Sheila', 'Stephanie', 'Kunal', 'Alp', 'Keith', 'Daryl', 'Neeraj', 'Eileen', 'Ahmed', 'Keegan', 'Adam', 'Reid', 'Sarah']) user_last_names = ['Holmstead', 'Boyle', 'Goel', 'Wong', 'Sharma', 'White', 'Whittaker', 'Hong', 'Moeur', 'Turk', 'Wyngar', 'Wong', 'Seth', 'Nguyen', 'Bourabee', 'Go', 'Jensen', 'Johnson', 'Lockheart'] # TODO: Not 7 num_users = 14 for i in range(num_users): email = 'fake_email' + str(i) + '@scoryst.com' if models.User.objects.filter(email=email).count(): user2 = models.User.objects.get(email=email) else: user2 = get_user_model().objects.create_user(email, user_first_names[i], user_last_names[i], '0' + str(5715000 + random.randint(1001,9999)), 'demo') user2.save() course_user2 = models.CourseUser(user=user2, course=course, privilege=0) course_user2.save() users.append(user2) course_users.append(course_user2) num_pages = 6 pdf = open('scorystapp/fixtures/demo/midterm.pdf', 'r') exam = models.Exam(name='Midterm Exam', course=course, page_count=num_pages) exam.exam_pdf.save('new', File(pdf)) solutions_pdf = open('scorystapp/fixtures/demo/midterm_solution.pdf', 'r') exam.solutions_pdf.save('new', File(solutions_pdf)) exam.save() for i in range(num_pages): f = open('scorystapp/fixtures/demo/midterm' + str(i) + '.jpeg', 'r') exam_page = models.ExamPage(exam=exam, page_number=i+1) exam_page.page_jpeg.save('new', File(f)) exam_page.save() f.close() question_parts = [] for question_part_data in questions: question_part = models.QuestionPart(exam=exam, question_number=question_part_data['question'], part_number=question_part_data['part'], max_points=question_part_data['max_points'], pages=question_part_data['pages']) question_part.save() question_parts.append(question_part) rubric = models.Rubric(question_part=question_part, description='Correct answer', points=0) rubric.save() # rubric2 = models.Rubric(question_part=question_part, description='Incorrect answer', points=question_part_data['max_points']) # rubric2.save() for rubric in question_part_data['rubrics']: rubric3 = models.Rubric(question_part=question_part, description=rubric['description'], points=rubric['points']) rubric3.save() for i in range(num_users): name = users[i].first_name.lower() pdf = open('scorystapp/fixtures/demo/%s.pdf' % name, 'r') user_question_data = json.load(open('scorystapp/fixtures/demo/json/%s.json' % name, 'r')) num_pages = user_question_data[0]['num_pages'] exam_answer = models.Submission(exam=exam, course_user=course_users[i], page_count=num_pages) exam_answer.pdf.save('new', File(pdf)) exam_answer.save() pdf.close() for j in range(num_pages): f = open('scorystapp/fixtures/demo/' + name + str(j) + '.jpeg', 'r') exam_answer_page = models.SubmissionPage(exam_answer=exam_answer, page_number=j+1) exam_answer_page.page_jpeg.save('new', File(f)) exam_answer_page.save() f.close() for j in range(len(question_parts)): response = models.Response(exam_answer=exam_answer, question_part=question_parts[j], pages=user_question_data[j]['pages']) response.save() self.stdout.write('Successfully initialized database')
def roster(request, cur_course_user): """ Allows the instructor to manage a course roster. """ cur_course = cur_course_user.course if request.method == 'POST': form = forms.AddPeopleForm(request.POST) if form.is_valid(): people = form.cleaned_data.get('people') privilege = form.cleaned_data.get('privilege') course_users = [] for person in people.splitlines(): first_name, last_name, email, student_id = person.split(',') email = email.lower() # for each person, find/create a corresponding user try: user = models.User.objects.get(email=email) except models.User.DoesNotExist: password = utils.generate_random_string(50) user = models.User.objects.create_user( email, first_name, last_name, student_id, password) else: # update existing user's student ID to match entered value user.student_id = student_id user.save() try: course_user = models.CourseUser.objects.get( user=user.pk, course=cur_course.pk) except models.CourseUser.DoesNotExist: # add that user to the course course_user = models.CourseUser(user=user, course=cur_course, privilege=privilege) else: # if the user is already in the course, simply update his/her privileges course_user.privilege = privilege course_user.save() course_users.append(course_user) email_sender.send_added_to_course_email(request, course_users, True) return shortcuts.redirect(request.path) else: form = forms.AddPeopleForm() course_users = models.CourseUser.objects.filter(course=cur_course.pk) return helpers.render( request, 'roster.epy', { 'title': 'Roster', 'add_people_form': form, 'course': cur_course, 'course_users': course_users, 'is_instructor': cur_course_user.privilege == models.CourseUser.INSTRUCTOR })
def handle(self, *args, **options): os.system('python manage.py reset_db --noinput') os.system('python manage.py syncdb --noinput') superuser_data = json.load( open('scorystapp/fixtures/development/superuser.json')) get_user_model().objects.create_superuser(superuser_data['email'], superuser_data['first_name'], superuser_data['last_name'], superuser_data['id'], superuser_data['password']) rubrics_data = json.load( open('scorystapp/fixtures/development/rubrics.json')) course = models.Course(name='CS144', term=0) course.save() # Make the superuser the Instructor for this course user = models.User.objects.get(pk=1) course_user = models.CourseUser(user=user, course=course, privilege=2) course_user.save() users = [] course_users = [] num_rubrics = 5 user_first_names = sorted([ 'John', 'Cynthia', 'Albert', 'Arushi', 'Chenyuan', 'Jose', 'Brett', 'Crystal', 'Jenny', 'Andy', 'Benjamin', 'George', 'Sheila', 'Stephanie', 'Kunal', 'Alp', 'Keith', 'Daryl', 'Neeraj', 'Eileen', 'Ahmed', 'Keegan', 'Adam', 'Reid', 'Sarah' ]) user_last_names = [ 'Holmstead', 'Boyle', 'Wu', 'Goel', 'Wong', 'Garcia', 'White', 'Whittaker', 'Hong', 'Moeur', 'Turk', 'Wyngar', 'Wong', 'Seth', 'Nguyen', 'Bourabee', 'Go', 'Jensen', 'Johnson', 'Lockheart' ] for i in range(len(user_last_names)): email = 'fake_email' + str(i) + '@gmail.com' user2 = models.User(email=email, first_name=user_first_names[i], last_name=user_last_names[i], student_id='0' + str(5715000 + random.randint(1001, 9999)), is_signed_up=True) user2.save() course_user2 = models.CourseUser(user=user2, course=course, privilege=0) course_user2.save() users.append(user2) course_users.append(course_user2) num_pages = 8 pdf = open('scorystapp/fixtures/demo/kv.pdf', 'r') exam = models.Exam(name='Midterm Exam', course=course, page_count=num_pages) exam.exam_pdf.save('new', File(pdf)) solutions_pdf = open('scorystapp/fixtures/demo/solutions.pdf', 'r') exam.solutions_pdf.save('new', File(solutions_pdf)) exam.save() for i in range(num_pages): f = open('scorystapp/fixtures/demo/kv' + str(i) + '.jpeg', 'r') exam_page = models.ExamPage(exam=exam, page_number=i + 1) exam_page.page_jpeg.save('new', File(f)) exam_page.save() f.close() question_parts = [] # i is for questions, j is for parts # Requires: i*j = num_pages for i in range(num_pages / 2): for j in range(2): question_part = models.QuestionPart(exam=exam, question_number=i + 1, part_number=j + 1, max_points=10, pages=2 * i + j + 1) question_part.save() question_parts.append(question_part) rubric = models.Rubric(question_part=question_part, description='Correct answer', points=0) rubric.save() rubric2 = models.Rubric(question_part=question_part, description='Incorrect answer', points=10) rubric2.save() random.shuffle(rubrics_data) for k in range(num_rubrics - 2): rubric3 = models.Rubric( question_part=question_part, description=rubrics_data[k]['description'], points=rubrics_data[k]['points']) rubric3.save() course_user_a = course_users[0] course_user_b = course_users[1] # Exam for first course user - uses karanveer's exam pdf = open('scorystapp/fixtures/demo/kv.pdf', 'r') exam_answer = models.Submission(exam=exam, course_user=course_user_a, page_count=num_pages) exam_answer.pdf.save('new', File(pdf)) exam_answer.save() pdf.close() for i in range(num_pages): f = open('scorystapp/fixtures/demo/kv' + str(i) + '.jpeg', 'r') exam_answer_page = models.SubmissionPage(exam_answer=exam_answer, page_number=i + 1) exam_answer_page.page_jpeg.save('new', File(f)) exam_answer_page.save() f.close() for i in range(num_pages): response = models.Response(exam_answer=exam_answer, question_part=question_parts[i], pages=i + 1) response.save() # Exam for second course user - uses squishy's exam pdf = open('scorystapp/fixtures/demo/cglu.pdf', 'r') exam_answer = models.Submission(exam=exam, course_user=course_user_b, page_count=num_pages) exam_answer.pdf.save('new', File(pdf)) exam_answer.save() pdf.close() for i in range(num_pages): f = open('scorystapp/fixtures/demo/cglu' + str(i) + '.jpeg', 'r') exam_answer_page = models.SubmissionPage(exam_answer=exam_answer, page_number=i + 1) exam_answer_page.page_jpeg.save('new', File(f)) exam_answer_page.save() f.close() for i in range(num_pages): response = models.Response(exam_answer=exam_answer, question_part=question_parts[i], pages=i + 1) response.save() self.stdout.write('Successfully initialized database') # Create another exam that has no students mapped so can be editted num_pages = 5 exam = models.Exam(name='Final Exam', course=course, page_count=num_pages) pdf = open('scorystapp/fixtures/demo/cs221.pdf', 'r') exam.exam_pdf.save('new', File(pdf)) exam.save() for i in range(num_pages): f = open('scorystapp/fixtures/demo/cs221' + str(i) + '.jpeg', 'r') exam_page = models.ExamPage(exam=exam, page_number=i + 1) exam_page.page_jpeg.save('new', File(f)) exam_page.save() f.close() question_parts = [] # i is for questions, j is for parts # Requires: i*j = num_pages for i in range(2): for j in range(2): question_part = models.QuestionPart(exam=exam, question_number=i + 1, part_number=j + 1, max_points=10, pages=2 * i + j + 2) question_part.save() rubric = models.Rubric(question_part=question_part, description='Correct answer', points=0) rubric.save() rubric2 = models.Rubric(question_part=question_part, description='Incorrect answer', points=10) rubric2.save() random.shuffle(rubrics_data) for k in range(num_rubrics - 2): rubric3 = models.Rubric( question_part=question_part, description=rubrics_data[k]['description'], points=rubrics_data[k]['points']) rubric3.save()
def handle(self, *args, **options): num_students = 2 num_pages = 4 num_rubrics = 2 student_email = '*****@*****.**' rubrics_data = json.load( open('scorystapp/static/development/rubrics.json')) # Get args, if any if options['numstudents']: num_students = options[ 'numstudents'] if options['numstudents'] > 0 else num_students if options['numpages']: num_pages = options[ 'numpages'] if options['numpages'] > 0 else num_pages # Ensure number of pages is even if num_pages % 2 == 1: num_pages += 1 if options['numrubrics']: num_rubrics = options[ 'numrubrics'] if options['numrubrics'] > 2 else num_rubrics if num_rubrics > len(rubrics_data) + 2: num_rubrics = len(rubrics_data) + 2 if options['studentemail']: student_email = options['studentemail'] course = models.Course(name='Test Course', term=0) course.save() user = models.User.objects.get(pk=1) course_user = models.CourseUser(user=user, course=course, privilege=2) course_user.save() users = [] course_users = [] for i in range(num_students): if i == 0: email = student_email else: # Use some fake email email = 'fake_email' + str(i) + '@gmail.com' user2 = models.User(email=email, first_name='Student', last_name=str(i), student_id=i + 1, is_signed_up=True) user2.save() course_user2 = models.CourseUser(user=user2, course=course, privilege=0) course_user2.save() users.append(user2) course_users.append(course_user2) pdf = open('scorystapp/static/development/exam.pdf', 'r') exam = models.Exam(name='Test Exam', course=course, page_count=num_pages) exam.exam_pdf.save('new', File(pdf)) exam.save() for i in range(num_pages): f = open('scorystapp/static/development/img' + str(i) + '.jpeg', 'r') exam_page = models.ExamPage(exam=exam, page_number=i + 1) exam_page.page_jpeg.save('new', File(f)) exam_page.save() f.close() question_parts = [] # i is for questions, j is for parts # Requires: i*j = num_pages for i in range(num_pages / 2): for j in range(2): question_part = models.QuestionPart(exam=exam, question_number=i + 1, part_number=j + 1, max_points=10, pages=2 * i + j + 1) question_part.save() question_parts.append(question_part) rubric = models.Rubric(question_part=question_part, description='Correct answer', points=0) rubric.save() rubric2 = models.Rubric(question_part=question_part, description='Incorrect answer', points=10) rubric2.save() random.shuffle(rubrics_data) for k in range(num_rubrics - 2): rubric3 = models.Rubric( question_part=question_part, description=rubrics_data[k]['description'], points=rubrics_data[k]['points']) rubric3.save() # Multithread the shit out of this bitch. def create_course_user_exam_answer(c, exam, num_pages, question_parts, rubric, course_user): pdf = open('scorystapp/static/development/exam.pdf', 'r') exam_answer = models.Submission(exam=exam, course_user=c, page_count=num_pages) exam_answer.pdf.save('new', File(pdf)) exam_answer.save() pdf.close() for i in range(num_pages): f = open( 'scorystapp/static/development/img' + str(i) + '.jpeg', 'r') exam_answer_page = models.SubmissionPage( exam_answer=exam_answer, page_number=i + 1) exam_answer_page.page_jpeg.save('new', File(f)) exam_answer_page.save() f.close() for i in range(num_pages): response = models.Response(exam_answer=exam_answer, question_part=question_parts[i], pages=i + 1) response.save() response.grader = course_user response.rubrics.add(rubric) response.save() for c in course_users: t = threading.Thread(target=create_course_user_exam_answer, args=(c, exam, num_pages, question_parts, rubric, course_user)).start() self.stdout.write('Successfully initialized database') # TODO: rewrite if not options['map']: return self.stdout.write('Beginning mapping code') pdf = open('scorystapp/static/development/exam.pdf', 'r') exam = models.Exam(name='Map Exams', course=course, page_count=num_pages) exam.exam_pdf.save('new', File(pdf)) exam.save() for i in range(num_pages): f = open('scorystapp/static/development/img' + str(i) + '.jpeg', 'r') exam_page = models.ExamPage(exam=exam, page_number=i + 1) exam_page.page_jpeg.save('new', File(f)) exam_page.save() f.close() def create_unmapped_exam(exam, num_pages): pdf = open('scorystapp/static/development/exam.pdf', 'r') exam_answer = models.Submission(exam=exam, page_count=num_pages) exam_answer.pdf.save('new', File(pdf)) exam_answer.save() pdf.close() for i in range(num_pages): f = open( 'scorystapp/static/development/img' + str(random.randint(0, 12)) + '.jpeg', 'r') exam_answer_page = models.SubmissionPage( exam_answer=exam_answer, page_number=i + 1) exam_answer_page.page_jpeg.save('new', File(f)) exam_answer_page.save() f.close() for _ in range(num_students): t = threading.Thread(target=create_unmapped_exam, args=(exam, num_pages)).start() self.stdout.write('Mapping db done')