From d432e6ac69e02e69d0050b338e56c2896b69709f Mon Sep 17 00:00:00 2001
From: Tim Fechner <tim.fechner@hs-hannover.de>
Date: Wed, 22 Nov 2017 13:49:08 +0100
Subject: [PATCH] Add breadcrumb utilities

To learn more about using breadcrumbs, read the WIKI:
https://lab.it.hs-hannover.de/django/hshassets/wikis/breadcrumbs
---
 README.md                                     | 19 +++++++++
 hshassets/app_settings/defaults.py            |  5 ++-
 hshassets/context_processors.py               |  9 +++++
 .../hshassets/includes/breadcrumbs.html       | 40 +++++++++++++++++++
 hshassets/views.py                            | 25 ++++++++++++
 setup.py                                      |  2 +-
 6 files changed, 98 insertions(+), 2 deletions(-)
 create mode 100644 hshassets/context_processors.py
 create mode 100644 hshassets/templates/hshassets/includes/breadcrumbs.html
 create mode 100644 hshassets/views.py

diff --git a/README.md b/README.md
index f2152571..025d05ec 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,25 @@ The current version of this package ships the following own stuff and awesome th
 Simply put this app into your projects `requirements.txt` file and run `pip install -r requirements.txt`. Once you
 installed it, you should put the name `hshcdn` into your projects `INSTALLED_APPS` setting. Tadaa!
 
+Also if you want to use breadcrubs (you likely do), you need to add the settings context processor to your `TEMPLATES` setting:
+
+```python
+TEMPLATES = [
+    {
+        'BACKEND': 'django.template.backends.django.DjangoTemplates',
+        # [...]
+        'OPTIONS': {
+            'context_processors': [
+                # [...]
+                'hshassets.context_processors.settings'  # <-- add this line
+            ],
+        },
+    },
+]
+```
+
+**More information about using breadcrumbs [can be found in the WIKI](https://lab.it.hs-hannover.de/django/hshassets/wikis/breadcrumbs)**
+
 ## Usage
 
 Simply create an `assets` folder in your app(s) directory. It can contain the folders `fonts` for custom fonts, `img`
diff --git a/hshassets/app_settings/defaults.py b/hshassets/app_settings/defaults.py
index 6144f9d6..d3e041a1 100644
--- a/hshassets/app_settings/defaults.py
+++ b/hshassets/app_settings/defaults.py
@@ -24,4 +24,7 @@ COLOR_SCHEME = 'service'
 # read utils.get_scss_include_paths() for further information
 SCSS_INCLUDE_PATHS = [
     ('hshassets', ['sass/lib/']),
-]
\ No newline at end of file
+]
+
+# URI to the live service.it portal
+SERVICE_IT_URI = 'https://service.it.hs-hannover.de/'
diff --git a/hshassets/context_processors.py b/hshassets/context_processors.py
new file mode 100644
index 00000000..9274bbef
--- /dev/null
+++ b/hshassets/context_processors.py
@@ -0,0 +1,9 @@
+"""
+hshassets brings an own middleware to make some settings variables available in templates
+"""
+
+from hshassets.app_settings import SERVICE_IT_URI
+
+
+def settings(request):
+    return {'service_it_uri': SERVICE_IT_URI}
diff --git a/hshassets/templates/hshassets/includes/breadcrumbs.html b/hshassets/templates/hshassets/includes/breadcrumbs.html
new file mode 100644
index 00000000..ef1f659f
--- /dev/null
+++ b/hshassets/templates/hshassets/includes/breadcrumbs.html
@@ -0,0 +1,40 @@
+{% load i18n %}
+
+{% comment %}
+    This template provides an breadcrumb bar for the hero / hero_keyvisual template. Use it like this:
+
+        {% block pre-section %}
+            {% include "hshassets/includes/breadcrumbs.html" %}
+        {% endblock %}
+
+    This snippet will render a breadcrumb bar under the <div class="hero"> element. To use it, you need to edit your
+    views that they inherit from "hshassets.views.BreadCrumbMixin". Also provide the attributes "breadcrumb_name" and
+    "breadcrumb_parent" (only if you have a parent, otherwise the view will be the root element). Example:
+
+        class ExploreView(ListView, BreadCrumbMixin):
+            template_name = 'myapp/explore.html'
+
+            breadcrumb_name = _('Explore')
+            breadcrumb_parent = 'about'
+
+    Please note that this template should be used via {% include "" %}, not {% extends "" %}!
+    This way you can specify your own navigation or hero text or general content stuff.
+{% endcomment %}
+
+<div class="breadcrumb-wrapper">
+    <div class="container">
+        <nav class="breadcrumb" aria-label="breadcrumbs">
+            <ul>
+
+                <li><a href="{{ service_it_uri }}"><span class="icon"><i class="fa fa-home"></i></span> IT Services</a></li>
+
+                {% for breadcrumb in breadcrumb_path %}
+                    <li {% if forloop.last %}class="is-active"{% endif %}>
+                        <a href="{{ breadcrumb.url }}">{{ breadcrumb.name }}</a>
+                    </li>
+                {% endfor %}
+
+            </ul>
+        </nav>
+    </div>
+</div>
diff --git a/hshassets/views.py b/hshassets/views.py
new file mode 100644
index 00000000..bf350aad
--- /dev/null
+++ b/hshassets/views.py
@@ -0,0 +1,25 @@
+from django.urls import reverse, resolve
+from django.views.generic.base import ContextMixin
+
+
+class BreadCrumbMixin(ContextMixin):
+
+    breadcrumb_parent = None
+    breadcrumb_name = None
+
+    def get_breadcrumb_path(self, breadcrumb_path, url_name=''):
+
+        if self.breadcrumb_parent:
+            parent_view_class = resolve(reverse(self.breadcrumb_parent)).func.view_class
+            breadcrumb_path.extend(parent_view_class().get_breadcrumb_path(breadcrumb_path, self.breadcrumb_parent))
+
+        return breadcrumb_path + [{'name': self.breadcrumb_name, 'url': reverse(url_name) if url_name else '#'}]
+
+    def get_context_data(self, *args, **kwargs):
+        assert self.breadcrumb_name, 'Missing attribute "breadcrumb_name" on {}'.format(self.__class__)
+
+        context = super().get_context_data(*args, **kwargs)
+        context.update({
+            'breadcrumb_path': self.get_breadcrumb_path([])
+        })
+        return context
diff --git a/setup.py b/setup.py
index bb9bed1b..535b6399 100644
--- a/setup.py
+++ b/setup.py
@@ -7,7 +7,7 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
 
 setup(
     name='django-hshassets',
-    version='1.2.2',
+    version='1.3.0',
     packages=find_packages(),
     include_package_data=True,
     license='MIT License',
-- 
GitLab