diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-11-09 00:15:23 -0300 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-11-09 00:15:23 -0300 |
commit | fb8037803ec5fd8cba613c22310077324cbea49c (patch) | |
tree | f8f4e55668c560b84c68a465dace6243d0041e79 | |
parent | 2373ab019c5515153fb085a5c2c80c1ac179f873 (diff) | |
download | askbot-fb8037803ec5fd8cba613c22310077324cbea49c.tar.gz askbot-fb8037803ec5fd8cba613c22310077324cbea49c.tar.bz2 askbot-fb8037803ec5fd8cba613c22310077324cbea49c.zip |
applied the karma limit to post links to questions posted before signing in
-rw-r--r-- | askbot/const/message_keys.py | 5 | ||||
-rw-r--r-- | askbot/forms.py | 26 | ||||
-rw-r--r-- | askbot/models/__init__.py | 16 | ||||
-rw-r--r-- | askbot/models/question.py | 42 |
4 files changed, 59 insertions, 30 deletions
diff --git a/askbot/const/message_keys.py b/askbot/const/message_keys.py index 74fcbf7f..291381cb 100644 --- a/askbot/const/message_keys.py +++ b/askbot/const/message_keys.py @@ -54,7 +54,4 @@ def get_i18n_message(key): 'Please contact the forum administrator to reach a resolution.' ) } - if key in messages: - return messages.get(key) - else: - raise KeyError(key) + return messages[key] diff --git a/askbot/forms.py b/askbot/forms.py index a4365776..ed47e20e 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -4,6 +4,7 @@ import re from django import forms from askbot import const from askbot.const import message_keys +from django.core.exceptions import PermissionDenied from django.forms.util import ErrorList from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ungettext_lazy, string_concat @@ -11,7 +12,6 @@ from django.utils.text import get_text_list from django.contrib.auth.models import User from django_countries import countries from askbot.utils.forms import NextUrlField, UserNameField -from askbot.utils.markup import URL_RE from askbot.mail import extract_first_email_address from recaptcha_works.fields import RecaptchaField from askbot.conf import settings as askbot_settings @@ -302,20 +302,16 @@ class EditorField(forms.CharField): ) % self.min_length raise forms.ValidationError(msg) - if re.search(URL_RE, value): - min_rep = askbot_settings.MIN_REP_TO_SUGGEST_LINK - if self.user.is_anonymous(): - raise forms.ValidationError( - _('Links or images cannot be posted anonymously') - ) - elif self.user.reputation < min_rep: - raise forms.ValidationError( - ungettext_lazy( - 'At at least %d karma point is required to post links', - 'At at least %d karma points are required to post links', - min_rep - ) % min_rep - ) + if self.user.is_anonymous(): + #we postpone this validation if user is posting + #before logging in, up until publishing the post + return value + + try: + self.user.assert_can_post_text(value) + except PermissionDenied, e: + raise forms.ValidationError(unicode(e)) + return value diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 4648748e..8d7583d6 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -13,6 +13,7 @@ import collections import datetime import hashlib import logging +import re import urllib import uuid from celery import states @@ -56,6 +57,7 @@ from askbot.models.repute import Award, Repute, Vote from askbot.models.widgets import AskWidget, QuestionWidget from askbot import auth from askbot.utils.decorators import auto_now_timestamp +from askbot.utils.markup import URL_RE from askbot.utils.slug import slugify from askbot.utils.html import replace_links_with_text from askbot.utils.html import sanitize_html @@ -634,6 +636,19 @@ def user_assert_can_upload_file(request_user): ) +def user_assert_can_post_text(self, text): + """Raises exceptions.PermissionDenied, if user does not have + privilege to post given text, depending on the contents + """ + if re.search(URL_RE, text): + min_rep = askbot_settings.MIN_REP_TO_SUGGEST_LINK + if self.is_authenticated() and self.reputation < min_rep: + message = _( + 'Could not post, because your karma is insufficient to publish links' + ) + raise django_exceptions.PermissionDenied(message) + + def user_assert_can_post_question(self): """raises exceptions.PermissionDenied with text that has the reason for the denial @@ -2867,6 +2882,7 @@ User.add_to_class('assert_can_upload_file', user_assert_can_upload_file) User.add_to_class('assert_can_post_question', user_assert_can_post_question) User.add_to_class('assert_can_post_answer', user_assert_can_post_answer) User.add_to_class('assert_can_post_comment', user_assert_can_post_comment) +User.add_to_class('assert_can_post_text', user_assert_can_post_text) User.add_to_class('assert_can_edit_post', user_assert_can_edit_post) User.add_to_class('assert_can_edit_deleted_post', user_assert_can_edit_deleted_post) User.add_to_class('assert_can_see_deleted_post', user_assert_can_see_deleted_post) diff --git a/askbot/models/question.py b/askbot/models/question.py index 43844c82..f7ead369 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -11,6 +11,7 @@ from django.core.urlresolvers import reverse from django.utils.hashcompat import md5_constructor from django.utils.translation import ugettext as _ from django.utils.translation import ungettext +from django.utils.translation import string_concat import askbot from askbot.conf import settings as askbot_settings @@ -1418,16 +1419,35 @@ class AnonymousQuestion(DraftContent): tagnames = models.CharField(max_length=125) is_anonymous = models.BooleanField(default=False) - def publish(self,user): + def publish(self, user): added_at = datetime.datetime.now() #todo: wrong - use User.post_question() instead - Thread.objects.create_new( - title = self.title, - added_at = added_at, - author = user, - wiki = self.wiki, - is_anonymous = self.is_anonymous, - tagnames = self.tagnames, - text = self.text, - ) - self.delete() + try: + user.assert_can_post_text(self.text) + Thread.objects.create_new( + title = self.title, + added_at = added_at, + author = user, + wiki = self.wiki, + is_anonymous = self.is_anonymous, + tagnames = self.tagnames, + text = self.text, + ) + self.delete() + except django_exceptions.PermissionDenied, error: + #delete previous draft questions (only one is allowed anyway) + prev_drafts = DraftQuestion.objects.filter(author=user) + prev_drafts.delete() + #convert this question to draft + DraftQuestion.objects.create( + author=user, + title=self.title, + text=self.text, + tagnames=self.tagnames + ) + #add message with a link to the ask page + extra_message = _( + 'Please, <a href="%s">review your question</a>.' + ) % reverse('ask') + message = string_concat(unicode(error), u' ', extra_message) + user.message_set.create(message=unicode(message)) |