File: serializers.py

package info (click to toggle)
django-simple-captcha 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 656 kB
  • sloc: python: 1,661; makefile: 103; sh: 21
file content (32 lines) | stat: -rw-r--r-- 1,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from rest_framework import serializers
from rest_framework.fields import empty

from captcha.validators import captcha_validate


class CaptchaSerializer(serializers.Serializer):
    """Serializer captcha code and captcha hashkey"""

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

    def run_validation(self, data=empty):
        values = super().run_validation(data=data)
        captcha_validate(values["captcha_hashkey"], values["captcha_code"])
        return values


class CaptchaModelSerializer(serializers.ModelSerializer):
    """Model serializer captcha code and captcha hashkey"""

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

    def run_validation(self, data=empty):
        values = super().run_validation(data=data)
        captcha_validate(values["captcha_hashkey"], values["captcha_code"])
        return values