Files
edx-platform/openedx/features/survey_report/context_processors.py
Asespinel 76330b36b0 feat: added setting to disable the survey report banner entirely (#34092)
* feat: added setting to disable the survey report banner entirely

* fix: fixed unit test with new setting

* refactor: changed conditions for better code readability

* feat: added exception to stop the report from generating if the setting is set to false

* chore: updated the readme file to include the new setting

* refactor: move survey settings to common and disable admin by setting

* docs: typos in README

Co-authored-by: Tim McCormack <tmccormack@edx.org>

* refactor: set default values to survey report settings

* refactor: rename ENABLE_SURVEY_REPORT setting to SURVEY_REPORT_ENABLE

* test: fix quality tests

---------

Co-authored-by: Alejandro Cardenas <alecar.main@gmail.com>
Co-authored-by: Tim McCormack <tmccormack@edx.org>
2024-01-30 12:29:09 -05:00

65 lines
2.2 KiB
Python

"""
This module provides context processors for integrating survey report functionality
into Django admin sites.
It includes functions for determining whether to display a survey report banner and
calculating the date threshold for displaying the banner.
Functions:
- admin_extra_context(request):
Sends extra context to every admin site, determining whether to display the
survey report banner based on defined settings and conditions.
- should_show_survey_report_banner():
Determines whether to show the survey report banner based on the threshold.
- get_months_threshold(months):
Calculates the date threshold based on the specified number of months.
Dependencies:
- Django: settings, reverse, shortcuts
- datetime: datetime
- dateutil.relativedelta: relativedelta
Usage:
This module is designed to be imported into Django projects with admin functionality.
It enhances the admin interface by providing dynamic context for displaying a survey
report banner based on defined conditions and settings.
"""
from django.conf import settings
from django.urls import reverse
from datetime import datetime
from dateutil.relativedelta import relativedelta
from .models import SurveyReport
def admin_extra_context(request):
"""
This function sends extra context to every admin site.
The current threshold to show the banner is one month but this can be redefined in the future.
"""
if not settings.SURVEY_REPORT_ENABLE or not request.path.startswith(reverse('admin:index')):
return {'show_survey_report_banner': False}
return {'show_survey_report_banner': should_show_survey_report_banner()}
def should_show_survey_report_banner():
"""
Determine whether to show the survey report banner based on the threshold.
"""
months_threshold = get_months_threshold(settings.SURVEY_REPORT_CHECK_THRESHOLD)
try:
latest_report = SurveyReport.objects.latest('created_at')
return latest_report.created_at.date() <= months_threshold
except SurveyReport.DoesNotExist:
return True
def get_months_threshold(months):
"""
Calculate the date threshold based on the specified number of months.
"""
return datetime.today().date() - relativedelta(months=months)