Repository: spacetelescope/jwql

Issue:
Find better way to pass context to base HTML template
Currently, our `base.html` template requires some data to be passed to it, such as:

```python
context = {'inst': '',
           'inst_list': JWST_INSTRUMENT_NAMES,
           'tools': MONITORS,
            'user': user}
```

This means the each view defined in `views.py` requires this context to be gathered and passed.  This is a bit annoying and not very DRY.  Perhaps there is a more elegant solution. 


Discussion:

Resolving patch:
diff --git a/jwql/website/apps/jwql/context_processors.py b/jwql/website/apps/jwql/context_processors.py
new file mode 100644
--- /dev/null
+++ b/jwql/website/apps/jwql/context_processors.py
@@ -0,0 +1,54 @@
+"""Provides functions that define context inherent to all views.
+
+The functions within this module define ``context`` that will be
+included in requests, in addition to any specific ``context`` provided
+in the view.
+
+Authors
+-------
+
+    - Matthew Bourque
+
+Use
+---
+
+    This module is defined under the ``TEMPLATES.OPTIONS`` setting in
+    ``settings.py``, e.g.:
+    ::
+
+        TEMPLATES = [
+            {'OPTIONS': {'context_processors': ['jwql.website.apps.jwql.context_processors.base_context'],},}
+        ]
+
+    As such, it will automatically be executed upon each request.
+"""
+
+import jwql
+from jwql.utils.constants import JWST_INSTRUMENT_NAMES, MONITORS
+
+from .oauth import auth_info
+
+
+@auth_info
+def base_context(request, user):
+    """Provide the context needed for the ``base.html`` template.
+
+    Parameters
+    ----------
+    request : HttpRequest object
+        Incoming request from the webpage
+
+    Returns
+    -------
+    context : dict
+        A dictionary containing data needed to render the ``base.html``
+        template
+    """
+
+    context = {}
+    context['inst_list'] = JWST_INSTRUMENT_NAMES
+    context['tools'] = MONITORS
+    context['user'] = user
+    context['version'] = jwql.__version__
+
+    return context
diff --git a/jwql/website/apps/jwql/views.py b/jwql/website/apps/jwql/views.py
--- a/jwql/website/apps/jwql/views.py
+++ b/jwql/website/apps/jwql/views.py
@@ -49,16 +49,13 @@
 from .data_containers import thumbnails
 from .data_containers import thumbnails_ajax
 from .forms import FileSearchForm
-from .oauth import auth_info
-from jwql.utils.constants import JWST_INSTRUMENT_NAMES, MONITORS, JWST_INSTRUMENT_NAMES_MIXEDCASE
+from jwql.utils.constants import JWST_INSTRUMENT_NAMES_MIXEDCASE
 from jwql.utils.utils import get_base_url, get_config
-import jwql
 
 FILESYSTEM_DIR = os.path.join(get_config()['jwql_dir'], 'filesystem')
 
 
-@auth_info
-def about(request, user):
+def about(request):
     """Generate the ``about`` page
 
     Parameters
@@ -74,17 +71,12 @@ def about(request, user):
     template = 'about.html'
     acknowledgements = get_acknowledgements()
     context = {'acknowledgements': acknowledgements,
-               'inst': '',
-               'inst_list': JWST_INSTRUMENT_NAMES,
-               'tools': MONITORS,
-               'user': user,
-               'version': jwql.__version__}
+               'inst': ''}
 
     return render(request, template, context)
 
 
-@auth_info
-def archived_proposals(request, user, inst):
+def archived_proposals(request, inst):
     """Generate the page listing all archived proposals in the database
 
     Parameters
@@ -104,10 +96,7 @@ def archived_proposals(request, user, inst):
 
     template = 'archive.html'
     context = {'inst': inst,
-               'tools': MONITORS,
-               'user': user,
-               'base_url': get_base_url(),
-               'version': jwql.__version__}
+               'base_url': get_base_url()}
 
     return render(request, template, context)
 
@@ -138,18 +127,15 @@ def archived_proposals_ajax(request, inst):
 
     context = {'inst': inst,
                'all_filenames': all_filenames,
-               'tools': MONITORS,
                'num_proposals': proposal_info['num_proposals'],
                'thumbnails': {'proposals': proposal_info['proposals'],
                               'thumbnail_paths': proposal_info['thumbnail_paths'],
-                              'num_files': proposal_info['num_files']},
-               'version': jwql.__version__}
+                              'num_files': proposal_info['num_files']}}
 
     return JsonResponse(context, json_dumps_params={'indent': 2})
 
 
