Commit fea9cc12 by Awais Committed by Awais Qureshi

Adding course-users-roles during the course import.

EDUCATOR-698
parent a44c2f02
......@@ -1228,32 +1228,6 @@ class CourseRunDetailTests(TestCase):
# Verify that user cannot see edit button if he has no role for course.
self.assert_can_edit_permission(can_edit=False)
def test_create_course_user_roles_if_not_exist(self):
"""
Verify that course user roles created if default roles exist on viewing detail page.
"""
self.user.groups.add(self.organization_extension.group)
default_roles = [
PublisherUserRole.MarketingReviewer, PublisherUserRole.ProjectCoordinator, PublisherUserRole.Publisher
]
self.assertEqual(self.course.course_user_roles.filter(role__in=default_roles).count(), 0)
factories.OrganizationUserRoleFactory(
organization=self.organization_extension.organization, role=PublisherUserRole.MarketingReviewer
)
factories.OrganizationUserRoleFactory(
organization=self.organization_extension.organization, role=PublisherUserRole.ProjectCoordinator
)
factories.OrganizationUserRoleFactory(
organization=self.organization_extension.organization, role=PublisherUserRole.Publisher
)
self.client.get(self.page_url)
expected_count = self.course.course_user_roles.filter(course=self.course, role__in=default_roles).count()
self.assertEqual(len(default_roles), expected_count)
# pylint: disable=attribute-defined-outside-init
@ddt.ddt
......@@ -2057,32 +2031,6 @@ class CourseDetailViewTests(TestCase):
self.client.post(reverse('publisher:publisher_courses_edit', args=[self.course.id]), post_data)
def test_create_course_user_roles_if_not_exist(self):
"""
Verify that course user roles created if default roles exist on viewing detail page.
"""
self._assign_user_permission()
default_roles = [
PublisherUserRole.MarketingReviewer, PublisherUserRole.ProjectCoordinator, PublisherUserRole.Publisher
]
self.assertEqual(self.course.course_user_roles.filter(role__in=default_roles).count(), 0)
factories.OrganizationUserRoleFactory(
organization=self.organization_extension.organization, role=PublisherUserRole.MarketingReviewer
)
factories.OrganizationUserRoleFactory(
organization=self.organization_extension.organization, role=PublisherUserRole.ProjectCoordinator
)
factories.OrganizationUserRoleFactory(
organization=self.organization_extension.organization, role=PublisherUserRole.Publisher
)
self.client.get(self.detail_page_url)
expected_count = self.course.course_user_roles.filter(course=self.course, role__in=default_roles).count()
self.assertEqual(len(default_roles), expected_count)
@ddt.ddt
class CourseEditViewTests(TestCase):
......@@ -3233,7 +3181,7 @@ class CreateAdminImportCourseTest(TestCase):
self._make_users_valid(True)
post_data = {'start_id': 100}
response = self.client.post(self.page_url, post_data)
self.assertContains(response, 'Invalid Course ID')
self.assertContains(response, 'Course matching query does not exist.')
def test_import_with_failure(self):
""" Verify page shows error in case of any error. """
......@@ -3242,6 +3190,41 @@ class CreateAdminImportCourseTest(TestCase):
response = self.client.post(self.page_url, post_data)
self.assertContains(response, 'Some error occurred')
def test_course_user_roles_creation(self):
""" Verify course add the course-users roles after importing the course. """
# organization should be available for import
organization = OrganizationFactory()
self.course.authoring_organizations.add(organization)
self._add_org_roles(organization)
self._make_users_valid(True)
post_data = {'start_id': self.course.pk}
response = self.client.post(self.page_url, post_data)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Course Imported')
default_roles = [
PublisherUserRole.MarketingReviewer, PublisherUserRole.ProjectCoordinator, PublisherUserRole.Publisher
]
publisher_course = Course.objects.get(course_metadata_pk=self.course.pk)
expected_count = publisher_course.course_user_roles.filter(role__in=default_roles).count()
self.assertEqual(len(default_roles), expected_count)
def test_course_user_roles_without_organization_extension(self):
""" Verify that course import works fine even without org extension. """
# organization should be available for import
organization = OrganizationFactory()
self.course.authoring_organizations.add(organization)
self._make_users_valid(True)
post_data = {'start_id': self.course.pk}
response = self.client.post(self.page_url, post_data)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Course Imported')
def _make_users_valid(self, switch):
""" make user eligible for the page."""
self.client.logout()
......@@ -3252,3 +3235,19 @@ class CreateAdminImportCourseTest(TestCase):
self.client.login(username=self.user.username, password=USER_PASSWORD)
return self.client.get(self.page_url)
def _add_org_roles(self, organization):
""" Create default roles for the organization """
organization_extension = factories.OrganizationExtensionFactory(organization=organization)
self.user.groups.add(organization_extension.group)
factories.OrganizationUserRoleFactory(
organization=organization_extension.organization, role=PublisherUserRole.MarketingReviewer
)
factories.OrganizationUserRoleFactory(
organization=organization_extension.organization, role=PublisherUserRole.ProjectCoordinator
)
factories.OrganizationUserRoleFactory(
organization=organization_extension.organization, role=PublisherUserRole.Publisher
)
......@@ -155,11 +155,6 @@ class CourseRunDetailView(mixins.LoginRequiredMixin, mixins.PublisherPermissionM
user = self.request.user
course_run = CourseRunWrapper(self.get_object())
course = course_run.course
organization = course.organization_extension.organization
if organization.organization_user_roles.filter(role__in=DEFAULT_ROLES).count() == len(DEFAULT_ROLES):
mixins.check_and_create_course_user_roles(course)
context['object'] = course_run
context['comment_object'] = course_run
......@@ -444,10 +439,6 @@ class CourseDetailView(mixins.LoginRequiredMixin, mixins.PublisherPermissionMixi
user = self.request.user
course = self.object
organization = course.organization_extension.organization
if organization.organization_user_roles.filter(role__in=DEFAULT_ROLES).count() == len(DEFAULT_ROLES):
mixins.check_and_create_course_user_roles(course)
context['can_edit'] = mixins.check_course_organization_permission(
user, course, OrganizationExtension.EDIT_COURSE
......@@ -958,11 +949,22 @@ class AdminImportCourse(mixins.LoginRequiredMixin, TemplateView):
publisher_course = Course.objects.filter(course_metadata_pk=start_id)
if publisher_course.exists():
publisher_course = publisher_course.first()
organization = publisher_course.organizations.first()
# if org has default organization then add its roles.
if (
hasattr(organization, 'organization_extension') and
organization.organization_user_roles.filter(
role__in=DEFAULT_ROLES).count() == len(DEFAULT_ROLES)
):
mixins.check_and_create_course_user_roles(publisher_course)
messages.success(request, 'Course Imported')
else:
messages.error(request, 'Some error occurred. Please check authoring organizations of course.')
except CourseMetaData.DoesNotExist:
messages.error(request, 'Invalid Course ID')
except Exception as ex: # pylint: disable=broad-except
messages.error(request, str(ex))
return super(AdminImportCourse, self).get(request, args, **kwargs)
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-14 17:09+0500\n"
"POT-Creation-Date: 2017-06-20 16:39+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -1336,7 +1336,6 @@ msgstr ""
#: templates/publisher/_render_optional_field.html
#: templates/publisher/course_detail.html
#: templates/publisher/course_run_detail/_all.html
#: templates/publisher/course_run_detail/_credit_seat.html
#: templates/publisher/course_run_detail/_drupal.html
#: templates/publisher/course_run_detail/_studio.html
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-14 17:09+0500\n"
"POT-Creation-Date: 2017-06-20 16:39+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-14 17:09+0500\n"
"POT-Creation-Date: 2017-06-20 16:39+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -1539,7 +1539,6 @@ msgstr "Àççépt Àll Ⱡ'σяєм ιρѕυм ∂σłσ#"
#: templates/publisher/_render_optional_field.html
#: templates/publisher/course_detail.html
#: templates/publisher/course_run_detail/_all.html
#: templates/publisher/course_run_detail/_credit_seat.html
#: templates/publisher/course_run_detail/_drupal.html
#: templates/publisher/course_run_detail/_studio.html
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-14 17:09+0500\n"
"POT-Creation-Date: 2017-06-20 16:39+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment