summaryrefslogtreecommitdiff
path: root/app/utils/views.py
blob: b337cc56d950c2427476d1a1c527be0249067ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 django.template.defaultfilters import slugify 

from media.models import LuxImage, LuxVideo, LuxAudio
from recordings.models import Audio


BREADCRUMBS = {
    'AP':'dialogue',
    'Book':'Book Notes',
    'NewsletterMailing':'lttr',
    'LuxImage':'lttr',
    'Sighting':'dialogue'
}

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)

        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.label.split(".")[1]],)
        except KeyError:
            if self.object._meta.verbose_name_plural == 'posts':
                if self.object.get_post_type_display() != 'src':
                    context['breadcrumbs'] = (self.object.get_post_type_display()+"s",)
                else:
                    context['breadcrumbs'] = (self.object.get_post_type_display(),)
                context['crumb_url'] = "/%ss/" % slugify(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' % slugify(self.object._meta.verbose_name_plural))
            except:
                # special case for books:
                if self.object._meta.verbose_name_plural == 'books':
                    context['crumb_url'] = reverse('books:list')
                elif self.object._meta.verbose_name_plural == 'Animal/Plant':
                    context['crumb_url'] = reverse('sightings:list')
                else:
                    # 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)