-@auth_info
-def archive_thumbnails(request, user, inst, proposal):
+def archive_thumbnails(request, inst, proposal):
     """Generate the page listing all archived images in the database
     for a certain proposal
 
@@ -173,10 +159,7 @@ def archive_thumbnails(request, user, inst, proposal):
     template = 'thumbnails.html'
     context = {'inst': inst,
                'prop': proposal,
-               'tools': MONITORS,
-               'user': user,
-               'base_url': get_base_url(),
-               'version': jwql.__version__}
+               'base_url': get_base_url()}
 
     return render(request, template, context)
 
@@ -208,8 +191,7 @@ def archive_thumbnails_ajax(request, inst, proposal):
     return JsonResponse(data, json_dumps_params={'indent': 2})
 
 
-@auth_info
-def dashboard(request, user):
+def dashboard(request):
     """Generate the dashbaord page
 
     Parameters
@@ -227,21 +209,16 @@ def dashboard(request, user):
     dashboard_components, dashboard_html = get_dashboard_components()
 
     context = {'inst': '',
-               'inst_list': JWST_INSTRUMENT_NAMES,
-               'tools': MONITORS,
-               'user': user,
                'outputs': output_dir,
                'filesystem_html': os.path.join(output_dir, 'monitor_filesystem',
                                                'filesystem_monitor.html'),
                'dashboard_components': dashboard_components,
-               'dashboard_html': dashboard_html,
-               'version': jwql.__version__}
+               'dashboard_html': dashboard_html}
 
     return render(request, template, context)
 
 
-@auth_info
-def engineering_database(request, user):
+def engineering_database(request):
     """Generate the EDB page.
 
     Parameters
@@ -261,17 +238,12 @@ def engineering_database(request, user):
 
     template = 'engineering_database.html'
     context = {'inst': '',
-               'inst_list': JWST_INSTRUMENT_NAMES,
-               'user': user,
-               'tools': MONITORS,
-               'edb_components': edb_components,
-               'version': jwql.__version__}
+               'edb_components': edb_components}
 
     return render(request, template, context)
 
 
-@auth_info
-def home(request, user):
+def home(request):
     """Generate the home page
 
     Parameters
@@ -297,17 +269,12 @@ def home(request, user):
 
     template = 'home.html'
     context = {'inst': '',
-               'inst_list': JWST_INSTRUMENT_NAMES,
-               'tools': MONITORS,
-               'form': form,
-               'user': user,
-               'version': jwql.__version__}
+               'form': form}
 
     return render(request, template, context)
 
 
-@auth_info
-def instrument(request, user, inst):
+def instrument(request, inst):
     """Generate the instrument tool index page.
 
     Parameters
@@ -335,16 +302,12 @@ def instrument(request, user, inst):
     doc_url = url_dict[inst.lower()]
 
     context = {'inst': inst,
-               'tools': MONITORS,
-               'user': user,
-               'doc_url': doc_url,
-               'version': jwql.__version__}
+               'doc_url': doc_url}
 
     return render(request, template, context)
 
 
-@auth_info
-def unlooked_images(request, user, inst):
+def unlooked_images(request, inst):
     """Generate the page listing all unlooked images in the database
 
     Parameters
@@ -364,13 +327,11 @@ def unlooked_images(request, user, inst):
 
     template = 'thumbnails.html'
     context = thumbnails(inst)
-    context['user'] = user
 
     return render(request, template, context)
 
 
-@auth_info
-def view_header(request, user, inst, file):
+def view_header(request, inst, file):
     """Generate the header view page
 
     Parameters
@@ -394,18 +355,15 @@ def view_header(request, user, inst, file):
     header = get_header_info(file)
     file_root = '_'.join(file.split('_')[:-1])
 
-    return render(request, template,
-                  {'inst': inst,
-                   'file': file,
-                   'tools': MONITORS,
-                   'user': user,
-                   'header': header,
-                   'file_root': file_root,
-                   'version': jwql.__version__})
+    context = {'inst': inst,
+               'file': file,
+               'header': header,
+               'file_root': file_root}
+
+    return render(request, template, context)
 
 
-@auth_info
-def view_image(request, user, inst, file_root, rewrite=False):
+def view_image(request, inst, file_root, rewrite=False):
     """Generate the image view page
 
     Parameters
@@ -431,12 +389,9 @@ def view_image(request, user, inst, file_root, rewrite=False):
     image_info = get_image_info(file_root, rewrite)
     context = {'inst': inst,
                'file_root': file_root,
-               'tools': MONITORS,
-               'user': user,
                'jpg_files': image_info['all_jpegs'],
                'fits_files': image_info['all_files'],
                'suffixes': image_info['suffixes'],
-               'num_ints': image_info['num_ints'],
-               'version': jwql.__version__}
+               'num_ints': image_info['num_ints']}
 
     return render(request, template, context)
diff --git a/jwql/website/jwql_proj/settings.py b/jwql/website/jwql_proj/settings.py
--- a/jwql/website/jwql_proj/settings.py
+++ b/jwql/website/jwql_proj/settings.py
@@ -73,6 +73,7 @@
         'OPTIONS': {
             'environment': 'jwql.website.jwql_proj.jinja2.environment',
             'extensions': ['jwql.website.jwql_proj.jinja2.DjangoNow'],
+            'context_processors': ['jwql.website.apps.jwql.context_processors.base_context'],
         },
     }
 ]


Test patch: