diff options
Diffstat (limited to 'forum/auth.py')
-rwxr-xr-x | forum/auth.py | 98 |
1 files changed, 62 insertions, 36 deletions
diff --git a/forum/auth.py b/forum/auth.py index 3533b9ce..5d6e71c4 100755 --- a/forum/auth.py +++ b/forum/auth.py @@ -5,16 +5,14 @@ The actions a User is authorised to perform are dependent on their reputation and superuser status. """ import datetime -from django.contrib.contenttypes.models import ContentType from django.utils.translation import ugettext as _ from django.db import transaction from models import Repute from models import Question from models import Answer +from models import mark_offensive, delete_post_or_answer from const import TYPE_REPUTATION import logging -question_type = ContentType.objects.get_for_model(Question) -answer_type = ContentType.objects.get_for_model(Answer) VOTE_UP = 15 FLAG_OFFENSIVE = 15 @@ -198,7 +196,9 @@ def calculate_reputation(origin, offset): return 1 @transaction.commit_on_success -def onFlaggedItem(item, post, user): +def onFlaggedItem(item, post, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() item.save() post.offensive_flag_count = post.offensive_flag_count + 1 @@ -209,12 +209,12 @@ def onFlaggedItem(item, post, user): post.author.save() question = post - if ContentType.objects.get_for_model(post) == answer_type: + if isinstance(post, Answer): question = post.question reputation = Repute(user=post.author, negative=int(REPUTATION_RULES['lose_by_flagged']), - question=question, reputed_at=datetime.datetime.now(), + question=question, reputed_at=timestamp, reputation_type=-4, reputation=post.author.reputation) reputation.save() @@ -228,7 +228,7 @@ def onFlaggedItem(item, post, user): reputation = Repute(user=post.author, negative=int(REPUTATION_RULES['lose_by_flagged_lastrevision_3_times']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=-6, reputation=post.author.reputation) reputation.save() @@ -241,21 +241,28 @@ def onFlaggedItem(item, post, user): reputation = Repute(user=post.author, negative=int(REPUTATION_RULES['lose_by_flagged_lastrevision_5_times']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=-7, reputation=post.author.reputation) reputation.save() post.deleted = True - #post.deleted_at = datetime.datetime.now() + #post.deleted_at = timestamp #post.deleted_by = Admin post.save() - + mark_offensive.send( + sender=post.__class__, + instance=post, + mark_by=user + ) @transaction.commit_on_success -def onAnswerAccept(answer, user): +def onAnswerAccept(answer, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() + answer.accepted = True - answer.accepted_at = datetime.datetime.now() + answer.accepted_at = timestamp answer.question.answer_accepted = True answer.save() answer.question.save() @@ -266,7 +273,7 @@ def onAnswerAccept(answer, user): reputation = Repute(user=answer.author, positive=int(REPUTATION_RULES['gain_by_answer_accepted']), question=answer.question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=2, reputation=answer.author.reputation) reputation.save() @@ -277,13 +284,15 @@ def onAnswerAccept(answer, user): reputation = Repute(user=user, positive=int(REPUTATION_RULES['gain_by_accepting_answer']), question=answer.question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=3, reputation=user.reputation) reputation.save() @transaction.commit_on_success -def onAnswerAcceptCanceled(answer, user): +def onAnswerAcceptCanceled(answer, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() answer.accepted = False answer.accepted_at = None answer.question.answer_accepted = False @@ -296,7 +305,7 @@ def onAnswerAcceptCanceled(answer, user): reputation = Repute(user=answer.author, negative=int(REPUTATION_RULES['lose_by_accepted_answer_cancled']), question=answer.question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=-2, reputation=answer.author.reputation) reputation.save() @@ -307,13 +316,15 @@ def onAnswerAcceptCanceled(answer, user): reputation = Repute(user=user, negative=int(REPUTATION_RULES['lose_by_canceling_accepted_answer']), question=answer.question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=-1, reputation=user.reputation) reputation.save() @transaction.commit_on_success -def onUpVoted(vote, post, user): +def onUpVoted(vote, post, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() vote.save() post.vote_up_count = int(post.vote_up_count) + 1 @@ -322,25 +333,28 @@ def onUpVoted(vote, post, user): if not post.wiki: author = post.author - if Repute.objects.get_reputation_by_upvoted_today(author) < int(REPUTATION_RULES['scope_per_day_by_upvotes']): + todays_rep_gain = Repute.objects.get_reputation_by_upvoted_today(author) + if todays_rep_gain < int(REPUTATION_RULES['scope_per_day_by_upvotes']): author.reputation = calculate_reputation(author.reputation, int(REPUTATION_RULES['gain_by_upvoted'])) author.save() question = post - if ContentType.objects.get_for_model(post) == answer_type: + if isinstance(post, Answer): question = post.question reputation = Repute(user=author, positive=int(REPUTATION_RULES['gain_by_upvoted']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=1, reputation=author.reputation) reputation.save() @transaction.commit_on_success -def onUpVotedCanceled(vote, post, user): +def onUpVotedCanceled(vote, post, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() vote.delete() post.vote_up_count = int(post.vote_up_count) - 1 @@ -356,19 +370,21 @@ def onUpVotedCanceled(vote, post, user): author.save() question = post - if ContentType.objects.get_for_model(post) == answer_type: + if isinstance(post, Answer): question = post.question reputation = Repute(user=author, negative=int(REPUTATION_RULES['lose_by_upvote_canceled']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=-8, reputation=author.reputation) reputation.save() @transaction.commit_on_success -def onDownVoted(vote, post, user): +def onDownVoted(vote, post, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() vote.save() post.vote_down_count = int(post.vote_down_count) + 1 @@ -382,13 +398,13 @@ def onDownVoted(vote, post, user): author.save() question = post - if ContentType.objects.get_for_model(post) == answer_type: + if isinstance(post, Answer): question = post.question reputation = Repute(user=author, negative=int(REPUTATION_RULES['lose_by_downvoted']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=-3, reputation=author.reputation) reputation.save() @@ -400,13 +416,15 @@ def onDownVoted(vote, post, user): reputation = Repute(user=user, negative=int(REPUTATION_RULES['lose_by_downvoting']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=-5, reputation=user.reputation) reputation.save() @transaction.commit_on_success -def onDownVotedCanceled(vote, post, user): +def onDownVotedCanceled(vote, post, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() vote.delete() post.vote_down_count = int(post.vote_down_count) - 1 @@ -422,13 +440,13 @@ def onDownVotedCanceled(vote, post, user): author.save() question = post - if ContentType.objects.get_for_model(post) == answer_type: + if isinstance(post, Answer): question = post.question reputation = Repute(user=author, positive=int(REPUTATION_RULES['gain_by_downvote_canceled']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=4, reputation=author.reputation) reputation.save() @@ -440,12 +458,13 @@ def onDownVotedCanceled(vote, post, user): reputation = Repute(user=user, positive=int(REPUTATION_RULES['gain_by_canceling_downvote']), question=question, - reputed_at=datetime.datetime.now(), + reputed_at=timestamp, reputation_type=5, reputation=user.reputation) reputation.save() -def onDeleteCanceled(post, user): +#here timestamp is not used, I guess added for consistency +def onDeleteCanceled(post, user, timestamp=None): post.deleted = False post.deleted_by = None post.deleted_at = None @@ -462,10 +481,12 @@ def onDeleteCanceled(post, user): tag.deleted_at = None tag.save() -def onDeleted(post, user): +def onDeleted(post, user, timestamp=None): + if timestamp is None: + timestamp = datetime.datetime.now() post.deleted = True post.deleted_by = user - post.deleted_at = datetime.datetime.now() + post.deleted_at = timestamp post.save() if isinstance(post, Question): @@ -473,7 +494,7 @@ def onDeleted(post, user): if tag.used_count == 1: tag.deleted = True tag.deleted_by = user - tag.deleted_at = datetime.datetime.now() + tag.deleted_at = timestamp else: tag.used_count = tag.used_count - 1 tag.save() @@ -496,3 +517,8 @@ def onDeleted(post, user): elif isinstance(post, Answer): Question.objects.update_answer_count(post.question) logging.debug('updated answer count to %d' % post.question.answer_count) + delete_post_or_answer.send( + sender=post.__class__, + instance=post, + delete_by=user + ) |