Exemple #1
0
def _handle_full_exam_edit(request, data, course, exam_id=None):
    """
  All of the fields for an exam are set using the form `data`. If there is no
  given `exam_id`, first creates the exam.
  """
    grade_down = (data['grade_type'] == 'down')

    if exam_id:  # exam is being edited
        qp = models.QuestionPart.objects.filter(assessment__pk=exam_id)
        qp.delete()

        exam = shortcuts.get_object_or_404(models.Exam, id=int(exam_id))
        exam.grade_down = grade_down
    else:  # assessment is exam and is being created
        # We set page_count = 0 here and update it after uploading images
        exam = models.Exam(course=course,
                           name=data['name'],
                           grade_down=grade_down,
                           page_count=0)

    exam.save()

    # Create question parts (with correct answer rubrics) for the exam
    question_part_info = json.loads(data['question_part_points'])
    for i, part_info in enumerate(question_part_info):
        for j, (points, pages) in enumerate(part_info):
            pages = pages.replace(' ', '')
            new_question_part = models.QuestionPart(assessment=exam,
                                                    question_number=i + 1,
                                                    part_number=j + 1,
                                                    max_points=points,
                                                    pages=pages)
            new_question_part.save()

            correct_answer_rubric_points = 0 if exam.grade_down else points
            new_rubric = models.Rubric(question_part=new_question_part,
                                       description="Correct answer",
                                       points=correct_answer_rubric_points)
            new_rubric.save()

    _handle_partial_exam_edit(request, exam.id, data, course, False)
Exemple #2
0
def _handle_full_homework_edit(request, data, course, homework_id=None):
    """
  All of the fields for an exam are set using the form `data`. If there is no
  given `homework_id`, first creates the homework.
  """
    grade_down = (data['grade_type'] == 'down')

    if homework_id:
        qp = models.QuestionPart.objects.filter(assessment__pk=homework_id)
        qp.delete()

        homework = shortcuts.get_object_or_404(models.Homework, pk=homework_id)
        homework.grade_down = grade_down
    else:
        homework = models.Homework(course=course,
                                   name=data['name'],
                                   grade_down=grade_down,
                                   soft_deadline=data['soft_deadline'],
                                   hard_deadline=data['hard_deadline'])

    homework.save()

    # Create question parts for the assessment
    question_part_points = json.loads(data['question_part_points'])
    for i, part_points in enumerate(question_part_points):
        for j, points in enumerate(part_points):
            new_question_part = models.QuestionPart(assessment=homework,
                                                    question_number=i + 1,
                                                    part_number=j + 1,
                                                    max_points=points)
            new_question_part.save()

            correct_answer_rubric_points = 0 if homework.grade_down else points
            new_rubric = models.Rubric(question_part=new_question_part,
                                       description="Correct answer",
                                       points=correct_answer_rubric_points)
            new_rubric.save()

    _handle_partial_homework_edit(request, homework.id, data, course, False)
Exemple #3
0
    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()
Exemple #4
0
  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')
Exemple #5
0
    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')