Skip to content

Commit

Permalink
feat: updated captcha optional til active (#4)
Browse files Browse the repository at this point in the history
* updated captcha optional til active

* fix fixed installed paramater

* fixed default margin between fields or set in dj-settings more options

* Update djangocms_form_builder/cms_plugins/legacy.py

indeed import not needed to change

Co-authored-by: Fabian Braun <fsbraun@gmx.de>

* fixed mb- added to frontend-spacing paramater

* isort and flake8 fixed

---------

Co-authored-by: Fabian Braun <fsbraun@gmx.de>
  • Loading branch information
svandeneertwegh and fsbraun authored Jan 28, 2023
1 parent d784a61 commit 0eea46e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions djangocms_form_builder/cms_plugins/ajax_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def get_fieldsets(self, request, obj=None):
("captcha_widget", "captcha_requirement", "captcha_config"),
block=None,
position=1,
blockname=_("reCaptcha"),
blockname=_("Captcha"),
blockattrs={}
if recaptcha.keys_available
else dict(
Expand Down Expand Up @@ -345,7 +345,7 @@ def traverse(instance):

# Add recaptcha field in necessary
if recaptcha.installed and self.instance.captcha_widget:
fields[recaptcha.field_name] = recaptcha.get_recaptcha_field(self.instance.captcha_config)
fields[recaptcha.field_name] = recaptcha.get_recaptcha_field(self.instance)

# Collect meta options for Meta class
meta_options = dict(form_name=self.instance.form_name)
Expand Down
4 changes: 2 additions & 2 deletions djangocms_form_builder/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ class Meta:
attributes = AttributesFormField()

captcha_widget = forms.ChoiceField(
label=_("reCaptcha widget"),
label=_("Captcha widget"),
required=False,
initial="v2-invisible" if recaptcha.installed else "",
choices=settings.EMPTY_CHOICE + recaptcha.RECAPTCHA_CHOICES,
choices=settings.EMPTY_CHOICE + recaptcha.CAPTCHA_CHOICES,
help_text=mark_safe_lazy(
_(
'Read more in the <a href="{link}" target="_blank">documentation</a>.'
Expand Down
4 changes: 2 additions & 2 deletions djangocms_form_builder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class Meta:
attributes = AttributesField()

captcha_widget = models.CharField(
verbose_name=_("reCaptcha widget"),
verbose_name=_("captcha widget"),
max_length=16,
blank=True,
default="v2-invisible" if recaptcha.installed else "",
choices=settings.EMPTY_CHOICE + recaptcha.RECAPTCHA_CHOICES,
choices=settings.EMPTY_CHOICE + recaptcha.CAPTCHA_CHOICES,
help_text=mark_safe_lazy(
_(
'Read more in the <a href="{link}" target="_blank">documentation</a>.'
Expand Down
65 changes: 34 additions & 31 deletions djangocms_form_builder/recaptcha.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
from django import forms
from django.apps import apps
from django.conf import settings
from django.utils.translation import gettext_lazy as _

from .helpers import coerce_decimal

CAPTCHA_WIDGETS = {}
CAPTCHA_FIELDS = {}
CAPTCHA_CHOICES = ()

if apps.is_installed("captcha"):
"""Soft dependency on django-captcha for reCaptchaField"""

from captcha.fields import ReCaptchaField # NOQA
from captcha.widgets import ( # NOQA
ReCaptchaV2Checkbox,
ReCaptchaV2Invisible,
ReCaptchaV3,
)
from captcha.widgets import ReCaptchaV2Checkbox, ReCaptchaV2Invisible # NOQA

installed = True
CAPTCHA_WIDGETS['v2-checkbox'] = ReCaptchaV2Checkbox
CAPTCHA_WIDGETS['v2-invisible'] = ReCaptchaV2Invisible

else:
ReCaptchaV2Invisible = forms.HiddenInput # NOQA
ReCaptchaV2Checkbox = forms.HiddenInput # NOQA
ReCaptchaV3 = forms.HiddenInput # NOQA
installed = False
CAPTCHA_FIELDS['v2-checkbox'] = ReCaptchaField
CAPTCHA_FIELDS['v2-invisible'] = ReCaptchaField

CAPTCHA_CHOICES += (
("v2-checkbox", f"reCaptcha - {_('v2 checkbox')}"),
("v2-invisible", f"reCaptcha - {_('v2 invisible')}"),
)

class ReCaptchaField: # NOQA
def __init__(self, *args, **kwargs):
pass
if apps.is_installed("hcaptcha"):
"""Soft dependency on django-hcaptcha for hcaptcha"""

from hcaptcha.fields import hCaptchaField # NOQA
from hcaptcha.widgets import hCaptchaWidget # NOQA

WIDGETS = {
"v2-checkbox": ReCaptchaV2Checkbox,
"v2-invisible": ReCaptchaV2Invisible,
}
CAPTCHA_FIELDS['hcaptcha'] = hCaptchaField
CAPTCHA_WIDGETS['hcaptcha'] = hCaptchaWidget

CAPTCHA_CHOICES += (
("hcaptcha", _("hCaptcha")),
)

RECAPTCHA_CHOICES = (
("v2-checkbox", _("v2 checkbox")),
("v2-invisible", _("v2 invisible")),
)
if len(CAPTCHA_CHOICES) > 0:
installed = True
else:
installed = False


def get_recaptcha_field(config):
def get_recaptcha_field(instance):
config = instance.captcha_config
widget_params = {
"attrs": {
key: value
Expand All @@ -57,14 +62,12 @@ def get_recaptcha_field(config):
if config.get("captcha_widget", "") == "v3":
widget_params["attrs"]["required_score"] = coerce_decimal(
config.get("captcha_requirement", 0.5)
)
) # installing recaptcha 3 ?
if not widget_params["api_params"]:
del widget_params["api_params"]
field = ReCaptchaField(
label="",
widget=WIDGETS[config.get("captcha_widget", "v2-invisible")](
**widget_params,
),
field = CAPTCHA_FIELDS[instance.captcha_widget](
widget=CAPTCHA_WIDGETS[instance.captcha_widget](**widget_params),
label=""
)
return field

Expand All @@ -74,5 +77,5 @@ def get_recaptcha_field(config):
and hasattr(settings, "RECAPTCHA_PRIVATE_KEY")
)

field_name = "recaptcha_field"
field_name = "captcha_field"
RECAPTCHA_PUBLIC_KEY = getattr(settings, "RECAPTCHA_PUBLIC_KEY", "")
11 changes: 9 additions & 2 deletions djangocms_form_builder/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
framework = getattr(django_settings, "DJANGOCMS_FRONTEND_FRAMEWORK", "bootstrap5")
theme = getattr(django_settings, "DJANGOCMS_FRONTEND_THEME", "djangocms_frontend")


SPACER_SIZE_CHOICES = (("mb-3", "Default"),)
DEFAULT_SPACER_SIZE_CHOICES = (("mb-3", "Default"),)
TAG_CHOICES = (("div", "div"),)
FORM_TEMPLATE = getattr(
django_settings,
Expand All @@ -29,6 +28,14 @@
theme_render_path = f"{theme}.frameworks.{framework}"
theme_forms_path = f"{theme}.forms"

if not getattr(django_settings, 'DJANGO_FORM_BUILDER_SPACER_CHOICES', False):
if not getattr(django_settings, 'DJANGOCMS_FRONTEND_SPACER_SIZES', False):
SPACER_SIZE_CHOICES = DEFAULT_SPACER_SIZE_CHOICES
else:
SPACER_SIZE_CHOICES = ((f"mb-{key}", value) for key, value in django_settings.DJANGOCMS_FRONTEND_SPACER_SIZES)
else:
SPACER_SIZE_CHOICES = django_settings.DJANGO_FORM_BUILDER_SPACER_CHOICES


def render_factory(cls, theme_module, render_module):
parents = tuple(
Expand Down

0 comments on commit 0eea46e

Please sign in to comment.