-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathviews.py
97 lines (71 loc) · 3.38 KB
/
views.py
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
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
from django.views.generic import DetailView, ListView
from .models import Collection, Photo
# https://docs.djangoproject.com/en/4.2/ref/models/querysets/
class PhotoListView(ListView):
model = Photo
paginate_by = 6 # Display 6 photos per page
def get_filtered_photos(self):
"""Return a filtered queryset of Photos that are published."""
return Photo.objects.filter(published=True)
def get_sorted_photos(self, qs):
"""Sort and return a Photo queryset depending on the `sort` query string (if applicable).
Args:
qs (QuerySet): the (filtered) QuerySet to be sorted.
"""
# Retrieve `sort` query string value, otherwise None
sort = self.request.GET.get('sort')
if sort == "new":
# Order by descending date (most recent earlier)
return qs.order_by('-date_taken')
elif sort == "old":
# Order by ascending date (oldest earlier)
return qs.order_by('date_taken')
# Order by featured (featured at start) then by descending date (most recent earlier)
return qs.order_by('-featured', '-date_taken')
def get_queryset(self):
filtered_qs = self.get_filtered_photos()
return self.get_sorted_photos(filtered_qs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['sorting'] = self.request.GET.get('sort', 'default')
return context
class CollectionView(PhotoListView):
template_name = "photos/collection.html"
def get_filtered_photos(self):
"""Return a filtered queryset of Photos that are in the collection."""
collection = get_object_or_404(Collection, slug=self.kwargs['collection_slug'])
if not collection.published:
raise Http404()
return Photo.objects.filter(published=True, collections__in=[collection])
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['collection'] = get_object_or_404(Collection, slug=self.kwargs['collection_slug'])
return context
class SearchView(PhotoListView):
template_name = "photos/search.html"
def get_filtered_photos(self):
"""Return a filtered queryset of Photos whose primary content includes the search query."""
query = self.request.GET.get('query', None)
if query is not None:
# https://docs.djangoproject.com/en/4.2/topics/db/queries/#complex-lookups-with-q-objects
lookup = Q(title__icontains=query) | \
Q(description__icontains=query) | \
Q(location__icontains=query)
return Photo.objects.filter(lookup)
return Photo.objects.filter(published=True)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['search_query'] = self.request.GET.get('query', '')
return context
def get(self, request, *args, **kwargs):
query = request.GET.get('query', None)
if query is None:
return redirect('homepage')
return super().get(request, *args, **kwargs)
class PhotoDetailView(DetailView):
model = Photo
# Return a 404 if the photo isn't published
queryset = Photo.objects.filter(published=True)