Modified render_template and renamed it to render_response (consistent with
[archweb_dev-nj.git] / packages / views.py
blobaaf4deed6e5c7966f74e8d4f4c126a75edda3d80
1 from django.http import HttpResponse, HttpResponseRedirect
2 from django.shortcuts import get_object_or_404
3 from django.core.mail import send_mail
4 from django.template import Context, loader
5 from django.core import validators
6 from django.contrib.auth.decorators import login_required
7 from django.contrib.auth.models import User
8 from archweb_dev.lib.utils import validate, render_response
9 from datetime import datetime
10 from archweb_dev.packages.models import Package, PackageFile, Repo, Category
12 def update(request):
13 if request.POST.has_key('adopt'):
14 mode = 'adopt'
15 message = 'Adoption was successful'
16 if request.POST.has_key('disown'):
17 mode = 'disown'
18 message = 'Disown was successful'
19 try:
20 maint = User.objects.get(username=request.user.username)
21 except User.DoesNotExist:
22 return render_response(request, 'error_page.html', {'errmsg':'No maintainer record found! Are you a maintainer?'})
23 ids = request.POST.getlist('pkgid')
24 for id in ids:
25 pkg = Package.objects.get(id=id)
26 if mode == 'adopt' and pkg.maintainer_id == 0:
27 pkg.maintainer = maint
28 elif mode == 'disown' and pkg.maintainer == maint:
29 pkg.maintainer_id = 0
30 else:
31 message = "You are not the current maintainer"
32 pkg.save()
33 return render_response(request, 'status_page.html', {'message':message})
35 def details(request, pkgid=0, name='', repo=''):
36 if pkgid == 0:
37 p = Package.objects.filter(pkgname=name)
38 if repo: p = p.filter(repo__name__exact=repo)
39 # if more then one result, send to the search view
40 if len(p) > 1: return search(request, name)
41 if len(p) < 1: return render_response(request, 'error_page.html',
42 {'errmsg': 'No matching packages.'})
43 pkgid = p[0].id
45 pkg = get_object_or_404(Package, id=pkgid)
46 return render_response(request, 'packages/details.html', {'pkg':pkg})
48 def search(request, query=''):
49 if request.GET.has_key('q'):
50 # take the q GET var over the one passed on the URL
51 query = request.GET['q'].strip()
53 # fetch the form vars
54 repo = request.GET.get('repo', 'all')
55 category = request.GET.get('category', 'all')
56 lastupdate = request.GET.get('lastupdate', '')
57 limit = int(request.GET.get('limit', '50'))
58 skip = int(request.GET.get('skip', '0'))
59 sort = request.GET.get('sort', '')
60 maint = request.GET.get('maint', 'all')
62 # build the form lists
63 repos = Repo.objects.order_by('name')
64 cats = Category.objects.order_by('category')
65 # copy GET data over and add the lists
66 c = request.GET.copy()
67 c['repos'], c['categories'] = repos, cats
68 c['limit'], c['skip'] = limit, skip
69 c['lastupdate'] = lastupdate
70 c['sort'] = sort
71 # 'q' gets renamed to 'query', so it's not in GET
72 c['query'] = query
74 # validate
75 errors = {}
76 validate(errors, 'Last Update', lastupdate, validators.isValidANSIDate, True, request)
77 validate(errors, 'Page Limit', str(limit), validators.isOnlyDigits, True, request)
78 validate(errors, 'Page Skip', str(skip), validators.isOnlyDigits, True, request)
79 if errors:
80 c['errors'] = errors
81 return render_response(request, 'packages/search.html', c)
83 if query:
84 res1 = Package.objects.filter(pkgname__icontains=query)
85 res2 = Package.objects.filter(pkgdesc__icontains=query)
86 results = res1 | res2
87 else:
88 results = Package.objects.all()
89 if repo != 'all': results = results.filter(repo__name__exact=repo)
90 if category != 'all': results = results.filter(category__category__exact=category)
91 if maint != 'all': results = results.filter(maintainer=maint)
92 if lastupdate: results = results.filter(last_update__gte=datetime(int(lastupdate[0:4]),int(lastupdate[5:7]),int(lastupdate[8:10])))
93 # select_related() shouldn't be needed -- we're working around a Django bug
94 #results = results.select_related().order_by('repos.name', 'category', 'pkgname')
96 # sort results
97 if sort == '':
98 results = results.order_by('repo', 'category', 'pkgname')
99 else:
100 # duplicate sort fields shouldn't hurt anything
101 results = results.order_by(sort, 'repo', 'category', 'pkgname')
103 qs = request.GET.copy()
104 # build pagination urls
105 if results.count() > (skip + limit):
106 qs['skip'] = skip + limit
107 c['nextpage'] = '?' + qs.urlencode()
108 if skip > 0:
109 qs['skip'] = max(0, skip - limit)
110 c['prevpage'] = '?' + qs.urlencode()
111 # pass the querystring to the template so we can build sort queries
112 c['querystring'] = request.GET
114 # if only there's only one result, pass right to the package details view
115 if results.count() == 1: return details(request, results[0].id)
116 # limit result set
117 if limit > 0: results = results[skip:(skip+limit)]
119 c['results'] = results
120 return render_response(request, 'packages/search.html', c)
122 def files(request, pkgid):
123 pkg = get_object_or_404(Package, id=pkgid)
124 files = PackageFile.objects.filter(pkg=pkgid)
125 return render_response(request, 'packages/files.html', {'pkg':pkg,'files':files})
127 def flaghelp(request):
128 return render_response(request, 'packages/flaghelp.html')
130 def flag(request, pkgid):
131 pkg = get_object_or_404(Package, id=pkgid)
132 context = {'pkg': pkg}
133 if request.POST.has_key('confirmemail'):
134 email = request.POST['confirmemail']
135 if request.POST.has_key('usermessage'):
136 message = request.POST['usermessage']
137 else:
138 message = None
139 # validate
140 errors = {}
141 validate(errors, 'Email Address', email, validators.isValidEmail, False, request)
142 if errors:
143 context['errors'] = errors
144 return render_response(request, 'packages/flag.html', context)
146 context['confirmemail'] = email
147 pkg.needupdate = 1
148 pkg.save()
149 if pkg.maintainer_id > 0:
150 # send notification email to the maintainer
151 t = loader.get_template('packages/outofdate.txt')
152 c = Context({
153 'email': request.POST['confirmemail'],
154 'message': message,
155 'pkgname': pkg.pkgname,
156 'weburl': 'http://www.archlinux.org/packages/' + str(pkg.id) + '/'
158 send_mail('arch: Package [%s] marked out-of-date' % pkg.pkgname,
159 t.render(c),
160 'Arch Website Notification <nobody@archlinux.org>',
161 [pkg.maintainer.email],
162 fail_silently=True)
163 return render_response(request, 'packages/flag.html', context)
165 @login_required
166 def unflag(request, pkgid):
167 pkg = get_object_or_404(Package, id=pkgid)
168 if pkg.maintainer.username != request.user.username:
169 return render_response(request, 'error_page.html', {'errmsg': 'You do not own this package.'})
170 pkg.needupdate = 0
171 pkg.save()
172 return HttpResponseRedirect('/packages/%d/' % (pkg.id))