Skip to main content

wWagtail honeypot package

Project description

Wagtail Honeypot

Use this package to add a optional honeypot field to your wagtail forms.

How it works

When the Wagtail Form is submitted, and the honeypot field is enabled the honeypot field & value is in the POST data.

If the field value contains any text the form is not processed...

# process_form_submission is overriding the function in AbstractEmailForm

def process_form_submission(self, form):
    honeypot_name = getattr(settings, "HONEYPOT_NAME", "whf_name")
    if honeypot_name in form.data and not form.data[honeypot_name]:
        # the submission is returned only if the honeypot field is empty
        return super().process_form_submission(form)

The function above will block the notification emails that are sent and the saving of the form data. You can provide your own process_form_submission method if you need an alternative behaviour.

Installation

pip install wagtail-honeypot

Wagtail Setup

The default honey pot field name is whf_name. The name is used when the field is rendered...

<input type="text" name="whf_name" id="whf_name" data-whf_name="" tabindex="-1" autocomplete="off">

If you would like to changed the field name, you can do so by adding the following to your settings.

HONEYPOT_NAME="foo"

The field would then be rendered...

<input type="text" name="foo" id="foo" data-foo="" tabindex="-1" autocomplete="off">

The name of the field is used to identify the honey pot field when the form is submitted.

Honeypot Template Tag

To render the honeypot field in your form page template use the provided template tag.

{% load honeypot_tags %}  # load the template tag

<form>
...
{% honeypot_field %}  # add the honeypot field to your form
...
</form>

Honeypot Model Mixin

The mixin will add a honeypot field to your form page model.

honeypot = models.BooleanField(default=False, verbose_name="Honeypot Enabled")

It also adds a form panel you can use.

If you follow the official Wagtail docs for the Form Builder your form should look something like this...

class FormPage(HoneypotMixin):  # use HoneypotMixin in pace of AbstractEmailForm
    intro = RichTextField(blank=True)
    thank_you_text = RichTextField(blank=True)

    content_panels = HoneypotMixin.content_panels + [
        FieldPanel('intro', classname="full"),
        InlinePanel('form_fields', label="Form fields"),
        FieldPanel('thank_you_text', classname="full"),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel('subject'),
        ], "Email"),
    ]

    edit_handler = TabbedInterface([
        ObjectList(content_panels, heading='Content'),
        ObjectList(HoneypotMixin.honeypot_panels, heading='Honeypot'),
        ObjectList(Page.promote_panels, heading='Promote'),  # a tab for the honeypot settings
        ObjectList(Page.settings_panels, heading='Settings', classname="settings"),
    ])

Hide the Honeypot field

If you create a form now you will see that the honeypot field is visible and could be submitted with any value. That would block the form submission and that's how it should work.

You can try it out by submitting the form with the honeypot field set to any value. It won't save the form submission.

Use css to hide the honeypot field

Add the following css style to your own sites css...

input[data-whf_name] {
    position: absolute;
    top: 0;
    left: 0;
    margin-left: 100vw;
}

Use javascript to hide the honeypot field

var whf_name = "whf_name";
var data_whf_name = "[data-" + whf_name + "]";

document.querySelectorAll(data_whf_name).forEach(function(el) {
    el.classList.add(whf_name);
    el.setAttribute("style", "position: absolute;top: 0;left: 0;margin-left: 100%;");
});

The end result is the field should be visibly hidden and not be available to receive any value form a site visitor. When rendered, the input field will have the html attributes tabindex="-1" autocomplete="off" to prevent a site visitor from using the tab key to move to the field and disable any autocomplete browser feature.

A more complete example is form_page.html from the package test files.

Todo

  • add a time check for form submission

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail-honeypot-0.1.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

wagtail_honeypot-0.1.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file wagtail-honeypot-0.1.0.tar.gz.

File metadata

  • Download URL: wagtail-honeypot-0.1.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for wagtail-honeypot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 73db13828a665921b66f839b3ed9f1c94a0851f013948899609aa259a8407887
MD5 5e95af9decca86e90f8ab952b128e0e4
BLAKE2b-256 fadb61d33bdeef04a322bba3bdb3c26a0ff2b8c2db68336c0f1959dd60bbfd4d

See more details on using hashes here.

File details

Details for the file wagtail_honeypot-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wagtail_honeypot-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for wagtail_honeypot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e46d69e3dd8c7cf0a006d8a8b0b4af6fe13c123c54f75d24d958f1d18c1474eb
MD5 b412c90745dc1ea36d7a999ab6a33c1a
BLAKE2b-256 241db3cdcd1ccb4bed0a2c1e3100a00a4f707764bdd1e7207400488f3e1f0d64

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page