| 1 | from rest_framework import serializers |
| 2 | from rest_framework.fields import empty |
| 3 | |
| 4 | from captcha.validators import captcha_validate |
| 5 | |
| 6 | |
| 7 | class CaptchaSerializer(serializers.Serializer): |
| 8 | """Serializer captcha code and captcha hashkey""" |
| 9 | |
| 10 | captcha_code = serializers.CharField(max_length=32, write_only=True, required=True) |
| 11 | captcha_hashkey = serializers.CharField( |
| 12 | max_length=40, write_only=True, required=True |
| 13 | ) |
| 14 | |
| 15 | def run_validation(self, data=empty): |
| 16 | values = super().run_validation(data=data) |
| 17 | captcha_validate(values["captcha_hashkey"], values["captcha_code"]) |
| 18 | return values |
| 19 | |
| 20 | |
| 21 | class CaptchaModelSerializer(serializers.ModelSerializer): |
| 22 | """Model serializer captcha code and captcha hashkey""" |
| 23 | |
| 24 | captcha_code = serializers.CharField(max_length=32, write_only=True, required=True) |
| 25 | captcha_hashkey = serializers.CharField( |
| 26 | max_length=40, write_only=True, required=True |
| 27 | ) |
| 28 | |
| 29 | def run_validation(self, data=empty): |
| 30 | values = super().run_validation(data=data) |
| 31 | captcha_validate(values["captcha_hashkey"], values["captcha_code"]) |
| 32 | return values |