Example #1
0
def get_usage(counts=True):
    model_table = Category._meta.db_table
    join_table_name = Information.categories.through._meta.db_table
    model_pk = "%s.%s" % (model_table, Category._meta.pk.column)
    query = """
    SELECT DISTINCT %(model_pk)s%(count_sql)s
    FROM
        %(join_table)s
        INNER JOIN %(model_table)s
            ON %(join_table)s.category_id = %(model_pk)s
    GROUP BY %(model_pk)s
    """ % {
        "count_sql": counts and (", COUNT(%s) AS cnt" % model_pk) or "",
        "model_table": model_table,
        "model_pk": model_pk,
        "join_table": join_table_name,
    }

    cursor = connection.cursor()
    cursor.execute(query)
    categories = []
    for row in cursor.fetchall():
        t = Category(*row[:1])
        if counts:
            t.count = row[1]
        categories.append(t)
    return categories
Example #2
0
    link = models.CharField(_('link'), max_length=256)
    keywords = models.CharField(_('keywords'), max_length=256,
                                null=True, blank=True,
                                help_text=_('Comma separated keywords'))
    lang = models.CharField(_('language'), max_length=3, choices=LANGUAGES)

    published_on = models.DateTimeField(_('published on'), auto_now_add=True)
    last_changed = models.DateTimeField(_('last change'), auto_now=True,
                                        editable=False)

    categories = models.ManyToManyField(Category, verbose_name=_('categories'),
        related_name='informations', null=True, blank=True)

    class Meta:
        get_latest_by = 'published_on'
        ordering = ['-published_on']
        verbose_name = _('information')
        verbose_name_plural = _('informations')

    @models.permalink
    def get_absolute_url(self):
        return ('infoch-detail', (), {'slug': self.slug})

    def __unicode__(self):
        return self.title


Category.add_to_class('moderator', models.ForeignKey(User,
                        verbose_name=_('moderator'),
                        related_name='managed_categories'))