def add_allowance_for_user(cls, exam_id, user_info, key, value):
        """
        Add or (Update) an allowance for a user within a given exam
        """
        user_id = None

        # see if key is a tuple, if it is, then the first element is the key
        if isinstance(key, tuple) and len(key) > 0:  # pylint: disable=len-as-condition
            key = key[0]

        if not cls.is_allowance_value_valid(key, value):
            err_msg = (
                u'allowance_value "{value}" should be non-negative integer value.'
            ).format(value=value)
            raise AllowanceValueNotAllowedException(err_msg)
        # were we passed a PK?
        if isinstance(user_info, int):
            user_id = user_info
        else:
            # we got a string, so try to resolve it
            users = USER_MODEL.objects.filter(username=user_info)
            if not users.exists():
                users = USER_MODEL.objects.filter(email=user_info)

            if not users.exists():
                err_msg = (u'Cannot find user against {user_info}').format(
                    user_info=user_info)
                raise UserNotFoundException(err_msg)

            user_id = users[0].id

        exam = ProctoredExam.get_exam_by_id(exam_id)
        if exam and not exam.is_active:
            err_msg = (
                'Attempted to add an allowance for user_id={user_id} in exam_id={exam_id}, but '
                'this exam is not active.'.format(user_id=user_id,
                                                  exam_id=exam_id))
            raise ProctoredExamNotActiveException(err_msg)

        try:
            student_allowance = cls.objects.get(proctored_exam_id=exam_id,
                                                user_id=user_id,
                                                key=key)
            student_allowance.value = value
            student_allowance.save()
            action = "updated"
        except cls.DoesNotExist:  # pylint: disable=no-member
            student_allowance = cls.objects.create(proctored_exam_id=exam_id,
                                                   user_id=user_id,
                                                   key=key,
                                                   value=value)
            action = "created"
        return student_allowance, action
Esempio n. 2
0
    def add_allowance_for_user(cls, exam_id, user_info, key, value):
        """
        Add or (Update) an allowance for a user within a given exam
        """
        user_id = None

        # see if key is a tuple, if it is, then the first element is the key
        if isinstance(key, tuple) and len(key) > 0:
            key = key[0]

        # were we passed a PK?
        if isinstance(user_info, (int, long)):
            user_id = user_info
        else:
            # we got a string, so try to resolve it
            users = User.objects.filter(username=user_info)
            if not users.exists():
                users = User.objects.filter(email=user_info)

            if not users.exists():
                err_msg = ('Cannot find user against {user_info}').format(
                    user_info=user_info)
                raise UserNotFoundException(err_msg)

            user_id = users[0].id

        try:
            student_allowance = cls.objects.get(proctored_exam_id=exam_id,
                                                user_id=user_id,
                                                key=key)
            student_allowance.value = value
            student_allowance.save()
        except cls.DoesNotExist:  # pylint: disable=no-member
            cls.objects.create(proctored_exam_id=exam_id,
                               user_id=user_id,
                               key=key,
                               value=value)