diff options
author | luxagraf <sng@luxagraf.net> | 2020-01-27 20:37:10 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2020-01-27 20:37:10 -0500 |
commit | 6e8b62d8c38f6ca366f240ea45ad738ebfd62943 (patch) | |
tree | b8c695cdd5c7f1f7729fa6750ed49c1eb2eae3e8 /app/utils/views.py | |
parent | 09abece4982e8dceabe1dd8d678639205a4a6208 (diff) |
abstracted breadcrumbs into seperate template
Diffstat (limited to 'app/utils/views.py')
-rw-r--r-- | app/utils/views.py | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/app/utils/views.py b/app/utils/views.py index 60cb7de..152b2b7 100644 --- a/app/utils/views.py +++ b/app/utils/views.py @@ -1,7 +1,8 @@ from itertools import chain import json +from django.urls import reverse from django.http import Http404, HttpResponse -from django.views.generic import ListView +from django.views.generic import ListView, DetailView from django.apps import apps from django.shortcuts import render_to_response from django.shortcuts import render @@ -11,9 +12,15 @@ from photos.models import LuxImage, LuxVideo from recordings.models import Audio +BREADCRUMBS = { + 'SrcPost':'SRC', + 'Book':'Book Notes', + 'Entry':'Jrnl' +} + class PaginatedListView(ListView): """ - handles my own pagination system + handles my own pagination system and breadcrumbs """ context_object_name = 'object_list' @@ -29,6 +36,49 @@ class PaginatedListView(ListView): request.base_path = path return super(PaginatedListView, self).dispatch(request, *args, **kwargs) + def get_context_data(self, **kwargs): + ''' + Adds breadcrumb path to every view + ''' + # Call the base implementation first to get a context + context = super(PaginatedListView, self).get_context_data(**kwargs) + try: + context['breadcrumbs'] = (BREADCRUMBS[self.model.__name__],) + except KeyError: + context['breadcrumbs'] = (self.model._meta.verbose_name_plural,) + return context + + +class LuxDetailView(DetailView): + """ + handles breadcrumbs for detail pages + """ + def get_context_data(self, **kwargs): + ''' + Adds breadcrumb path to every view + ''' + # Call the base implementation first to get a context + context = super(LuxDetailView, self).get_context_data(**kwargs) + print(self.object._meta.verbose_name_plural) + try: + context['breadcrumbs'] = (BREADCRUMBS[self.object._meta.model],) + except KeyError: + if self.object._meta.verbose_name_plural == 'posts': + context['breadcrumbs'] = (self.object.get_post_type_display()+"s",) + context['crumb_url'] = "/%ss/" % self.object.get_post_type_display() + else: + context['breadcrumbs'] = (self.object._meta.verbose_name_plural,) + try: + context['crumb_url'] + except KeyError: + try: + context['crumb_url'] = reverse('%s:list' % self.object._meta.verbose_name_plural) + except: + # special case for pages: + context['breadcrumbs'] = (self.object.title,) + context['crumb_url'] = None + return context + def insert_image(request): """ |