Commit ac5f3649 by Tasawer Nawaz Committed by GitHub

Merge pull request #509 from edx/tasawer/ecom-6666-fron-end-of-istitution-admin

ECOM-6666 front end of istitution admin
parents c9ccb22f e1623e86
......@@ -53,7 +53,9 @@ class CustomCourseForm(CourseForm):
)
title = forms.CharField(label=_('Course Title'), required=True)
number = forms.CharField(label=_('Course Number'), required=True)
team_admin = forms.ModelChoiceField(queryset=User.objects.all(), required=True)
# users will be loaded through AJAX call based on organization
team_admin = forms.ModelChoiceField(queryset=User.objects.none(), required=True)
class Meta(CourseForm.Meta):
model = Course
......@@ -64,6 +66,13 @@ class CustomCourseForm(CourseForm):
'level_type', 'organization', 'is_seo_review', 'keywords',
)
def __init__(self, *args, **kwargs):
queryset = kwargs.pop('team_admin_queryset', None)
if queryset:
self.declared_fields['team_admin'].queryset = queryset
super(CustomCourseForm, self).__init__(*args, **kwargs)
class UpdateCourseForm(BaseCourseForm):
""" Course form to update specific fields for already created course. """
......
......@@ -242,6 +242,11 @@ class CreateUpdateCourseViewTests(TestCase):
course = Course.objects.get(number=data['number'])
self._assert_test_data(response, course, seat_type, 0)
def test_create_form_with_single_organization(self):
"""Verify that if there is only one organization then that organization will be shown as text. """
response = self.client.get(reverse('publisher:publisher_courses_new'))
self.assertContains(response, '<input id="id_organization" name="organization" type="hidden"')
def _post_data(self, data, course, course_run, seat):
course_dict = model_to_dict(course)
course_dict.update(**data)
......
......@@ -16,6 +16,7 @@ from django_fsm import TransitionNotAllowed
from guardian.shortcuts import get_objects_for_user
from rest_framework.generics import UpdateAPIView
from course_discovery.apps.core.models import User
from course_discovery.apps.publisher.choices import PublisherUserRole
from course_discovery.apps.publisher.forms import (
CourseForm, CourseRunForm, SeatForm, CustomCourseForm, CustomCourseRunForm,
......@@ -178,9 +179,13 @@ class CreateCourseView(mixins.LoginRequiredMixin, CreateView):
def post(self, request, *args, **kwargs):
ctx = self.get_context_data()
course_form = self.course_form(request.POST, request.FILES)
# add selected team admin into choice of ChoiceField
team_admin_queryset = User.objects.filter(id=self.request.POST.get('team_admin'))
course_form = self.course_form(request.POST, request.FILES, team_admin_queryset=team_admin_queryset)
run_form = self.run_form(request.POST)
seat_form = self.seat_form(request.POST)
if course_form.is_valid() and run_form.is_valid() and seat_form.is_valid():
try:
with transaction.atomic():
......
......@@ -23,4 +23,36 @@ $(document).ready(function(){
$(".field-admin-name").hide();
$("#field-team-admin").show();
});
var org_id = $('#organization-name').data('org_id');
if (org_id){
loadAdminUsers(org_id);
}
});
$(document).on('change', '#id_organization', function (e) {
var org_id = $(this).value();
// it will reset the select input
$("#id_team_admin").prop("selectedIndex", 0);
if (org_id) {
loadAdminUsers(org_id);
}
});
function loadAdminUsers(org_id) {
$.getJSON({
url: '/publisher/api/admins/organizations/'+ org_id +'/users/',
success: function (data) {
var teamAdminDropDown = $('#id_team_admin');
teamAdminDropDown.empty();
// it will looks same like other django model choice fields
teamAdminDropDown.append('<option selected="selected">---------</option>');
$.each(data.results, function (i, user) {
teamAdminDropDown.append($('<option> </option>').val(user.id).html(user.full_name));
});
}
});
}
......@@ -714,3 +714,7 @@ select {
@include padding(20px, 20px, 20px, 20px);
border: 2px solid #169bd5;
}
#id_organization {
width: auto;
}
......@@ -35,22 +35,28 @@
<div class="field-title">{% trans "INSTITUTION INFORMATION" %}</div>
<div class="row">
<div class="col col-6 help-text">
{% trans "Please choose the school that will be providing the course. Once chosen then you can select an administrator for the studio shell." %}
</div>
<div class="col col-6">
<label class="field-label">{{ course_form.organization.label_tag }}
<span class="required">* {% trans "required" %}</span>
<div class="col col-6 help-text">
{% trans "Please choose the school that will be providing the course. Once chosen then you can select an administrator for the studio shell." %}
</div>
<div class="col col-6">
{% if course_form.organization.choices|length > 1 %}
<label class="field-label">{{ course_form.organization.label_tag }} <span
class="required">* {% trans "required" %}</span> </label>
{{ course_form.organization }}
{% else %}
{% with course_form.organization.field.queryset|first as organization %}
<label id="organization-name" class="field-label"
data-org_id="{{ organization.id }}">{{ course_form.organization.label_tag }} {{ organization.name }}
</label>
{{ course_form.organization }}
<input id="id_organization" name="organization" type="hidden" value="{{ organization.id }}">
{% endwith %}
{% endif %}
<label class="field-label">
{{ course_form.team_admin.label_tag }}
<span class="required">* {% trans "required" %}</span>
</label>
{{ course_form.team_admin }}
<label class="field-label">
{{ course_form.team_admin.label_tag }} <span class="required">* {% trans "required" %}</span>
</label> {{ course_form.team_admin }}
</div>
</div>
</div>
<div class="field-title">{% trans "Course Title" %}</div>
......
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