Skip to content

Commit

Permalink
Merge pull request #15 from nickmoreton/release-1.0.0
Browse files Browse the repository at this point in the history
Support Wagtail 4
  • Loading branch information
nickmoreton authored Sep 10, 2022
2 parents 8002ee0 + 55ae8a1 commit 463befc
Show file tree
Hide file tree
Showing 52 changed files with 2,488 additions and 199 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
# E501: Line too long
# W503: line break before binary operator (superseded by W504 line break after binary operator)
ignore = E501,W503
exclude = migrations,node_modules
62 changes: 27 additions & 35 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,42 @@ on:
- main

jobs:
linting:
code-quality:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.10"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
fetch-depth: 0
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install Dependencies
run: pip install -e ".[testing]" -U pip setuptools wheel
- name: Flake8
run: flake8 wagtail_honeypot
- name: Isort
run: isort wagtail_honeypot --check
- name: Black
run: black wagtail_honeypot --check
python-version: ${{ matrix.python }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pre-commit
- name: Run pre-commit hooks
run: |
pre-commit install
pre-commit run --all-files
test-sqlite:
test-python:
runs-on: ubuntu-latest
needs: linting
strategy:
matrix:
python: ['3.7', '3.8']
django: ['3.0', '3.1']
wagtail: ['2.14', '2.15']
include:
- python: '3.9'
django: '3.2'
wagtail: '2.16'
- python: '3.10'
django: '4.0'
wagtail: '3.0'
python: ["3.7", "3.8", "3.9", "3.10"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v2
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
- name: Install Tox
run: python -m pip install tox
- name: Test
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox tox-gh-actions
- name: Test with tox
run: tox
env:
TOXENV: python${{ matrix.python }}-django${{ matrix.django }}-wagtail${{ matrix.wagtail }}-sqlite
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/node_modules
/test-static
/test-media
/sandbox/sandbox/settings/local.py
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8
- repo: https://github.com/jackdewinter/pymarkdown
rev: main
hooks:
- id: pymarkdown
args:
- --disable-rules
- line-length
- scan
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
## [1.0.0] - 2022-09-10
- [#15](https://github.com/nickmoreton/wagtail-honeypot/pull/15) Add Wagtail 3/4 support and improve developer documentation and tools

## [Yanked]
- [#14](https://github.com/nickmoreton/wagtail-honeypot/pull/14) Add Wagtail 3.0 support

## [0.2.0] - 2022-02-24
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include LICENSE *.rst *.txt *.md
graft wagtail_honeypot
global-exclude __pycache__
global-exclude *.py[co]
global-exclude sandbox
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
run:
@echo "Running sandbox..."
python sandbox/manage.py runserver 0:8000

mail:
@echo "Running sandbox mail..."
cp sandbox/sandbox/settings/local.py.example.py sandbox/sandbox/settings/local.py
docker run -p 8025:8025 -p 1025:1025 mailhog/mailhog

test:
python testmanage.py test

tox:
tox --skip-missing-interpreters
82 changes: 31 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Use this package to add optional honeypot protection to your Wagtail forms.

![Alt text](sample.jpg?raw=true "Title")

Honeypot protection is a way to trick bots into submitting data in fields that should remain empty. The package provides a text field that should remain empty and checks a time interval between the form being displayed and submitted. The default interval is 3 seconds. If the form is submitted before the interval expires the submission is ignored.

## How it works
Expand Down Expand Up @@ -44,16 +46,12 @@ def time_diff(value, interval):

You can provide your own `process_form_submission` method if you need an alternative behaviour.

## Installation
## Installation and setup

```bash
pip install wagtail-honeypot
```

## Wagtail Setup

### Settings

Add the package to your settings

```python
Expand All @@ -64,37 +62,9 @@ INSTALLED_APPS = [
]
```

### Honeypot Text Field

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

*You can change the text field name by adding the following to your settings.*

```python
HONEYPOT_NAME="foo"
```

### Honeypot Time Field
- A fully working minimal Wagtail site can inspected see [here](/sandbox/)

```html
<input type="hidden" name="whf_time" id="whf_time" data-whf_name="" tabindex="-1" autocomplete="off">
```

*You can change the time field name by adding the following to your settings.*

```python
HONEYPOT_TIME="bar"
```

*You can change the time interval by adding the following to your settings.*

```python
HONEYPOT_INTERVAL=1
```

### Honeypot Template Tag
### Add The Honeypot Template Tag to your form page template

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

Expand All @@ -108,29 +78,29 @@ To render the honeypot fields in your form page template use the provided templa
</form>
```

### Honeypot Model Mixin
### Use The Honeypot Model Mixin

The mixin will add a honeypot field to your form page model.
The mixin will add a honeypot enable/disable checkbox 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](https://docs.wagtail.org/en/stable/reference/contrib/forms/index.html) your form should look something like this...

Note: if you site uses Wagtail v3.0+ you may need to use the new import paths for fields and panels. [Wagtail release notes](https://docs.wagtail.org/en/stable/releases/3.0.html)

```python
# your imports here, see note ^^
# wagtail form imports here

class FormPage(HoneypotMixin):
# FormPage should inherit from HoneypotMixin

class FormPage(HoneypotMixin): # <-- add the mixin
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)

content_panels = AbstractEmailForm.content_panels + [
FieldPanel("intro", classname="full"),
FieldPanel("intro"),
InlinePanel("form_fields", label="Form fields"),
FieldPanel("thank_you_text", classname="full"),
FieldPanel("thank_you_text"),
MultiFieldPanel(
[
FieldRowPanel(
Expand All @@ -150,21 +120,28 @@ class FormPage(HoneypotMixin): # <-- add the mixin
[
ObjectList(content_panels, heading="Content"),
ObjectList(HoneypotMixin.honeypot_panels, heading="Honeypot"),
ObjectList(Page.promote_panels, heading="Promote"),
ObjectList(Page.settings_panels, heading="Settings", classname="settings"),
ObjectList(HoneypotMixin.promote_panels, heading="Promote"),
ObjectList(
HoneypotMixin.settings_panels, heading="Settings", classname="settings"
),
]
)

# OR add a the honeypot checkbox without the extra tab
content_panels = content_panels + HoneypotMixin.honeypot_panels
```

You'll need to run `makemigrations` and `migrate` here

**Create a form page and enable the Honeypot protection.**

### Hide the Honeypot field
## Hide the Honeypot field

View the newly created form page. 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.
You can try it out by submitting the form with the honeypot field set to any value. It won't save the form submission or send it as an email if you have enabled that in your form page.

#### Use CSS to hide the honeypot field
### Use CSS to hide the honeypot field

Add the following CSS style to your own site's CSS...

Expand All @@ -177,7 +154,7 @@ input[data-whf_name] {
}
```

#### Use Javascript to hide the honeypot field
### and/or Use Javascript to hide the honeypot field

```javascript
var whf_name = "whf_name";
Expand All @@ -195,11 +172,14 @@ When rendered, the fields will have the HTML attributes `tabindex="-1" autocompl

A more complete example is [form_page.html](wagtail_honeypot/templates/wagtail_honeypot_test/form_page.html) from the package test files.


## Versions

Wagtail honey pot can be used in environments:

- Python 3.7+
- Django 3.0+
- Wagtail 2.14+
- Wagtail 2.14+

## Contributions

Contributions or ideas to improve this package are welcome. [See Developer Docs](docs/developer.md)
61 changes: 61 additions & 0 deletions docs/developer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Configuration

Honeypot protection is a way to trick bots into submitting data in fields that should remain empty when submitted by a website visitor.

The package provides a text field that should remain empty. If the form is submitted with a value in this field the submission is ignored.

## Honeypot Text Field

This is how the default honeypot text field is rerendered.

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

You can change the text field name by adding the following to your settings.

```python
HONEYPOT_NAME = "your-field-name"
```

It also provides a time field and checks a time interval between the form being displayed and submitted.

The default interval is 3 seconds. If the form is submitted before the interval expires the submission is ignored.

### Honeypot Time Field

This is how the default honeypot time field is rerendered.

```html
<input type="hidden" name="whf_time" id="whf_time" data-whf_name="" tabindex="-1" autocomplete="off">
```

You can change the time field name by adding the following to your settings.

```python
HONEYPOT_TIME = "your-field-name"
```

You can change the time interval by adding the following to your settings.

```python
HONEYPOT_INTERVAL = 1 # seconds
```

## Development Setup

There is a [sandbox](../sandbox/) app provided that is a fully configured minimal setup using Wagtail v4.

It's already initialised with admin login details of Username: `admin` Password: `changeme`

To run the sandbox run `make run` and view the site at `http://localhost:8000` or add `/admin` to login.

You can see if emails are sent or not via the console.

### It's sometimes convenient to send emails via a SMTP server

If you have docker and docker-compose installed

- run `make mail` to spin up a Mailhog instance and simulate a `real` email inbox.
- view the Mailhog app in your browser at `http://localhost:8025`
- running this command will add a local.py file to settings in hte sandbox app with the correct EMAIL_BACKEND and credentials
Loading

0 comments on commit 463befc

Please sign in to comment.