Repository: ddionrails/ddionrails

Issue:
Move context from ddionrails.studies.models into own module
### Subject of the issue

The function context() in ddionrails.studies.models is used as a context processor. It should not live in the models.py module.
The context() function is called in every template.

https://github.com/ddionrails/ddionrails/blob/b0d1c6062670fd51ba16a5eb2b77621e0ad982d2/ddionrails/studies/models.py#L138-L139


Discussion:

Resolving patch:
diff --git a/config/settings/base.py b/config/settings/base.py
--- a/config/settings/base.py
+++ b/config/settings/base.py
@@ -104,7 +104,7 @@
                 "django.template.context_processors.request",
                 "django.contrib.auth.context_processors.auth",
                 "django.contrib.messages.context_processors.messages",
-                "ddionrails.studies.models.context",
+                "ddionrails.studies.context_processors.studies_processor",
             ]
         },
     }
diff --git a/ddionrails/studies/context_processors.py b/ddionrails/studies/context_processors.py
new file mode 100644
--- /dev/null
+++ b/ddionrails/studies/context_processors.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+
+""" Context Processors for ddionrails.studies app """
+
+from typing import Dict
+
+from django.core.handlers.wsgi import WSGIRequest
+from django.db.models.query import QuerySet
+
+from .models import Study
+
+
+# request is a required parameter
+def studies_processor(
+    request: WSGIRequest
+) -> Dict[str, QuerySet]:  # pylint: disable=unused-argument
+    """ Context processor returns all studies in a dictionary """
+
+    return dict(studies=Study.objects.all().only("name", "label"))
diff --git a/ddionrails/studies/models.py b/ddionrails/studies/models.py
--- a/ddionrails/studies/models.py
+++ b/ddionrails/studies/models.py
@@ -133,7 +133,3 @@ def get_topiclist(self, language: str = "en") -> Optional[List]:
                     return topiclist.get("topics")
         except TopicList.DoesNotExist:
             return None
-
-
-def context(request):
-    return dict(all_studies=Study.objects.all().only("name", "label", "description"))


Test patch:
diff --git a/tests/studies/test_context_processors.py b/tests/studies/test_context_processors.py
new file mode 100644
--- /dev/null
+++ b/tests/studies/test_context_processors.py
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+# pylint: disable=missing-docstring,no-self-use,too-few-public-methods
+
+""" Test cases for ddionrails.studies.context_processors """
+
+import pytest
+
+from ddionrails.studies.context_processors import studies_processor
+from ddionrails.studies.models import Study
+
+pytestmark = [pytest.mark.studies]  # pylint: disable=invalid-name
+
+
+def test_studies_processor_with_study(study, rf):  # pylint: disable=invalid-name
+    some_request = rf.get("/")
+    response = studies_processor(some_request)
+    queryset = Study.objects.all()
+    expected = dict(studies=queryset)
+    assert list(expected["studies"]) == list(response["studies"])
+
+
+@pytest.mark.django_db
+def test_studies_processor_without_study(rf):  # pylint: disable=invalid-name
+    some_request = rf.get("/")
+    response = studies_processor(some_request)
+    empty_queryset = Study.objects.none()
+    expected = dict(studies=empty_queryset)
+    assert list(expected["studies"]) == list(response["studies"])
diff --git a/tests/studies/test_models.py b/tests/studies/test_models.py
--- a/tests/studies/test_models.py
+++ b/tests/studies/test_models.py
@@ -7,7 +7,7 @@
 
 import pytest
 
-from ddionrails.studies.models import Study, TopicList, context
+from ddionrails.studies.models import TopicList
 
 pytestmark = [pytest.mark.studies, pytest.mark.models]  # pylint: disable=invalid-name
 
@@ -82,17 +82,3 @@ def test_get_topiclist_method_without_topic_list(
         result = study.get_topiclist()
         expected = None
         assert expected is result
-
-
-def test_context_function_with_study(study, rf):
-    some_request = rf.get("/")
-    response = context(some_request)
-    queryset = Study.objects.filter(id=study.id)
-    assert str(response) == str({"all_studies": queryset})
-
-
-def test_context_function_without_study(rf, db):
-    some_request = rf.get("/")
-    response = context(some_request)
-    empty_queryset = Study.objects.none()
-    assert str(response) == str({"all_studies": empty_queryset})