From 7d276136edc655a195268c1351204b42eff6dcae Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:00:09 +1200 Subject: [PATCH] simplify stats code #319 --- app/models.py | 16 +++++++++------- app/templates/base.html | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/models.py b/app/models.py index c38ed38e..16f59720 100644 --- a/app/models.py +++ b/app/models.py @@ -84,19 +84,21 @@ class Instance(db.Model): def votes_are_public(self): return self.software.lower() == 'lemmy' or self.software.lower() == 'mbin' or self.software.lower() == 'kbin' - # the db execute returns a cursorresult. the all() returns a list with one item in it. - # the [0] gets the one sqlalchemy Row object, the Row.count then is the number we show def post_count(self): - return db.session.execute(text(f'SELECT count(*) FROM post WHERE instance_id = {self.id}')).all()[0].count + return db.session.execute(text('SELECT COUNT(id) as c FROM "post" WHERE instance_id = :instance_id'), + {'instance_id': self.id}).scalar() def post_replies_count(self): - return db.session.execute(text(f'SELECT count(*) FROM post_reply WHERE instance_id = {self.id}')).all()[0].count - + return db.session.execute(text('SELECT COUNT(id) as c FROM "post_reply" WHERE instance_id = :instance_id'), + {'instance_id': self.id}).scalar() + def known_communities_count(self): - return db.session.execute(text(f'SELECT count(*) FROM community WHERE instance_id = {self.id}')).all()[0].count + return db.session.execute(text('SELECT COUNT(id) as c FROM "community" WHERE instance_id = :instance_id'), + {'instance_id': self.id}).scalar() def known_users_count(self): - return db.session.execute(text(f'SELECT count(*) FROM "user" WHERE instance_id = {self.id}')).all()[0].count + return db.session.execute(text('SELECT COUNT(id) as c FROM "user" WHERE instance_id = :instance_id'), + {'instance_id': self.id}).scalar() def __repr__(self): return ''.format(self.domain) diff --git a/app/templates/base.html b/app/templates/base.html index 5b005f60..1e3db0e0 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -206,7 +206,6 @@