示例#1
0
    def save_model(self, request, obj, form, change):
        # If trying to do an update, check to see if there's an original user associated with the model
        try:
            original_user = self.model.objects.get(id=obj.id).user
        except self.model.DoesNotExist:
            original_user = None

        obj.save()
        publisher_courses = obj.organization.publisher_courses

        courses_without_role = publisher_courses.exclude(
            course_user_roles__role=obj.role)

        CourseUserRole.objects.bulk_create([
            CourseUserRole(course=course, user=obj.user, role=obj.role)
            for course in courses_without_role
        ])

        if original_user:
            CourseUserRole.objects.filter(
                course__organizations__in=[obj.organization],
                role=obj.role,
                user=original_user,
            ).update(user=obj.user)
        else:
            CourseUserRole.objects.filter(
                course__organizations__in=[obj.organization],
                role=obj.role,
            ).update(user=obj.user)

        # Assign user a group according to its role.
        group = Group.objects.get(name=self.role_groups_dict.get(obj.role))
        if group not in obj.user.groups.all():
            obj.user.groups.add(
                *(group, Group.objects.get(name=INTERNAL_USER_GROUP_NAME)))
示例#2
0
    def save_model(self, request, obj, form, change):
        obj.save()
        publisher_courses = obj.organization.publisher_courses

        courses_without_role = publisher_courses.exclude(course_user_roles__role=obj.role)

        CourseUserRole.objects.bulk_create(
            [CourseUserRole(course=course, user=obj.user, role=obj.role) for course in courses_without_role]
        )

        CourseUserRole.objects.filter(course__organizations__in=[obj.organization], role=obj.role).update(user=obj.user)

        # Assign user a group according to its role.
        group = Group.objects.get(name=self.role_groups_dict.get(obj.role))
        if group not in obj.user.groups.all():
            obj.user.groups.add(*(group, Group.objects.get(name=INTERNAL_USER_GROUP_NAME)))