X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/fa803e983501c159302c30e87f7f82ef8c03746b..49662df87328b4fb2f235c55eebe9d810ca17f82:/forum/models/user.py diff --git a/forum/models/user.py b/forum/models/user.py index abb3989..3e92e16 100644 --- a/forum/models/user.py +++ b/forum/models/user.py @@ -1,12 +1,12 @@ from base import * -from forum import const from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import User as DjangoUser, AnonymousUser as DjangoAnonymousUser from django.db.models import Q try: from hashlib import md5 except: - import md5 + from md5 import new as md5 + import string from random import Random @@ -26,7 +26,7 @@ class UserManager(CachedManager): class AnonymousUser(DjangoAnonymousUser): def get_visible_answers(self, question): - return question.answers.filter(deleted=False) + return question.answers.filter(deleted=None) def can_view_deleted_post(self, post): return False @@ -76,18 +76,19 @@ class AnonymousUser(DjangoAnonymousUser): def can_upload_files(self): return False +def true_if_is_super_or_staff(fn): + def decorated(self, *args, **kwargs): + return self.is_superuser or self.is_staff or fn(self, *args, **kwargs) + return decorated + class User(BaseModel, DjangoUser): is_approved = models.BooleanField(default=False) email_isvalid = models.BooleanField(default=False) - email_key = models.CharField(max_length=32, null=True) - reputation = DenormalizedField(default=1) - gold = DenormalizedField(default=0) - silver = DenormalizedField(default=0) - bronze = DenormalizedField(default=0) - - questions_per_page = models.SmallIntegerField(choices=QUESTIONS_PER_PAGE_CHOICES, default=10) - hide_ignored_questions = models.BooleanField(default=False) + reputation = models.PositiveIntegerField(default=0) + gold = models.PositiveIntegerField(default=0) + silver = models.PositiveIntegerField(default=0) + bronze = models.PositiveIntegerField(default=0) last_seen = models.DateTimeField(default=datetime.datetime.now) real_name = models.CharField(max_length=100, blank=True) @@ -95,7 +96,12 @@ class User(BaseModel, DjangoUser): location = models.CharField(max_length=100, blank=True) date_of_birth = models.DateField(null=True, blank=True) about = models.TextField(blank=True) - + + subscriptions = models.ManyToManyField('Node', related_name='subscribers', through='QuestionSubscription') + + vote_up_count = DenormalizedField("actions", canceled=False, action_type="voteup") + vote_down_count = DenormalizedField("actions", canceled=False, action_type="votedown") + objects = UserManager() @property @@ -104,10 +110,16 @@ class User(BaseModel, DjangoUser): def save(self, *args, **kwargs): if self.reputation < 0: - self.reputation = 1 + self.reputation = 0 + + new = not bool(self.id) super(User, self).save(*args, **kwargs) + if new: + sub_settings = SubscriptionSettings(user=self) + sub_settings.save() + def get_absolute_url(self): return self.get_profile_url() @@ -120,158 +132,111 @@ class User(BaseModel, DjangoUser): def delete_messages(self): self.message_set.all().delete() + @models.permalink def get_profile_url(self): - return "/%s%d/%s" % (_('users/'), self.id, slugify(self.username)) + return ('user_profile', (), {'id': self.id, 'slug': slugify(self.username)}) def get_profile_link(self): profile_link = u'%s' % (self.get_profile_url(),self.username) return mark_safe(profile_link) + def get_visible_answers(self, question): + return question.answers.filter(deleted=None, in_moderation=None) + def get_vote_count_today(self): today = datetime.date.today() - return self.votes.filter(voted_at__range=(today - datetime.timedelta(days=1), today)).count() - - def get_up_vote_count(self): - return self.votes.filter(vote=1).count() - - def get_down_vote_count(self): - return self.votes.filter(vote=-1).count() + return self.actions.filter(canceled=False, action_type__in=("voteup", "votedown"), + action_date__range=(today - datetime.timedelta(days=1), today)).count() def get_reputation_by_upvoted_today(self): today = datetime.datetime.now() - sum = self.reputes.filter( - models.Q(reputation_type=TYPE_REPUTATION_GAIN_BY_UPVOTED) | - models.Q(reputation_type=TYPE_REPUTATION_LOST_BY_UPVOTE_CANCELED), - reputed_at__range=(today - datetime.timedelta(days=1), today)).aggregate(models.Sum('value')) - - if sum.get('value__sum', None) is not None: return sum['value__sum'] + sum = self.reputes.filter(reputed_at__range=(today - datetime.timedelta(days=1), today)).aggregate(models.Sum('value')) + #todo: redo this, maybe transform in the daily cap + #if sum.get('value__sum', None) is not None: return sum['value__sum'] return 0 def get_flagged_items_count_today(self): today = datetime.date.today() - return self.flaggeditems.filter(flagged_at__range=(today - datetime.timedelta(days=1), today)).count() - - def get_visible_answers(self, question): - if self.is_superuser: - return question.answers - else: - return question.answers.filter(models.Q(deleted=False) | models.Q(deleted_by=self)) + return self.actions.filter(canceled=False, action_type="flag", + action_date__range=(today - datetime.timedelta(days=1), today)).count() + @true_if_is_super_or_staff def can_view_deleted_post(self, post): - return self.is_superuser or post.author == self + return post.author == self + @true_if_is_super_or_staff def can_vote_up(self): - return self.reputation >= int(settings.REP_TO_VOTE_UP) or self.is_superuser + return self.reputation >= int(settings.REP_TO_VOTE_UP) + @true_if_is_super_or_staff def can_vote_down(self): - return self.reputation >= int(settings.REP_TO_VOTE_DOWN) or self.is_superuser + return self.reputation >= int(settings.REP_TO_VOTE_DOWN) def can_flag_offensive(self, post=None): if post is not None and post.author == self: return False - return self.is_superuser or self.reputation >= int(settings.REP_TO_FLAG) + return self.is_superuser or self.is_staff or self.reputation >= int(settings.REP_TO_FLAG) + @true_if_is_super_or_staff def can_view_offensive_flags(self, post=None): if post is not None and post.author == self: return True - return self.is_superuser or self.reputation >= int(settings.REP_TO_VIEW_FLAGS) + return self.reputation >= int(settings.REP_TO_VIEW_FLAGS) + @true_if_is_super_or_staff def can_comment(self, post): return self == post.author or self.reputation >= int(settings.REP_TO_COMMENT - ) or self.is_superuser or (post.__class__.__name__ == "Answer" and self == post.question.author) + ) or (post.__class__.__name__ == "Answer" and self == post.question.author) + @true_if_is_super_or_staff def can_like_comment(self, comment): - return self != comment.user and (self.reputation >= int(settings.REP_TO_LIKE_COMMENT) or self.is_superuser) + return self != comment.author and (self.reputation >= int(settings.REP_TO_LIKE_COMMENT)) + @true_if_is_super_or_staff def can_edit_comment(self, comment): - return (comment.user == self and comment.added_at >= datetime.datetime.now() - datetime.timedelta(minutes=60) + return (comment.author == self and comment.added_at >= datetime.datetime.now() - datetime.timedelta(minutes=60) ) or self.is_superuser + @true_if_is_super_or_staff def can_delete_comment(self, comment): - return self == comment.user or self.reputation >= int(settings.REP_TO_DELETE_COMMENTS) or self.is_superuser + return self == comment.author or self.reputation >= int(settings.REP_TO_DELETE_COMMENTS) + @true_if_is_super_or_staff def can_accept_answer(self, answer): - return self.is_superuser or self == answer.question.author + return self == answer.question.author + @true_if_is_super_or_staff def can_edit_post(self, post): - return self.is_superuser or self == post.author or self.reputation >= int(settings.REP_TO_EDIT_OTHERS + return self == post.author or self.reputation >= int(settings.REP_TO_EDIT_OTHERS ) or (post.wiki and self.reputation >= int(settings.REP_TO_EDIT_WIKI)) + @true_if_is_super_or_staff def can_retag_questions(self): return self.reputation >= int(settings.REP_TO_RETAG) + @true_if_is_super_or_staff def can_close_question(self, question): - return self.is_superuser or (self == question.author and self.reputation >= int(settings.REP_TO_CLOSE_OWN) + return (self == question.author and self.reputation >= int(settings.REP_TO_CLOSE_OWN) ) or self.reputation >= int(settings.REP_TO_CLOSE_OTHERS) + @true_if_is_super_or_staff def can_reopen_question(self, question): - return self.is_superuser or (self == question.author and self.reputation >= settings.REP_TO_REOPEN_OWN) + return self == question.author and self.reputation >= settings.REP_TO_REOPEN_OWN + @true_if_is_super_or_staff def can_delete_post(self, post): - return self.is_superuser or (self == post.author and (post.__class__.__name__ == "Answer" or - not post.answers.filter(~Q(author=self)).count())) + if post.node_type == "comment": + return self.can_delete_comment(post) + + return (self == post.author and (post.__class__.__name__ == "Answer" or + not post.answers.exclude(author=self).count())) + @true_if_is_super_or_staff def can_upload_files(self): - return self.is_superuser or self.reputation >= int(settings.REP_TO_UPLOAD) - - class Meta: - app_label = 'forum' - -class Activity(GenericContent): - """ - We keep some history data for user activities - """ - user = models.ForeignKey(User) - activity_type = models.SmallIntegerField(choices=TYPE_ACTIVITY) - active_at = models.DateTimeField(default=datetime.datetime.now) - is_auditted = models.BooleanField(default=False) + return self.reputation >= int(settings.REP_TO_UPLOAD) class Meta: app_label = 'forum' - db_table = u'activity' - - def __unicode__(self): - return u'[%s] was active at %s' % (self.user.username, self.active_at) - - def save(self, *args, **kwargs): - super(Activity, self).save(*args, **kwargs) - if self._is_new: - activity_record.send(sender=self.activity_type, instance=self) - - @property - def node(self): - if self.activity_type in (const.TYPE_ACTIVITY_ANSWER, const.TYPE_ACTIVITY_ASK_QUESTION, - const.TYPE_ACTIVITY_MARK_ANSWER, const.TYPE_ACTIVITY_COMMENT_QUESTION, const.TYPE_ACTIVITY_COMMENT_ANSWER): - return self.content_object.leaf - - if self.activity_type in (const.TYPE_ACTIVITY_UPDATE_ANSWER, const.TYPE_ACTIVITY_UPDATE_QUESTION): - return self.content_object.node.leaf - - raise NotImplementedError() - - @property - def type_as_string(self): - if self.activity_type == const.TYPE_ACTIVITY_ASK_QUESTION: - return _("asked") - elif self.activity_type == const.TYPE_ACTIVITY_ANSWER: - return _("answered") - elif self.activity_type == const.TYPE_ACTIVITY_MARK_ANSWER: - return _("marked an answer") - elif self.activity_type == const.TYPE_ACTIVITY_UPDATE_QUESTION: - return _("edited a question") - elif self.activity_type == const.TYPE_ACTIVITY_COMMENT_QUESTION: - return _("commented a question") - elif self.activity_type == const.TYPE_ACTIVITY_COMMENT_ANSWER: - return _("commented an answer") - elif self.activity_type == const.TYPE_ACTIVITY_UPDATE_ANSWER: - return _("edited an answer") - elif self.activity_type == const.TYPE_ACTIVITY_PRIZE: - return _("received badge") - else: - raise NotImplementedError() - - -activity_record = django.dispatch.Signal(providing_args=['instance']) class SubscriptionSettings(models.Model): user = models.OneToOneField(User, related_name='subscription_settings') @@ -279,10 +244,10 @@ class SubscriptionSettings(models.Model): enable_notifications = models.BooleanField(default=True) #notify if - member_joins = models.CharField(max_length=1, default='n', choices=const.NOTIFICATION_CHOICES) - new_question = models.CharField(max_length=1, default='d', choices=const.NOTIFICATION_CHOICES) - new_question_watched_tags = models.CharField(max_length=1, default='i', choices=const.NOTIFICATION_CHOICES) - subscribed_questions = models.CharField(max_length=1, default='i', choices=const.NOTIFICATION_CHOICES) + member_joins = models.CharField(max_length=1, default='n') + new_question = models.CharField(max_length=1, default='d') + new_question_watched_tags = models.CharField(max_length=1, default='i') + subscribed_questions = models.CharField(max_length=1, default='i') #auto_subscribe_to all_questions = models.BooleanField(default=False)