Debian Codegraph @

captcha/helpers.py

raw symbols SCIP
1 import operator as operator_module
2 import random
3
4 from django.urls import reverse
5
6 from captcha.conf import settings
7
8
9 def math_challenge():
10 _ARITHMETIC_OPS = {
11 "+": operator_module.add,
12 "-": operator_module.sub,
13 "*": operator_module.mul,
14 }
15
16 operands = (random.randint(1, 10), random.randint(1, 10))
17 operator = random.choice(list(_ARITHMETIC_OPS.keys()))
18 if operands[0] < operands[1] and "-" == operator:
19 operands = (operands[1], operands[0])
20 challenge = "%d%s%d" % (operands[0], operator, operands[1])
21 result = _ARITHMETIC_OPS[operator](operands[0], operands[1])
22 return (
23 "{}=".format(challenge.replace("*", settings.CAPTCHA_MATH_CHALLENGE_OPERATOR)),
24 str(result),
25 )
26
27
28 def random_char_challenge():
29 chars, ret = "abcdefghijklmnopqrstuvwxyz", ""
30 for i in range(settings.CAPTCHA_LENGTH):
31 ret += random.choice(chars)
32 return ret.upper(), ret
33
34
35 def unicode_challenge():
36 chars, ret = "äàáëéèïíîöóòüúù", ""
37 for i in range(settings.CAPTCHA_LENGTH):
38 ret += random.choice(chars)
39 return ret.upper(), ret
40
41
42 def word_challenge():
43 fd = open(settings.CAPTCHA_WORDS_DICTIONARY, "r")
44 lines = fd.readlines()
45 fd.close()
46 while True:
47 word = random.choice(lines).strip()
48 if (
49 len(word) >= settings.CAPTCHA_DICTIONARY_MIN_LENGTH
50 and len(word) <= settings.CAPTCHA_DICTIONARY_MAX_LENGTH
51 ):
52 break
53 return word.upper(), word.lower()
54
55
56 def huge_words_and_punctuation_challenge():
57 "Yay, undocumneted. Mostly used to test Issue 39 - http://code.google.com/p/django-simple-captcha/issues/detail?id=39"
58 fd = open(settings.CAPTCHA_WORDS_DICTIONARY, "rb")
59 lines = fd.readlines()
60 fd.close()
61 word = ""
62 while True:
63 word1 = random.choice(lines).strip()
64 word2 = random.choice(lines).strip()
65 punct = random.choice(settings.CAPTCHA_PUNCTUATION)
66 word = "%s%s%s" % (word1, punct, word2)
67 if (
68 len(word) >= settings.CAPTCHA_DICTIONARY_MIN_LENGTH
69 and len(word) <= settings.CAPTCHA_DICTIONARY_MAX_LENGTH
70 ):
71 break
72 return word.upper(), word.lower()
73
74
75 def noise_arcs(draw, image):
76 size = image.size
77 draw.arc([-20, -20, size[0], 20], 0, 295, fill=settings.CAPTCHA_FOREGROUND_COLOR)
78 draw.line(
79 [-20, 20, size[0] + 20, size[1] - 20], fill=settings.CAPTCHA_FOREGROUND_COLOR
80 )
81 draw.line([-20, 0, size[0] + 20, size[1]], fill=settings.CAPTCHA_FOREGROUND_COLOR)
82 return draw
83
84
85 def noise_dots(draw, image):
86 size = image.size
87 for p in range(int(size[0] * size[1] * 0.1)):
88 draw.point(
89 (random.randint(0, size[0]), random.randint(0, size[1])),
90 fill=settings.CAPTCHA_FOREGROUND_COLOR,
91 )
92 return draw
93
94
95 def noise_null(draw, image):
96 return draw
97
98
99 def random_letter_color_challenge(idx, plaintext_captcha):
100 # Generate colorful but balanced RGB values
101 red = random.randint(64, 150)
102 green = random.randint(64, 150)
103 blue = random.randint(64, 150)
104
105 # Ensure at least one channel is higher to make it colorful
106 channels = [red, green, blue]
107 channels[0] = random.randint(150, 255)
108 random.shuffle(channels)
109
110 # Format the color as a hex string
111 return f"#{channels[0]:02X}{channels[1]:02X}{channels[2]:02X}"
112
113
114 def post_smooth(image):
115 from PIL import ImageFilter
116
117 return image.filter(ImageFilter.SMOOTH)
118
119
120 def captcha_image_url(key):
121 """Return url to image. Need for ajax refresh and, etc"""
122 return reverse("captcha-image", args=[key])
123
124
125 def captcha_audio_url(key):
126 """Return url to image. Need for ajax refresh and, etc"""
127 return reverse("captcha-audio", args=[key])