Skip to content

Commit

Permalink
Reformatted queries and updated get_score docstring
Browse files Browse the repository at this point in the history
git-svn-id: https://django-voting.googlecode.com/svn/trunk@36 662f01ad-f42a-0410-a340-718c64ddaef4
  • Loading branch information
insin committed Mar 14, 2007
1 parent 6e2e9aa commit 1e100f4
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions models.py
Expand Up @@ -5,12 +5,14 @@
class VoteManager(models.Manager):
def get_score(self, obj):
"""
Get the total score for ``obj`` and the number of votes it's received.
Get a dictionary containing the total score for ``obj`` and
the number of votes it's received.
"""
query = """
SELECT SUM(vote), COUNT(*) FROM %s
SELECT SUM(vote), COUNT(*)
FROM %s
WHERE content_type_id = %%s
AND object_id = %%s""" % self.model._meta.db_table
AND object_id = %%s""" % backend.quote_name(self.model._meta.db_table)
ctype = ContentType.objects.get_for_model(obj)
cursor = connection.cursor()
cursor.execute(query, [ctype.id, obj.id])
Expand Down Expand Up @@ -70,9 +72,10 @@ def get_top(self, Model, limit=10, reversed=False):
"""
ctype = ContentType.objects.get_for_model(Model)
query = """
SELECT object_id, SUM(vote) AS score FROM %s
SELECT object_id, SUM(vote)
FROM %s
WHERE content_type_id = %%s
GROUP BY object_id""" % self.model._meta.db_table
GROUP BY object_id""" % backend.quote_name(self.model._meta.db_table)
if reversed:
query += 'HAVING SUM(vote) < 0 ORDER BY SUM(vote) ASC LIMIT %s'
else:
Expand Down

0 comments on commit 1e100f4

Please sign in to comment.