Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
radiopanik
django-panik-newsletter
Commits
bc1fd6d8
Commit
bc1fd6d8
authored
Jan 31, 2021
by
fred
Browse files
add secondary bot check
parent
4a66ad13
Changes
4
Hide whitespace changes
Inline
Side-by-side
newsletter/migrations/0004_auto_20210131_1714.py
0 → 100644
View file @
bc1fd6d8
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2021-01-31 17:14
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'newsletter'
,
'0003_auto_20210131_1630'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'subscriber'
,
name
=
'bot_check1'
,
field
=
models
.
BooleanField
(
default
=
False
),
),
migrations
.
AddField
(
model_name
=
'subscriber'
,
name
=
'bot_check2'
,
field
=
models
.
BooleanField
(
default
=
False
),
),
]
newsletter/models.py
View file @
bc1fd6d8
...
...
@@ -33,16 +33,18 @@ class Subscriber(models.Model) :
is_validated
=
models
.
NullBooleanField
()
is_registered
=
models
.
NullBooleanField
()
password
=
models
.
CharField
(
max_length
=
100
)
bot_check1
=
models
.
BooleanField
(
default
=
False
)
bot_check2
=
models
.
BooleanField
(
default
=
False
)
def
__unicode__
(
self
)
:
return
self
.
email
def
save
(
self
,
*
args
,
**
kwargs
):
super
(
Subscriber
,
self
).
save
(
*
args
,
**
kwargs
)
if
self
.
is_validated
is
None
:
self
.
send_confirmation_email
()
def
is_from_bot
(
self
):
return
bool
(
self
.
bot_check1
or
self
.
bot_check2
)
def
send_confirmation_email
(
self
):
if
self
.
is_from_bot
():
return
self
.
password
=
hashlib
.
sha1
(
force_bytes
(
str
(
random
.
random
()))).
hexdigest
()
confirm_subject
=
loader
.
get_template
(
'newsletter/confirmation_email_subject.txt'
)
confirm_body
=
loader
.
get_template
(
'newsletter/confirmation_email_body.txt'
)
...
...
newsletter/templates/newsletter/subscriber_form_content.html
View file @
bc1fd6d8
...
...
@@ -3,6 +3,14 @@
{% csrf_token %}
<p>
{% trans 'Every Monday, the news of the Panik week:' %}
</p>
<input
required
name=
"email"
type=
"email"
placeholder=
"votre adresse email"
>
<div
style=
"display: none"
><label><input
type=
"checkbox"
name=
"validation"
>
Cocher empêchera la création.
</input></label>
<div
style=
"display: none; display:block;"
>
<label><input
type=
"checkbox"
name=
"validation"
>
Cocher empêchera la création.
</input></label>
<label><input
type=
"checkbox"
name=
"validation2"
id=
"validation2"
>
Celle-ci doit par contre être cochée (déso les personnes sans js).
</input></label>
<script>
(
function
()
{
const
checkbox
=
document
.
getElementById
(
'
validation2
'
);
checkbox
.
checked
=
true
;
})();
</script>
</div>
<button>
{% trans 'Subscribe' %}
</button>
newsletter/views.py
View file @
bc1fd6d8
...
...
@@ -33,11 +33,14 @@ class SubscribeView(CreateView):
success_url
=
reverse_lazy
(
'newsletter-thanks'
)
def
form_valid
(
self
,
form
):
if
'validation'
in
self
.
request
.
POST
:
# anti-bot checkbox, if present, do not create subscription,
# but redirect to success page nevertheless
return
HttpResponseRedirect
(
self
.
success_url
)
return
super
().
form_valid
(
form
)
resp
=
super
().
form_valid
(
form
)
# anti-bot checkboxes, if triggered, do not send confirmation email
# but redirect to success page nevertheless
self
.
object
.
bot_check1
=
bool
(
'validation'
in
self
.
request
.
POST
)
self
.
object
.
bot_check2
=
bool
(
'validation2'
not
in
self
.
request
.
POST
)
if
not
self
.
object
.
is_from_bot
():
self
.
object
.
send_confirmation_email
()
return
resp
subscribe
=
SubscribeView
.
as_view
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment