Skip to content
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
11 changes: 9 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ user to fill.

<!-- in header block -->
{% include "feedback/header.html" %}

<!-- in body block -->
{% include "feedback/button.html" %}

Expand All @@ -28,7 +28,7 @@ user to fill.
<!-- in body block -->
{% include "feedback/feedback.html" %}
<div class="feedback_button"/>


+ All feedback can be seen in the Django admin interface

Expand All @@ -37,3 +37,10 @@ user to fill.
+ Feedback can optionally be emailed to you as well, as it is submitted. Specify your email address in settings.py:

FEEDBACK_EMAIL = "me@example.com"

+ Feedback can be protected with a simple text captcha by adding the following to your settings.py file::

FEEDBACK_CAPTCHAS = [
(_("question1"), ["answer1"]),
(_("question2"), ["answer2", "alternative ansnwer 2"]),
]
15 changes: 15 additions & 0 deletions feedback/forms.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
#!/usr/bin/env python
from django import forms
from django.conf import settings
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _

from feedback.models import Feedback

class FeedbackForm(forms.ModelForm):
'''The form shown when giving feedback'''
def __init__(self, *args, **kwargs):
super(FeedbackForm, self).__init__(*args, **kwargs)
self.captchas = getattr(settings, 'FEEDBACK_CAPTCHAS', {})
if self.captchas:
self.fields['captchaquestion'] = forms.CharField()
self.fields['captcha'] = forms.CharField()

def clean_captcha(self):
for question, answers in self.captchas:
if self.cleaned_data['captchaquestion'] == question and self.cleaned_data['captcha'] in answers:
return self.cleaned_data['captcha']
raise forms.ValidationError(_("Captcha incorrect"))

class Meta:
model = Feedback
fields = "__all__"
Expand Down
Binary file modified feedback/locale/cs/LC_MESSAGES/django.mo
Binary file not shown.
3 changes: 3 additions & 0 deletions feedback/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"

msgid "Captcha incorrect"
msgstr "Nesprávná captcha"

#: models.py:7
msgid "site"
msgstr "stránka"
Expand Down
6 changes: 6 additions & 0 deletions feedback/templates/feedback/captcha.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% if question %}
<div class="captcha error"> </div>
<label for="captcha">{{ question }}</label>
<input name="captcha" class="captchainput" style="margin-bottom: 5px;"/>
<input type="hidden" id="captchaquestion" name="captchaquestion" value="{{ question_id }}"/>
{% endif %}
4 changes: 4 additions & 0 deletions feedback/templates/feedback/feedback.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load i18n %}
{% load captcha %}
<div id="feedback_drop" class="hiding"></div>
<div id="feedback_popup" class="hiding">
<form class="feedback" action="/feedback/ajax{{ request.path }}" method="POST">
Expand All @@ -15,6 +16,9 @@ <h4>{% trans "Enter your feedback" %}</h4>
<div class="text error"> </div>
<label for="text">{% trans "message" %}</label>
<textarea name="text" class="messageinput"></textarea>
{% block captcha %}
{% captcha %}
{% endblock %}
<div class="buttons">
<button class="feedback_submit_button">{% trans "Send" %}</button>
</div>
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions feedback/templatetags/captcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django import template
from django.conf import settings
import random

register = template.Library()

@register.inclusion_tag("feedback/captcha.html")
def captcha():
captchas = getattr(settings, 'FEEDBACK_CAPTCHAS', {})
if captchas:
question = random.randint(0, len(captchas) - 1)
return {
"question": captchas[question][0],
"question_id": captchas[question][0]
}
return {}
26 changes: 26 additions & 0 deletions feedback/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

from django.test import TestCase
from django.test.utils import override_settings
try:
from django.urls import reverse
except ImportError: # Django<2.0
Expand All @@ -24,4 +25,29 @@ def test_error_view(self):
self.assertEqual(response.content, b'{"errors": {"text": ["This field is required."]}}')
self.assertEqual(response.status_code, 200)


@override_settings(
FEEDBACK_CAPTCHAS = [("foo", ["bar"])]
)
class ViewWithCaptchasTestCase(TestCase):
def test_feedback_view_correct_captcha(self):
post_data = {
"text": "sample test text",
"captcha": "bar",
"captchaquestion": "foo",
}
response = self.client.post(reverse('feedback', kwargs={'url': 'test_url'}), post_data)
self.assertEqual(response.content, b"{}")
self.assertEqual(response.status_code, 200)

def test_feedback_view_wrong_captcha(self):
post_data = {
"text": "sample test text",
"captcha": "baz",
"captchaquestion": "foo",
}
response = self.client.post(reverse('feedback', kwargs={'url': 'test_url'}), post_data)
self.assertEqual(response.content, b'{"errors": {"captcha": ["Captcha incorrect"]}}')
self.assertEqual(response.status_code, 200)

# vim: et sw=4 sts=4