Commit 15ceff77 authored by eliott's avatar eliott
Browse files

Modified render_template and renamed it to render_response (consistent with

archweb_pub conventions).
Moved pkgmaint_guide to a template.
parent d1cc0f54
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core import validators from django.core import validators
from archweb_dev.utils import render_template from archweb_dev.utils import render_response
from archweb_dev.packages.models import Package from archweb_dev.packages.models import Package
from archweb_dev.todolists.models import Todolist, TodolistPkg from archweb_dev.todolists.models import Todolist, TodolistPkg
from archweb_dev.settings import DATA_DIR from archweb_dev.settings import DATA_DIR
...@@ -11,57 +11,58 @@ ...@@ -11,57 +11,58 @@
@login_required @login_required
def index(request): def index(request):
try: try:
thismaint = User.objects.get(username=request.user.username) thismaint = User.objects.get(username=request.user.username)
except User.DoesNotExist: except User.DoesNotExist:
# weird, we don't have a maintainer record for this logged-in user # weird, we don't have a maintainer record for this logged-in user
thismaint = None thismaint = None
# get a list of incomplete package todo lists # get a list of incomplete package todo lists
todos = Todolist.objects.get_incomplete() todos = Todolist.objects.get_incomplete()
# get flagged-package stats for all maintainers # get flagged-package stats for all maintainers
stats = Package.objects.get_flag_stats() stats = Package.objects.get_flag_stats()
if thismaint: if thismaint:
# get list of flagged packages for this maintainer # get list of flagged packages for this maintainer
pkgs = Package.objects.filter(maintainer=thismaint.id).filter(needupdate=True).order_by('repo', 'pkgname') pkgs = Package.objects.filter(maintainer=thismaint.id).filter(needupdate=True).order_by('repo', 'pkgname')
else: else:
pkgs = None pkgs = None
return render_template('devel/index.html', request, return render_response(request, 'devel/index.html',
{'stats':stats, 'pkgs':pkgs, 'todos':todos, 'maint':thismaint}) {'stats':stats, 'pkgs':pkgs, 'todos':todos, 'maint':thismaint})
@login_required @login_required
#@is_maintainer #@is_maintainer
def change_notify(request): def change_notify(request):
maint = User.objects.get(username=request.user.username) maint = User.objects.get(username=request.user.username)
notify = request.POST.get('notify', 'no') notify = request.POST.get('notify', 'no')
try: try:
maint.get_profile().notify = notify == 'yes' maint.get_profile().notify = notify == 'yes'
except UserProfile.DoesNotExist: except UserProfile.DoesNotExist:
UserProfile(user_id=maint.id ,notify=notify == 'yes').save() UserProfile(user_id=maint.id ,notify=notify == 'yes').save()
maint.get_profile().save() maint.get_profile().save()
return HttpResponseRedirect('/devel/') return HttpResponseRedirect('/devel/')
@login_required @login_required
def change_profile(request): def change_profile(request):
errors = {} errors = {}
if request.POST: if request.POST:
passwd1, passwd2 = request.POST['passwd'], request.POST['passwd2'] passwd1, passwd2 = request.POST['passwd'], request.POST['passwd2']
email = request.POST['email'] email = request.POST['email']
# validate # validate
if passwd1 != passwd2: if passwd1 != passwd2:
errors['password'] = [' Passwords do not match. '] errors['password'] = [' Passwords do not match. ']
validate(errors, 'Email', email, validators.isValidEmail, False, request) validate(errors, 'Email', email, validators.isValidEmail, False, request)
# apply changes # apply changes
if not errors: if not errors:
request.user.email = email request.user.email = email
if passwd1: if passwd1:
request.user.set_password(passwd1) request.user.set_password(passwd1)
request.user.save() request.user.save()
return HttpResponseRedirect('/devel/') return HttpResponseRedirect('/devel/')
return render_template('devel/profile.html', request, {'errors':errors,'email':request.user.email}) return render_response(request, 'devel/profile.html', {'errors':errors,'email':request.user.email})
@login_required @login_required
def guide(request): def guide(request):
return HttpResponse(file(DATA_DIR + '/pkgmaint_guide.txt').read(), return render_response(request, 'devel/pkgmaint_guide.txt', {'errors':errors,'email':request.user.email})
mimetype='text/plain') return HttpResponse(file(DATA_DIR + '/pkgmaint_guide.txt').read(),
mimetype='text/plain')
from django.core import validators
from django.conf import settings
from django.core.cache import cache
from django.shortcuts import render_to_response
from django.template import RequestContext
def validate(errdict, fieldname, fieldval, validator, blankallowed, request):
"""
A helper function that allows easy access to Django's validators without
going through a Manipulator object. Will return a dict of all triggered
errors.
"""
if blankallowed and not fieldval:
return
alldata = ' '.join(request.POST.values()) + ' '.join(request.GET.values())
try:
validator(fieldval, alldata)
except validators.ValidationError, e:
if not errdict.has_key(fieldname):
errdict[fieldname] = []
errdict[fieldname].append(e)
def prune_cache(django_page_url):
if not settings.CACHE:
return
cache_prefix = 'views.decorators.cache.cache_page.'
cache_prefix += settings.CACHE_MIDDLEWARE_KEY_PREFIX + '.'
cache_postfix = '.d41d8cd98f00b204e9800998ecf8427e'
cache.delete('%s%s%s' % (cache_prefix,django_page_url,cache_postfix))
def render_response(req, *args, **kwargs):
kwargs['context_instance'] = RequestContext(req)
return render_to_response(*args, **kwargs)
...@@ -3,80 +3,80 @@ ...@@ -3,80 +3,80 @@
from django.contrib.auth.decorators import user_passes_test from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django import forms from django import forms
from archweb_dev.utils import render_template from archweb_dev.lib.utils import render_response
from archweb_dev.news.models import News from archweb_dev.news.models import News
from datetime import date from datetime import date
def view(request, newsid): def view(request, newsid):
news = get_object_or_404(News, id=newsid) news = get_object_or_404(News, id=newsid)
return render_template('news/view.html', request, {'news':news}) return render_response(request, 'news/view.html', {'news':news})
def list(request): def list(request):
news = News.objects.order_by('-postdate', '-id') news = News.objects.order_by('-postdate', '-id')
return render_template('news/list.html', request, {'news':news}) return render_response(request, 'news/list.html', {'news':news})
@user_passes_test(lambda u: u.has_perm('news.add_news')) @user_passes_test(lambda u: u.has_perm('news.add_news'))
def add(request): def add(request):
try: try:
m = User.objects.get(username=request.user.username) m = User.objects.get(username=request.user.username)
except User.DoesNotExist: except User.DoesNotExist:
return render_template('error_page.html', request, return render_response(request, 'error_page.html',
{'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'}) {'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'})
manipulator = News.AddManipulator() manipulator = News.AddManipulator()
if request.POST: if request.POST:
data = request.POST.copy() data = request.POST.copy()
# add in the author ID # add in the author ID
data['author'] = m.id data['author'] = m.id
errors = manipulator.get_validation_errors(data) errors = manipulator.get_validation_errors(data)
if not errors: if not errors:
manipulator.do_html2python(data) manipulator.do_html2python(data)
manipulator.save(data) manipulator.save(data)
return HttpResponseRedirect('/news/') return HttpResponseRedirect('/news/')
else: else:
errors = {} errors = {}
data = {} data = {}
form = forms.FormWrapper(manipulator, data, errors) form = forms.FormWrapper(manipulator, data, errors)
return render_template('news/add.html', request, {'form': form}) return render_response(request, 'news/add.html', {'form': form})
@user_passes_test(lambda u: u.has_perm('news.delete_news')) @user_passes_test(lambda u: u.has_perm('news.delete_news'))
def delete(request, newsid): def delete(request, newsid):
news = get_object_or_404(News, id=newsid) news = get_object_or_404(News, id=newsid)
#if news.author.id != request.user.id: #if news.author.id != request.user.id:
# return render_template('error_page.html', request, {'errmsg': 'You do not own this news item'}) # return render_response(request, 'error_page.html', {'errmsg': 'You do not own this news item'})
if request.POST: if request.POST:
news.delete() news.delete()
return HttpResponseRedirect('/news/') return HttpResponseRedirect('/news/')
return render_template('news/delete.html', request) return render_response(request, 'news/delete.html')
@user_passes_test(lambda u: u.has_perm('news.change_news')) @user_passes_test(lambda u: u.has_perm('news.change_news'))
def edit(request, newsid): def edit(request, newsid):
try: try:
m = User.objects.get(username=request.user.username) m = User.objects.get(username=request.user.username)
except User.DoesNotExist: except User.DoesNotExist:
return render_template('error_page.html', request, return render_response(request, 'error_page.html',
{'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'}) {'errmsg': 'Cannot find a maintainer record for you! No posting allowed.'})
try: try:
manipulator = News.ChangeManipulator(newsid) manipulator = News.ChangeManipulator(newsid)
except News.DoesNotExist: except News.DoesNotExist:
raise Http404 raise Http404
news = manipulator.original_object news = manipulator.original_object
# if news.author != m: # if news.author != m:
# return render_template('error_page.html', request, {'errmsg': 'You do not own this news item'}) # return render_response(request, 'error_page.html', {'errmsg': 'You do not own this news item'})
if request.POST: if request.POST:
data = request.POST.copy() data = request.POST.copy()
# add in the author ID # add in the author ID
data['author'] = news.author.id data['author'] = news.author.id
errors = manipulator.get_validation_errors(data) errors = manipulator.get_validation_errors(data)
if not errors: if not errors:
manipulator.do_html2python(data) manipulator.do_html2python(data)
manipulator.save(data) manipulator.save(data)
return HttpResponseRedirect('/news/') return HttpResponseRedirect('/news/')
else: else:
errors = {} errors = {}
data = news.__dict__ data = news.__dict__
form = forms.FormWrapper(manipulator, data, errors) form = forms.FormWrapper(manipulator, data, errors)
return render_template('news/add.html', request, {'form': form, 'news':news}) return render_response(request, 'news/add.html', {'form': form, 'news':news})
...@@ -5,168 +5,168 @@ ...@@ -5,168 +5,168 @@
from django.core import validators from django.core import validators
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User from django.contrib.auth.models import User
from archweb_dev.utils import validate, render_template from archweb_dev.lib.utils import validate, render_response
from datetime import datetime from datetime import datetime
from archweb_dev.packages.models import Package, PackageFile, Repo, Category from archweb_dev.packages.models import Package, PackageFile, Repo, Category
def update(request): def update(request):
if request.POST.has_key('adopt'): if request.POST.has_key('adopt'):
mode = 'adopt' mode = 'adopt'
message = 'Adoption was successful' message = 'Adoption was successful'
if request.POST.has_key('disown'): if request.POST.has_key('disown'):
mode = 'disown' mode = 'disown'
message = 'Disown was successful' message = 'Disown was successful'
try: try:
maint = User.objects.get(username=request.user.username) maint = User.objects.get(username=request.user.username)
except User.DoesNotExist: except User.DoesNotExist:
return render_template('error_page.html', request, {'errmsg':'No maintainer record found! Are you a maintainer?'}) return render_response(request, 'error_page.html', {'errmsg':'No maintainer record found! Are you a maintainer?'})
ids = request.POST.getlist('pkgid') ids = request.POST.getlist('pkgid')
for id in ids: for id in ids:
pkg = Package.objects.get(id=id) pkg = Package.objects.get(id=id)
if mode == 'adopt' and pkg.maintainer_id == 0: if mode == 'adopt' and pkg.maintainer_id == 0:
pkg.maintainer = maint pkg.maintainer = maint
elif mode == 'disown' and pkg.maintainer == maint: elif mode == 'disown' and pkg.maintainer == maint:
pkg.maintainer_id = 0 pkg.maintainer_id = 0
else: else:
message = "You are not the current maintainer" message = "You are not the current maintainer"
pkg.save() pkg.save()
return render_template('status_page.html', request, {'message':message}) return render_response(request, 'status_page.html', {'message':message})
def details(request, pkgid=0, name='', repo=''): def details(request, pkgid=0, name='', repo=''):
if pkgid == 0: if pkgid == 0:
p = Package.objects.filter(pkgname=name) p = Package.objects.filter(pkgname=name)
if repo: p = p.filter(repo__name__exact=repo) if repo: p = p.filter(repo__name__exact=repo)
# if more then one result, send to the search view # if more then one result, send to the search view
if len(p) > 1: return search(request, name) if len(p) > 1: return search(request, name)
if len(p) < 1: return render_template('error_page.html', request, if len(p) < 1: return render_response(request, 'error_page.html',
{'errmsg': 'No matching packages.'}) {'errmsg': 'No matching packages.'})
pkgid = p[0].id pkgid = p[0].id
pkg = get_object_or_404(Package, id=pkgid) pkg = get_object_or_404(Package, id=pkgid)
return render_template('packages/details.html', request, {'pkg':pkg}) return render_response(request, 'packages/details.html', {'pkg':pkg})
def search(request, query=''): def search(request, query=''):
if request.GET.has_key('q'): if request.GET.has_key('q'):
# take the q GET var over the one passed on the URL # take the q GET var over the one passed on the URL
query = request.GET['q'].strip() query = request.GET['q'].strip()
# fetch the form vars # fetch the form vars
repo = request.GET.get('repo', 'all') repo = request.GET.get('repo', 'all')
category = request.GET.get('category', 'all') category = request.GET.get('category', 'all')
lastupdate = request.GET.get('lastupdate', '') lastupdate = request.GET.get('lastupdate', '')
limit = int(request.GET.get('limit', '50')) limit = int(request.GET.get('limit', '50'))
skip = int(request.GET.get('skip', '0')) skip = int(request.GET.get('skip', '0'))
sort = request.GET.get('sort', '') sort = request.GET.get('sort', '')
maint = request.GET.get('maint', 'all') maint = request.GET.get('maint', 'all')
# build the form lists # build the form lists
repos = Repo.objects.order_by('name') repos = Repo.objects.order_by('name')
cats = Category.objects.order_by('category') cats = Category.objects.order_by('category')
# copy GET data over and add the lists # copy GET data over and add the lists
c = request.GET.copy() c = request.GET.copy()
c['repos'], c['categories'] = repos, cats c['repos'], c['categories'] = repos, cats
c['limit'], c['skip'] = limit, skip c['limit'], c['skip'] = limit, skip
c['lastupdate'] = lastupdate c['lastupdate'] = lastupdate
c['sort'] = sort c['sort'] = sort
# 'q' gets renamed to 'query', so it's not in GET # 'q' gets renamed to 'query', so it's not in GET
c['query'] = query c['query'] = query
# validate # validate
errors = {} errors = {}
validate(errors, 'Last Update', lastupdate, validators.isValidANSIDate, True, request) validate(errors, 'Last Update', lastupdate, validators.isValidANSIDate, True, request)
validate(errors, 'Page Limit', str(limit), validators.isOnlyDigits, True, request) validate(errors, 'Page Limit', str(limit), validators.isOnlyDigits, True, request)
validate(errors, 'Page Skip', str(skip), validators.isOnlyDigits, True, request) validate(errors, 'Page Skip', str(skip), validators.isOnlyDigits, True, request)
if errors: if errors:
c['errors'] = errors c['errors'] = errors
return render_template('packages/search.html', request, c) return render_response(request, 'packages/search.html', c)
if query: if query:
res1 = Package.objects.filter(pkgname__icontains=query) res1 = Package.objects.filter(pkgname__icontains=query)
res2 = Package.objects.filter(pkgdesc__icontains=query) res2 = Package.objects.filter(pkgdesc__icontains=query)
results = res1 | res2 results = res1 | res2
else: else:
results = Package.objects.all() results = Package.objects.all()
if repo != 'all': results = results.filter(repo__name__exact=repo) if repo != 'all': results = results.filter(repo__name__exact=repo)
if category != 'all': results = results.filter(category__category__exact=category) if category != 'all': results = results.filter(category__category__exact=category)
if maint != 'all': results = results.filter(maintainer=maint) if maint != 'all': results = results.filter(maintainer=maint)
if lastupdate: results = results.filter(last_update__gte=datetime(int(lastupdate[0:4]),int(lastupdate[5:7]),int(lastupdate[8:10]))) if lastupdate: results = results.filter(last_update__gte=datetime(int(lastupdate[0:4]),int(lastupdate[5:7]),int(lastupdate[8:10])))
# select_related() shouldn't be needed -- we're working around a Django bug # select_related() shouldn't be needed -- we're working around a Django bug
#results = results.select_related().order_by('repos.name', 'category', 'pkgname') #results = results.select_related().order_by('repos.name', 'category', 'pkgname')
# sort results # sort results
if sort == '': if sort == '':
results = results.order_by('repo', 'category', 'pkgname') results = results.order_by('repo', 'category', 'pkgname')
else: else:
# duplicate sort fields shouldn't hurt anything # duplicate sort fields shouldn't hurt anything
results = results.order_by(sort, 'repo', 'category', 'pkgname') results = results.order_by(sort, 'repo', 'category', 'pkgname')
qs = request.GET.copy() qs = request.GET.copy()
# build pagination urls # build pagination urls
if results.count() > (skip + limit): if results.count() > (skip + limit):
qs['skip'] = skip + limit qs['skip'] = skip + limit
c['nextpage'] = '?' + qs.urlencode() c['nextpage'] = '?' + qs.urlencode()
if skip > 0: if skip > 0:
qs['skip'] = max(0, skip - limit) qs['skip'] = max(0, skip - limit)
c['prevpage'] = '?' + qs.urlencode() c['prevpage'] = '?' + qs.urlencode()
# pass the querystring to the template so we can build sort queries # pass the querystring to the template so we can build sort queries
c['querystring'] = request.GET c['querystring'] = request.GET
# if only there's only one result, pass right to the package details view # if only there's only one result, pass right to the package details view
if results.count() == 1: return details(request, results[0].id) if results.count() == 1: return details(request, results[0].id)
# limit result set # limit result set
if limit > 0: results = results[skip:(skip+limit)] if limit > 0: results = results[skip:(skip+limit)]
c['results'] = results c['results'] = results
return render_template('packages/search.html', request, c) return render_response(request, 'packages/search.html', c)
def files(request, pkgid): def files(request, pkgid):
pkg = get_object_or_404(Package, id=pkgid) pkg = get_object_or_404(Package, id=pkgid)
files = PackageFile.objects.filter(pkg=pkgid) files = PackageFile.objects.filter(pkg=pkgid)
return render_template('packages/files.html', request, {'pkg':pkg,'files':files}) return render_response(request, 'packages/files.html', {'pkg':pkg,'files':files})
def flaghelp(request): def flaghelp(request):
return render_template('packages/flaghelp.html', request) return render_response(request, 'packages/flaghelp.html')
def flag(request, pkgid): def flag(request, pkgid):
pkg = get_object_or_404(Package, id=pkgid) pkg = get_object_or_404(Package, id=pkgid)
context = {'pkg': pkg} context = {'pkg': pkg}
if request.POST.has_key('confirmemail'): if request.POST.has_key('confirmemail'):
email = request.POST['confirmemail'] email = request.POST['confirmemail']
if request.POST.has_key('usermessage'): if request.POST.has_key('usermessage'):
message = request.POST['usermessage'] message = request.POST['usermessage']
else: else:
message = None message = None
# validate # validate
errors = {} errors = {}
validate(errors, 'Email Address', email, validators.isValidEmail, False, request) validate(errors, 'Email Address', email, validators.isValidEmail, False, request)
if errors: if errors:
context['errors'] = errors context['errors'] = errors
return render_template('packages/flag.html', request, context) return render_response(request, 'packages/flag.html', context)
context['confirmemail'] = email context['confirmemail'] = email
pkg.needupdate = 1 pkg.needupdate = 1
pkg.save() pkg.save()
if pkg.maintainer_id > 0: if pkg.maintainer_id > 0:
# send notification email to the maintainer # send notification email to the maintainer
t = loader.get_template('packages/outofdate.txt') t = loader.get_template('packages/outofdate.txt')
c = Context({ c = Context({
'email': request.POST['confirmemail'], 'email': request.POST['confirmemail'],
'message': message, 'message': message,
'pkgname': pkg.pkgname, 'pkgname': pkg.pkgname,
'weburl': 'http://www.archlinux.org/packages/' + str(pkg.id) + '/' 'weburl': 'http://www.archlinux.org/packages/' + str(pkg.id) + '/'
}) })
send_mail('arch: Package [%s] marked out-of-date' % pkg.pkgname, send_mail('arch: Package [%s] marked out-of-date' % pkg.pkgname,
t.render(c), t.render(c),
'Arch Website Notification <nobody@archlinux.org>',