Skip to content

Commit

Permalink
Some cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mbi committed Oct 3, 2023
1 parent 9536d86 commit a6562d2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
37 changes: 8 additions & 29 deletions captcha/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BaseCaptchaTextInput(MultiWidget):

def __init__(self, attrs=None):
widgets = (CaptchaHiddenInput(attrs), CaptchaAnswerInput(attrs))
super(BaseCaptchaTextInput, self).__init__(widgets, attrs)
super().__init__(widgets, attrs)

def decompress(self, value):
if value:
Expand Down Expand Up @@ -103,42 +103,23 @@ def __init__(
):
self.id_prefix = id_prefix
self.generator = generator

self.output_format = None

if self.output_format:
for key in ("image", "hidden_field", "text_field"):
if "%%(%s)s" % key not in self.output_format:
raise ImproperlyConfigured(
"All of %s must be present in your CAPTCHA_OUTPUT_FORMAT setting. Could not find %s"
% (
", ".join(
[
"%%(%s)s" % k
for k in ("image", "hidden_field", "text_field")
]
),
"%%(%s)s" % key,
)
)

super(CaptchaTextInput, self).__init__(attrs)
super().__init__(attrs)

def build_attrs(self, *args, **kwargs):
ret = super(CaptchaTextInput, self).build_attrs(*args, **kwargs)
ret = super().build_attrs(*args, **kwargs)
if self.id_prefix and "id" in ret:
ret["id"] = "%s_%s" % (self.id_prefix, ret["id"])
return ret

def id_for_label(self, id_):
ret = super(CaptchaTextInput, self).id_for_label(id_)
ret = super().id_for_label(id_)
if self.id_prefix and "id" in ret:
ret = "%s_%s" % (self.id_prefix, ret)
return ret

def get_context(self, name, value, attrs):
"""Add captcha specific variables to context."""
context = super(CaptchaTextInput, self).get_context(name, value, attrs)
context = super().get_context(name, value, attrs)
context["image"] = self.image_url()
context["audio"] = self.audio_url()
return context
Expand All @@ -159,9 +140,7 @@ def render(self, name, value, attrs=None, renderer=None):
extra_kwargs = {}
extra_kwargs["renderer"] = renderer

return super(CaptchaTextInput, self).render(
name, self._value, attrs=attrs, **extra_kwargs
)
return super().render(name, self._value, attrs=attrs, **extra_kwargs)


class CaptchaField(MultiValueField):
Expand All @@ -184,15 +163,15 @@ def __init__(self, *args, **kwargs):
),
)

super(CaptchaField, self).__init__(fields, *args, **kwargs)
super().__init__(fields, *args, **kwargs)

def compress(self, data_list):
if data_list:
return ",".join(data_list)
return None

def clean(self, value):
super(CaptchaField, self).clean(value)
super().clean(value)
response, value[1] = (value[1] or "").strip().lower(), ""
if not settings.CAPTCHA_GET_FROM_POOL:
CaptchaStore.remove_expired()
Expand Down
2 changes: 1 addition & 1 deletion captcha/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def save(self, *args, **kwargs):
).encode("utf8")
self.hashkey = hashlib.sha1(key_).hexdigest()
del key_
super(CaptchaStore, self).save(*args, **kwargs)
super().save(*args, **kwargs)

def __str__(self):
return self.challenge
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ setenv =
deps =
django42: Django>=4.2a,<4.3
django50: Django>=5.0a,<5.1
py{39,310}-django{42,50}: python3-memcached
py{39,310,311}-django{42,50}: python3-memcached
jinja2

extras =
Expand Down

0 comments on commit a6562d2

Please sign in to comment.