from itertools import chain from operator import attrgetter from django.views.generic.dates import YearArchiveView, MonthArchiveView from django.views.generic.detail import DetailView from utils.views import PaginatedListView, LuxDetailView from .models import FieldNote from photos.models import LuxImage class FieldNoteListView(PaginatedListView): """ Main Archive of Field Notes, which also includes photo posts """ model = FieldNote template_name = "fieldnotes/fieldnote_list.html" def dispatch(self, request, *args, **kwargs): path = request.path.split('/')[1:-1] if int(path[-1]) == self.kwargs['page']: path = "/".join(t for t in path[:-1]) print(path) 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_queryset(self): """ Return a list of Notes and Photos combined in reverse chronological order """ qs1 = FieldNote.objects.filter(status=1).order_by('-pub_date').prefetch_related('location') qs2 = LuxImage.objects.filter(is_public=True, title__startswith="daily_").prefetch_related('sizes').prefetch_related('location') result_list = sorted( chain(qs1, qs2), key=attrgetter('pub_date') ) return result_list[:: -1] class FieldNoteDetailView(LuxDetailView): model = FieldNote slug_field = "slug" class FieldNoteDetailViewTXT(FieldNoteDetailView): template_name = "jrnl/entry.txt" class FieldNoteYearArchiveView(YearArchiveView): queryset = FieldNote.objects.filter(status=1) date_field = "pub_date" template_name = "fieldnotes/fieldnote_archive_list_date.html" make_object_list = True class FieldNoteMonthArchiveView(MonthArchiveView): queryset = FieldNote.objects.filter(status=1) date_field = "pub_date" make_object_list = True template_name = "fieldnotes/fieldnote_archive_list_date.html"