diff options
author | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2011-11-21 14:58:29 -0300 |
---|---|---|
committer | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2011-11-21 16:03:22 -0300 |
commit | 9b8e514dd87b6ff8bf1e2fd8141b9bbbb63333fb (patch) | |
tree | 43827a3a282e3f30eac20e884b659bd4e0ec2f70 | |
parent | 9d08a3953810b02791d9da0f0dd939042b5d4b01 (diff) | |
download | askbot-9b8e514dd87b6ff8bf1e2fd8141b9bbbb63333fb.tar.gz askbot-9b8e514dd87b6ff8bf1e2fd8141b9bbbb63333fb.tar.bz2 askbot-9b8e514dd87b6ff8bf1e2fd8141b9bbbb63333fb.zip |
Added spam filtering with recaptcha in feedback form
-rw-r--r-- | askbot/forms.py | 46 | ||||
-rw-r--r-- | askbot/skins/default/templates/feedback.html | 13 | ||||
-rw-r--r-- | askbot/views/meta.py | 18 |
3 files changed, 55 insertions, 22 deletions
diff --git a/askbot/forms.py b/askbot/forms.py index 0769f180..0479ad49 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -43,7 +43,7 @@ def clean_marked_tagnames(tagnames): wildcards.append(tagname) else: pure_tags.append(tagname) - + return pure_tags, wildcards def filter_choices(remove_choices = None, from_choices = None): @@ -75,7 +75,7 @@ COUNTRY_CHOICES = (('unknown',_('select country')),) + countries.COUNTRIES class CountryField(forms.ChoiceField): """this is better placed into the django_coutries app""" - + def __init__(self, *args, **kwargs): """sets label and the country choices """ @@ -253,7 +253,7 @@ class SummaryField(forms.CharField): class DumpUploadForm(forms.Form): """This form handles importing - data into the forum. At the moment it only + data into the forum. At the moment it only supports stackexchange import. """ dump_file = forms.FileField() @@ -397,7 +397,7 @@ class ChangeUserStatusForm(forms.Form): self.fields['user_status'].choices = user_status_choices #set prompt option as default - self.fields['user_status'].default = 'select' + self.fields['user_status'].default = 'select' self.moderator = moderator self.subject = subject @@ -542,8 +542,26 @@ class FeedbackForm(forms.Form): name = forms.CharField(label=_('Your name:'), required=False) email = forms.EmailField(label=_('Email (not shared with anyone):'), required=False) message = forms.CharField(label=_('Your message:'), max_length=800,widget=forms.Textarea(attrs={'cols':60})) + no_email = forms.BooleanField(label=_("I don't want to give my email"), required=False) next = NextUrlField() + def __init__(self, is_auth=False, *args, **kwargs): + super(FeedbackForm, self).__init__(*args, **kwargs) + if not is_auth: + if askbot_settings.USE_RECAPTCHA: + self._add_recaptcha_field() + + def _add_recaptcha_field(self): + self.fields['recaptcha'] = RecaptchaField( + private_key = askbot_settings.RECAPTCHA_SECRET, + public_key = askbot_settings.RECAPTCHA_KEY + ) + def clean(self): + super(FeedbackForm, self).clean() + if not self.cleaned_data['no_email'] and not self.cleaned_data['email']: + raise forms.ValidationError(_('Please mark "I dont want to give my mail" field.')) + return self.cleaned_data + class FormWithHideableFields(object): """allows to swap a field widget to HiddenInput() and back""" @@ -567,7 +585,7 @@ class FormWithHideableFields(object): class AskForm(forms.Form, FormWithHideableFields): """the form used to askbot questions - field ask_anonymously is shown to the user if the + field ask_anonymously is shown to the user if the if ALLOW_ASK_ANONYMOUSLY live setting is True however, for simplicity, the value will always be present in the cleaned data, and will evaluate to False if the @@ -600,7 +618,7 @@ class AskForm(forms.Form, FormWithHideableFields): """ if askbot_settings.ALLOW_ASK_ANONYMOUSLY == False: self.cleaned_data['ask_anonymously'] = False - return self.cleaned_data['ask_anonymously'] + return self.cleaned_data['ask_anonymously'] class AskByEmailForm(forms.Form): @@ -860,16 +878,16 @@ class EditUserForm(forms.Form): ) realname = forms.CharField( - label=_('Real name'), - required=False, - max_length=255, + label=_('Real name'), + required=False, + max_length=255, widget=forms.TextInput(attrs={'size' : 35}) ) website = forms.URLField( - label=_('Website'), - required=False, - max_length=255, + label=_('Website'), + required=False, + max_length=255, widget=forms.TextInput(attrs={'size' : 35}) ) @@ -957,7 +975,7 @@ class TagFilterSelectionForm(forms.ModelForm): if before != after: return True return False - + class EmailFeedSettingField(forms.ChoiceField): def __init__(self, *arg, **kwarg): @@ -1086,7 +1104,7 @@ class SimpleEmailSubscribeForm(forms.Form): ('n',_('no community email please, thanks')) ) subscribe = forms.ChoiceField( - widget=forms.widgets.RadioSelect, + widget=forms.widgets.RadioSelect, error_messages={'required':_('please choose one of the options above')}, choices=SIMPLE_SUBSCRIBE_CHOICES ) diff --git a/askbot/skins/default/templates/feedback.html b/askbot/skins/default/templates/feedback.html index 60ea2fca..ab9f2298 100644 --- a/askbot/skins/default/templates/feedback.html +++ b/askbot/skins/default/templates/feedback.html @@ -26,6 +26,14 @@ {% endif %} </label><br/>{{form.email}} </div> + <div class="form-row"> + <label>{{form.no_email.label}} + {% if form.errors.no_email %} + <span class="error">{% trans %}(this field is required){% endtrans %}</span> + {% endif %} + {{form.no_email}} + </label> + </div> {% endif %} <div class="form-row"> <label>{{form.message.label}} @@ -36,6 +44,11 @@ <br/> {{form.message}} </div> + {% if form.recaptcha %} + <div class="form-row"> + {{form.recaptcha}} + </div> + {% endif %} {{form.next}} <div class="submit-row"> <input type="submit" class="submit" value="{% trans %}Send Feedback{% endtrans %}"/> diff --git a/askbot/views/meta.py b/askbot/views/meta.py index 301a0925..493ab8ec 100644 --- a/askbot/views/meta.py +++ b/askbot/views/meta.py @@ -33,13 +33,13 @@ def config_variable(request, variable_name = None, mimetype = None): return HttpResponse(output, mimetype = mimetype) def about(request, template='about.html'): - return generic_view(request, template = template, page_class = 'meta') + return generic_view(request, template = template, page_class = 'meta') def page_not_found(request, template='404.html'): - return generic_view(request, template) + return generic_view(request, template) def server_error(request, template='500.html'): - return generic_view(request, template) + return generic_view(request, template) def faq(request): if getattr(askbot_settings, 'FORUM_FAQ',''): @@ -64,7 +64,7 @@ def faq(request): #'send_email_key_url': reverse('send_email_key'), 'ask_question_url': reverse('ask'), 'page_class': 'meta', - } + } return render_into_skin('faq_static.html', data, request) @csrf.csrf_protect @@ -72,9 +72,10 @@ def feedback(request): data = {'page_class': 'meta'} form = None if request.method == "POST": - form = FeedbackForm(request.POST) + form = FeedbackForm(is_auth = request.user.is_authenticated(), + data = request.POST) if form.is_valid(): - if not request.user.is_authenticated: + if not request.user.is_authenticated(): data['email'] = form.cleaned_data.get('email',None) data['message'] = form.cleaned_data['message'] data['name'] = form.cleaned_data.get('name',None) @@ -85,7 +86,8 @@ def feedback(request): request.user.message_set.create(message=msg) return HttpResponseRedirect(get_next_url(request)) else: - form = FeedbackForm(initial={'next':get_next_url(request)}) + form = FeedbackForm(is_auth = request.user.is_authenticated(), + initial={'next':get_next_url(request)}) data['form'] = form return render_into_skin('feedback.html', data, request) @@ -96,7 +98,7 @@ def privacy(request): def badges(request):#user status/reputation system #todo: supplement database data with the stuff from badges.py - known_badges = badge_data.BADGES.keys() + known_badges = badge_data.BADGES.keys() badges = BadgeData.objects.filter(slug__in = known_badges).order_by('slug') my_badges = [] if request.user.is_authenticated(): |