Commit ff0ac78a by Waheed Ahmed

Fixed search/add staff.

ECOM-7416
parent 54bedfae
import uuid
from dal import autocomplete
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q
from django.template.loader import render_to_string
......@@ -55,20 +56,19 @@ class VideoAutocomplete(autocomplete.Select2QuerySetView):
return []
class PersonAutocomplete(autocomplete.Select2QuerySetView):
class PersonAutocomplete(LoginRequiredMixin, autocomplete.Select2QuerySetView):
def get_queryset(self):
if self.request.user.is_authenticated() and self.request.user.is_staff:
queryset = Person.objects.all()
if self.q:
qs = queryset.filter(Q(given_name__icontains=self.q) | Q(family_name__icontains=self.q))
if not qs:
try:
q_uuid = uuid.UUID(self.q).hex
qs = queryset.filter(uuid=q_uuid)
except ValueError:
pass
return qs
queryset = Person.objects.all()
if self.q:
qs = queryset.filter(Q(given_name__icontains=self.q) | Q(family_name__icontains=self.q))
if not qs:
try:
q_uuid = uuid.UUID(self.q).hex
qs = queryset.filter(uuid=q_uuid)
except ValueError:
pass
return qs
return []
......
import json
from urllib.parse import quote
import ddt
from django.core.urlresolvers import reverse
......@@ -204,3 +205,30 @@ class AutocompleteTests(TestCase):
reverse('admin_metadata:person-autocomplete') + '?q={q}'.format(q=uuid)
)
self._assert_response(response, 0)
def test_instructor_autocomplete_without_staff_user(self):
""" Verify instructor autocomplete returns the data if user is not staff. """
non_staff_user = UserFactory()
self.client.logout()
self.client.login(username=non_staff_user.username, password=USER_PASSWORD)
response = self.client.get(
reverse('admin_metadata:person-autocomplete') + '?q={q}'.format(q='ins')
)
self._assert_response(response, 2)
def test_instructor_autocomplete_without_login(self):
""" Verify instructor autocomplete returns the zero record if user is not logged in. """
self.client.logout()
person_autocomplete_url = reverse(
'admin_metadata:person-autocomplete'
) + '?q={q}'.format(q=self.instructors[0].uuid)
response = self.client.get(person_autocomplete_url)
self.assertRedirects(
response,
expected_url='{url}?next={next}'.format(url=reverse('login'), next=quote(person_autocomplete_url)),
status_code=302,
target_status_code=302
)
......@@ -187,7 +187,7 @@ class CustomCourseRunForm(CourseRunForm):
start = forms.DateTimeField(label=_('Course Start Date'), required=True)
end = forms.DateTimeField(label=_('Course End Date'), required=True)
staff = PersonModelMultipleChoice(
label=_('instructor'),
label=_('Instructor'),
queryset=Person.objects.all(),
widget=autocomplete.ModelSelect2Multiple(
url='admin_metadata:person-autocomplete',
......
......@@ -537,6 +537,7 @@ class CourseRunEditView(mixins.LoginRequiredMixin, mixins.PublisherPermissionMix
course_run = context.get('course_run')
course = course_run.course
context['course_form'] = self.course_form(
user=request.user,
instance=course,
initial=context.get('initial'),
organization=context.get('organization'),
......@@ -568,6 +569,7 @@ class CourseRunEditView(mixins.LoginRequiredMixin, mixins.PublisherPermissionMix
course_form = self.course_form(
request.POST, request.FILES,
user=request.user,
instance=course_run.course,
initial=context.get('initial'),
organization=context.get('organization'),
......
......@@ -7,14 +7,14 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-03-10 12:45+0500\n"
"POT-Creation-Date: 2017-03-13 14:43+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"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: \n"
#: apps/api/filters.py
#, python-brace-format
......@@ -594,8 +594,8 @@ msgstr ""
msgid "Course End Date"
msgstr ""
#: apps/publisher/forms.py
msgid "instructor"
#: apps/publisher/forms.py templates/publisher/add_update_course_form.html
msgid "Instructor"
msgstr ""
#: apps/publisher/forms.py
......@@ -1607,10 +1607,6 @@ msgid "Add New"
msgstr ""
#: templates/publisher/add_update_course_form.html
msgid "Instructor"
msgstr ""
#: templates/publisher/add_update_course_form.html
#: templates/publisher/course_edit_form.html
msgid "SUBJECT FIELD"
msgstr ""
......
......@@ -7,14 +7,14 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-03-10 12:45+0500\n"
"POT-Creation-Date: 2017-03-13 14:43+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"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: \n"
#: static/js/catalogs-change-form.js
msgid "Preview"
......
......@@ -7,14 +7,14 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-03-10 12:45+0500\n"
"POT-Creation-Date: 2017-03-13 14:43+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"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: apps/api/filters.py
......@@ -722,9 +722,9 @@ msgstr "Çöürsé Stärt Däté Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αм
msgid "Course End Date"
msgstr "Çöürsé Énd Däté Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт α#"
#: apps/publisher/forms.py
msgid "instructor"
msgstr "ïnstrüçtör Ⱡ'σяєм ιρѕυм ∂σłσ#"
#: apps/publisher/forms.py templates/publisher/add_update_course_form.html
msgid "Instructor"
msgstr "Ìnstrüçtör Ⱡ'σяєм ιρѕυм ∂σłσ#"
#: apps/publisher/forms.py
#: templates/publisher/course_run_detail/_preview_accept_popup.html
......@@ -1912,10 +1912,6 @@ msgid "Add New"
msgstr "Àdd Néw Ⱡ'σяєм ιρѕυм #"
#: templates/publisher/add_update_course_form.html
msgid "Instructor"
msgstr "Ìnstrüçtör Ⱡ'σяєм ιρѕυм ∂σłσ#"
#: templates/publisher/add_update_course_form.html
#: templates/publisher/course_edit_form.html
msgid "SUBJECT FIELD"
msgstr "SÛBJÉÇT FÌÉLD Ⱡ'σяєм ιρѕυм ∂σłσя ѕι#"
......
......@@ -7,14 +7,14 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-03-10 12:45+0500\n"
"POT-Creation-Date: 2017-03-13 14:43+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"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: static/js/catalogs-change-form.js
......
......@@ -29,13 +29,19 @@
<label class="field-label">{% trans "Organization" %}:
<span class="required">* {% trans "required" %}</span>
</label>
{% if course_form.organization.field.queryset.all.count > 1 %}
{% if edit_mode %}
<span class="read-only-field">{{ organization_name }}</span>
{{ course_form.organization }}
{% else %}
{% with course_form.organization.field.queryset|first as organization %}
<span class="read-only-field">{{ organization.name }}</span>
<input id="id_organization" name="organization" type="hidden" value="{{ organization.id }}">
{% endwith %}
{% if course_form.organization.field.queryset.all.count > 1 %}
{{ course_form.organization }}
{% else %}
{% with course_form.organization.field.queryset|first as organization %}
<span class="read-only-field">{{ organization.name }}</span>
<input id="id_organization" name="organization" type="hidden" value="{{ organization.id }}">
{% endwith %}
{% endif %}
{% endif %}
<label class="field-label" for="bio">{% trans "Bio" %}:
......
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