From e9d84d0ec114a73ac1dbe6b6544fb7f8f0f4bbeb Mon Sep 17 00:00:00 2001 From: Asespinel <79876430+Asespinel@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:19:51 -0500 Subject: [PATCH] feat: added banner message for survey report (#33633) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: added banner message for survey report * refactor: addressed PR feedback and changes * fix: fixed styles on admin templates * refactor: changed script location to survey report block * chore: removed whitespaces and renamed the context processor files * feat: added banner message for survey report * refactor: separated survey report template from admin and deleted base template * refactor: changed months variable into a configurable setting --------- Co-authored-by: Maria Fernanda Magallanes Zubillaga Co-authored-by: MarĂ­a Fernanda Magallanes <35668326+MaferMazu@users.noreply.github.com> --- lms/envs/common.py | 7 +- lms/envs/production.py | 2 + lms/envs/test.py | 1 + lms/templates/admin/base_site.html | 4 + openedx/features/survey_report/api.py | 1 + .../survey_report/context_processors.py | 34 +++++++++ .../commands/tests/test_generate_report.py | 4 +- .../templates/survey_report/admin_banner.html | 74 +++++++++++++++++++ 8 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 openedx/features/survey_report/context_processors.py create mode 100644 openedx/features/survey_report/templates/survey_report/admin_banner.html diff --git a/lms/envs/common.py b/lms/envs/common.py index a481acd33423..ed8d520660c9 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -1342,7 +1342,12 @@ def _make_mako_template_dirs(settings): 'openedx.core.djangoapps.site_configuration.context_processors.configuration_context', # Mobile App processor (Detects if request is from the mobile app) - 'lms.djangoapps.mobile_api.context_processor.is_from_mobile_app' + 'lms.djangoapps.mobile_api.context_processor.is_from_mobile_app', + + # Context processor necesarry for the survey report message appear on the admin site + 'openedx.features.survey_report.context_processors.admin_extra_context' + + ] # Django templating diff --git a/lms/envs/production.py b/lms/envs/production.py index 45accf0c4ae1..72329b5a59f5 100644 --- a/lms/envs/production.py +++ b/lms/envs/production.py @@ -1131,6 +1131,8 @@ def get_env_setting(setting): 'https://hooks.zapier.com/hooks/catch/11595998/3ouwv7m/') ANONYMOUS_SURVEY_REPORT = False +SURVEY_REPORT_CHECK_THRESHOLD = ENV_TOKENS.get('SURVEY_REPORT_CHECK_THRESHOLD', 6) + AVAILABLE_DISCUSSION_TOURS = ENV_TOKENS.get('AVAILABLE_DISCUSSION_TOURS', []) ############## NOTIFICATIONS EXPIRY ############## diff --git a/lms/envs/test.py b/lms/envs/test.py index 39996e857ac5..6d87a05848a1 100644 --- a/lms/envs/test.py +++ b/lms/envs/test.py @@ -663,6 +663,7 @@ ############## Settings for survey report ############## SURVEY_REPORT_EXTRA_DATA = {} SURVEY_REPORT_ENDPOINT = "https://example.com/survey_report" +SURVEY_REPORT_CHECK_THRESHOLD = 6 ANONYMOUS_SURVEY_REPORT = False ######################## Subscriptions API SETTINGS ######################## diff --git a/lms/templates/admin/base_site.html b/lms/templates/admin/base_site.html index 4ea86307696e..fa93f9c3e84a 100644 --- a/lms/templates/admin/base_site.html +++ b/lms/templates/admin/base_site.html @@ -20,3 +20,7 @@

{{ site_header|default:_('D {% endblock %} + +{% block header %}{{ block.super }} + {% include "survey_report/admin_banner.html" %} +{% endblock %} \ No newline at end of file diff --git a/openedx/features/survey_report/api.py b/openedx/features/survey_report/api.py index bce26b19b0b8..bb46959337c0 100644 --- a/openedx/features/survey_report/api.py +++ b/openedx/features/survey_report/api.py @@ -53,6 +53,7 @@ def generate_report() -> None: data = get_report_data() data["state"] = SURVEY_REPORT_GENERATED update_report(survey_report.id, data) + send_report_to_external_api(survey_report.id) except (Exception, ) as update_report_error: update_report(survey_report.id, {"state": SURVEY_REPORT_ERROR}) raise Exception(update_report_error) from update_report_error diff --git a/openedx/features/survey_report/context_processors.py b/openedx/features/survey_report/context_processors.py new file mode 100644 index 000000000000..967291d969a8 --- /dev/null +++ b/openedx/features/survey_report/context_processors.py @@ -0,0 +1,34 @@ +""" +This is the survey report contex_processor modules + +This is meant to determine the visibility of the survey report banner +across all admin pages in case a survey report has not been generated + +""" + +from datetime import datetime +from dateutil.relativedelta import relativedelta # for months test +from .models import SurveyReport +from django.urls import reverse +from django.conf import settings + + +def admin_extra_context(request): + """ + This function sends extra context to every admin site + + The current treshhold to show the banner is one month but this can be redefined in the future + + """ + months = settings.SURVEY_REPORT_CHECK_THRESHOLD + if not request.path.startswith(reverse('admin:index')): + return {'show_survey_report_banner': False, } + + try: + latest_report = SurveyReport.objects.latest('created_at') + months_treshhold = datetime.today().date() - relativedelta(months=months) # Calculate date one month ago + show_survey_report_banner = latest_report.created_at.date() <= months_treshhold + except SurveyReport.DoesNotExist: + show_survey_report_banner = True + + return {'show_survey_report_banner': show_survey_report_banner, } diff --git a/openedx/features/survey_report/management/commands/tests/test_generate_report.py b/openedx/features/survey_report/management/commands/tests/test_generate_report.py index 74204981d520..a1afddabe3f3 100644 --- a/openedx/features/survey_report/management/commands/tests/test_generate_report.py +++ b/openedx/features/survey_report/management/commands/tests/test_generate_report.py @@ -16,8 +16,9 @@ class GenerateReportTest(TestCase): Test for generate_report command. """ + @mock.patch('openedx.features.survey_report.api.send_report_to_external_api') @mock.patch('openedx.features.survey_report.api.get_report_data') - def test_generate_report(self, mock_get_report_data): + def test_generate_report(self, mock_get_report_data, mock_send_report): """ Test that generate_report command creates a survey report. """ @@ -30,6 +31,7 @@ def test_generate_report(self, mock_get_report_data): 'extra_data': {'extra': 'data'}, } mock_get_report_data.return_value = report_test_data + mock_send_report.return_value = None out = StringIO() call_command('generate_report', no_send=True, stdout=out) diff --git a/openedx/features/survey_report/templates/survey_report/admin_banner.html b/openedx/features/survey_report/templates/survey_report/admin_banner.html new file mode 100644 index 000000000000..6a8e2ea92e8c --- /dev/null +++ b/openedx/features/survey_report/templates/survey_report/admin_banner.html @@ -0,0 +1,74 @@ +{% block survey_report_banner %} +{% if show_survey_report_banner %} + + +{% endif %} + + + + +{% endblock %}