Skip to content

Commit

Permalink
old py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Ogle committed Nov 2, 2010
1 parent 272bb84 commit fc3d5ec
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions jsdemo/quaid.py
Expand Up @@ -2,7 +2,7 @@
from django.http import HttpResponse
from django.utils import simplejson as json
from django import forms
from decorator import decorator
#from decorator import decorator

class ClientException(Exception):
"""
Expand All @@ -27,46 +27,49 @@ def validate(form):
return form.cleaned_data
raise FormException(form.errors)

def client_async(func, *args, **kwargs):
def client_async(func):
"""
A decorator to interface with async client requests,
including returning controller exceptions.
"""
STATUS_FAIL = 'fail'
STATUS_SUCCESS = 'success'
status = 200
# run the function
try:
res = func(*args)
if res == True:
res = {u'status': STATUS_SUCCESS}
elif res == False:
res = {u'status': STATUS_FAIL}
else:
res = {
u'status': STATUS_SUCCESS,
u'results': res
}

except ClientException, (e):
status = 400
def new(*args, **kwargs):

res = {'status': STATUS_FAIL}
STATUS_FAIL = 'fail'
STATUS_SUCCESS = 'success'
status = 200
# run the function
try:
res = func(*args, **kwargs)
if res == True:
res = {u'status': STATUS_SUCCESS}
elif res == False:
res = {u'status': STATUS_FAIL}
else:
res = {
u'status': STATUS_SUCCESS,
u'results': res
}

res['errors'] = [{'value': None, 'message': e.msg, 'code': e.code}]
if e.field:
res['errors'][0]['field'] = e.field

except FormException, (e):
status = 400
res = {'status': STATUS_FAIL}
except ClientException, (e):
status = 400

res = {'status': STATUS_FAIL}

res['errors'] = [{'value': None, 'message': e.msg, 'code': e.code}]
if e.field:
res['errors'][0]['field'] = e.field

except FormException, (e):
status = 400
res = {'status': STATUS_FAIL}

res['errors'] = []
for k, v in e.errors.items():
res['errors'].extend([{'value': None, 'message': unicode(msg), 'field': k} for msg in v])

res['errors'] = []
for k, v in e.errors.items():
res['errors'].extend([{'value': None, 'message': unicode(msg), 'field': k} for msg in v])
return HttpResponse(json.dumps(res), status=status, mimetype='application/json')

return HttpResponse(json.dumps(res), status=status, mimetype='application/json')
client_async = decorator(client_async)
return new

def index(req):
return render_to_response('quaid.html', {'form':None})
Expand All @@ -83,7 +86,6 @@ def util(req):
def validation(req):
return render_to_response('quaid_validation.html', {'form':None})

@client_async
def example1(req):

import time
Expand All @@ -109,8 +111,8 @@ class ExampleForm(forms.Form):
'programmers': programmers,
'programmers_message': programmers == 0 and 'Yeah, that\'s a hardware problem' or 'Sadly, no.',
}
example1 = client_async(example1)

@client_async
def asyncload1(req):

import time
Expand All @@ -124,8 +126,8 @@ def asyncload1(req):
'b': '%s THIS IS B! zOMG!' % num
}
return types.get(t) or 'WTF y0?'
asyncload1 = client_async(asyncload1)

@client_async
def asyncload2(req):

num = int(req.POST.get('num'))
Expand All @@ -134,8 +136,8 @@ def asyncload2(req):
time.sleep(num % 2)

return str(num)
@client_async
asyncload2 = client_async(asyncload2)

def asyncform(req):

name = req.POST.get('name')
Expand All @@ -148,6 +150,7 @@ def asyncform(req):
raise ClientException('This is just an error of some kind.')

return name
asyncform = client_async(asyncform)

def error_example(req):
type = req.POST.get('errtype')
Expand Down Expand Up @@ -198,7 +201,6 @@ def error_example(req):

return HttpResponse(json.dumps(res), status=status)

@client_async
def validation_example(req):

class ExampleForm(forms.Form):
Expand All @@ -213,3 +215,4 @@ class ExampleForm(forms.Form):
return {
'email': email,
}
validation_example = client_async(validation_example)

0 comments on commit fc3d5ec

Please sign in to comment.