Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for rest_framework #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions captcha/serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from captcha.conf import settings
from captcha.models import CaptchaStore

from django.utils import timezone
import django
if django.VERSION >= (3, 0):
from django.utils.translation import gettext_lazy as ugettext_lazy
else:
from django.utils.translation import ugettext_lazy

from rest_framework.exceptions import ValidationError
from rest_framework import serializers
from rest_framework.fields import empty

'''
Support for rest_framework
Use CaptchaSerializer replace Serializer,
Use CaptchaModelSerializer replace ModelSerializer
'''

def captcha_validation(hashkey, code):
if not settings.CAPTCHA_GET_FROM_POOL:
CaptchaStore.remove_expired()
if settings.CAPTCHA_TEST_MODE and code == 'passed':
try:
CaptchaStore.objects.get(hashkey=hashkey).delete()
except CaptchaStore.DoesNotExist:
pass
else:
try:
CaptchaStore.objects.get(response=code, hashkey=hashkey, expiration__gt=timezone.now()).delete()
except CaptchaStore.DoesNotExist:
errors={}
errors['captcha'] = [ugettext_lazy("Invalid CAPTCHA")]
raise ValidationError(errors)


class CaptchaSerializer(serializers.Serializer):

captcha_hashkey = serializers.CharField(max_length=40, write_only=True, required=True)
captcha_code = serializers.CharField(max_length=32, write_only=True, required=True)

def run_validation(self, data=empty):
values = super().run_validation(data=data)
code, hashkey= values['captcha_code'].lower(), values['captcha_hashkey'].lower()
captcha_validation(hashkey, code)
return values


class CaptchaModelSerializer(serializers.ModelSerializer):

captcha_hashkey = serializers.CharField(max_length=40, write_only=True, required=True)
captcha_code = serializers.CharField(max_length=32, write_only=True, required=True)

def run_validation(self, data=empty):
values = super().run_validation(data=data)
code, hashkey= values['captcha_code'].lower(), values['captcha_hashkey'].lower()
captcha_validation(hashkey, code)
return values