Porównaj commity

...

4 Commity

Autor SHA1 Wiadomość Data
Jaap Joris Vens 9588d7ac72 Bump version number 2022-12-25 01:16:09 +01:00
Jaap Joris Vens 1fda2f40c1 Spam-resistant contact form
Submitting the form now returns a 302 mailto: redirect, which should
open the user's email client with a pre-composed email. In case it
doesn't work, the example form view now renders text content where
{{ section.href }} should be mentioned as an alternative.

Of course, publishing an email address on the internet will still lead
to spam, but at least Django won't be the one sending it.
2022-12-25 01:10:41 +01:00
Jaap Joris Vens 347ee7ac3b Upgrade pre-commit hooks 2022-12-25 01:08:42 +01:00
Jaap Joris Vens ac49478bbb Re-introduce section IDs
We may have had these before, and they may have been removed because
of concerns that IDs must be unique that non-unique IDs would be
caught by Django-Tidy. Or maybe not, I can't remember.
2022-12-25 00:25:13 +01:00
10 zmienionych plików z 36 dodań i 33 usunięć

Wyświetl plik

@ -1,28 +1,28 @@
repos:
- repo: https://github.com/rtts/djhtml
rev: v1.4.11
rev: v1.5.2
hooks:
- id: djhtml
- repo: https://github.com/myint/autoflake
rev: v1.4
rev: v2.0.0
hooks:
- id: autoflake
args: ['--in-place', '--remove-all-unused-imports']
- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.11.4
hooks:
- id: isort
args: ['--resolve-all-configs', '--line-length', '88']
- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.12.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
rev: 6.0.0
hooks:
- id: flake8

Wyświetl plik

@ -1,2 +1,2 @@
__version__ = "1.0.7"
__version__ = "1.1.0"
default_app_config = "cms.apps.CmsConfig"

Wyświetl plik

@ -1,6 +1,7 @@
from urllib.parse import quote
from django import forms
from django.conf import settings
from django.core.mail import EmailMessage
from django.utils.translation import gettext_lazy as _
from . import registry
@ -112,24 +113,19 @@ class SectionForm(forms.ModelForm):
class ContactForm(forms.Form):
sender = forms.EmailField(label=_("Your email address"))
spam_protection = forms.CharField(label=_("Your message"), widget=forms.Textarea())
message = forms.CharField(
label=_("Your message"), widget=forms.Textarea(), initial="Hi there!"
"""
Spam-resistant contact form.
"""
body = forms.CharField(
label=_("Your message"), widget=forms.Textarea(), required=False
)
def save(self):
body = self.cleaned_data.get("spam_protection")
if len(body.split()) < 7:
return
spamcheck = self.cleaned_data.get("message")
if spamcheck != "Hi there!":
return
def save(self, address):
"""
Return a mailto: link.
"""
email = EmailMessage(
to=settings.DEFAULT_TO_EMAIL,
body=body,
subject=_("Contact form"),
headers={"Reply-To": self.cleaned_data.get("sender")},
)
email.send()
subject = quote(settings.CONTACT_FORM_EMAIL_SUBJECT, safe="")
body = quote(self.cleaned_data.get("body"), safe="")
return f"mailto:{address}?subject={subject}&body={body}"

Wyświetl plik

@ -107,6 +107,7 @@ class PageView(detail.DetailView):
view = registry.get_view(section, request)
form = view.get_form(method="post")
if form.is_valid():
view.object = section
return view.form_valid(form)
section.invalid_form = form

Wyświetl plik

@ -7,9 +7,9 @@ PROJECT_NAME = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
DEBUG = "runserver" in sys.argv
KEYFILE = f"/tmp/{PROJECT_NAME}.secret"
ADMINS = [("JJ Vens", "jj@rtts.eu")]
ADMINS = [("Jaap Joris Vens", "jj@rtts.eu")]
DEFAULT_FROM_EMAIL = "noreply@rtts.eu"
DEFAULT_TO_EMAIL = ["jj@rtts.eu"]
CONTACT_FORM_EMAIL_SUBJECT = "Contact form"
ALLOWED_HOSTS = ["*"]
ROOT_URLCONF = PROJECT_NAME + ".urls"
WSGI_APPLICATION = PROJECT_NAME + ".wsgi.application"

Wyświetl plik

@ -1,5 +1,6 @@
{% load i18n cms %}
<section class="contact">
<section class="contact" id="{{section.title|slugify}}">
<div class="wrapper">
<div class="title">
<h1>{{section.title}}</h1>

Wyświetl plik

@ -1,6 +1,6 @@
{% load thumbnail i18n cms %}
<section class="images">
<section class="images" id="{{section.title|slugify}}">
<div class="images">
{% for image in section.images.all %}
<div class="image">

Wyświetl plik

@ -1,6 +1,6 @@
{% load i18n cms %}
<section class="text">
<section class="text" id="{{section.title|slugify}}">
<div class="wrapper">
<div class="title">
<h1>

Wyświetl plik

@ -1,6 +1,6 @@
{% load embed_video_tags i18n cms %}
<section class="video">
<section class="video" id="{{section.title|slugify}}">
{% if section.video %}
<div class="video">
<div class="iframe">

Wyświetl plik

@ -1,6 +1,7 @@
from cms.decorators import section_view
from cms.forms import ContactForm
from cms.views import SectionFormView, SectionView
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
@ -28,7 +29,11 @@ class Video(SectionView):
@section_view
class Contact(SectionFormView):
verbose_name = _("Contact")
fields = []
fields = ["content", "href"]
form_class = ContactForm
success_url = "/thanks/"
template_name = "contact.html"
def form_valid(self, form):
response = HttpResponse(status=302)
response["Location"] = form.save(self.object.href)
return response