-
Notifications
You must be signed in to change notification settings - Fork 22
Description
Similar to #5
Implemented wagtail-django-recaptcha using the first method in the documentation. Captcha field seems to function except I don't get the red "This field is required." when captcha field is left unchecked. I do have a custom serve method on my form page model but I have tried commenting this custom function out and it doesn't seem to make a difference.
I apologize in advance as this is probably operator error but I have gone through the madewithwagtail.org repo and can't seem to find any obvious cause.
Running wagtail 2.2
models.py code:
`class FormField(AbstractFormField):
page = ParentalKey('FormPage', on_delete=models.CASCADE, related_name='form_fields')
class FormPage(WagtailCaptchaEmailForm):
hero_image = models.ForeignKey(
'wagtailimages.Image', null=True, blank=True,
on_delete=models.SET_NULL, related_name='+'
)
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)
content_panels = AbstractEmailForm.content_panels + [
ImageChooserPanel('hero_image'),
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"),
]
def serve(self, request, *args, **kwargs):
print('CONTACT FORM SUBMISSION')
sku = ''
model = ''
if request.method == 'POST':
form = self.get_form(request.POST, page=self, user=request.user)
if form.is_valid():
print('Form Submitted')
self.process_form_submission(form)
# Update the original landing page context with other data
# landing_page_context = self.get_context(request)
context = self.get_context(request)
context['message'] = 'Success message has been sent.'
form = self.get_form(page=self, user=request.user)
context['form'] = form
return render(
request,
self.get_template(request),
context
)
else:
form = self.get_form(page=self, user=request.user)
sku = request.GET.get('sku', False)
model = request.GET.get('model', False)
# model = request.GET.get('model')
context = self.get_context(request)
context['form'] = form
context['sku'] = sku
context['model'] = model
return render(
request,
self.get_template(request),
context
)
def send_mail(self, form):
# `self` is the FormPage, `form` is the form's POST data on submit
# Email addresses are parsed from the FormPage's addresses field
addresses = [x.strip() for x in self.to_address.split(',')]
# Subject can be adjusted, be sure to include the form's defined subject field
submitted_date_str = date.today().strftime('%x')
subject = self.subject # add date to email subject
content = []
# Add a title (not part of original method)
content.append('{}: {}'.format('Form', self.title))
for field in form:
# add the value of each field as a new line
value = field.value()
if field.name == 'email':
email = value
print('EMAIL FIELD: ', value)
if isinstance(value, list):
value = ', '.join(value)
content.append('{}: {}'.format(field.label, value))
# Add a link to the form page
content.append('{}: {}'.format('Submitted Via', self.full_url))
# Add the date the form was submitted
content.append('{}: {}'.format('Submitted on', submitted_date_str))
# Content is joined with a new line to separate each text line
content = '\n'.join(content)
# wagtail.wagtailadmin.utils - send_mail function is called
# This function extends the Django default send_mail function
# send_mail(subject, content, addresses, self.from_address)
send_mail(subject, content, addresses, email)`
Dockerfile:
RUN pip install whitenoise
RUN pip install gunicorn
RUN pip install gevent
RUN pip install 'Django>=2.0,<2.1'
RUN pip install 'wagtail>=2.2,<2.3'
RUN pip install 'django-debug-toolbar'
RUN pip install 'psycopg2~=2.6'
RUN pip install 'elasticsearch==6.0.0,<6.3.0'
RUN pip install 'django_sendmail_backend'
RUN pip install 'django_compressor==2.2'
RUN pip install 'django-libsass==0.7'
RUN pip install 'django-recaptcha'
RUN pip install 'wagtail-django-recaptcha'
RUN pip install 'django-honeypot==0.7.0'
RUN pip install geopy
base.py installed_apps and recaptcha settings:
`INSTALLED_APPS = [
'home',
'products',
'search',
'pages',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.contrib.settings',
'wagtail.search',
'wagtail.admin',
'wagtail.contrib.modeladmin',
'wagtail.core',
'compressor',
'modelcluster',
'taggit',
'honeypot',
# 'antispam',
'captcha',
'wagtailcaptcha',
# 'antispam.akismet',
# 'antispam.honeypot',
# 'antispam.captcha',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
]
RECAPTCHA_PUBLIC_KEY = Actual public key
RECAPTCHA_PRIVATE_KEY = Actual private key
NOCAPTCHA = True
`