Commit bd3f45fe by tasawernawaz Committed by Tasawer Nawaz

Fix value error in instructor lookup ECOM-7083

parent d99b003c
......@@ -66,13 +66,9 @@ class PersonAutocomplete(autocomplete.Select2QuerySetView):
return []
def get_result_label(self, result):
profile_image = result.profile_image_url
if hasattr(result.profile_image, 'url'):
profile_image = result.profile_image.url
context = {
'uuid': result.uuid,
'profile_image': profile_image,
'profile_image': result.get_profile_image_url,
'full_name': result.full_name,
'position': result.position if hasattr(result, 'position') else None
}
......
......@@ -222,6 +222,15 @@ class Person(TimeStampedModel):
def full_name(self):
return ' '.join((self.given_name, self.family_name,))
@property
def get_profile_image_url(self):
url = self.profile_image_url
if not url:
if self.profile_image and hasattr(self.profile_image, 'url'):
url = self.profile_image.url
return url
class Position(TimeStampedModel):
""" Position model.
......
......@@ -163,7 +163,7 @@ class AutocompleteTests(TestCase):
response = self.client.get(reverse('admin_metadata:person-autocomplete'))
self._assert_response(response, 0)
def test_instructor_label(self):
def test_instructor_position_in_label(self):
""" Verify that instructor label contains position of instructor if it exists."""
position_title = 'professor'
PositionFactory.create(person=self.instructors[0], title=position_title, organization=self.organizations[0])
......@@ -174,6 +174,13 @@ class AutocompleteTests(TestCase):
position=position_title,
organization=self.organizations[0].name))
def test_instructor_image_in_label(self):
""" Verify that instructor label contains profile image url.
"""
response = self.client.get(reverse('admin_metadata:person-autocomplete'))
self.assertContains(response, self.instructors[0].get_profile_image_url)
self.assertContains(response, self.instructors[1].get_profile_image_url)
def _assert_response(self, response, expected_length):
""" Assert autocomplete response. """
self.assertEqual(response.status_code, 200)
......
......@@ -300,6 +300,21 @@ class PersonTests(TestCase):
expected = self.person.given_name + ' ' + self.person.family_name
self.assertEqual(self.person.full_name, expected)
def test_get_profile_image_url(self):
"""
Verify that property returns profile_image_url if profile_image_url
exists other wise it returns uploaded image url.
"""
self.assertEqual(self.person.get_profile_image_url, self.person.profile_image_url)
# create another person with out profile_image_url
person = factories.PersonFactory(profile_image_url=None)
self.assertEqual(person.get_profile_image_url, person.profile_image.url)
# create another person with out profile_image_url and profile_image
person = factories.PersonFactory(profile_image_url=None, profile_image=None)
self.assertFalse(person.get_profile_image_url)
def test_str(self):
""" Verify casting an instance to a string returns the person's full name. """
self.assertEqual(str(self.person), self.person.full_name)
......
......@@ -26,7 +26,7 @@ class UserModelChoiceField(forms.ModelChoiceField):
class PersonModelMultipleChoice(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
context = {
'profile_image': obj.profile_image_url,
'profile_image': obj.get_profile_image_url,
'full_name': obj.full_name
}
return str(render_to_string('publisher/_personFieldLabel.html', context=context))
......
......@@ -33,14 +33,15 @@ class PersonModelMultipleChoiceTests(TestCase):
Verify that PersonModelMultipleChoice returns `full_name` and `profile_image_url` as choice label.
"""
course_form = CustomCourseRunForm()
course_form.fields['staff'].empty_label = None
person = PersonFactory()
course_form.fields['staff'].queryset = Person.objects.all()
course_form.fields['staff'].empty_label = None
# we need to loop through choices because it is a ModelChoiceIterator
for __, choice_label in course_form.fields['staff'].choices:
expected = '<img src="{profile_image}"/><span>{full_name}</span>'.format(
expected = '<img src="{url}"/><span>{full_name}</span>'.format(
full_name=person.full_name,
profile_image=person.profile_image_url
url=person.get_profile_image_url
)
self.assertEqual(choice_label.strip(), expected)
......@@ -191,9 +191,9 @@ function closeModal(event, modal) {
$(document).on('change', '#id_staff', function (e) {
var id = this.value,
$instructorSelector = $('.instructor-select'),
var $instructorSelector = $('.instructor-select'),
$instructor = $instructorSelector.find('.select2-selection__choice'),
id = $instructor.find('.instructor-option').last().prop("id"),
image_source,
name;
$instructorSelector.find('.select2-selection__clear').remove();
......
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