From 39a548fd2629f3b6383990264b2e331b3aea99fb Mon Sep 17 00:00:00 2001 From: eliott Date: Sat, 3 Nov 2007 03:45:10 -0400 Subject: Initial import for public release... Special Note Prior to git import, approx 90% of the code was done by Judd Vinet. Thanks Judd! --- utils.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 utils.py (limited to 'utils.py') diff --git a/utils.py b/utils.py new file mode 100644 index 00000000..7848fceb --- /dev/null +++ b/utils.py @@ -0,0 +1,69 @@ +from django.core import validators +from django.template import RequestContext +from django.shortcuts import render_to_response +from django.http import HttpResponseRedirect +from string import * +import sgmllib + +#from archlinux.packages.models import Maintainer +#from archlinux.settings import BADPRIVS_URL +#def is_maintainer(view_func, badprivs_url=BADPRIVS_URL): +# """ +# Decorator for views that checks that the logged-in user has a corresponding +# record in the Maintainers table. If not, the user is forwarded to a +# "bad-privileges" page. +# """ +# def _dec(view_func): +# def _checkuser(request, *args, **kwargs): +# try: +# m = Maintainer.objects.get(username=request.user.username) +# except Maintainer.DoesNotExist: +# return HttpResponseRedirect(badprivs_url) +# return view_func(request, *args, **kwargs) +# +# return _checkuser +# return _dec(view_func) + +def render_template(template, request, context=None): + """ + A shortcut to render_to_response with a RequestContext. Also includes + request.path in the context, so both 'path' and 'user' are accessible to + the template. + """ + if context: + context['path'] = request.path + return render_to_response(template, context_instance=RequestContext(request, context)) + else: + return render_to_response(template, context_instance=RequestContext(request)) + +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) + + +# XXX: unused right now, probably not needed +class Stripper(sgmllib.SGMLParser): + """Helper class to strip HTML tags""" + def __init__(self): + sgmllib.SGMLParser.__init__(self) + + def strip(self, some_html): + """Strips all HTML tags and leading/trailing whitespace""" + self.theString = "" + self.feed(some_html) + self.close() + return self.theString + + def handle_data(self, data): + self.theString += data -- cgit v1.2.3-2-g168b