forked from python/pythondotorg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
149 lines (118 loc) · 4.45 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
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
from django.db.models import Prefetch
from django.urls import reverse
from django.views.generic import DetailView, TemplateView, ListView, RedirectView
from django.http import Http404
from .models import OS, Release, ReleaseFile
class DownloadLatestPython2(RedirectView):
""" Redirect to latest Python 2 release """
permanent = False
def get_redirect_url(self, **kwargs):
try:
latest_python2 = Release.objects.latest_python2()
except Release.DoesNotExist:
latest_python2 = None
if latest_python2:
return latest_python2.get_absolute_url()
else:
return reverse('download')
class DownloadLatestPython3(RedirectView):
""" Redirect to latest Python 3 release """
permanent = False
def get_redirect_url(self, **kwargs):
try:
latest_python3 = Release.objects.latest_python3()
except Release.DoesNotExist:
latest_python3 = None
if latest_python3:
return latest_python3.get_absolute_url()
else:
return reverse('download')
class DownloadBase:
""" Include latest releases in all views """
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'latest_python2': Release.objects.latest_python2(),
'latest_python3': Release.objects.latest_python3(),
})
return context
class DownloadHome(DownloadBase, TemplateView):
template_name = 'downloads/index.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
try:
latest_python2 = Release.objects.latest_python2()
except Release.DoesNotExist:
latest_python2 = None
try:
latest_python3 = Release.objects.latest_python3()
except Release.DoesNotExist:
latest_python3 = None
python_files = []
for o in OS.objects.all():
data = {
'os': o,
}
if latest_python2 is not None:
data['python2'] = latest_python2.download_file_for_os(o.slug)
if latest_python3 is not None:
data['python3'] = latest_python3.download_file_for_os(o.slug)
python_files.append(data)
context.update({
'releases': Release.objects.downloads(),
'latest_python2': latest_python2,
'latest_python3': latest_python3,
'python_files': python_files,
})
return context
class DownloadFullOSList(DownloadBase, ListView):
template_name = 'downloads/full_os_list.html'
context_object_name = 'os_list'
model = OS
class DownloadOSList(DownloadBase, DetailView):
template_name = 'downloads/os_list.html'
context_object_name = 'os'
model = OS
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
release_files = ReleaseFile.objects.select_related(
'os',
).filter(os=self.object)
context.update({
'os_slug': self.object.slug,
'releases': Release.objects.released().prefetch_related(
Prefetch('files', queryset=release_files),
).order_by('-release_date'),
'pre_releases': Release.objects.published().pre_release().prefetch_related(
Prefetch('files', queryset=release_files),
).order_by('-release_date'),
})
return context
class DownloadReleaseDetail(DownloadBase, DetailView):
template_name = 'downloads/release_detail.html'
model = Release
context_object_name = 'release'
def get_object(self):
try:
return self.get_queryset().select_related().get(
slug=self.kwargs['release_slug']
)
except self.model.DoesNotExist:
raise Http404
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Manually add release files for better ordering
context['release_files'] = []
# Add source files
context['release_files'].extend(
list(self.object.files.filter(os__slug='source').order_by('name'))
)
# Add all other OSes
context['release_files'].extend(
list(
self.object.files.exclude(
os__slug='source'
).order_by('os__slug', 'name')
)
)
return context