Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widget support the kwargs to add custom html attributes #353

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions flask_wtf/recaptcha/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
RECAPTCHA_SCRIPT = u'https://www.google.com/recaptcha/api.js'
RECAPTCHA_TEMPLATE = u'''
<script src='%s' async defer></script>
<div class="g-recaptcha" %s></div>
<div %s></div>
'''

__all__ = ['RecaptchaWidget']


class RecaptchaWidget(object):

def recaptcha_html(self, public_key):
def recaptcha_html(self, public_key, **kwargs):
html = current_app.config.get('RECAPTCHA_HTML')
if html:
return Markup(html)
Expand All @@ -27,7 +27,9 @@ def recaptcha_html(self, public_key):

attrs = current_app.config.get('RECAPTCHA_DATA_ATTRS', {})
attrs['sitekey'] = public_key
snippet = u' '.join([u'data-%s="%s"' % (k, attrs[k]) for k in attrs])
snippet_attrs = [u'%s="%s"' % (k, v) for k, v in kwargs.items()]
snippet_attrs += [u'data-%s="%s"' % (k, attrs[k]) for k in attrs]
snippet = u' '.join(snippet_attrs)
return Markup(RECAPTCHA_TEMPLATE % (script, snippet))

def __call__(self, field, error=None, **kwargs):
Expand All @@ -37,5 +39,8 @@ def __call__(self, field, error=None, **kwargs):
public_key = current_app.config['RECAPTCHA_PUBLIC_KEY']
except KeyError:
raise RuntimeError('RECAPTCHA_PUBLIC_KEY config not set')

return self.recaptcha_html(public_key)
defaults = (('id', field.id), ('class', 'g-recaptcha'))
for key, default_value in defaults:
if key not in kwargs:
kwargs[key] = default_value
return self.recaptcha_html(public_key, **kwargs)