from captcha.conf import settings from captcha.models import CaptchaStore, get_safe_now from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse, NoReverseMatch from django.forms import ValidationError from django.forms.fields import CharField, MultiValueField from django.forms.widgets import TextInput, MultiWidget, HiddenInput from django.utils.translation import ugettext, ugettext_lazy from six import u class BaseCaptchaTextInput(MultiWidget): """ Base class for Captcha widgets """ def __init__(self, attrs=None): widgets = ( HiddenInput(attrs), TextInput(attrs), ) super(BaseCaptchaTextInput, self).__init__(widgets, attrs) def decompress(self, value): if value: return value.split(',') return [None, None] def fetch_captcha_store(self, name, value, attrs=None): """ Fetches a new CaptchaStore This has to be called inside render """ try: reverse('captcha-image', args=('dummy',)) except NoReverseMatch: raise ImproperlyConfigured('Make sure you\'ve included captcha.urls as explained in the INSTALLATION section on http://readthedocs.org/docs/django-simple-captcha/en/latest/usage.html#installation') key = CaptchaStore.generate_key() # these can be used by format_output and render self._value = [key, u('')] self._key = key self.id_ = self.build_attrs(attrs).get('id', None) def id_for_label(self, id_): if id_: return id_ + '_1' return id_ def image_url(self): return reverse('captcha-image', kwargs={'key': self._key}) def audio_url(self): return reverse('captcha-audio', kwargs={'key': self._key}) if settings.CAPTCHA_FLITE_PATH else None def refresh_url(self): return reverse('captcha-refresh') class CaptchaTextInput(BaseCaptchaTextInput): def __init__(self, attrs=None, **kwargs): self._args = kwargs self._args['output_format'] = self._args.get('output_format') or settings.CAPTCHA_OUTPUT_FORMAT for key in ('image', 'hidden_field', 'text_field'): if '%%(%s)s' % key not in self._args['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) def format_output(self, rendered_widgets): hidden_field, text_field = rendered_widgets text_field = text_field.replace('