help_context_processor.py 4.99 KB
Newer Older
1 2 3 4 5 6 7 8
"""
Common functionality for Django Template Context Processor for
Online Contextual Help.
"""

import ConfigParser
import logging

9 10 11
from django.conf import settings

from openedx.core.release import doc_version
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

log = logging.getLogger(__name__)


def common_doc_url(request, config_file_object):  # pylint: disable=unused-argument
    """
    This function is added in the list of TEMPLATES 'context_processors' OPTION, which is a django setting for
    a tuple of callables that take a request object as their argument and return a dictionary of items
    to be merged into the RequestContext.

    This function returns a dict with get_online_help_info, making it directly available to all mako templates.

    Args:
        request: Currently not used, but is passed by django to context processors.
            May be used in the future for determining the language of choice.
        config_file_object: Configuration file object.
    """

    def get_online_help_info(page_token=None):
        """
        Args:
            page_token: A string that identifies the page for which the help information is requested.
                It should correspond to an option in the docs/config_file_object.ini file.  If it doesn't, the "default"
                option is used instead.

        Returns:
            A dict mapping the following items
                * "doc_url" - a string with the url corresponding to the online help location for the given page_token.
                * "pdf_url" - a string with the url corresponding to the location of the PDF help file.
        """

        def get_config_value_with_default(section_name, option, default_option="default"):
            """
            Args:
                section_name: name of the section in the configuration from which the option should be found
                option: name of the configuration option
                default_option: name of the default configuration option whose value should be returned if the
                    requested option is not found
            """
            if option:
                try:
                    return config_file_object.get(section_name, option)
                except (ConfigParser.NoOptionError, AttributeError):
                    log.debug("Didn't find a configuration option for '%s' section and '%s' option",
                              section_name, option)
            return config_file_object.get(section_name, default_option)

        def get_doc_url():
            """
            Returns:
                The URL for the documentation
            """
64 65 66 67 68 69 70 71 72 73 74 75 76 77

            # Read an optional configuration property that sets the base
            # URL of documentation links. By default, DOC_LINK_BASE_URL
            # is null, this test determines whether it is set to a non-null
            # value. If it is set, this funtion will use its string value
            # as the base of documentation link URLs. If it is not set, the
            # function reads the base of the documentation link URLs from
            # the .ini configuration file, lms_config.ini or cms_config.ini.
            if settings.DOC_LINK_BASE_URL:
                doc_base_url = settings.DOC_LINK_BASE_URL
            else:
                doc_base_url = config_file_object.get("help_settings", "url_base")

            # Construct and return the URL for the documentation link.
78
            return "{url_base}/{language}/{version}/{page_path}".format(
79
                url_base=doc_base_url,
80
                language=get_config_value_with_default("locales", settings.LANGUAGE_CODE),
81
                version=doc_version(),
82 83 84 85 86 87
                page_path=get_config_value_with_default("pages", page_token),
            )

        def get_pdf_url():
            """
            Returns:
88 89
                The URL for the PDF document using the pdf_settings and the
                help_settings (version) in the configuration
90
            """
91 92 93 94 95 96 97 98 99 100 101 102 103 104

            # Read an optional configuration property that sets the base
            # URL of pdf links. By default, DOC_LINK_BASE_URL
            # is null, this test determines whether it is set to a non-null
            # value. If it is set, this funtion will use its string value
            # as the base of documentation link URLs. If it is not set, the
            # function reads the base of the documentation link URLs from
            # the .ini configuration file, lms_config.ini or cms_config.ini.
            if settings.DOC_LINK_BASE_URL:
                pdf_base_url = settings.DOC_LINK_BASE_URL
            else:
                pdf_base_url = config_file_object.get("pdf_settings", "pdf_base")

            # Construct and return the URL for the PDF link.
105
            return "{pdf_base}/{version}/{pdf_file}".format(
106
                pdf_base=pdf_base_url,
107
                version=doc_version(),
108 109 110 111 112 113 114 115 116
                pdf_file=config_file_object.get("pdf_settings", "pdf_file"),
            )

        return {
            "doc_url": get_doc_url(),
            "pdf_url": get_pdf_url(),
        }

    return {'get_online_help_info': get_online_help_info}