Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
Here is the JSON structure the api is expecting
{
"tag":"test", // or whatever the tag should be
"data":
{"
first_name":"firstname",
"last_name":"lastname",
"email":"email@email.com",
"password":"app123",
"date_created":"06/05/1990",
}
}
"""
from [path yo your api] import [ClassName] as cn
@csrf_exempt # CSRF disabled for testing purposes
def api_test(request):
if request.method == 'POST':
try:
c = connections['app-backend'].cursor() # This is the database name from your settings.py page
json_data = json.loads(request.body)
if json_data['tag'] == 'register':
return HttpResponse(cn().register(c, json_data))
elif json_data['tag'] == 'test':
return HttpResponse(cn().test())
else:
raise Http404
except Exception, e:
return HttpResponse(e) # You will want to log this exception
finally:
c.close()
elif request.method == 'GET':
c = connections['app-backend'].cursor()
try:
query = urllib.unquote(request.GET.get('json'))
json_query = ast.literal_eval(query)
json_final = json.dumps(json_query)
json_data = json.loads(json_final)
if json_data['tag'] == 'account_page':
return HttpResponse(vs().account_page(c, json_data))
elif json_data['tag'] == 'test':
return HttpResponse(vs().test())
else:
raise Http404
except Exception, e:
return HttpResponse(json_response = json.dumps({"success": 0, "errors": e}))
finally:
c.close(
else:
raise Http404 # I prefer to return 404 to prevent automated bots from fuzzing my site
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment