Beispiel #1
0
class LocalizedUserProfile(models.Model):
    auth_user = models.ForeignKey(User,
                                  related_name='localized_askbot_profiles')
    about = models.TextField(blank=True)
    language_code = LanguageCodeField(db_index=True)
    reputation = models.PositiveIntegerField(default=0, db_index=True)
    is_claimed = models.BooleanField(
        default=False,
        db_index=True,
        help_text='True, if user selects this language')

    class Meta:
        app_label = 'askbot'

    def anonymize(self):
        """Removes personal data"""
        self.about = ''
        self.reputation = 0
        self.is_claimed = False

    def get_cache_key(self):
        return get_localized_profile_cache_key(self.auth_user,
                                               self.language_code)

    def get_reputation(self):
        return self.reputation + const.MIN_REPUTATION

    def update_cache(self):
        key = self.get_cache_key()
        cache.set(key, self)

    def save(self, *args, **kwargs):
        self.update_cache()
        super(LocalizedUserProfile, self).save(*args, **kwargs)
Beispiel #2
0
class Repute(models.Model):
    """The reputation histories for user"""
    user = models.ForeignKey(User)
    # TODO: combine positive and negative to one value
    positive = models.SmallIntegerField(default=0)
    negative = models.SmallIntegerField(default=0)
    question = models.ForeignKey('Post', null=True, blank=True)
    language_code = LanguageCodeField()
    reputed_at = models.DateTimeField(default=timezone.now)
    reputation_type = models.SmallIntegerField(choices=const.TYPE_REPUTATION)
    reputation = models.IntegerField(default=1)

    # comment that must be used if reputation_type == 10
    # assigned_by_moderator - so that reason can be displayed
    # in that case Question field will be blank
    comment = models.CharField(max_length=128, null=True)

    objects = ReputeManager()

    def __unicode__(self):
        return u'[%s]\' reputation changed at %s' % (self.user.username,
                                                     self.reputed_at)

    class Meta:
        app_label = 'askbot'
        db_table = u'repute'
        verbose_name = _("repute")
        verbose_name_plural = _("repute")

    def save(self, *args, **kwargs):
        if self.question:
            self.language_code = self.question.language_code
        else:
            self.language_code = get_language()
        super(Repute, self).save(*args, **kwargs)

    def get_explanation_snippet(self):
        """returns HTML snippet with a link to related question
        or a text description for a the reason of the reputation change

        in the implementation description is returned only
        for Repute.reputation_type == 10 - "assigned by the moderator"

        part of the purpose of this method is to hide this idiosyncracy
        """
        if self.reputation_type == 10:  # TODO: hide magic number
            return _('<em>Changed by moderator. Reason:</em> %(reason)s') \
                                                    % {'reason': self.comment}
        else:  # .negative is < 0 so we add!
            delta = self.positive + self.negative
            link_title_data = {
                'points': abs(delta),
                'username': self.user.username,
                'question_title': self.question.thread.title
            }

            return '<a href="%(url)s">%(question_title)s</a>' % {
                'url': self.question.get_absolute_url(),
                'question_title': escape(self.question.thread.title),
            }
Beispiel #3
0
class TagSynonym(models.Model):

    source_tag_name = models.CharField(max_length=255, unique=True)
    target_tag_name = models.CharField(max_length=255, db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)
    owned_by = models.ForeignKey(User, related_name='tag_synonyms')
    auto_rename_count = models.IntegerField(default=0)
    last_auto_rename_at = models.DateTimeField(auto_now=True)
    language_code = LanguageCodeField()

    class Meta:
        app_label = 'askbot'

    def __unicode__(self):
        return u'%s -> %s' % (self.source_tag_name, self.target_tag_name)
Beispiel #4
0
class Tag(models.Model):
    #a couple of status constants
    STATUS_SUGGESTED = 0
    STATUS_ACCEPTED = 1

    name = models.CharField(max_length=255)
    created_by = models.ForeignKey(django_settings.AUTH_USER_MODEL,
                                   related_name='created_tags')
    language_code = LanguageCodeField()
    suggested_by = models.ManyToManyField(
        django_settings.AUTH_USER_MODEL, related_name='suggested_tags',
        help_text = 'Works only for suggested tags for tag moderation'
    )

    status = models.SmallIntegerField(default = STATUS_ACCEPTED)

    # Denormalised data
    used_count = models.PositiveIntegerField(default=0)

    deleted     = models.BooleanField(default=False)
    deleted_at  = models.DateTimeField(null=True, blank=True)
    deleted_by  = models.ForeignKey(django_settings.AUTH_USER_MODEL,
                                    null=True, blank=True,
                                    related_name='deleted_tags')

    tag_wiki = models.OneToOneField(
                                'Post',
                                null=True,
                                related_name = 'described_tag'
                            )

    objects = TagManager()

    class Meta:
        app_label = 'askbot'
        db_table = u'tag'
        ordering = ('-used_count', 'name')
        unique_together = ('name', 'language_code')

    def __unicode__(self):
        return self.name

    def decrement_used_count(self, delta=1):
        if self.used_count >= delta:
            self.used_count = self.used_count - delta