Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    @property
    def weight(self):
        # All this because we want to run only one query in the database.
        # This is VERY CRITICAL, it's a BIG BIG query and we should do
        # something to avoid running this too frequently or at all
        
        # REALLY KEEP THIS IN MIND AND INSPECT HERE IF THERE ARE
        # PERFORMANCE PROBLEMS.
        num = union_all(
            select([debates_table, tags_to_debates_table, tags_table],
                and_(
                    tags_to_debates_table.c.tag_name==self.name,
                    tags_to_debates_table.c.debate_id==debates_table.c.id,
                    tags_table.c.name==self.name
                )
            ).alias("debates_subq").count(),
            select([links_table, tags_to_links_table, tags_table],
                and_(
                    tags_to_links_table.c.tag_name==self.name,
                    tags_to_links_table.c.link_id==links_table.c.id,
                    tags_table.c.name==self.name
                )
            ).alias("links_subq").count(),
            select([posts_table, tags_to_posts_table, tags_table],
                and_(
                    tags_to_posts_table.c.tag_name==self.name,
                    tags_to_posts_table.c.post_slug==posts_table.c.slug,
                    tags_table.c.name==self.name
                )
            ).alias("posts_subq").count()
        ).alias('num')
        wght = select([func.sum(list(num.columns)[0])], bind=sac.engine).execute().fetchone()[0]
        return wght