Commit 2a87747b by Asad Azam Committed by AsadAzam

Validate course state owner role via admin

parent 777769af
...@@ -10,7 +10,7 @@ from course_discovery.apps.publisher.constants import (INTERNAL_USER_GROUP_NAME, ...@@ -10,7 +10,7 @@ from course_discovery.apps.publisher.constants import (INTERNAL_USER_GROUP_NAME,
PROJECT_COORDINATOR_GROUP_NAME, PUBLISHER_GROUP_NAME, PROJECT_COORDINATOR_GROUP_NAME, PUBLISHER_GROUP_NAME,
REVIEWER_GROUP_NAME) REVIEWER_GROUP_NAME)
from course_discovery.apps.publisher.forms import ( from course_discovery.apps.publisher.forms import (
CourseRunAdminForm, CourseRunStateAdminForm, OrganizationExtensionForm, CourseRunAdminForm, CourseRunStateAdminForm, CourseStateAdminForm, OrganizationExtensionForm,
PublisherUserCreationForm, UserAttributesAdminForm PublisherUserCreationForm, UserAttributesAdminForm
) )
from course_discovery.apps.publisher.models import (Course, CourseRun, CourseRunState, CourseState, CourseUserRole, from course_discovery.apps.publisher.models import (Course, CourseRun, CourseRunState, CourseState, CourseUserRole,
...@@ -73,6 +73,7 @@ class OrganizationUserRoleAdmin(SimpleHistoryAdmin): ...@@ -73,6 +73,7 @@ class OrganizationUserRoleAdmin(SimpleHistoryAdmin):
@admin.register(CourseState) @admin.register(CourseState)
class CourseStateAdmin(SimpleHistoryAdmin): class CourseStateAdmin(SimpleHistoryAdmin):
form = CourseStateAdminForm
raw_id_fields = ('changed_by',) raw_id_fields = ('changed_by',)
list_display = ['id', 'name', 'approved_by_role', 'owner_role', 'course', 'marketing_reviewed'] list_display = ['id', 'name', 'approved_by_role', 'owner_role', 'course', 'marketing_reviewed']
search_fields = ['id', 'course__title'] search_fields = ['id', 'course__title']
......
...@@ -17,8 +17,8 @@ from course_discovery.apps.ietf_language_tags.models import LanguageTag ...@@ -17,8 +17,8 @@ from course_discovery.apps.ietf_language_tags.models import LanguageTag
from course_discovery.apps.publisher.choices import CourseRunStateChoices, PublisherUserRole from course_discovery.apps.publisher.choices import CourseRunStateChoices, PublisherUserRole
from course_discovery.apps.publisher.mixins import LanguageModelSelect2Multiple, get_user_organizations from course_discovery.apps.publisher.mixins import LanguageModelSelect2Multiple, get_user_organizations
from course_discovery.apps.publisher.models import ( from course_discovery.apps.publisher.models import (
Course, CourseRun, CourseRunState, CourseUserRole, OrganizationExtension, OrganizationUserRole, PublisherUser, Course, CourseRun, CourseRunState, CourseState, CourseUserRole, OrganizationExtension, OrganizationUserRole,
Seat, User PublisherUser, Seat, User
) )
from course_discovery.apps.publisher.utils import VALID_CHARS_IN_COURSE_NUM_AND_ORG_KEY, is_internal_user from course_discovery.apps.publisher.utils import VALID_CHARS_IN_COURSE_NUM_AND_ORG_KEY, is_internal_user
from course_discovery.apps.publisher.validators import validate_text_count from course_discovery.apps.publisher.validators import validate_text_count
...@@ -546,6 +546,22 @@ class CourseRunStateAdminForm(forms.ModelForm): ...@@ -546,6 +546,22 @@ class CourseRunStateAdminForm(forms.ModelForm):
return cleaned_data return cleaned_data
class CourseStateAdminForm(forms.ModelForm):
class Meta:
model = CourseState
fields = '__all__'
def clean(self):
cleaned_data = self.cleaned_data
owner_role = cleaned_data.get('owner_role')
course = cleaned_data.get('course')
if not CourseUserRole.objects.filter(course=course, role=owner_role):
raise forms.ValidationError(
_('Please create {} course user role before assigning it owner role'.format(owner_role))
)
return cleaned_data
class AdminImportCourseForm(forms.Form): class AdminImportCourseForm(forms.Form):
start_id = forms.IntegerField(min_value=1, label='This course id will import.') start_id = forms.IntegerField(min_value=1, label='This course id will import.')
create_course_run = forms.BooleanField( create_course_run = forms.BooleanField(
......
...@@ -12,10 +12,12 @@ from course_discovery.apps.core.tests.factories import UserFactory ...@@ -12,10 +12,12 @@ from course_discovery.apps.core.tests.factories import UserFactory
from course_discovery.apps.course_metadata.tests.factories import OrganizationFactory from course_discovery.apps.course_metadata.tests.factories import OrganizationFactory
from course_discovery.apps.publisher.choices import CourseRunStateChoices, PublisherUserRole from course_discovery.apps.publisher.choices import CourseRunStateChoices, PublisherUserRole
from course_discovery.apps.publisher.forms import ( from course_discovery.apps.publisher.forms import (
CourseForm, CourseRunForm, CourseRunStateAdminForm, PublisherUserCreationForm, SeatForm CourseForm, CourseRunForm, CourseRunStateAdminForm, CourseStateAdminForm, PublisherUserCreationForm, SeatForm
) )
from course_discovery.apps.publisher.models import Seat from course_discovery.apps.publisher.models import Seat
from course_discovery.apps.publisher.tests.factories import CourseFactory, OrganizationExtensionFactory, SeatFactory from course_discovery.apps.publisher.tests.factories import (
CourseFactory, CourseUserRoleFactory, OrganizationExtensionFactory, SeatFactory
)
class UserModelChoiceFieldTests(TestCase): class UserModelChoiceFieldTests(TestCase):
...@@ -99,6 +101,37 @@ class CourseRunStateAdminFormTests(TestCase): ...@@ -99,6 +101,37 @@ class CourseRunStateAdminFormTests(TestCase):
self.assertEqual(run_state_form.clean(), run_state_form.cleaned_data) self.assertEqual(run_state_form.clean(), run_state_form.cleaned_data)
class CourseStateAdminFormTests(TestCase):
"""
Tests for the publisher "CourseStateAdminForm".
"""
def test_clean_with_invalid_owner_role(self):
"""
Test that 'clean' raises 'ValidationError' if the user role that has been assigned owner does not exist
"""
course_state_form = CourseStateAdminForm()
course_state_form.cleaned_data = {
'owner_role': PublisherUserRole.CourseTeam
}
with self.assertRaises(ValidationError):
course_state_form.clean()
def test_clean_with_valid_owner_role(self):
"""
Test that 'clean' does not raise 'ValidationError' if the user role that has been assigned owner does exist
"""
course = CourseFactory()
user = UserFactory()
CourseUserRoleFactory(course=course, user=user, role=PublisherUserRole.CourseTeam)
course_state_form = CourseStateAdminForm()
course_state_form.cleaned_data = {
'owner_role': PublisherUserRole.CourseTeam,
'course': course
}
self.assertEqual(course_state_form.clean(), course_state_form.cleaned_data)
class PublisherCourseRunEditFormTests(TestCase): class PublisherCourseRunEditFormTests(TestCase):
""" """
Tests for the publisher 'CourseRunForm'. Tests for the publisher 'CourseRunForm'.
......
...@@ -7,7 +7,7 @@ msgid "" ...@@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-20 20:28+0000\n" "POT-Creation-Date: 2018-01-12 11:58+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
...@@ -843,6 +843,10 @@ msgid "Owner role can not be publisher if the state is draft or review" ...@@ -843,6 +843,10 @@ msgid "Owner role can not be publisher if the state is draft or review"
msgstr "" msgstr ""
#: apps/publisher/forms.py #: apps/publisher/forms.py
msgid "Please create {} course user role before assigning it owner role"
msgstr ""
#: apps/publisher/forms.py
msgid "Create initial run for the course" msgid "Create initial run for the course"
msgstr "" msgstr ""
......
...@@ -7,7 +7,7 @@ msgid "" ...@@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-20 20:28+0000\n" "POT-Creation-Date: 2018-01-12 11:58+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
......
...@@ -7,7 +7,7 @@ msgid "" ...@@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-20 20:28+0000\n" "POT-Creation-Date: 2018-01-12 11:58+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
...@@ -1014,6 +1014,12 @@ msgstr "" ...@@ -1014,6 +1014,12 @@ msgstr ""
" ∂σłσя ѕιт αмєт, ¢σηѕє¢тєтυя α#" " ∂σłσя ѕιт αмєт, ¢σηѕє¢тєтυя α#"
#: apps/publisher/forms.py #: apps/publisher/forms.py
msgid "Please create {} course user role before assigning it owner role"
msgstr ""
"Pléäsé çréäté {} çöürsé üsér rölé ßéföré ässïgnïng ït öwnér rölé Ⱡ'σяєм "
"ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тєтυя α#"
#: apps/publisher/forms.py
msgid "Create initial run for the course" msgid "Create initial run for the course"
msgstr "" msgstr ""
"Çréäté ïnïtïäl rün för thé çöürsé Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тє#" "Çréäté ïnïtïäl rün för thé çöürsé Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тє#"
......
...@@ -7,7 +7,7 @@ msgid "" ...@@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-20 20:28+0000\n" "POT-Creation-Date: 2018-01-12 11:58+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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