Example #1
0
def sub2(subject_id):

    username = request.get_cookie("username")
    subject = Subject.get(Subject.id == subject_id)
    user = User.get(User.id == int(username))
    print(user)

    result = Result.select().where(Result.username == int(username))
    if (len(result) != 0) and (result[0].is_taken == True):
        return redirect("/result")

    for question in Question.filter(Question.subject_code == subject):
        res = request.forms.get(f"question-{question.id}")
        response = Response(qid=question.id,
                            subject_code=subject_id,
                            user=user,
                            response=res)
        response.save()

        try:
            result = Result.get(Result.username == user,
                                Result.subject_code == subject)
        except:
            result = Result(username=user, subject_code=subject, is_taken=True)

        if int(res) == question.right_option:
            result.marks_obtained += 1

            if result.marks_obtained == 20:
                result.grade = "A"
                result.state = "Outstanding!"

            elif result.marks_obtained > 13:
                result.grade = "B"
                result.state = "Good!"

            elif result.marks_obtained > 7:
                result.grade = "C"
                result.state = "Average!"

            else:
                result.grade = "D"
                result.state = "Poor!"
        else:
            result.marks_obtained += 0

        result.save()
        subject = subject.select()[0]

    return redirect("/choosesub")
def generateResult():
    result = Result()

    student = random.choice(students)
    enrolledCourses = student.getEnrolledCourses()
    randomCourse = random.choice(enrolledCourses)
    exams = randomCourse.getExams()

    result.studentID = student.studentID
    result.examID = exams[0].examID
    result.grade = random.randint(10, 100)

    if result.grade > 55:
        result.passed = 'Y'
    else:
        result.passed = 'N'
    result.insert()
    return result