Commit f5da1339 by Awais Committed by Awais Qureshi

FIxing multiple history object issue while updating course and course-run.

ECOM-7034
parent 2361dd47
...@@ -1598,6 +1598,10 @@ class CourseEditViewTests(TestCase): ...@@ -1598,6 +1598,10 @@ class CourseEditViewTests(TestCase):
""" """
Verify that publisher admin can update an existing course. Verify that publisher admin can update an existing course.
""" """
# only 1 history object exists for a course.
self.assertEqual(self.course.history.all().count(), 1)
self.user.groups.add(Group.objects.get(name=ADMIN_GROUP_NAME)) self.user.groups.add(Group.objects.get(name=ADMIN_GROUP_NAME))
post_data = self._post_data(self.organization_extension) post_data = self._post_data(self.organization_extension)
...@@ -1621,6 +1625,9 @@ class CourseEditViewTests(TestCase): ...@@ -1621,6 +1625,9 @@ class CourseEditViewTests(TestCase):
self.assertEqual(course.title, updated_course_title) self.assertEqual(course.title, updated_course_title)
self.assertEqual(course.changed_by, self.user) self.assertEqual(course.changed_by, self.user)
# After updating 2 history object exists for a course.
self.assertEqual(self.course.history.all().count(), 2)
def test_update_course_with_non_internal_user(self): def test_update_course_with_non_internal_user(self):
""" """
Verify that non-internal user cannot update the course. Verify that non-internal user cannot update the course.
...@@ -1654,6 +1661,7 @@ class CourseEditViewTests(TestCase): ...@@ -1654,6 +1661,7 @@ class CourseEditViewTests(TestCase):
) )
self.assertEqual(self.course.course_team_admin, self.user) self.assertEqual(self.course.course_team_admin, self.user)
self.assertEqual(self.course.history.all().count(), 2)
def test_update_course_organization(self): def test_update_course_organization(self):
""" """
...@@ -1678,6 +1686,7 @@ class CourseEditViewTests(TestCase): ...@@ -1678,6 +1686,7 @@ class CourseEditViewTests(TestCase):
) )
self.assertEqual(self.course.organizations.first(), organization_extension.organization) self.assertEqual(self.course.organizations.first(), organization_extension.organization)
self.assertEqual(self.course.history.all().count(), 2)
def _assign_permissions(self, organization_extension): def _assign_permissions(self, organization_extension):
""" """
...@@ -1820,6 +1829,10 @@ class CourseRunEditViewTests(TestCase): ...@@ -1820,6 +1829,10 @@ class CourseRunEditViewTests(TestCase):
@ddt.data(INTERNAL_USER_GROUP_NAME, ADMIN_GROUP_NAME) @ddt.data(INTERNAL_USER_GROUP_NAME, ADMIN_GROUP_NAME)
def test_update_course_run_without_seat(self, publisher_group): def test_update_course_run_without_seat(self, publisher_group):
""" Verify that internal users can update the data from course run edit page.""" """ Verify that internal users can update the data from course run edit page."""
self.assertEqual(self.new_course_run.history.all().count(), 1)
self.assertEqual(self.new_course_run.course.history.all().count(), 1)
self.client.logout() self.client.logout()
user, __ = create_non_staff_user_and_login(self) user, __ = create_non_staff_user_and_login(self)
...@@ -1847,6 +1860,9 @@ class CourseRunEditViewTests(TestCase): ...@@ -1847,6 +1860,9 @@ class CourseRunEditViewTests(TestCase):
# 1st email is of course-creation # 1st email is of course-creation
self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(mail.outbox), 1)
self.assertEqual(course_run.history.all().count(), 2)
self.assertEqual(course_run.course.history.all().count(), 2)
@ddt.data('start', 'end', 'pacing_type') @ddt.data('start', 'end', 'pacing_type')
def test_update_with_errors(self, field): def test_update_with_errors(self, field):
""" Verify that course run edit page throws error in case of missing required field.""" """ Verify that course run edit page throws error in case of missing required field."""
...@@ -1879,6 +1895,7 @@ class CourseRunEditViewTests(TestCase): ...@@ -1879,6 +1895,7 @@ class CourseRunEditViewTests(TestCase):
def test_update_course_run_with_seat(self): def test_update_course_run_with_seat(self):
""" Verify that course run edit page create seat object also if not exists previously.""" """ Verify that course run edit page create seat object also if not exists previously."""
self.client.logout() self.client.logout()
user, __ = create_non_staff_user_and_login(self) user, __ = create_non_staff_user_and_login(self)
...@@ -1909,6 +1926,8 @@ class CourseRunEditViewTests(TestCase): ...@@ -1909,6 +1926,8 @@ class CourseRunEditViewTests(TestCase):
self.assertEqual(course_run.seats.first().type, Seat.PROFESSIONAL) self.assertEqual(course_run.seats.first().type, Seat.PROFESSIONAL)
self.assertEqual(course_run.seats.first().price, 10) self.assertEqual(course_run.seats.first().price, 10)
self.assertEqual(course_run.seats.first().history.all().count(), 1)
def test_logging(self): def test_logging(self):
""" Verify view logs the errors in case of errors. """ """ Verify view logs the errors in case of errors. """
with patch('django.forms.models.BaseModelForm.is_valid') as mocked_is_valid: with patch('django.forms.models.BaseModelForm.is_valid') as mocked_is_valid:
......
...@@ -315,23 +315,27 @@ class CourseEditView(mixins.PublisherPermissionMixin, UpdateView): ...@@ -315,23 +315,27 @@ class CourseEditView(mixins.PublisherPermissionMixin, UpdateView):
""" """
If the form is valid, update organization and team_admin. If the form is valid, update organization and team_admin.
""" """
self.object = form.save() self.object = form.save(commit=False)
self.object.changed_by = self.request.user self.object.changed_by = self.request.user
self.object.save() self.object.save()
organization_extension = get_object_or_404( organization = form.cleaned_data['organization']
OrganizationExtension, organization=form.data['organization'] if self.object.organizations.first() != organization:
) organization_extension = get_object_or_404(OrganizationExtension, organization=organization)
self.object.organizations.remove(self.object.organizations.first()) self.object.organizations.remove(self.object.organizations.first())
self.object.organizations.add(organization_extension.organization) self.object.organizations.add(organization_extension.organization)
course_admin_role = get_object_or_404( team_admin = form.cleaned_data['team_admin']
CourseUserRole, course=self.object, role=PublisherUserRole.CourseTeam if self.object.course_team_admin != team_admin:
) course_admin_role = get_object_or_404(
course_admin_role.user_id = form.data['team_admin'] CourseUserRole, course=self.object, role=PublisherUserRole.CourseTeam
course_admin_role.save() )
return super(CourseEditView, self).form_valid(form) course_admin_role.user = team_admin
course_admin_role.save()
messages.success(self.request, _('Course updated successfully.'))
return HttpResponseRedirect(self.get_success_url())
class CourseDetailView(mixins.LoginRequiredMixin, mixins.PublisherPermissionMixin, DetailView): class CourseDetailView(mixins.LoginRequiredMixin, mixins.PublisherPermissionMixin, DetailView):
...@@ -541,11 +545,11 @@ class CourseRunEditView(mixins.LoginRequiredMixin, mixins.PublisherPermissionMix ...@@ -541,11 +545,11 @@ class CourseRunEditView(mixins.LoginRequiredMixin, mixins.PublisherPermissionMix
try: try:
with transaction.atomic(): with transaction.atomic():
course = course_form.save() course = course_form.save(commit=False)
course.changed_by = self.request.user course.changed_by = self.request.user
course.save() course.save()
course_run = run_form.save() course_run = run_form.save(commit=False)
course_run.changed_by = self.request.user course_run.changed_by = self.request.user
course_run.save() course_run.save()
......
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-02-03 14:54+0500\n" "POT-Creation-Date: 2017-02-03 15:35+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
#: apps/api/filters.py #: apps/api/filters.py
#, python-brace-format #, python-brace-format
...@@ -752,6 +752,10 @@ msgid "Please fill all required fields." ...@@ -752,6 +752,10 @@ msgid "Please fill all required fields."
msgstr "" msgstr ""
#: apps/publisher/views.py #: apps/publisher/views.py
msgid "Course updated successfully."
msgstr ""
#: apps/publisher/views.py
#, python-brace-format #, python-brace-format
msgid "Course run created successfully for course \"{course_title}\"." msgid "Course run created successfully for course \"{course_title}\"."
msgstr "" msgstr ""
......
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-02-03 14:54+0500\n" "POT-Creation-Date: 2017-02-03 15:35+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
#: static/js/catalogs-change-form.js #: static/js/catalogs-change-form.js
msgid "Preview" msgid "Preview"
......
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-02-03 14:54+0500\n" "POT-Creation-Date: 2017-02-03 15:35+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: apps/api/filters.py #: apps/api/filters.py
...@@ -895,6 +895,10 @@ msgstr "" ...@@ -895,6 +895,10 @@ msgstr ""
"Pléäsé fïll äll réqüïréd fïélds. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тє#" "Pléäsé fïll äll réqüïréd fïélds. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тє#"
#: apps/publisher/views.py #: apps/publisher/views.py
msgid "Course updated successfully."
msgstr "Çöürsé üpdätéd süççéssfüllý. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢#"
#: apps/publisher/views.py
#, python-brace-format #, python-brace-format
msgid "Course run created successfully for course \"{course_title}\"." msgid "Course run created successfully for course \"{course_title}\"."
msgstr "" msgstr ""
......
...@@ -7,14 +7,14 @@ msgid "" ...@@ -7,14 +7,14 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-02-03 14:54+0500\n" "POT-Creation-Date: 2017-02-03 15:35+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Language: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: static/js/catalogs-change-form.js #: static/js/catalogs-change-form.js
......
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