mbi / django-simple-captcha

Django Simple Captcha is an extremely simple, yet highly customizable Django application to add captcha images to any Django form.
http://django-simple-captcha.readthedocs.io/en/latest/
MIT License
1.38k stars 324 forks source link

No 'field_template' arg for CaptchaTextInput #119

Open Antonboom opened 7 years ago

Antonboom commented 7 years ago

Hello! Why do you take a custom template from the arguments, but render the template from the settings anyway?

class CaptchaTextInput(BaseCaptchaTextInput):
    def __init__(self, attrs=None, **kwargs):
        self._args = kwargs
        self._args['field_template'] = self._args.get('field_template') or settings.CAPTCHA_FIELD_TEMPLATE

    def format_output(self, rendered_widgets):
        hidden_field, text_field = rendered_widgets    # <---- Unused variables

        if self._args['output_format']:
            return self._args['output_format'] % {...}

        elif self._args['field_template']:
            context = {
                'image': mark_safe(self.image_and_audio),
                'hidden_field': mark_safe(self.hidden_field),
                'text_field': mark_safe(self.text_field)
            }
            return render_to_string(settings.CAPTCHA_FIELD_TEMPLATE, context)    # <---- This line

In addition, it would also be nice to take from the arguments.

    def render(self, name, value, attrs=None):
        self.fetch_captcha_store(name, value, attrs, self._args.get('generator'))

        context = {...}
        if settings.CAPTCHA_FLITE_PATH:    # <-------- self._args
            context.update({'audio': self.audio_url()})

        self.image_and_audio = render_to_string(settings.CAPTCHA_IMAGE_TEMPLATE, context)     # <-------- self._args
        self.hidden_field = render_to_string(settings.CAPTCHA_HIDDEN_FIELD_TEMPLATE, context)     # <-------- self._args
        self.text_field = render_to_string(settings.CAPTCHA_TEXT_FIELD_TEMPLATE, context)     # <-------- self._args

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

This will not allow you to bind all the captcha in the project to the same settings.

ziima commented 6 years ago

field_template fixed in #139.

ziima commented 6 years ago

_args removed in #133.