From aa8cd29e4dd36815277380c1c1e7b15397ab8b2f Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 2 Mar 2019 15:36:21 +0100 Subject: [PATCH] Add some context processors for site variables and git version --- Pipfile | 1 + Pipfile.lock | 24 +++++++++++++++++++++++- friends_map/settings.py | 20 ++++++++++++++++++++ map/context_processors.py | 32 ++++++++++++++++++++++++++++++++ map/templates/map/base.html | 2 +- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 map/context_processors.py diff --git a/Pipfile b/Pipfile index 8a20dc9..d57d909 100644 --- a/Pipfile +++ b/Pipfile @@ -5,6 +5,7 @@ name = "pypi" [packages] django = "*" +gitpython = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index fd03c48..5a79fdd 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "627ef89f247ecee27e9ef0dabe116108d09c47abf171c900a8817befa64f9dd2" + "sha256": "8f5375aad28234aa513d8d0b3921e77eded8dcb11a81afcebae00a618d4aec94" }, "pipfile-spec": 6, "requires": { @@ -24,12 +24,34 @@ "index": "pypi", "version": "==2.1.7" }, + "gitdb2": { + "hashes": [ + "sha256:83361131a1836661a155172932a13c08bda2db3674e4caa32368aa6eb02f38c2", + "sha256:e3a0141c5f2a3f635c7209d56c496ebe1ad35da82fe4d3ec4aaa36278d70648a" + ], + "version": "==2.0.5" + }, + "gitpython": { + "hashes": [ + "sha256:563221e5a44369c6b79172f455584c9ebbb122a13368cc82cb4b5addff788f82", + "sha256:8237dc5bfd6f1366abeee5624111b9d6879393d84745a507de0fda86043b65a8" + ], + "index": "pypi", + "version": "==2.1.11" + }, "pytz": { "hashes": [ "sha256:32b0891edff07e28efe91284ed9c31e123d84bea3fd98e1f72be2508f43ef8d9", "sha256:d5f05e487007e29e03409f9398d074e158d920d36eb82eaf66fb1136b0c5374c" ], "version": "==2018.9" + }, + "smmap2": { + "hashes": [ + "sha256:0555a7bf4df71d1ef4218e4807bbf9b201f910174e6e08af2e138d4e517b4dde", + "sha256:29a9ffa0497e7f2be94ca0ed1ca1aa3cd4cf25a1f6b4f5f87f74b46ed91d609a" + ], + "version": "==2.0.5" } }, "develop": {} diff --git a/friends_map/settings.py b/friends_map/settings.py index 61a2185..8344d2c 100644 --- a/friends_map/settings.py +++ b/friends_map/settings.py @@ -64,6 +64,8 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'map.context_processors.app_settings', + 'map.context_processors.git_version', ], }, }, @@ -123,3 +125,21 @@ STATIC_URL = '/static/' AUTH_USER_MODEL = 'map.Friend' +APP = { + 'site': { + 'name': "FriendsMap", + 'url': os.environ.get('SITE_URL', "http://127.0.0.1:8000"), + 'repository': { + 'url': "https://github.com/Crocmagnon/friends_map", + 'bugtracker': "https://github.com/Crocmagnon/friends_map/issues", + }, + 'licences': { + 'source': { + 'code': "GPL v3", + 'url_license': "http://www.gnu.org/licenses/gpl-3.0.html", + } + }, + }, +} + + diff --git a/map/context_processors.py b/map/context_processors.py new file mode 100644 index 0000000..7e1745c --- /dev/null +++ b/map/context_processors.py @@ -0,0 +1,32 @@ +from git import Repo + +from django.conf import settings + + +def get_git_version(): + """ + Get the git version of the site. + """ + try: + repo = Repo(settings.BASE_DIR) + commit = repo.head.commit.hexsha + name = commit[:7] + return {'name': name, 'url': u'{}/commit/{}'.format(settings.APP['site']['repository']['url'], commit)} + except (KeyError, TypeError): + return {'name': '', 'url': ''} + + +def git_version(request): + """ + A context processor to include the git version on all pages. + """ + return {'git_version': get_git_version()} + + +def app_settings(request): + """ + A context processor with all APP settings. + """ + return { + 'app': settings.APP, + } diff --git a/map/templates/map/base.html b/map/templates/map/base.html index d91ff41..81e58da 100644 --- a/map/templates/map/base.html +++ b/map/templates/map/base.html @@ -19,7 +19,7 @@ {% block add-head %} {% endblock %} - FriendsMap · {% block title %}Home{% endblock %} + {{ app.site.name }} · {% block title %}Home{% endblock %} {#{% include 'map/navbar.html' %}#}