def test_add_course_roles_with_existing_record(self): """ Verify that method `add_course_roles` does not create the duplicate course user role. """ __, created = CourseUserRole.add_course_roles( self.course, self.marketing_reviewer_role, self.user) self.assertTrue(created) __, created = CourseUserRole.add_course_roles( self.course, self.marketing_reviewer_role, self.user) self.assertFalse(created)
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)))
def check_and_create_course_user_roles(course): organization_extension = course.organization_extension if not course.course_team_admin: course_team_users = User.objects.filter(groups__name=organization_extension.group.name) CourseUserRole.add_course_roles( course=course, role=PublisherUserRole.CourseTeam, user=course_team_users.first() ) if not course.project_coordinator: add_course_role(course, organization_extension, PublisherUserRole.ProjectCoordinator) if not course.publisher: add_course_role(course, organization_extension, PublisherUserRole.Publisher) if not course.marketing_reviewer: add_course_role(course, organization_extension, PublisherUserRole.MarketingReviewer)
def test_add_course_roles(self): """ Verify that method `add_course_roles` created the course user role. """ course_role, created = CourseUserRole.add_course_roles( self.course, self.marketing_reviewer_role, self.user) self.assertTrue(created) self.assertEqual(course_role.course, self.course) self.assertEqual(course_role.user, self.user) self.assertEqual(course_role.role, self.marketing_reviewer_role)
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)))
def add_course_role(course, organization_extension, role): default_role = organization_extension.organization.organization_user_roles.get(role=role) CourseUserRole.add_course_roles(course=course, role=default_role.role, user=default_role.user)
def post(self, request, *args, **kwargs): ctx = self.get_context_data() # pass selected organization to CustomCourseForm to populate related # choices into institution admin field user = self.request.user organization = self.request.POST.get('organization') course_form = self.course_form(request.POST, request.FILES, user=user, organization=organization) run_form = self.run_form(request.POST) seat_form = self.seat_form(request.POST) if course_form.is_valid() and run_form.is_valid( ) and seat_form.is_valid(): try: with transaction.atomic(): seat = None if request.POST.get('type'): seat = seat_form.save(commit=False) run_course = run_form.save(commit=False) course = course_form.save(commit=False) course.changed_by = user course.save() # commit false does not save m2m object. Keyword field is m2m. course_form.save_m2m() run_course.course = course run_course.changed_by = user run_course.save() # commit false does not save m2m object. run_form.save_m2m() if seat: seat.course_run = run_course seat.changed_by = user seat.save() organization_extension = get_object_or_404( OrganizationExtension, organization=course_form.data['organization']) course.organizations.add( organization_extension.organization) # add default organization roles into course-user-roles course.assign_organization_role( organization_extension.organization) # add team admin as CourseTeam role again course CourseUserRole.add_course_roles( course=course, role=PublisherUserRole.CourseTeam, user=User.objects.get( id=course_form.data['team_admin'])) # Initialize workflow for Course. CourseState.objects.create( course=course, owner_role=PublisherUserRole.CourseTeam) # Initialize workflow for Course-run. CourseRunState.objects.create( course_run=run_course, owner_role=PublisherUserRole.CourseTeam) # pylint: disable=no-member messages.success( request, _("You have successfully created a course. You can edit the course information or enter " "information for the course About page at any time. " "An edX project coordinator will create a Studio instance for this course. When you " "receive an email notification that the Studio instance is ready, you can enter course " "content in Studio.")) # sending email for notifying new course is created. emails.send_email_for_course_creation(course, run_course) return HttpResponseRedirect(self.get_success_url( course.id)) except Exception as e: # pylint: disable=broad-except # pylint: disable=no-member error_message = _( 'An error occurred while saving your changes. {error}' ).format(error=str(e)) messages.error(request, error_message) if not messages.get_messages(request): messages.error(request, _('Please fill all required fields.')) if course_form.errors.get('image'): messages.error(request, course_form.errors.get('image')) ctx.update({ 'course_form': course_form, 'run_form': run_form, 'seat_form': seat_form }) return render(request, self.template_name, ctx, status=400)