From 6e8b62d8c38f6ca366f240ea45ad738ebfd62943 Mon Sep 17 00:00:00 2001 From: luxagraf Date: Mon, 27 Jan 2020 20:37:10 -0500 Subject: abstracted breadcrumbs into seperate template --- app/utils/views.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'app/utils/views.py') 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): """ -- cgit v1.2.3-70-g09d2