Skip to content

Commit

Permalink
Merge pull request #597 from edx/hammad/ENT-1878
Browse files Browse the repository at this point in the history
ENT-1878 | Automate catalog query when updating enterprise customer catalog
  • Loading branch information
HammadAhmadWaqas authored Oct 10, 2019
2 parents 61fad2f + 5efcd6f commit cd96a36
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Change Log
Unreleased
----------

[2.0.4] - 2019-10-10
--------------------

* Added preview button for EnterpriseCustomerCatalogs in EnterpriseCustomer admin page


[2.0.3] - 2019-10-09
---------------------

Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

from __future__ import absolute_import, unicode_literals

__version__ = "2.0.3"
__version__ = "2.0.4"

default_app_config = "enterprise.apps.EnterpriseConfig" # pylint: disable=invalid-name
14 changes: 14 additions & 0 deletions enterprise/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from enterprise.admin.actions import export_as_csv_action
from enterprise.admin.forms import (
EnterpriseCustomerAdminForm,
EnterpriseCustomerCatalogAdminForm,
EnterpriseCustomerIdentityProviderAdminForm,
EnterpriseCustomerReportingConfigAdminForm,
EnterpriseFeatureUserRoleAssignmentForm,
Expand Down Expand Up @@ -94,6 +95,7 @@ class EnterpriseCustomerCatalogInline(admin.TabularInline):
"""

model = EnterpriseCustomerCatalog
form = EnterpriseCustomerCatalogAdminForm
extra = 0
can_delete = False

Expand Down Expand Up @@ -160,6 +162,18 @@ class EnterpriseCustomerAdmin(DjangoObjectActions, SimpleHistoryAdmin):
class Meta(object):
model = EnterpriseCustomer

def change_view(self, request, object_id, form_url='', extra_context=None):
preview_content_filter = EnterpriseCustomerCatalogAdminForm.get_clicked_preview_content_filter(request.POST)
if preview_content_filter:
discovery_url = discovery_query_url(preview_content_filter, html_format=False)
return HttpResponseRedirect(discovery_url)
return super(EnterpriseCustomerAdmin, self).change_view(
request,
object_id,
form_url,
extra_context=extra_context
)

def get_form(self, request, obj=None, **kwargs):
"""
Retrieve the appropriate form to use, saving the request user
Expand Down
41 changes: 41 additions & 0 deletions enterprise/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""
from __future__ import absolute_import, unicode_literals

import json
import re
from logging import getLogger

from edx_rbac.admin.forms import UserRoleAssignmentAdminForm
Expand All @@ -27,6 +29,7 @@
split_usernames_and_emails,
validate_email_to_link,
)
from enterprise.admin.widgets import SubmitInput
from enterprise.api_client.discovery import CourseCatalogApiClient
from enterprise.api_client.lms import EnrollmentApiClient
from enterprise.models import (
Expand Down Expand Up @@ -336,6 +339,44 @@ class Meta:
)


class EnterpriseCustomerCatalogAdminForm(forms.ModelForm):
"""
form for EnterpriseCustomerCatalogAdmin class.
"""
class Meta:
model = EnterpriseCustomerCatalog
fields = "__all__"

preview_button = forms.Field(required=False, label='Actions', widget=SubmitInput(attrs={'value': _('Preview')}),
help_text=_("Hold Ctrl when clicking on button to open Preview in new tab"))

@staticmethod
def get_enterprise_customer_catalog_preview_button(post_data): # pylint: disable=invalid-name
"""
Return name of the preview button clicked by user from POST data.
e.g: 'enterprise_customer_catalogs-0-preview_button'
"""
catalog_preview_button = re.compile(r'enterprise_customer_catalogs-\d+-preview_button')
for key, _ in post_data.items():
if catalog_preview_button.match(key):
return key

@classmethod
def get_clicked_preview_content_filter(cls, post_data):
"""
Return content_filter for the EnterpriseCustomerCatalog against preview button clicked.
"""
catalog_preview_button = cls.get_enterprise_customer_catalog_preview_button(post_data)
if not catalog_preview_button:
return
content_filter_key = catalog_preview_button.replace('preview_button', 'content_filter')
content_filter = post_data.get(content_filter_key)
if not content_filter:
return
return json.loads(content_filter)


