Esempio n. 1
0
    def resolve_classes(self, info, **kwargs):
        '''
        if an enc_jwt argument is supplied to the classes query
        we can get back every class that a teacher has created
        
        if we do not supply an enc_jwt or the enc_jwt is wrong we are then
        returned with EVERY class created

        TODO: do not return every single class if JWT is wrong/missing
        '''
        try:
            enc_jwt = kwargs.get('enc_jwt')
            dec_jwt = decode_jwt(enc_jwt)
            teacher = Teacher.objects.get(TeacherID=dec_jwt['sub']['id'])
            '''
            grabs every class that contains a teacher with this email address

            << tableName >>__<< field >>__contains = << value to search for >>
            NOTE: these are double underscores between each field
            '''

            return teacher.class_set.all()

            # return Class.objects.filter(
            #     TeacherID__TeacherEmail__contains=teacher.TeacherEmail
            #     )

        except:
            raise GraphQLError('Please supply a valid JWT')
Esempio n. 2
0
    def resolve_teacher(self, info, **kwargs):
        enc_jwt = kwargs.get('enc_jwt')
        dec_jwt = decode_jwt(enc_jwt)
        teacherID = dec_jwt['sub']['id']
        teacher = Teacher.objects.filter(TeacherID=teacherID)

        return teacher
Esempio n. 3
0
    def resolve_teacher_quizzes(self, info, **kwargs):
        if 'enc_jwt' in kwargs:
            enc_jwt = kwargs.get('enc_jwt')
            dec_jwt = decode_jwt(enc_jwt)
            teacherID = dec_jwt['sub']['id']
            teacher = Teacher.objects.get(TeacherID=teacherID)

            return teacher.quiz_set.all()

        return GraphQLError('Please supply a valid JWT')
