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 test_linear_distribution(self): sizes = {} for tag in calculate_cloud(self.tags, steps=5, distribution=LINEAR): sizes[tag.font_size] = sizes.get(tag.font_size, 0) + 1 # This isn't a pre-calculated test, just making sure it's consistent self.assertEquals(sizes[1], 97) self.assertEquals(sizes[2], 12) self.assertEquals(sizes[3], 7) self.assertEquals(sizes[4], 2) self.assertEquals(sizes[5], 4)
def test_default_distribution(self): sizes = {} for tag in calculate_cloud(self.tags, steps=5): sizes[tag.font_size] = sizes.get(tag.font_size, 0) + 1 # This isn't a pre-calculated test, just making sure it's consistent self.assertEquals(sizes[1], 48) self.assertEquals(sizes[2], 30) self.assertEquals(sizes[3], 19) self.assertEquals(sizes[4], 15) self.assertEquals(sizes[5], 10)
def test_invalid_distribution(self): try: calculate_cloud(self.tags, steps=5, distribution="cheese") except ValueError, ve: self.assertEquals(str(ve), "Invalid distribution algorithm specified: cheese.")