annotate gpp/downloads/views.py @ 429:d0f0800eef0c

Making the jquery tabbed version of the messages app the current version and removing the old. Also figured out how to dynamically update the base template's count of unread messages when messages are read.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 May 2011 02:56:58 +0000
parents 41411066b16d
children 1a7ca5fa494f
rev   line source
gremmie@1 1 """
gremmie@1 2 Views for the downloads application.
gremmie@1 3 """
gremmie@1 4 import random
gremmie@1 5
gremmie@1 6 from django.shortcuts import render_to_response
gremmie@1 7 from django.template import RequestContext
gremmie@1 8 from django.contrib.auth.decorators import login_required
gremmie@1 9 from django.http import Http404
gremmie@1 10 from django.http import HttpResponse
gremmie@1 11 from django.http import HttpResponseRedirect
gremmie@1 12 from django.http import HttpResponseForbidden
gremmie@1 13 from django.http import HttpResponseBadRequest
bgneal@404 14 from django.http import HttpResponseNotFound
bgneal@92 15 from django.core.paginator import InvalidPage
gremmie@1 16 from django.core.urlresolvers import reverse
gremmie@1 17 from django.db.models import Q
gremmie@1 18 from django.views.decorators.http import require_POST
bgneal@404 19 import django.utils.simplejson as json
gremmie@1 20
gremmie@1 21 from core.paginator import DiggPaginator
gremmie@1 22 from core.functions import email_admins
bgneal@241 23 from core.functions import get_page
gremmie@1 24 from downloads.models import Category
gremmie@1 25 from downloads.models import Download
gremmie@1 26 from downloads.models import VoteRecord
gremmie@1 27 from downloads.forms import AddDownloadForm
gremmie@1 28
gremmie@1 29 #######################################################################
gremmie@1 30
bgneal@242 31 DLS_PER_PAGE = 10
gremmie@1 32
gremmie@1 33 def create_paginator(dls):
gremmie@1 34 return DiggPaginator(dls, DLS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
gremmie@1 35
gremmie@1 36 #######################################################################
gremmie@1 37
gremmie@1 38 @login_required
gremmie@1 39 def index(request):
gremmie@1 40 categories = Category.objects.all()
gremmie@1 41 total_dls = Download.public_objects.all().count()
gremmie@1 42 return render_to_response('downloads/index.html', {
gremmie@1 43 'categories': categories,
gremmie@1 44 'total_dls': total_dls,
gremmie@1 45 },
gremmie@1 46 context_instance = RequestContext(request))
gremmie@1 47
gremmie@1 48 #######################################################################
gremmie@1 49 # Maps URL component to database field name for the Download table:
gremmie@1 50
gremmie@1 51 DOWNLOAD_FIELD_MAP = {
bgneal@404 52 'title': 'title',
gremmie@1 53 'date': '-date_added',
gremmie@1 54 'rating': '-average_score',
gremmie@1 55 'hits': '-hits'
gremmie@1 56 }
gremmie@1 57
gremmie@1 58 @login_required
bgneal@241 59 def category(request, slug, sort='title'):
gremmie@1 60 try:
bgneal@241 61 cat = Category.objects.get(slug=slug)
gremmie@1 62 except Category.DoesNotExist:
gremmie@1 63 raise Http404
gremmie@1 64
gremmie@1 65 if sort not in DOWNLOAD_FIELD_MAP:
gremmie@1 66 sort = 'title'
gremmie@1 67 order_by = DOWNLOAD_FIELD_MAP[sort]
gremmie@1 68
bgneal@190 69 downloads = Download.public_objects.filter(category=cat.pk).order_by(
bgneal@190 70 order_by)
gremmie@1 71 paginator = create_paginator(downloads)
bgneal@241 72 page = get_page(request.GET)
gremmie@1 73 try:
bgneal@241 74 the_page = paginator.page(page)
gremmie@1 75 except InvalidPage:
gremmie@1 76 raise Http404
gremmie@1 77
gremmie@1 78 return render_to_response('downloads/download_list.html', {
gremmie@1 79 's' : sort,
gremmie@1 80 'category' : cat,
bgneal@404 81 'page' : the_page,
bgneal@404 82 },
gremmie@1 83 context_instance = RequestContext(request))
gremmie@1 84
gremmie@1 85 #######################################################################
gremmie@1 86
gremmie@1 87 @login_required
gremmie@1 88 def new(request):
bgneal@243 89 """Display new downloads with pagination."""
bgneal@243 90
bgneal@243 91 downloads = Download.public_objects.order_by('-date_added')
bgneal@243 92
bgneal@243 93 paginator = create_paginator(downloads)
bgneal@243 94 page = get_page(request.GET)
bgneal@243 95 try:
bgneal@243 96 the_page = paginator.page(page)
bgneal@243 97 except InvalidPage:
bgneal@243 98 raise Http404
bgneal@243 99
gremmie@1 100 return render_to_response('downloads/download_summary.html', {
bgneal@243 101 'page': the_page,
bgneal@243 102 'title': 'Newest Downloads',
bgneal@404 103 },
gremmie@1 104 context_instance = RequestContext(request))
gremmie@1 105
gremmie@1 106 #######################################################################
gremmie@1 107
gremmie@1 108 @login_required
gremmie@1 109 def popular(request):
bgneal@243 110 """Display popular downloads with pagination."""
bgneal@243 111
bgneal@243 112 downloads = Download.public_objects.order_by('-hits')
bgneal@243 113
bgneal@243 114 paginator = create_paginator(downloads)
bgneal@243 115 page = get_page(request.GET)
bgneal@243 116 try:
bgneal@243 117 the_page = paginator.page(page)
bgneal@243 118 except InvalidPage:
bgneal@243 119 raise Http404
bgneal@243 120
gremmie@1 121 return render_to_response('downloads/download_summary.html', {
bgneal@243 122 'page': the_page,
bgneal@243 123 'title': 'Popular Downloads',
bgneal@404 124 },
gremmie@1 125 context_instance = RequestContext(request))
gremmie@1 126
gremmie@1 127 #######################################################################
gremmie@1 128
gremmie@1 129 @login_required
gremmie@1 130 def rating(request):
bgneal@243 131 """Display downloads by rating with pagination."""
bgneal@243 132
bgneal@243 133 downloads = Download.public_objects.order_by('-average_score')
bgneal@243 134 paginator = create_paginator(downloads)
bgneal@243 135 page = get_page(request.GET)
bgneal@243 136 try:
bgneal@243 137 the_page = paginator.page(page)
bgneal@243 138 except InvalidPage:
bgneal@243 139 raise Http404
bgneal@243 140
gremmie@1 141 return render_to_response('downloads/download_summary.html', {
bgneal@243 142 'page': the_page,
bgneal@243 143 'title': 'Highest Rated Downloads',
bgneal@404 144 },
gremmie@1 145 context_instance = RequestContext(request))
gremmie@1 146
gremmie@1 147 #######################################################################
gremmie@1 148
gremmie@1 149 @login_required
bgneal@23 150 def details(request, id):
gremmie@1 151 download = Download.public_objects.get(pk=id)
gremmie@1 152 if download is None:
gremmie@1 153 raise Http404
bgneal@23 154 return render_to_response('downloads/download_detail.html', {
gremmie@1 155 'download' : download,
bgneal@404 156 },
gremmie@1 157 context_instance = RequestContext(request))
gremmie@1 158
gremmie@1 159 #######################################################################
gremmie@1 160
gremmie@1 161 @login_required
gremmie@1 162 def add(request):
gremmie@1 163 if request.method == 'POST':
gremmie@1 164 form = AddDownloadForm(request.POST, request.FILES)
gremmie@1 165 if form.is_valid():
gremmie@1 166 dl = form.save(commit=False)
gremmie@1 167 dl.user = request.user
gremmie@1 168 dl.ip_address = request.META.get('REMOTE_ADDR', None)
gremmie@1 169 dl.save()
gremmie@1 170 email_admins('New download for approval', """Hello,
gremmie@1 171
gremmie@1 172 A user has uploaded a new download for your approval.
gremmie@1 173 """)
gremmie@1 174 return HttpResponseRedirect(reverse('downloads-add_thanks'))
gremmie@1 175 else:
gremmie@1 176 form = AddDownloadForm()
gremmie@1 177
gremmie@1 178 return render_to_response('downloads/add.html', {
gremmie@1 179 'add_form': form,
gremmie@1 180 },
gremmie@1 181 context_instance=RequestContext(request))
gremmie@1 182
gremmie@1 183 #######################################################################
gremmie@1 184
gremmie@1 185 @login_required
gremmie@1 186 def thanks(request):
gremmie@1 187 return render_to_response('downloads/thanks.html', {
gremmie@1 188 },
gremmie@1 189 context_instance=RequestContext(request))
gremmie@1 190
gremmie@1 191 #######################################################################
gremmie@1 192
gremmie@1 193 @require_POST
gremmie@1 194 def rate_download(request):
gremmie@1 195 """This function is called by AJAX to rate a download."""
gremmie@1 196 if request.user.is_authenticated():
gremmie@1 197 id = request.POST.get('id', None)
gremmie@1 198 rating = request.POST.get('rating', None)
gremmie@1 199 if id is None or rating is None:
gremmie@1 200 return HttpResponseBadRequest('Missing id or rating.')
bgneal@404 201
gremmie@1 202 try:
gremmie@1 203 rating = int(rating)
gremmie@1 204 except ValueError:
gremmie@1 205 return HttpResponseBadRequest('Invalid rating.')
gremmie@1 206
gremmie@1 207 # rating will be from 0-4
gremmie@1 208 rating = min(5, max(1, rating))
gremmie@1 209
gremmie@1 210 try:
gremmie@1 211 download = Download.public_objects.get(pk=id)
gremmie@1 212 except Download.DoesNotExist:
gremmie@1 213 return HttpResponseBadRequest('Invalid download id.')
gremmie@1 214
gremmie@1 215 # prevent multiple votes from the same user
bgneal@241 216 vote_record, created = VoteRecord.objects.get_or_create(
bgneal@241 217 download=download, user=request.user)
gremmie@1 218 if created:
gremmie@1 219 new_score = download.vote(rating)
gremmie@1 220 download.save()
gremmie@1 221 return HttpResponse(str(new_score))
gremmie@1 222 else:
gremmie@1 223 return HttpResponse('-1')
gremmie@1 224
gremmie@1 225 return HttpResponseForbidden('You must be logged in to rate a download.')
bgneal@404 226
bgneal@404 227 #######################################################################
bgneal@404 228
bgneal@404 229 @require_POST
bgneal@404 230 def request_download(request):
bgneal@404 231 """
bgneal@404 232 This function is called by AJAX to request a download. We update the hit
bgneal@404 233 count and then return a JSON object of the form:
bgneal@404 234 { id: download-id, 'url': link-to-download }
bgneal@404 235
bgneal@404 236 """
bgneal@404 237 if request.user.is_authenticated():
bgneal@404 238 dl_id = request.POST.get('id')
bgneal@404 239 if dl_id:
bgneal@404 240 try:
bgneal@404 241 dl = Download.public_objects.get(pk=dl_id)
bgneal@404 242 except Download.DoesNotExist:
bgneal@404 243 return HttpResponseNotFound("Download not found")
bgneal@404 244
bgneal@404 245 dl.hits += 1
bgneal@404 246 dl.save()
bgneal@404 247
bgneal@404 248 s = json.dumps({'id': dl_id, 'url': dl.file.url})
bgneal@404 249 return HttpResponse(s, content_type='application/json')
bgneal@404 250
bgneal@404 251 return HttpResponseForbidden('An error occurred.')