Mercurial > public > sg101
comparison gpp/downloads/views.py @ 404:41411066b16d
Fixing #199; redid the downloads so the user has more control about how to save the file.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 27 Mar 2011 23:21:17 +0000 |
parents | 7ddd60164245 |
children | 1a7ca5fa494f |
comparison
equal
deleted
inserted
replaced
403:6e425c9b9d16 | 404:41411066b16d |
---|---|
9 from django.http import Http404 | 9 from django.http import Http404 |
10 from django.http import HttpResponse | 10 from django.http import HttpResponse |
11 from django.http import HttpResponseRedirect | 11 from django.http import HttpResponseRedirect |
12 from django.http import HttpResponseForbidden | 12 from django.http import HttpResponseForbidden |
13 from django.http import HttpResponseBadRequest | 13 from django.http import HttpResponseBadRequest |
14 from django.http import HttpResponseNotFound | |
14 from django.core.paginator import InvalidPage | 15 from django.core.paginator import InvalidPage |
15 from django.core.urlresolvers import reverse | 16 from django.core.urlresolvers import reverse |
16 from django.db.models import Q | 17 from django.db.models import Q |
17 from django.views.decorators.http import require_POST | 18 from django.views.decorators.http import require_POST |
19 import django.utils.simplejson as json | |
18 | 20 |
19 from core.paginator import DiggPaginator | 21 from core.paginator import DiggPaginator |
20 from core.functions import email_admins | 22 from core.functions import email_admins |
21 from core.functions import get_page | 23 from core.functions import get_page |
22 from downloads.models import Category | 24 from downloads.models import Category |
45 | 47 |
46 ####################################################################### | 48 ####################################################################### |
47 # Maps URL component to database field name for the Download table: | 49 # Maps URL component to database field name for the Download table: |
48 | 50 |
49 DOWNLOAD_FIELD_MAP = { | 51 DOWNLOAD_FIELD_MAP = { |
50 'title': 'title', | 52 'title': 'title', |
51 'date': '-date_added', | 53 'date': '-date_added', |
52 'rating': '-average_score', | 54 'rating': '-average_score', |
53 'hits': '-hits' | 55 'hits': '-hits' |
54 } | 56 } |
55 | 57 |
74 raise Http404 | 76 raise Http404 |
75 | 77 |
76 return render_to_response('downloads/download_list.html', { | 78 return render_to_response('downloads/download_list.html', { |
77 's' : sort, | 79 's' : sort, |
78 'category' : cat, | 80 'category' : cat, |
79 'page' : the_page, | 81 'page' : the_page, |
80 }, | 82 }, |
81 context_instance = RequestContext(request)) | 83 context_instance = RequestContext(request)) |
82 | 84 |
83 ####################################################################### | 85 ####################################################################### |
84 | 86 |
85 @login_required | 87 @login_required |
96 raise Http404 | 98 raise Http404 |
97 | 99 |
98 return render_to_response('downloads/download_summary.html', { | 100 return render_to_response('downloads/download_summary.html', { |
99 'page': the_page, | 101 'page': the_page, |
100 'title': 'Newest Downloads', | 102 'title': 'Newest Downloads', |
101 }, | 103 }, |
102 context_instance = RequestContext(request)) | 104 context_instance = RequestContext(request)) |
103 | 105 |
104 ####################################################################### | 106 ####################################################################### |
105 | 107 |
106 @login_required | 108 @login_required |
117 raise Http404 | 119 raise Http404 |
118 | 120 |
119 return render_to_response('downloads/download_summary.html', { | 121 return render_to_response('downloads/download_summary.html', { |
120 'page': the_page, | 122 'page': the_page, |
121 'title': 'Popular Downloads', | 123 'title': 'Popular Downloads', |
122 }, | 124 }, |
123 context_instance = RequestContext(request)) | 125 context_instance = RequestContext(request)) |
124 | 126 |
125 ####################################################################### | 127 ####################################################################### |
126 | 128 |
127 @login_required | 129 @login_required |
137 raise Http404 | 139 raise Http404 |
138 | 140 |
139 return render_to_response('downloads/download_summary.html', { | 141 return render_to_response('downloads/download_summary.html', { |
140 'page': the_page, | 142 'page': the_page, |
141 'title': 'Highest Rated Downloads', | 143 'title': 'Highest Rated Downloads', |
142 }, | 144 }, |
143 context_instance = RequestContext(request)) | 145 context_instance = RequestContext(request)) |
144 | |
145 ####################################################################### | |
146 | |
147 @login_required | |
148 @require_POST | |
149 def download(request, id): | |
150 download = Download.public_objects.get(pk=id) | |
151 if download is None: | |
152 raise Http404 | |
153 return _redirect_download(download) | |
154 | |
155 ####################################################################### | |
156 | |
157 def _redirect_download(download): | |
158 download.hits += 1 | |
159 download.save() | |
160 return HttpResponseRedirect(download.file.url) | |
161 | 146 |
162 ####################################################################### | 147 ####################################################################### |
163 | 148 |
164 @login_required | 149 @login_required |
165 def details(request, id): | 150 def details(request, id): |
166 download = Download.public_objects.get(pk=id) | 151 download = Download.public_objects.get(pk=id) |
167 if download is None: | 152 if download is None: |
168 raise Http404 | 153 raise Http404 |
169 return render_to_response('downloads/download_detail.html', { | 154 return render_to_response('downloads/download_detail.html', { |
170 'download' : download, | 155 'download' : download, |
171 }, | 156 }, |
172 context_instance = RequestContext(request)) | 157 context_instance = RequestContext(request)) |
173 | |
174 ####################################################################### | |
175 | |
176 @login_required | |
177 @require_POST | |
178 def random_download(request): | |
179 ids = Download.public_objects.values_list('id', flat=True) | |
180 if not ids: | |
181 raise Http404 | |
182 id = random.choice(ids) | |
183 download = Download.objects.get(pk=id) | |
184 return _redirect_download(download) | |
185 | 158 |
186 ####################################################################### | 159 ####################################################################### |
187 | 160 |
188 @login_required | 161 @login_required |
189 def add(request): | 162 def add(request): |
223 if request.user.is_authenticated(): | 196 if request.user.is_authenticated(): |
224 id = request.POST.get('id', None) | 197 id = request.POST.get('id', None) |
225 rating = request.POST.get('rating', None) | 198 rating = request.POST.get('rating', None) |
226 if id is None or rating is None: | 199 if id is None or rating is None: |
227 return HttpResponseBadRequest('Missing id or rating.') | 200 return HttpResponseBadRequest('Missing id or rating.') |
228 | 201 |
229 try: | 202 try: |
230 rating = int(rating) | 203 rating = int(rating) |
231 except ValueError: | 204 except ValueError: |
232 return HttpResponseBadRequest('Invalid rating.') | 205 return HttpResponseBadRequest('Invalid rating.') |
233 | 206 |
248 return HttpResponse(str(new_score)) | 221 return HttpResponse(str(new_score)) |
249 else: | 222 else: |
250 return HttpResponse('-1') | 223 return HttpResponse('-1') |
251 | 224 |
252 return HttpResponseForbidden('You must be logged in to rate a download.') | 225 return HttpResponseForbidden('You must be logged in to rate a download.') |
226 | |
227 ####################################################################### | |
228 | |
229 @require_POST | |
230 def request_download(request): | |
231 """ | |
232 This function is called by AJAX to request a download. We update the hit | |
233 count and then return a JSON object of the form: | |
234 { id: download-id, 'url': link-to-download } | |
235 | |
236 """ | |
237 if request.user.is_authenticated(): | |
238 dl_id = request.POST.get('id') | |
239 if dl_id: | |
240 try: | |
241 dl = Download.public_objects.get(pk=dl_id) | |
242 except Download.DoesNotExist: | |
243 return HttpResponseNotFound("Download not found") | |
244 | |
245 dl.hits += 1 | |
246 dl.save() | |
247 | |
248 s = json.dumps({'id': dl_id, 'url': dl.file.url}) | |
249 return HttpResponse(s, content_type='application/json') | |
250 | |
251 return HttpResponseForbidden('An error occurred.') |