665b5b2087bf0262d81821d9a6b2e746e3d0f385
[blender-dev-fund.git] / blender_fund_main / context_processors.py
1 """
2 Makes 'settings' available as template variable.
3 """
4
5 import django.conf
6 from django.http import HttpRequest
7 from django.urls import ResolverMatch
8
9
10 def settings(request):
11     return {'settings': django.conf.settings}
12
13
14 def page_id(request: HttpRequest) -> dict:
15     """Inject a 'page id' that's obtained from the view function or view name.
16
17     When using class-based views, set the `page_id` attribute on the class.
18     When using function-based views, set the `page_id` attribute on the function.
19     """
20     resolver_match: ResolverMatch = request.resolver_match
21     view_func = resolver_match.func
22
23     try:
24         # For class-based views, view_func is what View.as_view() returned.
25         view_page_id = view_func.view_class.page_id
26     except AttributeError:
27         try:
28             # For function-based views, view_func is the function itself.
29             view_page_id = view_func.page_id
30         except AttributeError:
31             # If there is no page_id, we reuse the view name.
32             view_page_id = resolver_match.view_name.replace(':', '-')
33
34     return {'page_id': view_page_id}