diff options
author | Egil Moeller <egil.moller@freecode.no> | 2014-02-14 14:50:51 +0100 |
---|---|---|
committer | Egil Moeller <egil.moller@freecode.no> | 2014-02-14 14:50:51 +0100 |
commit | 7ddd67ebdc119a7f95a24775c84f3bb5b8f274c5 (patch) | |
tree | 68425b25da9b8f106975e0f89cc53744f7a7922e | |
parent | 18dde60328cd3f710cba8d876d288c7857bb4af7 (diff) | |
download | askbot-7ddd67ebdc119a7f95a24775c84f3bb5b8f274c5.tar.gz askbot-7ddd67ebdc119a7f95a24775c84f3bb5b8f274c5.tar.bz2 askbot-7ddd67ebdc119a7f95a24775c84f3bb5b8f274c5.zip |
Added support for directly posting as anonymous with recaptcha
-rw-r--r-- | askbot/forms.py | 12 | ||||
-rw-r--r-- | askbot/templates/widgets/ask_form.html | 12 | ||||
-rw-r--r-- | askbot/templates/widgets/question_edit_tips.html | 2 | ||||
-rw-r--r-- | askbot/views/writers.py | 19 |
4 files changed, 35 insertions, 10 deletions
diff --git a/askbot/forms.py b/askbot/forms.py index a490a4ac..c0a30679 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -923,11 +923,20 @@ class AskForm(PostAsSomeoneForm, PostPrivatelyForm): required=False, ) + if (not user.is_authenticated() + and askbot_settings.USE_RECAPTCHA + and askbot_settings.ALLOW_ASK_ANONYMOUSLY): + self.fields['recaptcha'] = RecaptchaField( + private_key=askbot_settings.RECAPTCHA_SECRET, + public_key=askbot_settings.RECAPTCHA_KEY, + label='Are you human?', required=True) + #hide ask_anonymously field if getattr(django_settings, 'ASKBOT_MULTILINGUAL', False): self.fields['language'] = LanguageField() - if askbot_settings.ALLOW_ASK_ANONYMOUSLY is False: + if ( not askbot_settings.ALLOW_ASK_ANONYMOUSLY + or not user.is_authenticated()): self.hide_field('ask_anonymously') def clean_ask_anonymously(self): @@ -937,7 +946,6 @@ class AskForm(PostAsSomeoneForm, PostPrivatelyForm): self.cleaned_data['ask_anonymously'] = False return self.cleaned_data['ask_anonymously'] - ASK_BY_EMAIL_SUBJECT_HELP = _( 'Subject line is expected in the format: ' '[tag1, tag2, tag3,...] question title' diff --git a/askbot/templates/widgets/ask_form.html b/askbot/templates/widgets/ask_form.html index df37ba3c..0d3e9e30 100644 --- a/askbot/templates/widgets/ask_form.html +++ b/askbot/templates/widgets/ask_form.html @@ -61,10 +61,16 @@ </div> {% endif %} </div> - {% if not request.user.is_authenticated() %} - <input type="submit" name="post_anon" value="{% trans %}Login/Signup to Post{% endtrans %}" class="submit" /> + + {% if form.recaptcha.errors %} + <span class="form-error">{{ form.recaptcha.errors }}</span> + {% endif %} + {{ form.recaptcha }} + + {% if request.user.is_authenticated() or form.recaptcha %} + <input type="submit" name="post" value="{{ settings.WORDS_ASK_YOUR_QUESTION|escape }}" class="submit" /> {% else %} - <input type="submit" name="post" value="{{ settings.WORDS_ASK_YOUR_QUESTION|escape }}" class="submit" /> + <input type="submit" name="post_anon" value="{% trans %}Login/Signup to Post{% endtrans %}" class="submit" /> {% endif %} <div class="clean"></div> </form> diff --git a/askbot/templates/widgets/question_edit_tips.html b/askbot/templates/widgets/question_edit_tips.html index 636bd38d..78f6d3fb 100644 --- a/askbot/templates/widgets/question_edit_tips.html +++ b/askbot/templates/widgets/question_edit_tips.html @@ -3,7 +3,7 @@ {{ settings.QUESTION_INSTRUCTIONS|safe }} {% else %} <ul> - {% if not request.user.is_authenticated() %} + {% if not request.user.is_authenticated() and not (settings.USE_RECAPTCHA and settings.ALLOW_ASK_ANONYMOUSLY) %} <li class="warning">{% trans %}since you are not logged in right now, you will be asked to sign in or register after making your post{% endtrans %}</li> {% else %} {% if settings.EMAIL_VALIDATION %} diff --git a/askbot/views/writers.py b/askbot/views/writers.py index 819ee8d1..c1251850 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -30,6 +30,7 @@ from django.core.urlresolvers import reverse from django.core import exceptions from django.conf import settings from django.views.decorators import csrf +from django.contrib.auth.models import User from askbot import exceptions as askbot_exceptions from askbot import forms @@ -46,6 +47,7 @@ from askbot.views import context from askbot.templatetags import extra_filters_jinja as template_filters from askbot.importers.stackexchange import management as stackexchange#todo: may change from askbot.utils.slug import slugify +from recaptcha_works.decorators import fix_recaptcha_remote_ip # used in index page INDEX_PAGE_SIZE = 20 @@ -203,6 +205,7 @@ def import_data(request): } return render(request, 'import_data.html', data) +@fix_recaptcha_remote_ip #@login_required #actually you can post anonymously, but then must register @csrf.csrf_protect @decorators.check_authorization_to_post(ugettext_lazy('Please log in to make posts')) @@ -220,7 +223,7 @@ def ask(request):#view used to ask a new question request.user.message_set.create(message=_('Sorry, but you have only read access')) return HttpResponseRedirect(referer) - form = forms.AskForm(request.REQUEST, user=request.user) + form = forms.AskForm(request.POST, user=request.user) if request.method == 'POST': if form.is_valid(): timestamp = datetime.datetime.now() @@ -235,11 +238,19 @@ def ask(request):#view used to ask a new question if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter( - author=request.user - ) + author=request.user + ) drafts.delete() - user = form.get_post_user(request.user) + if ( request.user.is_authenticated() + or ( askbot_settings.USE_RECAPTCHA + and askbot_settings.ALLOW_ASK_ANONYMOUSLY)): + + if not request.user.is_authenticated(): + ask_anonymously = True + user = User.objects.filter(is_staff=True)[0].get_or_create_fake_user("anonymous", "anonymous@example.com") + else: + user = form.get_post_user(request.user) try: question = user.post_question( title=title, |