X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/a9eef437702d5df7a2f97010e6798c689371808c..b6f6103a8b578d13047219ef3812cea09001eea2:/forum/models/meta.py?ds=sidebyside diff --git a/forum/models/meta.py b/forum/models/meta.py old mode 100755 new mode 100644 index 3dfd3e8..fca763f --- a/forum/models/meta.py +++ b/forum/models/meta.py @@ -1,89 +1,99 @@ +from django.utils.translation import ugettext as _ from base import * -class VoteManager(models.Manager): - def get_up_vote_count_from_user(self, user): - if user is not None: - return self.filter(user=user, vote=1).count() - else: - return 0 - - def get_down_vote_count_from_user(self, user): - if user is not None: - return self.filter(user=user, vote=-1).count() - else: - return 0 - - def get_votes_count_today_from_user(self, user): - if user is not None: - today = datetime.date.today() - return self.filter(user=user, voted_at__range=(today, today + datetime.timedelta(1))).count() - - else: - return 0 - +class Vote(models.Model): + user = models.ForeignKey(User, related_name="votes") + node = models.ForeignKey(Node, related_name="votes") + value = models.SmallIntegerField() + action = models.OneToOneField(Action, related_name="vote") + voted_at = models.DateTimeField(default=datetime.datetime.now) -class Vote(MetaContent): - VOTE_UP = +1 - VOTE_DOWN = -1 - VOTE_CHOICES = ( - (VOTE_UP, u'Up'), - (VOTE_DOWN, u'Down'), - ) + class Meta: + app_label = 'forum' + unique_together = ('user', 'node') - vote = models.SmallIntegerField(choices=VOTE_CHOICES) - voted_at = models.DateTimeField(default=datetime.datetime.now) - objects = VoteManager() +class Flag(models.Model): + user = models.ForeignKey(User, related_name="flags") + node = models.ForeignKey(Node, related_name="flags") + reason = models.CharField(max_length=300) + action = models.OneToOneField(Action, related_name="flag") + flagged_at = models.DateTimeField(default=datetime.datetime.now) - class Meta(MetaContent.Meta): - unique_together = ('content_type', 'object_id', 'user') - db_table = u'vote' + class Meta: + app_label = 'forum' + unique_together = ('user', 'node') - def __unicode__(self): - return '[%s] voted at %s: %s' %(self.user, self.voted_at, self.vote) - - def is_upvote(self): - return self.vote == self.VOTE_UP - - def is_downvote(self): - return self.vote == self.VOTE_DOWN - - -class FlaggedItemManager(models.Manager): - def get_flagged_items_count_today(self, user): - if user is not None: - today = datetime.date.today() - return self.filter(user=user, flagged_at__range=(today, today + datetime.timedelta(1))).count() +class BadgesQuerySet(models.query.QuerySet): + def get(self, *args, **kwargs): + try: + pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact')][0] + except: + return super(BadgesQuerySet, self).get(*args, **kwargs) + + from forum.badges.base import BadgesMeta + badge = BadgesMeta.by_id.get(int(pk), None) + if not badge: + return super(BadgesQuerySet, self).get(*args, **kwargs) + return badge.ondb + + +class BadgeManager(models.Manager): + use_for_related_fields = True + + def get_query_set(self): + return BadgesQuerySet(self.model) + +class Badge(models.Model): + GOLD = 1 + SILVER = 2 + BRONZE = 3 + + type = models.SmallIntegerField() + cls = models.CharField(max_length=50, null=True) + awarded_count = models.PositiveIntegerField(default=0) + + awarded_to = models.ManyToManyField(User, through='Award', related_name='badges') + + objects = BadgeManager() + + @property + def name(self): + cls = self.__dict__.get('_class', None) + return cls and cls.name or _("Unknown") + + @property + def description(self): + cls = self.__dict__.get('_class', None) + return cls and cls.description or _("No description available") + + @models.permalink + def get_absolute_url(self): + return ('badge', [], {'id': self.id, 'slug': slugify(self.name)}) + + def save(self, *args, **kwargs): + if isinstance(self.awarded_count, models.expressions.ExpressionNode): + super(Badge, self).save(*args, **kwargs) + self.awarded_count = self.__class__.objects.filter(id=self.id).values_list('awarded_count', flat=True)[0] else: - return 0 + super(Badge, self).save(*args, **kwargs) -class FlaggedItem(MetaContent): - """A flag on a Question or Answer indicating offensive content.""" - flagged_at = models.DateTimeField(default=datetime.datetime.now) - objects = FlaggedItemManager() + class Meta: + app_label = 'forum' - class Meta(MetaContent.Meta): - unique_together = ('content_type', 'object_id', 'user') - db_table = u'flagged_item' - def __unicode__(self): - return '[%s] flagged at %s' %(self.user, self.flagged_at) +class Award(models.Model): + user = models.ForeignKey(User) + badge = models.ForeignKey('Badge', related_name="awards") + node = models.ForeignKey(Node, null=True) -class Comment(MetaContent): - comment = models.CharField(max_length=300) - added_at = models.DateTimeField(default=datetime.datetime.now) + awarded_at = models.DateTimeField(default=datetime.datetime.now) - class Meta(MetaContent.Meta): - ordering = ('-added_at',) - db_table = u'comment' + trigger = models.ForeignKey(Action, related_name="awards", null=True) + action = models.OneToOneField(Action, related_name="award") - def save(self,**kwargs): - super(Comment,self).save(**kwargs) - try: - ping_google() - except Exception: - logging.debug('problem pinging google did you register you sitemap with google?') - def __unicode__(self): - return self.comment \ No newline at end of file + class Meta: + unique_together = ('user', 'badge', 'node') + app_label = 'forum' \ No newline at end of file