Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anti-spam captcha to poll creation #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions bitpoll/base/templates/base/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
<h1>{% trans 'Create a poll' %}</h1>
<hr/>
<form method="POST" class="form" action="{% url 'index' %}">
{% if not request.user.is_authenticated %}
<div class="form-step">
<div class="number">{% trans 'Step 0' %}</div>
<div class="title">{% trans 'Antispam' %}</div>
</div>
<div class="form-group">
<label for="{{ poll_form.spam_answer.name }}">{{ spam_challenge.x }} {{ spam_challenge.op }} {{ spam_challenge.y }} =</label>
<input class="form-control" type="text" title="Challenge" name="{{ poll_form.spam_answer.name }}" value="{{ poll_form.spam_answer.value|default_if_none:'' }}">
{% if poll_form.spam_answer.errors %}
<div class="form-errors">
<div class="alert alert-danger">
<div class="container">
<i class="fa fa-times"></i>{{ poll_form.spam_answer.errors }}
</div>
</div>
</div>
{% endif %}
<input type="hidden" name="{{ poll_form.spam_key.name }}" value="{{ spam_challenge.key }}">
</div>
{% endif %}
{% csrf_token %}
<div class="form-step">
<div class="number">{% trans 'Step 1' %}</div>
Expand Down
72 changes: 54 additions & 18 deletions bitpoll/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.template.response import TemplateResponse
from django.db.models import Q
from django.db import IntegrityError
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from bitpoll.base.autocomplete import autocomplete_users
from bitpoll.base.models import BitpollUser
Expand All @@ -16,6 +18,7 @@

from bitpoll.poll.forms import PollCreationForm
from bitpoll.poll.models import ChoiceValue, Poll, Vote, PollWatch
from bitpoll.poll.spam_util import create_anti_spam_challenge, get_spam_challenge_from_key, check_anti_spam_challange

from bitpoll.base.models import USER_LANG
from bitpoll.base.forms import BitpollUserSettingsForm
Expand All @@ -27,6 +30,10 @@
from django.conf import settings


# FIXME: when creating a poll, we don't have a poll_id, so we use this pseudo
# one, as the antispam stuff expects a `poll_id`
ANTISPAM_PSEUDO_POLL_ID = 0

def index(request):
"""
:param request
Expand All @@ -47,26 +54,54 @@ def index(request):
post_data['url'] = generate_random_slug()
form = PollCreationForm(post_data)
if form.is_valid():
current_poll = form.save()
if request.user.is_authenticated:
current_poll.user = request.user
current_poll.save()
# TODO: lazy translation
# TODO: load from config
ChoiceValue(title="yes", icon="check", color="90db46", weight=1, poll=current_poll).save()
ChoiceValue(title="no", icon="ban", color="c43131", weight=0, poll=current_poll).save()
ChoiceValue(title="maybe", icon="question", color="ffe800", weight=0.5, poll=current_poll).save()
ChoiceValue(title="Only if absolutely necessary", icon="thumbs-down", color="B0E", weight=0.25,
poll=current_poll).save()

if current_poll.type == 'universal': # TODO: heir könnte auch auf die algemeine edit url weitergeleitet werden
return redirect('poll_editUniversalChoice', current_poll.url)
elif current_poll.type == 'date':
return redirect('poll_editDateChoice', current_poll.url)
try:
spam_ok = True
if not request.user.is_authenticated:
if 'spam_answer' in form.cleaned_data and 'spam_key' in form.cleaned_data:
spam_ok = check_anti_spam_challange(
form.cleaned_data['spam_key'],
form.cleaned_data['spam_answer'],
ANTISPAM_PSEUDO_POLL_ID)
else:
# FIXME: the typo in the message is so the translation
# keeps working, it's wrong in bitpoll/poll/views.py
# for comment anti-spam as well
spam_ok, err_message = False, _('This Field is rquired')

if spam_ok:
current_poll = form.save()
if request.user.is_authenticated:
current_poll.user = request.user
current_poll.save()
# TODO: lazy translation
# TODO: load from config
ChoiceValue(title="yes", icon="check", color="90db46", weight=1, poll=current_poll).save()
ChoiceValue(title="no", icon="ban", color="c43131", weight=0, poll=current_poll).save()
ChoiceValue(title="maybe", icon="question", color="ffe800", weight=0.5, poll=current_poll).save()
ChoiceValue(title="Only if absolutely necessary", icon="thumbs-down", color="B0E", weight=0.25,
poll=current_poll).save()

if current_poll.type == 'universal': # TODO: heir könnte auch auf die algemeine edit url weitergeleitet werden
return redirect('poll_editUniversalChoice', current_poll.url)
elif current_poll.type == 'date':
return redirect('poll_editDateChoice', current_poll.url)
else:
return redirect('poll_editDTChoiceDate', current_poll.url)
else:
form.add_error('spam_answer', _("Wrong result"))
spam_challenge = create_anti_spam_challenge(ANTISPAM_PSEUDO_POLL_ID)
except ValidationError as e:
spam_challenge = create_anti_spam_challenge(ANTISPAM_PSEUDO_POLL_ID)
form.add_error('spam_answer', _("Wrong result"))
else:
if 'spam_key' in request.POST:
spam_challenge = get_spam_challenge_from_key(form.cleaned_data['spam_key'], ANTISPAM_PSEUDO_POLL_ID)
else:
return redirect('poll_editDTChoiceDate', current_poll.url)
spam_challenge = create_anti_spam_challenge(ANTISPAM_PSEUDO_POLL_ID)
else:
form = PollCreationForm()
spam_challenge = create_anti_spam_challenge(ANTISPAM_PSEUDO_POLL_ID)

return TemplateResponse(request, "base/index.html", {
'poll_form': form,
'poll_count': Poll.objects.all().count(),
Expand All @@ -75,6 +110,7 @@ def index(request):
'public_polls': public_polls,
'randomize_url': randomize_url,
'random_url': generate_random_slug() if randomize_url else '',
'spam_challenge': spam_challenge,
})


Expand Down Expand Up @@ -159,4 +195,4 @@ def autocomplete(request):
'last_name': user.last_name
} for user in users]
})
return HttpResponse(result_json, content_type='application/json')
return HttpResponse(result_json, content_type='application/json')
4 changes: 4 additions & 0 deletions bitpoll/poll/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@


class PollCreationForm(ModelForm):
# TODO: same as for CommentForm, maybe simplify/merge?
spam_key = CharField(widget=HiddenInput(), required=False)
spam_answer = IntegerField(required=False)

class Meta:
model = Poll
fields = [
Expand Down