Пример #1
0
 def setUp(self):
     self.tags = []
     for line in open(os.path.join(os.path.dirname(__file__), 'tags.txt')).readlines():
         name, count = line.rstrip().split()
         tag = Tag(name=name)
         tag.count = int(count)
         self.tags.append(tag)
Пример #2
0
 def setUp(self):
     self.tags = []
     for line in open(os.path.join(os.path.dirname(__file__), 'test_tags.txt')).readlines():
         name, count = line.rstrip().split()
         tag = Tag(name=name)
         tag.count = int(count)
         self.tags.append(tag)
Пример #3
0
    def testTagClouds(self):
        tags = []
        for line in open(os.path.join(os.path.dirname(__file__),
                                      'tags.txt')).readlines():
            name, count = line.rstrip().split()
            tag = Tag(name=name)
            tag.count = int(count)
            tags.append(tag)

        sizes = {}
        for tag in calculate_cloud(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.assertEqual({1: 48, 2: 30, 3: 19, 4: 15, 5: 10}, sizes)

        sizes = {}
        for tag in calculate_cloud(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.assertEqual({1: 97, 2: 12, 3: 7, 4: 2, 5: 4}, sizes)

        self.assertRaises(ValueError,
                          calculate_cloud,
                          tags,
                          steps=5,
                          distribution='cheese')
Пример #4
0
 def setUp(self):
     self.tags = []
     for line in default_tags:
         name, count = line.rstrip().split()
         tag = Tag(name=name)
         tag.count = int(count)
         self.tags.append(tag)
Пример #5
0
def _tag_usage_for_queryset(model, counts=False, min_count=None, extra_joins=None,
                            extra_criteria=None, params=None):
        from tagging.models import Tag
        """
        Perform the custom SQL query for ``usage_for_model`` and
        ``usage_for_queryset``.
        """
        if min_count is not None: counts = True
        
        model_table = qn(model._meta.db_table)
        model_pk = '%s.%s' % (model_table, "object_id")
        query = """
        SELECT DISTINCT %(tag)s.id, %(tag)s.name%(count_sql)s
        FROM
            %(tag)s
            INNER JOIN %(tagged_item)s
                ON %(tag)s.id = %(tagged_item)s.tag_id
            INNER JOIN %(model)s
                ON %(tagged_item)s.object_id = %(model_pk)s
            %%s
        WHERE %(tagged_item)s.content_type_id = %(model)s.content_type_id
            %%s
        GROUP BY %(tag)s.id, %(tag)s.name
        %%s
        ORDER BY %(tag)s.name ASC""" % {
            'tag': qn(Tag._meta.db_table),
            'count_sql': counts and (', COUNT(%s)' % model_pk) or '',
            'tagged_item': qn(TaggedItem._meta.db_table),
            'model': model_table,
            'model_pk': model_pk,
        }
        
        min_count_sql = ''
        if min_count is not None:
            min_count_sql = 'HAVING COUNT(%s) >= %%s' % model_pk
            params.append(min_count)

        cursor = connection.cursor()
        cursor.execute(query % (extra_joins, extra_criteria, min_count_sql), params)
        
        tags = []
        for row in cursor.fetchall():
            t = Tag(*row[:2])
            if counts:
                t.count = row[2]
            tags.append(t)
            
        return calculate_cloud(tags, 4, LOGARITHMIC)
Пример #6
0
    def testTagClouds(self):
        tags = []
        for line in open(os.path.join(os.path.dirname(__file__), 'tags.txt')).readlines():
            name, count = line.rstrip().split()
            tag = Tag(name=name)
            tag.count = int(count)
            tags.append(tag)

        sizes = {}
        for tag in calculate_cloud(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.assertEqual({1: 48, 2: 30, 3: 19, 4: 15, 5: 10}, sizes)

        sizes = {}
        for tag in calculate_cloud(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.assertEqual({1: 97, 2: 12, 3: 7, 4: 2, 5: 4}, sizes)

        self.assertRaises(ValueError, calculate_cloud, tags, steps=5, distribution='cheese')