def cloud_for_model(self, model, steps=4, distribution=LOGARITHMIC, filters=None, min_count=None): """ Obtain a list of tags associated with instances of the given Model, giving each tag a ``count`` attribute indicating how many times it has been used and a ``font_size`` attribute for use in displaying a tag cloud. ``steps`` defines the range of font sizes - ``font_size`` will be an integer between 1 and ``steps`` (inclusive). ``distribution`` defines the type of font size distribution algorithm which will be used - logarithmic or linear. It must be either ``tagging.utils.LOGARITHMIC`` or ``tagging.utils.LINEAR``. To limit the tags displayed in the cloud to those associated with a subset of the Model's instances, pass a dictionary of field lookups to be applied to the given Model as the ``filters`` argument. To limit the tags displayed in the cloud to those with a ``count`` greater than or equal to ``min_count``, pass a value for the ``min_count`` argument. """ tags = list(self.usage_for_model(model, counts=True, filters=filters, min_count=min_count)) return calculate_cloud(tags, steps, distribution)
def cloud_for_category(self, category, **kwargs): """ returns tag cloud for all objects in certain category. """ priority, steps = kwargs.get('priority', PRIMARY_TAG), kwargs.get('steps', 4) distribution = kwargs.get('distribution', LOGARITHMIC) prio_sql = '' if type(priority) == int: prio_sql = 'tagging_taggeditem.priority = %d AND' % priority elif type(priority) == tuple: prio_sql = 'tagging_taggeditem.priority IN %s AND' % str(priority) else: raise TypeError('kwarg priority should be tuple or int type. %s' % str(type(priority))) category_sql = '' if category: category_sql = 'tagging_taggeditem.category_id = %d AND' % category.pk sql = """ SELECT tagging_tag.id, COUNT(tagging_tag.name) AS cnt FROM tagging_taggeditem, tagging_tag WHERE %s %s tagging_tag.id = tagging_taggeditem.tag_id GROUP BY tagging_tag.name ORDER BY cnt DESC """ % (category_sql, prio_sql) cur = connection.cursor() cur.execute(sql) data = cur.fetchall() tags = [] for tag_id, cnt in data: o = Tag.objects.get(pk=tag_id) setattr(o, 'count', cnt) tags.append(o) return calculate_cloud(tags, steps, distribution)