Beispiel #1
0
    def find_or_include(self, objective_id, student_id, tutor_id, by_user, common_assessors=True):
        """Include an objective among a users set of adopted objectives.

        :param objective_id: is the id of the `Objective` to be included.
        :param student_id: is the id of the `User` for whom the `Objective` is being included.
        :param tutor_id: is the id of the `User` who is including the `Objective`.
        :param by_user: is the `User` who is calling the action.
        :param common_assessors: determines whether common assessors should have their assessment updated for the 'Student'.
        """

        include_schema = {'objective_id': s.Use(int),
                         'user_id': s.Use(int),
                         'assessor_id': s.Use(int)
        }
        u = s.Schema(include_schema).validate({'objective_id': objective_id,
                                              'user_id': student_id,
                                              'assessor_id': tutor_id})

        self._check_user_id_or_admin(u['assessor_id'], by_user)

        userobjective = UserObjective.query.filter_by(**u).first()

        if userobjective is None:
            userobjective = UserObjective.create(**u)
            # DJG - this includes a repetition of the logic to first search for a userobjective before creating it - is it better to have a fat model which listens for the new userobjective to be created and then acts on that
            if common_assessors:
                self._set_common_assessors(userobjective)
                self._set_student_objective(userobjective)

        return userobjective