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 _
from course_discovery.apps.course_metadata.choices import CourseRunPacing
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):
......@@ -56,7 +61,7 @@ class CustomCourseForm(CourseForm):
number = forms.CharField(label=_('Course Number'), required=True)
# users will be loaded through AJAX call based on organization
team_admin = forms.ModelChoiceField(
team_admin = UserModelChoiceField(
queryset=User.objects.none(), required=True,
label=_('Organization Course Admin'),
)
......@@ -71,12 +76,10 @@ class CustomCourseForm(CourseForm):
)
def __init__(self, *args, **kwargs):
team_admin_id = kwargs.pop('team_admin_id', None)
if team_admin_id:
try:
self.declared_fields['team_admin'].queryset = User.objects.filter(id=team_admin_id)
except Exception: # pylint: disable=broad-except
pass
organization = kwargs.pop('organization', None)
if organization:
org_extension = OrganizationExtension.objects.get(organization=organization)
self.declared_fields['team_admin'].queryset = User.objects.filter(groups__name=org_extension.group)
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):
self.user = UserFactory()
self.organization_extension = factories.OrganizationExtensionFactory()
self.group = self.organization_extension.group
self.user.groups.add(self.group)
self.course = factories.CourseFactory()
self.course_run = factories.CourseRunFactory(course=self.course)
......@@ -263,15 +264,6 @@ class CreateUpdateCourseViewTests(TestCase):
self.assertContains(response,
'<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')
def test_create_without_selecting_radio_buttons(self, button_field):
"""
......
......@@ -179,9 +179,10 @@ class CreateCourseView(mixins.LoginRequiredMixin, CreateView):
def post(self, request, *args, **kwargs):
ctx = self.get_context_data()
# add selected team admin into choice of ChoiceField
team_admin_id = self.request.POST.get('team_admin')
course_form = self.course_form(request.POST, request.FILES, team_admin_id=team_admin_id)
# pass selected organization to CustomCourseForm to populate related
# choices into institution admin field
organization = self.request.POST.get('organization')
course_form = self.course_form(request.POST, request.FILES, organization=organization)
run_form = self.run_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