Moved admin media under media dir.
[archweb_dev-nj.git] / utils.py
blob1769471c8c09a9f66a2f657c82152721a52bdcbe
1 from django.core import validators
2 from django.template import RequestContext
3 from django.shortcuts import render_to_response
4 from django.http import HttpResponseRedirect
5 from string import *
6 import sgmllib
8 #from archweb_dev.packages.models import Maintainer
9 #from archweb_dev.settings import BADPRIVS_URL
10 #def is_maintainer(view_func, badprivs_url=BADPRIVS_URL):
11 # """
12 # Decorator for views that checks that the logged-in user has a corresponding
13 # record in the Maintainers table. If not, the user is forwarded to a
14 # "bad-privileges" page.
15 # """
16 # def _dec(view_func):
17 # def _checkuser(request, *args, **kwargs):
18 # try:
19 # m = Maintainer.objects.get(username=request.user.username)
20 # except Maintainer.DoesNotExist:
21 # return HttpResponseRedirect(badprivs_url)
22 # return view_func(request, *args, **kwargs)
24 # return _checkuser
25 # return _dec(view_func)
27 def render_template(template, request, context=None):
28 """
29 A shortcut to render_to_response with a RequestContext. Also includes
30 request.path in the context, so both 'path' and 'user' are accessible to
31 the template.
32 """
33 if context:
34 context['path'] = request.path
35 return render_to_response(template, context_instance=RequestContext(request, context))
36 else:
37 return render_to_response(template, context_instance=RequestContext(request))
39 def validate(errdict, fieldname, fieldval, validator, blankallowed, request):
40 """
41 A helper function that allows easy access to Django's validators without
42 going through a Manipulator object. Will return a dict of all triggered
43 errors.
44 """
45 if blankallowed and not fieldval:
46 return
47 alldata = ' '.join(request.POST.values()) + ' '.join(request.GET.values())
48 try:
49 validator(fieldval, alldata)
50 except validators.ValidationError, e:
51 if not errdict.has_key(fieldname): errdict[fieldname] = []
52 errdict[fieldname].append(e)
55 # XXX: unused right now, probably not needed
56 class Stripper(sgmllib.SGMLParser):
57 """Helper class to strip HTML tags"""
58 def __init__(self):
59 sgmllib.SGMLParser.__init__(self)
61 def strip(self, some_html):
62 """Strips all HTML tags and leading/trailing whitespace"""
63 self.theString = ""
64 self.feed(some_html)
65 self.close()
66 return self.theString
68 def handle_data(self, data):
69 self.theString += data