showing multi etymology now, some toggle is there to limit things.
[worddb.git] / utils.py
blob6cfa03f2168b675c140eef63a04b86f91b530837
1 # imports # {{{
2 from django.utils import simplejson
3 from django.http import HttpResponse, Http404, HttpResponseServerError
4 from django.http import HttpResponseRedirect
5 from django.utils.functional import Promise
6 from django.utils.translation import force_unicode
7 from django.utils.simplejson import JSONEncoder
8 from django.conf import settings
9 from django import forms
10 # }}}
12 # JSONResponse # {{{
13 class LazyEncoder(simplejson.JSONEncoder):
14 def default(self, o):
15 if isinstance(o, Promise):
16 return force_unicode(o)
17 else:
18 return super(LazyEncoder, self).default(o)
20 class JSONResponse(HttpResponse):
21 def __init__(self, data):
22 print data
23 HttpResponse.__init__(
24 self, content=simplejson.dumps(data, cls=LazyEncoder),
25 #mimetype="text/html",
27 # }}}
29 # ajax_form_handler # {{{
30 def ajax_form_handler(
31 request, form_cls, require_login=True, allow_get=settings.DEBUG
33 if require_login and not request.user.is_authenticated():
34 raise Http404("login required")
35 if not allow_get and request.method != "POST":
36 raise Http404("only post allowed")
37 if isinstance(form_cls, basestring):
38 # can take form_cls of the form: "worddb.app.forms.FormName"
39 from django.core.urlresolvers import get_mod_func
40 mod_name, form_name = get_mod_func(form_cls)
41 form_cls = getattr(__import__(mod_name, {}, {}, ['']), form_name)
42 form = form_cls(request, request.REQUEST)
43 if form.is_valid():
44 response = form.save()
45 if request.is_ajax() or request.REQUEST.get("is_ajax"):
46 return JSONResponse({ 'success': True, 'response': response })
47 else:
48 return HttpResponseRedirect(request.META.get("HTTP_REFERER", ""))
49 return JSONResponse({ 'success': False, 'errors': form.errors })
50 # }}}
52 # ajax_validator # {{{
53 def ajax_validator(request, form_cls):
54 if isinstance(form_cls, basestring):
55 from django.core.urlresolvers import get_mod_func
56 mod_name, form_name = get_mod_func(form_cls)
57 form_cls = getattr(__import__(mod_name, {}, {}, ['']), form_name)
58 form = form_cls(request.POST)
59 if "field" in request.GET:
60 errors = form.errors.get(request.GET["field"])
61 if errors: errors = errors.as_text()
62 else:
63 errors = form.errors
64 return JSONResponse(
65 { "errors": errors, "valid": not errors }
67 # }}}
69 # wf # {{{
70 def wf(form):
71 return "%s%s%s" % (
72 settings.WORD_FORM_DELIMITER, form, settings.WORD_FORM_DELIMITER
74 # }}}
76 # AJAXSimpleExceptionResponse www.djangosnippets.org/snippets/650/ # {{{
77 class AJAXSimpleExceptionResponse:
78 def process_exception(self, request, exception):
79 import sys, traceback
80 (exc_type, exc_info, tb) = sys.exc_info()
81 response = "%s\n" % exc_type.__name__
82 response += "%s\n\n" % exc_info
83 response += "TRACEBACK:\n"
84 for tb in traceback.format_tb(tb):
85 response += "%s\n" % tb
86 print response
87 if not settings.DEBUG: return
88 if not request.is_ajax(): return
89 return HttpResponseServerError(response)
90 # }}}
92 # no_new_lines # {{{
93 def no_new_lines(field):
94 def validator(self):
95 if "xx" in self.cleaned_data.get(field, ""):
96 raise forms.ValidationError("New line not allowed")
97 return self.cleaned_data.get(field)
98 return validator
99 # }}}