Commit 50197ac6 by tasawernawaz Committed by Tasawer Nawaz

Fixed institution drop down validation ECOM-6709

parent 57a8f840
...@@ -6,7 +6,12 @@ from django.utils.translation import ugettext_lazy as _ ...@@ -6,7 +6,12 @@ from django.utils.translation import ugettext_lazy as _
from course_discovery.apps.course_metadata.choices import CourseRunPacing from course_discovery.apps.course_metadata.choices import CourseRunPacing
from course_discovery.apps.course_metadata.models import Person, Organization from course_discovery.apps.course_metadata.models import Person, Organization
from course_discovery.apps.publisher.models import Course, CourseRun, Seat, User from course_discovery.apps.publisher.models import Course, CourseRun, Seat, User, OrganizationExtension
class UserModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.get_full_name()
class BaseCourseForm(forms.ModelForm): class BaseCourseForm(forms.ModelForm):
...@@ -56,7 +61,7 @@ class CustomCourseForm(CourseForm): ...@@ -56,7 +61,7 @@ class CustomCourseForm(CourseForm):
number = forms.CharField(label=_('Course Number'), required=True) number = forms.CharField(label=_('Course Number'), required=True)
# users will be loaded through AJAX call based on organization # users will be loaded through AJAX call based on organization
team_admin = forms.ModelChoiceField( team_admin = UserModelChoiceField(
queryset=User.objects.none(), required=True, queryset=User.objects.none(), required=True,
label=_('Organization Course Admin'), label=_('Organization Course Admin'),
) )
...@@ -71,12 +76,10 @@ class CustomCourseForm(CourseForm): ...@@ -71,12 +76,10 @@ class CustomCourseForm(CourseForm):
) )
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
team_admin_id = kwargs.pop('team_admin_id', None) organization = kwargs.pop('organization', None)
if team_admin_id: if organization:
try: org_extension = OrganizationExtension.objects.get(organization=organization)
self.declared_fields['team_admin'].queryset = User.objects.filter(id=team_admin_id) self.declared_fields['team_admin'].queryset = User.objects.filter(groups__name=org_extension.group)
except Exception: # pylint: disable=broad-except
pass
super(CustomCourseForm, self).__init__(*args, **kwargs) super(CustomCourseForm, self).__init__(*args, **kwargs)
......
from django.test import TestCase
from course_discovery.apps.core.models import User
from course_discovery.apps.core.tests.factories import UserFactory
from course_discovery.apps.publisher.forms import CustomCourseForm
class UserModelChoiceFieldTests(TestCase):
"""
Tests for the publisher model "UserModelChoiceField".
"""
def test_course_form(self):
"""
Verify that UserModelChoiceField returns `full_name` as choice label.
"""
course_form = CustomCourseForm()
user = UserFactory(username='test_user', full_name='Test Full Name')
course_form.fields['team_admin'].queryset = User.objects.all()
course_form.fields['team_admin'].empty_label = None
# we need to loop through choices because it is a ModelChoiceIterator
for __, choice_label in course_form.fields['team_admin'].choices:
self.assertEqual(choice_label, user.full_name)
...@@ -46,6 +46,7 @@ class CreateUpdateCourseViewTests(TestCase): ...@@ -46,6 +46,7 @@ class CreateUpdateCourseViewTests(TestCase):
self.user = UserFactory() self.user = UserFactory()
self.organization_extension = factories.OrganizationExtensionFactory() self.organization_extension = factories.OrganizationExtensionFactory()
self.group = self.organization_extension.group self.group = self.organization_extension.group
self.user.groups.add(self.group)
self.course = factories.CourseFactory() self.course = factories.CourseFactory()
self.course_run = factories.CourseRunFactory(course=self.course) self.course_run = factories.CourseRunFactory(course=self.course)
...@@ -263,15 +264,6 @@ class CreateUpdateCourseViewTests(TestCase): ...@@ -263,15 +264,6 @@ class CreateUpdateCourseViewTests(TestCase):
self.assertContains(response, self.assertContains(response,
'<select class="field-input input-select" id="id_organization" name="organization">') '<select class="field-input input-select" id="id_organization" name="organization">')
def test_create_with_invalid_team_admin(self):
""" Verify that view returns status_code=400 with invalid team admin. """
data = {'number': 'course_1', 'image': ''}
course_dict = self._post_data(data, self.course, self.course_run, self.seat)
course_dict['team_admin'] = "-------"
response = self.client.post(reverse('publisher:publisher_courses_new'), course_dict, files=data['image'])
self.assertEqual(response.status_code, 400)
@ddt.data('contacted_partner_manager', 'pacing_type') @ddt.data('contacted_partner_manager', 'pacing_type')
def test_create_without_selecting_radio_buttons(self, button_field): def test_create_without_selecting_radio_buttons(self, button_field):
""" """
......
...@@ -179,9 +179,10 @@ class CreateCourseView(mixins.LoginRequiredMixin, CreateView): ...@@ -179,9 +179,10 @@ class CreateCourseView(mixins.LoginRequiredMixin, CreateView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
ctx = self.get_context_data() ctx = self.get_context_data()
# add selected team admin into choice of ChoiceField # pass selected organization to CustomCourseForm to populate related
team_admin_id = self.request.POST.get('team_admin') # choices into institution admin field
course_form = self.course_form(request.POST, request.FILES, team_admin_id=team_admin_id) organization = self.request.POST.get('organization')
course_form = self.course_form(request.POST, request.FILES, organization=organization)
run_form = self.run_form(request.POST) run_form = self.run_form(request.POST)
seat_form = self.seat_form(request.POST) seat_form = self.seat_form(request.POST)
......
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