summaryrefslogtreecommitdiff
path: root/app/posts/views/jrnl_views.py
blob: 0dc2dc8e81a0da5994c59bc02cbffc01212fa9a0 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.views.generic.dates import DateDetailView
from django.urls import reverse
from django.views.generic.dates import YearArchiveView, MonthArchiveView
from django.contrib.syndication.views import Feed
from django.apps import apps
from django.shortcuts import get_object_or_404
from django.conf import settings
from django.db.models import Q

from utils.views import PaginatedListView

#from ..models import Entry, HomepageCurrator, Home
from ..models import Post, PostType
from locations.models import LuxCheckIn, Country, Region, Location
from sightings.models import Sighting


class JrnlListView(PaginatedListView):
    """
    Return a list of Entries in reverse chronological order
    """
    model = Post
    template_name = "posts/jrnl_list.html"
    queryset = Post.objects.filter(post_type=PostType.JRNL).filter(status__exact=1).order_by('-pub_date').prefetch_related('location').prefetch_related('featured_image')
    
    def get_context_data(self, **kwargs):
        context = super(JrnlListView, self).get_context_data(**kwargs)
        context['breadcrumbs'] = ['jrnl',]
        return context

class JrnlCountryListView(PaginatedListView):
    """
    Return a list of Entries by Country in reverse chronological order
    """
    model = Post
    template_name = "posts/jrnl_list.html"

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(JrnlCountryListView, self).get_context_data(**kwargs)
        try:
            context['region'] = Country.objects.get(slug__exact=self.kwargs['slug'])
        except:
            context['region'] = Region.objects.get(slug__exact=self.kwargs['slug'])
        context['breadcrumbs'] = ['jrnl',]
        return context

    def get_queryset(self):
        try:
            region = Country.objects.get(slug__exact=self.kwargs['slug'])
            qs = Post.objects.filter(
                post_type=PostType.JRNL,
                status__exact=1,
                location__state__country=region
            ).order_by('-pub_date')
        except:
            region = Region.objects.get(slug__exact=self.kwargs['slug'])
            qs = Post.objects.filter(
                post_type=PostType.JRNL,
                status__exact=1,
                location__state__country__lux_region=region.id
            ).order_by('-pub_date')
        return qs


class JrnlYearArchiveView(YearArchiveView):
    queryset = Post.objects.filter(status__exact=1).filter(post_type=PostType.JRNL).select_related()
    date_field = "pub_date"
    make_object_list = True
    allow_future = True
    template_name = "posts/jrnl_date.html"


class JrnlMonthArchiveView(MonthArchiveView):
    queryset = Post.objects.filter(status__exact=1).filter(post_type=PostType.JRNL).select_related()
    date_field = "pub_date"
    allow_future = True
    template_name = "posts/jrnl_date.html"


class JrnlDetailView(DateDetailView):
    model = Post
    date_field = 'pub_date'
    slug_field = "slug"
    template_name = "posts/jrnl_detail.html"

    def get_queryset(self):
        queryset = super(JrnlDetailView, self).get_queryset()
        return queryset.filter(post_type=PostType.JRNL).select_related('location').prefetch_related('field_notes').prefetch_related('books')

    def get_object(self, queryset=None):
        obj = get_object_or_404(
            self.model,
            slug=self.kwargs['slug'],
            pub_date__month=self.kwargs['month'],
            pub_date__year=self.kwargs['year']
        )
        self.location = obj.location
        return obj

    def get_context_data(self, **kwargs):
        context = super(JrnlDetailView, self).get_context_data(**kwargs)
        context['wildlife'] = Sighting.objects.filter(
            Q(location=self.location) |
            Q(location__in=Location.objects.filter(parent=self.location))
        ).select_related().order_by('ap_id', 'ap__apclass__kind').distinct("ap")
        related = []
        for obj in self.object.related.all():
            model = apps.get_model(obj.model_name.app_label, obj.model_name.model)
            related.append(model.objects.get(slug=obj.slug, pub_date=obj.pub_date))
        context['related'] = related
        context['breadcrumbs'] = ("jrnl",)
        context['crumb_url'] = reverse('jrnl:list')
        return context


class JrnlDetailViewTXT(JrnlDetailView):
    template_name = "posts/jrnl_detail.txt"


class JrnlLatestView(JrnlDetailView):
    template_name = "posts/jrnl_latest.html"

    def get_object(self, queryset=None):
        obj = self.model.objects.filter(status=1).latest()
        self.location = obj.location
        return obj


class JrnlRSSFeedView(Feed):
    title = "Luxagraf: Topographical Writings"
    link = "/jrnl/"
    description = "Latest postings to luxagraf.net"
    description_template = 'feeds/blog_description.html'

    def items(self):
        return Post.objects.filter(status__exact=1).filter(post_type=PostType.JRNL).order_by('-pub_date')[:10]
    
    def item_pubdate(self, item):
        """
        Takes an item, as returned by items(), and returns the item's
        pubdate.
        """
        return item.pub_date

'''
class HomepageList(ListView):
    """
    Return a main entry and list of Entries in reverse chronological order
    """
    model = Entry

    def get_home(self):
        return Home.objects.filter(pk=1).prefetch_related('featured_image').select_related('featured').select_related('featured__location').get()

    def get_queryset(self):
        queryset = super(HomepageList, self).get_queryset()
        self.home = self.get_home()
        return queryset.filter(status__exact=1).order_by('-pub_date').exclude().select_related('location').select_related('featured_image')[1:9]

    def get_template_names(self):
        return ['%s' % self.home.template_name]

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(HomepageList, self).get_context_data(**kwargs)
        context['homepage'] = self.home
        context['location'] = LuxCheckIn.objects.latest()
        context['IMAGES_URL'] = settings.IMAGES_URL
        return context
'''