Esempio n. 4
0
    def mutate(self, info, QuizID, QuestionText, isMajor, encJWT):
        decJWT = decode_jwt(encJWT)
        teacherID = decJWT['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)

        quiz = Quiz.objects.get(QuizID=QuizID)
        question = Question.objects.create(QuizID=quiz,
                                           QuestionText=QuestionText,
                                           isMajor=isMajor)

        return CreateQuestion(question=question)
Esempio n. 5
0
    def mutate(self, info, ChoiceText, isCorrect, status, QuestionID, encJWT):
        decJWT = decode_jwt(encJWT)
        teacherID = decJWT['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)

        question = Question.objects.get(QuestionID=QuestionID)
        choice = Choice.objects.create(QuestionID=question,
                                       ChoiceText=ChoiceText,
                                       isCorrect=isCorrect,
                                       status=status)

        return CreateChoice(choice=choice)
Esempio n. 6
0
    def mutate(self, info, ClassID, ClassName, enc_jwt):
        dec_jwt = decode_jwt(enc_jwt)
        teacherID = dec_jwt['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)
        updated_class = Class.objects.get(ClassID=ClassID)

        if teacher and updated_class:
            # object.save() cannot take any args, so just change entries first
            updated_class.ClassName = ClassName if len(
                ClassName) > 0 else updated_class.ClassName
            updated_class.save()

            # this is what GraphQL is going to return
            return UpdateClassName(updated_class=updated_class)

        else:
            raise GraphQLError('Something went wrong.')
Esempio n. 7
0
    def mutate(self, info, QuestionID, QuestionText, enc_jwt):
        dec_jwt = decode_jwt(enc_jwt)
        teacherID = dec_jwt['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)
        updated_question = Question.objects.get(QuestionID=QuestionID)

        if teacher and updated_question:
            # object.save() cannot take any args, so just change entries first
            updated_question.QuestionText = QuestionText if len(
                QuestionText) > 0 else updated_question.QuestionText
            updated_question.save()

            # this is what GraphQL is going to return
            return UpdateQuestionText(updated_question=updated_question)

        else:
            raise GraphQLError('Something went wrong.')
Esempio n. 8
0
    def mutate(self, info, QuizID, Classroom, encJWT):
        decJWT = decode_jwt(encJWT)
        teacherID = decJWT['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)
        classroom = Class.objects.get(ClassID=Classroom)
        quiz = Quiz.objects.get(QuizID=QuizID)

        # adds a new record in QuizScores for EACH student
        for student in classroom.student_set.all():
            QuizScores.objects.create(StudentID=student.StudentID,
                                      QuizID=quiz.QuizID,
                                      ClassID=classroom)

        quiz.Classes.add(classroom)
        quiz.save()

        return AddQuizToClass(quiz=quiz)
Esempio n. 9
0
    def mutate(self, info, QuizID, QuizName, enc_jwt):
        dec_jwt = decode_jwt(enc_jwt)
        teacherID = dec_jwt['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)
        updated_quiz = Quiz.objects.get(QuizID=QuizID)

        if teacher and updated_quiz:
            # object.save() cannot take any args, so just change entries first
            updated_quiz.QuizName = QuizName if len(
                QuizName) > 0 else updated_quiz.QuizName
            updated_quiz.save()

            # this is what GraphQL is going to return
            return UpdateQuizName(updated_quiz=updated_quiz)

        else:
            raise GraphQLError('Something went wrong.')
Esempio n. 10
0
    def mutate(self, info, QuizName, Public, encJWT):
        decJWT = decode_jwt(encJWT)
        teacherID = decJWT['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)
        quizzes = teacher.quiz_set.all()

        if teacher.Subscription == "" and len(quizzes) >= 10:
            return GraphQLError(
                'Free users cannot create more than 10 quizzes.')
        elif teacher.Subscription == "Basic" and len(quizzes) >= 25:
            return GraphQLError(
                'Basic users cannot create more than 25 quizzes.')
        else:
            quiz = Quiz.objects.create(Teacher=teacher,
                                       QuizName=QuizName,
                                       Public=Public)
            return CreateQuiz(quiz=quiz)
Esempio n. 11
0
    def mutate(self, info, incoming_jwt, TeacherName, TeacherEmail,
               OldPassword, NewPassword):
        dec_jwt = decode_jwt(incoming_jwt)
        teacherID = dec_jwt['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)

        old_pw = OldPassword.encode('utf-8')
        hashed_old_pw = teacher.TeacherPW.encode('utf-8')

        if old_pw:
            if teacher:
                if bcrypt.checkpw(old_pw, hashed_old_pw):
                    # object.save() cannot take any args, so just change entries directly first.
                    teacher.TeacherName = TeacherName if len(
                        TeacherName) > 0 else teacher.TeacherName
                    teacher.TeacherEmail = TeacherEmail if len(
                        TeacherEmail) > 0 else teacher.TeacherEmail

                    # No point in doing any new password hashing if there's no new password present...
                    if len(NewPassword) > 0:
                        new_password = NewPassword.encode('utf-8')
                        hashed_new_pw = bcrypt.hashpw(
                            new_password, bcrypt.gensalt()).decode('utf-8')
                        teacher.TeacherPW = hashed_new_pw

                    # Save the new teacher data back into the database
                    teacher.save()
                    # Make ID into a string so we can return it easily.
                    teacher.TeacherID = str(teacher.TeacherID)

                    jwt_string = create_jwt(teacher.TeacherID,
                                            teacher.TeacherName,
                                            teacher.TeacherEmail)

                    # this is what GraphQL is going to return
                    return UpdateTeacherInformation(teacher=teacher,
                                                    jwt_string=jwt_string)

                else:
                    raise GraphQLError('Incorrect password provided!')
            else:
                raise GraphQLError('Incorrect password provided!')
        else:
            raise GraphQLError('Incorrect password provided!')
Esempio n. 12
0
    def mutate(self, info, ChoiceID, ChoiceText, isCorrect, status, enc_jwt):
        dec_jwt = decode_jwt(enc_jwt)
        teacherID = dec_jwt['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)
        updated_choice = Choice.objects.get(ChoiceID=ChoiceID)

        if teacher and updated_choice:
            # object.save() cannot take any args, so just change entries first
            updated_choice.ChoiceText = ChoiceText if len(
                ChoiceText) > 0 else updated_choice.ChoiceText
            updated_choice.isCorrect = isCorrect if isCorrect != updated_choice.isCorrect else updated_choice.isCorrect
            updated_choice.status = status if status != updated_choice.status else updated_choice.status
            updated_choice.save()

            # this is what GraphQL is going to return
            return UpdateChoice(updated_choice=updated_choice)

        else:
            raise GraphQLError('Something went wrong.')
Esempio n. 13
0
    def mutate(self, info, ClassName, enc_jwt):
        dec_jwt = decode_jwt(enc_jwt)

        # `user` variable needs to be changed to use the ID given by the JWT
        # for now JWT does NOT return a userID
        teacherID = dec_jwt['sub']['id']
        teacher = Teacher.objects.get(TeacherID=teacherID)

        # if token is expired return expiration error to teacher
        if dec_jwt['exp'] < time.time():
            raise GraphQLError('Token has expired.')

        # if teacher does not exist in the database return error
        if not teacher:
            raise GraphQLError('This teacher does not exist')

        # this portion is unreachable if any of the above conditions are true
        new_class = Class.objects.create(ClassName=ClassName, Teacher=teacher)

        return CreateClass(new_class=new_class)