Skip to content

Commit

Permalink
final_fields in forms + captcha refresh generalized
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Apr 10, 2020
1 parent ab1f565 commit f9f1241
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
10 changes: 7 additions & 3 deletions django_form_builder/dynamic_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,11 @@ class CustomCaptchaComplexField(BaseCustomField):
is_complex = True

def __init__(self, *args, **kwargs):
# for auto-generated CaPTCHA
# you can pass in kwargs captcha_name and captcha_hidden_name
captcha_name = kwargs.pop("captcha_name") if kwargs.get("captcha_name") else ""
captcha_hidden_name = kwargs.pop("captcha_hidden_name") if kwargs.get("captcha_hidden_name") else ""

# CaPTCHA
parent_label = kwargs.get('label')

Expand All @@ -559,7 +564,7 @@ def __init__(self, *args, **kwargs):
logger.debug(text, value)

self.captcha_hidden.required = True
self.captcha_hidden.name = "{}_hidden_dyn".format(format_field_name(parent_label))
self.captcha_hidden.name = captcha_hidden_name or "{}_hidden_dyn".format(format_field_name(parent_label))
self.captcha_hidden.parent = self
self.captcha_hidden.label = ''

Expand All @@ -568,10 +573,9 @@ def __init__(self, *args, **kwargs):
self.captcha.define_value(custom_value=text,
hidden_field="id_{}".format(self.captcha_hidden.name))
self.captcha.label = parent_label
self.captcha.name = "{}_dyn".format(format_field_name(parent_label))
self.captcha.name = captcha_name or "{}_dyn".format(format_field_name(parent_label))
self.captcha.help_text = _("CaPTCHA: inserisci i caratteri/numeri raffigurati nell'immagine")
self.captcha.parent = self
# super().__init__(*args, **kwargs)

def get_fields(self):
return [self.captcha, self.captcha_hidden]
Expand Down
11 changes: 9 additions & 2 deletions django_form_builder/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BaseDynamicForm(forms.Form):
def __init__(self,
fields_source=dynamic_fields,
initial_fields={},
final_fields={},
constructor_dict={},
custom_params={},
ignore_format_field_name=False,
Expand All @@ -28,8 +29,11 @@ def __init__(self,
example:
"""
super().__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

# if initial fields are present
self.fields = initial_fields or self.fields

# Costruzione dinamica dei rimanenti fields del form
if constructor_dict:
constructor_dict = copy.deepcopy(constructor_dict)
Expand Down Expand Up @@ -75,6 +79,9 @@ def __init__(self,
if hasattr(field, 'choices'):
self.fields[name].choices = getattr(field, 'choices')

# if final fields are present
self.fields.update(final_fields)

def remove_not_compiled_fields(self):
"""
Rimuove da un form compilato tutti i campi non compilati
Expand Down Expand Up @@ -138,7 +145,7 @@ def clean(self, *args, **kwargs):
if type(field_obj) in (CaptchaField, CaptchaHiddenField):
self.data[field_name] = self.fields[field_name].widget.attrs['value']
# end CAPTCHA

for fname in self.fields:
field = self.fields[fname]
# formset is empty or not valid
Expand Down
2 changes: 1 addition & 1 deletion django_form_builder/templates/widgets/captcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<img class="captcha_dyn_form"
id="{{ widget.name }}_img"
src="data:image/png;base64,{{ image_b64 }}" />
<a onclick="refresh_captcha();">{% trans "Aggiorna" %}</a>
<a onclick="refresh_captcha_{{ widget.unique_id }}();">{% trans "Aggiorna" %}</a>
</div>
<br>
<input type="{{ widget.type }}"
Expand Down
6 changes: 5 additions & 1 deletion django_form_builder/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ def render(self, name, value, attrs=None, renderer=None):
context = self.get_context(name, value, attrs)
# captcha = get_captcha(value)
captcha = get_captcha(self.attrs['value'])
# javascript functions don't allow "-" char
unique_id = name.replace("-", "_")

context['image_b64'] = base64.b64encode(captcha.read()).decode()
context['widget']['value'] = ''
context['widget']['hidden_field'] = self.attrs['hidden_field']
context['widget']['unique_id'] = unique_id

inline_code = mark_safe(
'<script>'
'function refresh_captcha(){{'
'function refresh_captcha_{unique_id}(){{'
'$.get(location.href, function(data) {{'
'$("#{name}_img").attr("src",($(data).find("#{name}_img").attr("src")));'
'$("#{hidden_field}").val($(data).find("#{hidden_field}").val());'
'}});'
'}}'
'</script>'.format(name=name,
unique_id=unique_id,
hidden_field=self.attrs['hidden_field'])
)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='django-form-builder',
version='0.9.2',
version='0.9.3',
packages=find_packages(),
include_package_data=True,
license='BSD License',
Expand Down

0 comments on commit f9f1241

Please sign in to comment.