Skip to content

Commit

Permalink
Merge pull request #3 from nickaigi/master
Browse files Browse the repository at this point in the history
pep8 cleanup
  • Loading branch information
Bruno Bernardino committed Jul 31, 2015
2 parents a80421e + 1ff1846 commit 5c21e67
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions django-project/views.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,79 @@
from django.http import HttpResponse
from django.http import HttpResponseServerError
from django.http import HttpResponseForbidden
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render_to_response, redirect

from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import BaseRenderer
from rest_framework.parsers import JSONParser

from visualcaptcha import Captcha, Session


def index(request):
return render_to_response('index.html')


def start(request, howMany):
visualCaptcha = Captcha( Session(request.session) )
visualCaptcha = Captcha(Session(request.session))

visualCaptcha.generate( howMany )
jsonFrontendData = JSONRenderer().render( visualCaptcha.getFrontendData() )
response = HttpResponse( content = jsonFrontendData )
visualCaptcha.generate(howMany)
jsonFrontendData = JSONRenderer().render(visualCaptcha.getFrontendData())
response = HttpResponse(content=jsonFrontendData)
response['Access-Control-Allow-Origin'] = '*'

return response


def getImage(request, index):
visualCaptcha = Captcha( Session(request.session) )
visualCaptcha = Captcha(Session(request.session))

headers = {}
result = visualCaptcha.streamImage( headers, index, request.GET.get('retina') )
result = visualCaptcha.streamImage(
headers, index, request.GET.get('retina'))

if ( result == False ):
return HttpResponse( result, headers, 404 )
if result is False:
return HttpResponse(result, headers, 404)

return HttpResponse( result, headers )
return HttpResponse(result, headers)

def getAudio(request, audioType = 'mp3'):
visualCaptcha = Captcha( Session(request.session) )

def getAudio(request, audioType='mp3'):
visualCaptcha = Captcha(Session(request.session))

headers = {}
result = visualCaptcha.streamAudio( headers, audioType )
result = visualCaptcha.streamAudio(headers, audioType)

if result is False:
return HttpResponse(result, headers, 404)

if ( result == False ):
return HttpResponse( result, headers, 404 )
return HttpResponse(result, headers)

return HttpResponse( result, headers )

@csrf_exempt
def trySubmission(request):
visualCaptcha = Captcha( Session(request.session) )
visualCaptcha = Captcha(Session(request.session))

frontendData = visualCaptcha.getFrontendData();
frontendData = visualCaptcha.getFrontendData()

# If an image field name was submitted, try to validate it
if ( request.POST.get(frontendData['imageFieldName'],None) != None ):
if ( visualCaptcha.validateImage(request.POST[frontendData['imageFieldName'] ]) ):
response = HttpResponse(status = 200)
return redirect( '/?status=validImage' )
if request.POST.get(frontendData['imageFieldName'], None) is not None:
if visualCaptcha.validateImage(
request.POST[frontendData['imageFieldName']]):
response = HttpResponse(status=200)
return redirect('/?status=validImage')
else:
response = HttpResponse(status = 403)
return redirect( '/?status=failedImage' )
elif ( request.POST.get(frontendData['audioFieldName'],None) != None ):
# We set lowercase to allow case-insensitivity, but it's actually optional
if ( visualCaptcha.validateAudio(request.POST[frontendData['audioFieldName']].lower()) ):
response = HttpResponse(status = 200)
return redirect( '/?status=validAudio' )
response = HttpResponse(status=403)
return redirect('/?status=failedImage')
elif request.POST.get(frontendData['audioFieldName'], None) is not None:
# We set lowercase to allow case-insensitivity , but it's
# actually optional
if visualCaptcha.validateAudio(
request.POST[frontendData['audioFieldName']].lower()):
response = HttpResponse(status=200)
return redirect('/?status=validAudio')
else:
response = HttpResponse(status = 403)
return redirect( '/?status=failedAudio' )
response = HttpResponse(status=403)
return redirect('/?status=failedAudio')
else:
response = HttpResponse(status = 500)
return redirect( '/?status=failedPost' )
response = HttpResponse(status=500)
return redirect('/?status=failedPost')

return response

0 comments on commit 5c21e67

Please sign in to comment.