def available_to(self, user, matching_names=None): """List of Objectives available to the given User :param user: User :param matching_names: optional list of Objective names to further restrict availability. TODO: matching should probably match on ID, rather than name. It's based on the name because that's what the form that ultimately uses this query is using, but that could be changed. """ q = Objective.query.filter( and_( Objective.subject_id == user.subject_id, or_( Objective.created_by_id.in_(User.admin_usersQ().options( load_only("id"))), Objective.created_by_id == user.id))) if matching_names: q = q.filter(Objective.name.in_(matching_names)) return q.all()