예제 #1
0
class Category(models.Model, translations.TranslatedObjectMixin):
    """
    Category is language-aware and connected to the Entry model via
    a many to many relationship.
    """

    ordering = models.SmallIntegerField(_('ordering'), default=0)

    objects = translations.TranslatedObjectManager()

    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('categories')
        ordering = ['ordering']

    def __str__(self):
        try:
            translation = self.translation
        except models.ObjectDoesNotExist:
            return ugettext('Unnamed category')

        if translation:
            return '%s' % translation

        return ugettext('Unnamed category')
예제 #2
0
class Forum(models.Model, translations.TranslatedObjectMixin):
    category = models.ForeignKey(Category,
                                 related_name='forums',
                                 verbose_name=_('Category'))
    position = models.IntegerField(_('Position'), blank=True, default=0)
    moderators = models.ManyToManyField(User,
                                        blank=True,
                                        null=True,
                                        verbose_name=_('Moderators'))
    updated = models.DateTimeField(_('Updated'), auto_now=True)

    class Meta:
        ordering = ['position']
        verbose_name = _('Forum')
        verbose_name_plural = _('Forums')

    objects = translations.TranslatedObjectManager()

    def _get_last_post(self):
        def _get_last_post_inner():
            topic_children = self.topics.values_list('id', flat=True)
            return Post.objects.filter(topic__id__in=topic_children).latest()

        return _get_cached_prop_val(self, '_last_post', _get_last_post_inner)

    last_post = property(_get_last_post)

    def _get_topic_count(self):
        return _get_cached_prop_val(self, '_topic_count',
                                    lambda: self.topics.all().count())

    topic_count = property(_get_topic_count)

    def _get_post_count(self):
        def _get_post_count_inner():
            cnt = 0
            for t in self.topics.all():
                cnt += t.post_count
            return cnt

        return _get_cached_prop_val(self, '_post_count', _get_post_count_inner)

    post_count = property(_get_post_count)

    @property
    def posts(self):
        return Post.objects.filter(topic__forum__id=self.id).select_related()
예제 #3
0
class Category(models.Model, translations.TranslatedObjectMixin):
    """
    Category is language-aware and connected to the Entry model via
    a many to many relationship.
    """

    ordering = models.SmallIntegerField(_('ordering'), default=0)

    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('categories')
        ordering = ['ordering',]

    objects = translations.TranslatedObjectManager()

    def __unicode__(self):
        trans = translations.TranslatedObjectMixin.__unicode__(self)
        return trans or _('Unnamed category')
예제 #4
0
class Category(models.Model, translations.TranslatedObjectMixin):
    groups = models.ManyToManyField(
        Group,
        blank=True,
        null=True,
        verbose_name=_('Groups'),
        help_text=_('Only users from these groups can see this category'))

    ordering = models.SmallIntegerField(_('ordering'), default=0)

    class Meta:
        verbose_name = _('Category')
        verbose_name_plural = _('Categories')

    objects = translations.TranslatedObjectManager()

    def forum_count(self):
        return self.forums.all().count()

    @property
    def topics(self):
        return Topic.objects.filter(
            forum__category__id=self.id).select_related()

    @property
    def posts(self):
        return Post.objects.filter(
            topic__forum__category__id=self.id).select_related()

    def has_access(self, user):
        if self.groups.exists():
            if user.is_authenticated():
                if not self.groups.filter(user__pk=user.id).exists():
                    return False
            else:
                return False
        return True