class EnterpriseCustomerIdentityProviderAdminForm(forms.ModelForm):
"""
Alternate form for the EnterpriseCustomerIdentityProvider admin page.
Expand Down
15 changes: 15 additions & 0 deletions enterprise/admin/widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
"""
Widgets to be used in the enterprise djangoapp.
"""
from __future__ import absolute_import, unicode_literals

from django.forms.widgets import Input


class SubmitInput(Input):
"""
Widget for input type field
"""
input_type = 'submit'
template_name = 'django/forms/widgets/text.html'
12 changes: 7 additions & 5 deletions enterprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ def get_enterprise_worker_user():
return None


def discovery_query_url(content_filter):
def discovery_query_url(content_filter, html_format=True):
"""
Return discovery url for preview.
"""
Expand All @@ -1005,7 +1005,9 @@ def discovery_query_url(content_filter):
search_all_endpoint='search/all/',
query_string=urlencode(content_filter, doseq=True)
)
return format_html(
'<a href="{url}" target="_blank">Preview</a>',
url=disc_url
)
if html_format:
return format_html(
'<a href="{url}" target="_blank">Preview</a>',
url=disc_url
)
return disc_url
67 changes: 67 additions & 0 deletions tests/test_admin/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from __future__ import absolute_import, unicode_literals

import json
import unittest

import ddt
Expand All @@ -15,6 +16,7 @@
from django.core.files import File

from enterprise.admin.forms import (
EnterpriseCustomerCatalogAdminForm,
EnterpriseCustomerIdentityProviderAdminForm,
EnterpriseCustomerReportingConfigAdminForm,
ManageLearnersForm,
Expand Down Expand Up @@ -635,3 +637,68 @@ def test_form_catalogs_mixed_entcustomer(self):
data=self.form_data,
)
assert not form.is_valid()


@ddt.ddt
class EnterpriseCustomerCatalogAdminFormTest(unittest.TestCase):
"""
Tests Different type of utilities methods.
"""
dummy_content_filter_data = {
'field': 'value'
}
form = EnterpriseCustomerCatalogAdminForm
@ddt.unpack
@ddt.data(
(
{
'enterprise_customer_catalogs-1-preview_button': 'Preview',
'enterprise_customer_catalogs-0-content_filter': json.dumps({'field_0': 'value_0'}),
'enterprise_customer_catalogs-1-content_filter': json.dumps(dummy_content_filter_data),
'enterprise_customer_catalogs-2-content_filter': json.dumps({'field_2': 'value_2'}),
},
'enterprise_customer_catalogs-1-preview_button'
),
(
{
'enterprise_customer_catalogs-0-content_filter': json.dumps({'field_0': 'value_0'}),
'enterprise_customer_catalogs-1-content_filter': json.dumps(dummy_content_filter_data),
'enterprise_customer_catalogs-2-content_filter': json.dumps({'field_2': 'value_2'}),
},
None
),
)
def test_get_enterprise_customer_catalog_preview_button(self, post_data, catalog_preview_button):
assert self.form.get_enterprise_customer_catalog_preview_button(post_data) == catalog_preview_button

@ddt.unpack
@ddt.data(
(
{
'enterprise_customer_catalogs-1-preview_button': 'Preview',
'enterprise_customer_catalogs-0-content_filter': json.dumps({'field_0': 'value_0'}),
'enterprise_customer_catalogs-1-content_filter': json.dumps(dummy_content_filter_data),
'enterprise_customer_catalogs-2-content_filter': json.dumps({'field_2': 'value_2'}),
},
dummy_content_filter_data
),
# not clicked catalog_preview_button
(
{
'enterprise_customer_catalogs-0-content_filter': json.dumps({'field_0': 'value_0'}),
'enterprise_customer_catalogs-1-content_filter': json.dumps(dummy_content_filter_data),
'enterprise_customer_catalogs-2-content_filter': json.dumps({'field_2': 'value_2'}),
},
None
),
# missing content filter
(
{
'enterprise_customer_catalogs-1-preview_button': 'Preview',
},
None
)
)
def test_get_preview_content_filter(self, post_data, content_filter):
assert self.form.get_clicked_preview_content_filter(post_data) == content_filter
2 changes: 1 addition & 1 deletion tests/test_enterprise/views/test_router_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setUp(self):
'enterprise_course_run_enrollment_page',
args=[self.enterprise_customer.uuid, self.course_run_id]
))
self.request.user.id = 1 # pylint: disable=invalid-name
self.request.user.id = 1 # pylint: disable=invalid-name
self.kwargs = {
'enterprise_uuid': str(self.enterprise_customer.uuid),
'course_id': self.course_run_id,
Expand Down

0 comments on commit cd96a36

Please sign in to comment.