diff options
author | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
---|---|---|
committer | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
commit | bb3973ffb714c932e9ec6dd6a751228dc71fe1d3 (patch) | |
tree | 6fa32f9392ad2ec32271349b86a4c1388fd6ba95 /app/utils/views.py |
initial commit
Diffstat (limited to 'app/utils/views.py')
-rw-r--r-- | app/utils/views.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/app/utils/views.py b/app/utils/views.py new file mode 100644 index 0000000..6b69b25 --- /dev/null +++ b/app/utils/views.py @@ -0,0 +1,105 @@ +from itertools import chain +import json +from django.urls import reverse +from django.http import Http404, HttpResponse +from django.views.generic import ListView, DetailView +from django.apps import apps +from django.shortcuts import render +from django.template import RequestContext + +from media.models import LuxImage, LuxVideo, LuxAudio + + +BREADCRUMBS = { + 'SrcPost':'SRC', + 'Book':'Book Notes', + 'Entry':'Jrnl', + 'NewsletterMailing':'lttr', + 'LuxImage':'lttr' +} + +class PaginatedListView(ListView): + """ + handles my own pagination system and breadcrumbs + """ + context_object_name = 'object_list' + + def dispatch(self, request, *args, **kwargs): + path = request.path.split('/')[1:-1] + if path[-1] == self.kwargs['page']: + path = "/".join(t for t in path[:-1]) + request.page_url = "/" + path + '/%d/' + else: + request.page_url = request.path + '%d/' + request.page = int(self.kwargs['page']) + 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) + print('model=', self.model) + 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.slugify()) + except: + # special case for pages: + context['breadcrumbs'] = (self.object.title,) + context['crumb_url'] = None + return context + + +def insert_image(request): + """ + The view that handles the admin insert image/video feature + """ + images = LuxImage.objects.all()[:80] + videos = LuxVideo.objects.all()[:10] + audio = LuxAudio.objects.all()[:40] + object_list = sorted( + chain(images, videos, audio), + key=lambda instance: instance.pub_date, + reverse=True + ) + return render(request, 'admin/insert_images.html', {'object_list': object_list, 'textarea_id': request.GET['textarea']}) + + +def nav_json(request, app, model, pk): + model = apps.get_model(app_label=app, model_name=model) + p = model.objects.get(pk=pk) + data = {} + data['prev'] = p.get_previous_admin_url + data['next'] = p.get_next_admin_url + data = json.dumps(data) + return HttpResponse(data) |