Last active
August 29, 2015 14:17
-
-
Save JamesHovious/ffc9eef22857550beb3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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