Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

abstracted out queryset logic for top_objects to a function

  • Loading branch information...
commit 13b5f8b8228b7bc8b63c076b3e3f9809d6440f02 1 parent d82da8d
Patrick Altman authored February 03, 2011
30  agon/models.py
@@ -233,3 +233,33 @@ def points_awarded(target, since=None):
233 233
         ).filter(
234 234
             timestamp__gte=since
235 235
         ).aggregate(models.Sum("points")).get("points__sum", 0)
  236
+
  237
+
  238
+def get_top_objects(model, limit, time_limit):
  239
+    queryset = model.objects.all()
  240
+    
  241
+    if time_limit is None:
  242
+        if issubclass(model, User):
  243
+            queryset = queryset.annotate(
  244
+                num_points=models.Sum("targetstat_targets__points")
  245
+            )
  246
+        else:
  247
+            raise NotImplementedError("Only auth.User is supported at this time.")
  248
+    else:
  249
+        since = datetime.datetime.now() - time_limit
  250
+        if issubclass(model, User):
  251
+            queryset = queryset.filter(
  252
+                awardedpointvalue_targets__timestamp__gte=since
  253
+            ).annotate(
  254
+                num_points=models.Sum("awardedpointvalue_targets__points")
  255
+            )
  256
+        else:
  257
+            raise NotImplementedError("Only auth.User is supported at this time.")
  258
+    
  259
+    queryset = queryset.filter(num_points__isnull=False).order_by("-num_points")
  260
+    
  261
+    if limit is not None:
  262
+        queryset = queryset[:limit]
  263
+    
  264
+    return queryset
  265
+
37  agon/templatetags/agon_tags.py
... ...
@@ -1,12 +1,9 @@
1 1
 import datetime
2 2
 
3 3
 from django import template
4  
-from django.db.models import Sum
5 4
 from django.db.models.loading import cache as app_cache
6 5
 
7  
-from django.contrib.auth.models import User
8  
-
9  
-from agon.models import points_awarded
  6
+from agon.models import points_awarded, get_top_objects
10 7
 
11 8
 
12 9
 register = template.Library()
@@ -91,35 +88,15 @@ def render(self, context):
91 88
             if model is None:
92 89
                 raise incorrect_value
93 90
         
94  
-        queryset = model.objects.all()
95  
-        
96  
-        if self.time_limit is None:
97  
-            if issubclass(model, User):
98  
-                queryset = queryset.annotate(
99  
-                    num_points=Sum("targetstat_targets__points")
100  
-                )
101  
-            else:
102  
-                raise NotImplementedError("Only auth.User is supported at this time.")
103  
-        else:
104  
-            since = datetime.datetime.now() - self.time_limit
105  
-            if issubclass(model, User):
106  
-                queryset = queryset.filter(
107  
-                    awardedpointvalue_targets__timestamp__gte=since
108  
-                ).annotate(
109  
-                    num_points=Sum("awardedpointvalue_targets__points")
110  
-                )
111  
-            else:
112  
-                raise NotImplementedError("Only auth.User is supported at this time.")
113  
-        
114  
-        queryset = queryset.filter(
115  
-            num_points__isnull=False
116  
-        ).order_by("-num_points")
117  
-        
118 91
         if self.limit is not None:
119 92
             limit = self.limit.resolve(context)
120  
-            queryset = queryset[:limit]
121 93
         
122  
-        context[self.context_var] = queryset
  94
+        context[self.context_var] = get_top_objects(
  95
+            model,
  96
+            limit,
  97
+            self.time_limit
  98
+        )
  99
+        
123 100
         return u""
124 101
 
125 102
 

0 notes on commit 13b5f8b

Please sign in to comment.
Something went wrong with that request. Please try again.