summaryrefslogtreecommitdiffstats
path: root/forum/auth.py
diff options
context:
space:
mode:
Diffstat (limited to 'forum/auth.py')
-rw-r--r--[-rwxr-xr-x]forum/auth.py189
1 files changed, 123 insertions, 66 deletions
diff --git a/forum/auth.py b/forum/auth.py
index 5d6e71c4..5bc88297 100755..100644
--- a/forum/auth.py
+++ b/forum/auth.py
@@ -14,30 +14,87 @@ from models import mark_offensive, delete_post_or_answer
from const import TYPE_REPUTATION
import logging
-VOTE_UP = 15
-FLAG_OFFENSIVE = 15
-POST_IMAGES = 15
-LEAVE_COMMENTS = 50
-UPLOAD_FILES = 60
-VOTE_DOWN = 100
-CLOSE_OWN_QUESTIONS = 250
-RETAG_OTHER_QUESTIONS = 500
-REOPEN_OWN_QUESTIONS = 500
-EDIT_COMMUNITY_WIKI_POSTS = 750
-EDIT_OTHER_POSTS = 2000
-DELETE_COMMENTS = 2000
-VIEW_OFFENSIVE_FLAGS = 2000
-DISABLE_URL_NOFOLLOW = 2000
-CLOSE_OTHER_QUESTIONS = 3000
-LOCK_POSTS = 4000
+from livesettings import ConfigurationGroup, IntegerValue, config_register
+MINIMUM_REPUTATION_SETTINGS = ConfigurationGroup(
+ 'MIN_REP',
+ _('Minimum reputation settings'),
+ ordering=0)
+
+MIN_REP_DATA = (
+ #(key, min_rep, description),
+ ('VOTE_UP', 15, _('Vote')),
+ ('FLAG_OFFENSIVE', 15, _('Flag offensive')),
+ #('POST_IMAGES', 15, _('Upload images')),#todo: looks like unused
+ ('LEAVE_COMMENTS', 50, _('Leave comments')),
+ ('UPLOAD_FILES', 60, _('Upload files')),
+ ('VOTE_DOWN', 100, _('Downvote')),
+ ('CLOSE_OWN_QUESTIONS', 250, _('Close own questions')),
+ ('RETAG_OTHER_QUESTIONS', 500, _('Retag questions posted by other people')),
+ ('REOPEN_OWN_QUESTIONS', 500, _('Reopen own questions')),
+ ('EDIT_COMMUNITY_WIKI_POSTS', 750, _('Edit community wiki posts')),
+ ('EDIT_OTHER_POSTS', 2000, _('Edit posts authored by other people')),
+ ('DELETE_COMMENTS', 2000, _('Delete comments')),
+ ('VIEW_OFFENSIVE_FLAGS', 2000, _('View offensive flags')),
+ ('DISABLE_URL_NOFOLLOW', 2000, _('Disable url nofollow')),
+ ('CLOSE_OTHER_QUESTIONS', 3000, _('Close questions asked by others')),
+ ('LOCK_POSTS', 4000, _('Lock posts')),
+)
+
+#rolled into eval block to save on typing an debugging
+python_format_string = '%(VAR_NAME)s = config_register(IntegerValue(%(SETTINGS_GROUP_KEY)s,\'%(VAR_NAME)s\', default = %(DEFAULT_VALUE)d,description = u\'%(VAR_DESCRIPTION)s\', ordering=%(ORDERING)d))\n'
+i = 0
+for item in MIN_REP_DATA:
+ name = item[0]
+ value = item[1]
+ description = item[2]
+ python_string = python_format_string \
+ % {
+ 'SETTINGS_GROUP_KEY':'MINIMUM_REPUTATION_SETTINGS',
+ 'VAR_NAME':name,
+ 'DEFAULT_VALUE':value,
+ 'VAR_DESCRIPTION':description,
+ 'ORDERING':i,
+ }
+ i += 1
+ exec(python_string)
+
+VOTE_RULES_DATA = (
+ ('scope_votes_per_user_per_day', 30, _('Maximum votes per day')), # how many votes of one user has everyday
+ ('scope_flags_per_user_per_day', 5, _('Maximum flags per day')), # how many times user can flag posts everyday
+ ('scope_warn_votes_left', 5, _('How early to start warning about the vote per day limit')), # start when to warn user how many votes left
+ ('scope_deny_unvote_days', 1, _('Days to allow canceling votes')), # if 1 days passed, user can't cancel votes.
+ ('scope_flags_invisible_main_page', 3, _('Number of flags to hide post')), # post doesn't show on main page if has more than 3 offensive flags
+ ('scope_flags_delete_post', 5, _('Number of flags to delete post')),# post will be deleted if it has more than 5 offensive flags
+)
+
+VOTE_RULE_SETTINGS = ConfigurationGroup(
+ 'VOTE_RULES',
+ _('Vote and flag rules'),
+ ordering=0)
+
+i = 0
+for item in VOTE_RULES_DATA:
+ name = item[0]
+ value = item[1]
+ description = item[2]
+ python_string = python_format_string \
+ % {
+ 'SETTINGS_GROUP_KEY':'VOTE_RULE_SETTINGS',
+ 'VAR_NAME':name,
+ 'DEFAULT_VALUE':value,
+ 'VAR_DESCRIPTION':description,
+ 'ORDERING':i,
+ }
+ i += 1
+ exec(python_string)
VOTE_RULES = {
- 'scope_votes_per_user_per_day' : 30, # how many votes of one user has everyday
- 'scope_flags_per_user_per_day' : 5, # how many times user can flag posts everyday
- 'scope_warn_votes_left' : 10, # start when to warn user how many votes left
- 'scope_deny_unvote_days' : 1, # if 1 days passed, user can't cancel votes.
- 'scope_flags_invisible_main_page' : 3, # post doesn't show on main page if has more than 3 offensive flags
- 'scope_flags_delete_post' : 5, # post will be deleted if it has more than 5 offensive flags
+ 'scope_votes_per_user_per_day' : scope_votes_per_user_per_day, # how many votes of one user has everyday
+ 'scope_flags_per_user_per_day' : scope_flags_per_user_per_day, # how many times user can flag posts everyday
+ 'scope_warn_votes_left' : scope_warn_votes_left, # start when to warn user how many votes left
+ 'scope_deny_unvote_days' : scope_deny_unvote_days, # if 1 days passed, user can't cancel votes.
+ 'scope_flags_invisible_main_page' : scope_flags_invisible_main_page, # post doesn't show on main page if has more than 3 offensive flags
+ 'scope_flags_delete_post' : scope_flags_delete_post, # post will be deleted if it has more than 5 offensive flags
}
REPUTATION_RULES = {
@@ -64,13 +121,13 @@ def can_moderate_users(user):
def can_vote_up(user):
"""Determines if a User can vote Questions and Answers up."""
return user.is_authenticated() and (
- user.reputation >= VOTE_UP or
+ user.reputation >= VOTE_UP.value or
user.is_superuser)
def can_flag_offensive(user):
"""Determines if a User can flag Questions and Answers as offensive."""
return user.is_authenticated() and (
- user.reputation >= FLAG_OFFENSIVE or
+ user.reputation >= FLAG_OFFENSIVE.value or
user.is_superuser)
def can_add_comments(user,subject):
@@ -78,7 +135,7 @@ def can_add_comments(user,subject):
if user.is_authenticated():
if user.id == subject.author.id:
return True
- if user.reputation >= LEAVE_COMMENTS:
+ if user.reputation >= LEAVE_COMMENTS.value:
return True
if user.is_superuser:
return True
@@ -89,53 +146,53 @@ def can_add_comments(user,subject):
def can_vote_down(user):
"""Determines if a User can vote Questions and Answers down."""
return user.is_authenticated() and (
- user.reputation >= VOTE_DOWN or
+ user.reputation >= VOTE_DOWN.value or
user.is_superuser)
def can_retag_questions(user):
"""Determines if a User can retag Questions."""
return user.is_authenticated() and (
- RETAG_OTHER_QUESTIONS <= user.reputation < EDIT_OTHER_POSTS or
+ RETAG_OTHER_QUESTIONS.value <= user.reputation < EDIT_OTHER_POSTS.value or
user.is_superuser)
def can_edit_post(user, post):
"""Determines if a User can edit the given Question or Answer."""
return user.is_authenticated() and (
user.id == post.author_id or
- (post.wiki and user.reputation >= EDIT_COMMUNITY_WIKI_POSTS) or
- user.reputation >= EDIT_OTHER_POSTS or
+ (post.wiki and user.reputation >= EDIT_COMMUNITY_WIKI_POSTS.value) or
+ user.reputation >= EDIT_OTHER_POSTS.value or
user.is_superuser)
def can_delete_comment(user, comment):
"""Determines if a User can delete the given Comment."""
return user.is_authenticated() and (
user.id == comment.user_id or
- user.reputation >= DELETE_COMMENTS or
+ user.reputation >= DELETE_COMMENTS.value or
user.is_superuser)
def can_view_offensive_flags(user):
"""Determines if a User can view offensive flag counts."""
return user.is_authenticated() and (
- user.reputation >= VIEW_OFFENSIVE_FLAGS or
+ user.reputation >= VIEW_OFFENSIVE_FLAGS.value or
user.is_superuser)
def can_close_question(user, question):
"""Determines if a User can close the given Question."""
return user.is_authenticated() and (
(user.id == question.author_id and
- user.reputation >= CLOSE_OWN_QUESTIONS) or
- user.reputation >= CLOSE_OTHER_QUESTIONS or
+ user.reputation >= CLOSE_OWN_QUESTIONS.value) or
+ user.reputation >= CLOSE_OTHER_QUESTIONS.value or
user.is_superuser)
def can_lock_posts(user):
"""Determines if a User can lock Questions or Answers."""
return user.is_authenticated() and (
- user.reputation >= LOCK_POSTS or
+ user.reputation >= LOCK_POSTS.value or
user.is_superuser)
def can_follow_url(user):
"""Determines if the URL link can be followed by Google search engine."""
- return user.reputation >= DISABLE_URL_NOFOLLOW
+ return user.reputation >= DISABLE_URL_NOFOLLOW.value
def can_accept_answer(user, question, answer):
return (user.is_authenticated() and
@@ -146,7 +203,7 @@ def can_accept_answer(user, question, answer):
def can_reopen_question(user, question):
return (user.is_authenticated() and
user.id == question.author_id and
- user.reputation >= REOPEN_OWN_QUESTIONS) or user.is_superuser
+ user.reputation >= REOPEN_OWN_QUESTIONS.value) or user.is_superuser
def can_delete_post(user, post):
if user.is_superuser:
@@ -182,7 +239,7 @@ def can_view_user_edit(request_user, target_user):
return (request_user.is_authenticated() and request_user == target_user)
def can_upload_files(request_user):
- return (request_user.is_authenticated() and request_user.reputation >= UPLOAD_FILES) or \
+ return (request_user.is_authenticated() and request_user.reputation >= UPLOAD_FILES.value) or \
request_user.is_superuser
###########################################
@@ -205,7 +262,7 @@ def onFlaggedItem(item, post, user, timestamp=None):
post.save()
post.author.reputation = calculate_reputation(post.author.reputation,
- int(REPUTATION_RULES['lose_by_flagged']))
+ int(REPUTATION_RULES['lose_by_flagged'].value))
post.author.save()
question = post
@@ -213,33 +270,33 @@ def onFlaggedItem(item, post, user, timestamp=None):
question = post.question
reputation = Repute(user=post.author,
- negative=int(REPUTATION_RULES['lose_by_flagged']),
+ negative=int(REPUTATION_RULES['lose_by_flagged'].value),
question=question, reputed_at=timestamp,
reputation_type=-4,
reputation=post.author.reputation)
reputation.save()
#todo: These should be updated to work on same revisions.
- if post.offensive_flag_count == VOTE_RULES['scope_flags_invisible_main_page'] :
+ if post.offensive_flag_count == VOTE_RULES['scope_flags_invisible_main_page'].value :
post.author.reputation = calculate_reputation(post.author.reputation,
- int(REPUTATION_RULES['lose_by_flagged_lastrevision_3_times']))
+ int(REPUTATION_RULES['lose_by_flagged_lastrevision_3_times'].value))
post.author.save()
reputation = Repute(user=post.author,
- negative=int(REPUTATION_RULES['lose_by_flagged_lastrevision_3_times']),
+ negative=int(REPUTATION_RULES['lose_by_flagged_lastrevision_3_times'].value),
question=question,
reputed_at=timestamp,
reputation_type=-6,
reputation=post.author.reputation)
reputation.save()
- elif post.offensive_flag_count == VOTE_RULES['scope_flags_delete_post']:
+ elif post.offensive_flag_count == VOTE_RULES['scope_flags_delete_post'].value :
post.author.reputation = calculate_reputation(post.author.reputation,
- int(REPUTATION_RULES['lose_by_flagged_lastrevision_5_times']))
+ int(REPUTATION_RULES['lose_by_flagged_lastrevision_5_times'].value))
post.author.save()
reputation = Repute(user=post.author,
- negative=int(REPUTATION_RULES['lose_by_flagged_lastrevision_5_times']),
+ negative=int(REPUTATION_RULES['lose_by_flagged_lastrevision_5_times'].value),
question=question,
reputed_at=timestamp,
reputation_type=-7,
@@ -268,10 +325,10 @@ def onAnswerAccept(answer, user, timestamp=None):
answer.question.save()
answer.author.reputation = calculate_reputation(answer.author.reputation,
- int(REPUTATION_RULES['gain_by_answer_accepted']))
+ int(REPUTATION_RULES['gain_by_answer_accepted'].value))
answer.author.save()
reputation = Repute(user=answer.author,
- positive=int(REPUTATION_RULES['gain_by_answer_accepted']),
+ positive=int(REPUTATION_RULES['gain_by_answer_accepted'].value),
question=answer.question,
reputed_at=timestamp,
reputation_type=2,
@@ -279,10 +336,10 @@ def onAnswerAccept(answer, user, timestamp=None):
reputation.save()
user.reputation = calculate_reputation(user.reputation,
- int(REPUTATION_RULES['gain_by_accepting_answer']))
+ int(REPUTATION_RULES['gain_by_accepting_answer'].value))
user.save()
reputation = Repute(user=user,
- positive=int(REPUTATION_RULES['gain_by_accepting_answer']),
+ positive=int(REPUTATION_RULES['gain_by_accepting_answer'].value),
question=answer.question,
reputed_at=timestamp,
reputation_type=3,
@@ -300,10 +357,10 @@ def onAnswerAcceptCanceled(answer, user, timestamp=None):
answer.question.save()
answer.author.reputation = calculate_reputation(answer.author.reputation,
- int(REPUTATION_RULES['lose_by_accepted_answer_cancled']))
+ int(REPUTATION_RULES['lose_by_accepted_answer_cancled'].value))
answer.author.save()
reputation = Repute(user=answer.author,
- negative=int(REPUTATION_RULES['lose_by_accepted_answer_cancled']),
+ negative=int(REPUTATION_RULES['lose_by_accepted_answer_cancled'].value),
question=answer.question,
reputed_at=timestamp,
reputation_type=-2,
@@ -311,10 +368,10 @@ def onAnswerAcceptCanceled(answer, user, timestamp=None):
reputation.save()
user.reputation = calculate_reputation(user.reputation,
- int(REPUTATION_RULES['lose_by_canceling_accepted_answer']))
+ int(REPUTATION_RULES['lose_by_canceling_accepted_answer'].value))
user.save()
reputation = Repute(user=user,
- negative=int(REPUTATION_RULES['lose_by_canceling_accepted_answer']),
+ negative=int(REPUTATION_RULES['lose_by_canceling_accepted_answer'].value),
question=answer.question,
reputed_at=timestamp,
reputation_type=-1,
@@ -334,9 +391,9 @@ def onUpVoted(vote, post, user, timestamp=None):
if not post.wiki:
author = post.author
todays_rep_gain = Repute.objects.get_reputation_by_upvoted_today(author)
- if todays_rep_gain < int(REPUTATION_RULES['scope_per_day_by_upvotes']):
+ if todays_rep_gain < int(REPUTATION_RULES['scope_per_day_by_upvotes'].value):
author.reputation = calculate_reputation(author.reputation,
- int(REPUTATION_RULES['gain_by_upvoted']))
+ int(REPUTATION_RULES['gain_by_upvoted'].value))
author.save()
question = post
@@ -344,7 +401,7 @@ def onUpVoted(vote, post, user, timestamp=None):
question = post.question
reputation = Repute(user=author,
- positive=int(REPUTATION_RULES['gain_by_upvoted']),
+ positive=int(REPUTATION_RULES['gain_by_upvoted'].value),
question=question,
reputed_at=timestamp,
reputation_type=1,
@@ -366,7 +423,7 @@ def onUpVotedCanceled(vote, post, user, timestamp=None):
if not post.wiki:
author = post.author
author.reputation = calculate_reputation(author.reputation,
- int(REPUTATION_RULES['lose_by_upvote_canceled']))
+ int(REPUTATION_RULES['lose_by_upvote_canceled'].value))
author.save()
question = post
@@ -374,7 +431,7 @@ def onUpVotedCanceled(vote, post, user, timestamp=None):
question = post.question
reputation = Repute(user=author,
- negative=int(REPUTATION_RULES['lose_by_upvote_canceled']),
+ negative=int(REPUTATION_RULES['lose_by_upvote_canceled'].value),
question=question,
reputed_at=timestamp,
reputation_type=-8,
@@ -394,7 +451,7 @@ def onDownVoted(vote, post, user, timestamp=None):
if not post.wiki:
author = post.author
author.reputation = calculate_reputation(author.reputation,
- int(REPUTATION_RULES['lose_by_downvoted']))
+ int(REPUTATION_RULES['lose_by_downvoted'].value))
author.save()
question = post
@@ -402,7 +459,7 @@ def onDownVoted(vote, post, user, timestamp=None):
question = post.question
reputation = Repute(user=author,
- negative=int(REPUTATION_RULES['lose_by_downvoted']),
+ negative=int(REPUTATION_RULES['lose_by_downvoted'].value),
question=question,
reputed_at=timestamp,
reputation_type=-3,
@@ -410,11 +467,11 @@ def onDownVoted(vote, post, user, timestamp=None):
reputation.save()
user.reputation = calculate_reputation(user.reputation,
- int(REPUTATION_RULES['lose_by_downvoting']))
+ int(REPUTATION_RULES['lose_by_downvoting'].value))
user.save()
reputation = Repute(user=user,
- negative=int(REPUTATION_RULES['lose_by_downvoting']),
+ negative=int(REPUTATION_RULES['lose_by_downvoting'].value),
question=question,
reputed_at=timestamp,
reputation_type=-5,
@@ -436,7 +493,7 @@ def onDownVotedCanceled(vote, post, user, timestamp=None):
if not post.wiki:
author = post.author
author.reputation = calculate_reputation(author.reputation,
- int(REPUTATION_RULES['gain_by_downvote_canceled']))
+ int(REPUTATION_RULES['gain_by_downvote_canceled'].value))
author.save()
question = post
@@ -444,7 +501,7 @@ def onDownVotedCanceled(vote, post, user, timestamp=None):
question = post.question
reputation = Repute(user=author,
- positive=int(REPUTATION_RULES['gain_by_downvote_canceled']),
+ positive=int(REPUTATION_RULES['gain_by_downvote_canceled'].value),
question=question,
reputed_at=timestamp,
reputation_type=4,
@@ -452,11 +509,11 @@ def onDownVotedCanceled(vote, post, user, timestamp=None):
reputation.save()
user.reputation = calculate_reputation(user.reputation,
- int(REPUTATION_RULES['gain_by_canceling_downvote']))
+ int(REPUTATION_RULES['gain_by_canceling_downvote'].value))
user.save()
reputation = Repute(user=user,
- positive=int(REPUTATION_RULES['gain_by_canceling_downvote']),
+ positive=int(REPUTATION_RULES['gain_by_canceling_downvote'].value),
question=question,
reputed_at=timestamp,
reputation_type=5,