Permalink
Browse files

ok

  • Loading branch information...
yupbank committed Mar 2, 2012
1 parent 28da1c7 commit aaaf87e951f033341cda36194cd14aeab2c35512
Showing with 13 additions and 11 deletions.
  1. +13 −11 rest/utils.py
View
@@ -40,25 +40,27 @@ def get_lat_lng_range(lat0, lng0, distance=1.0):
def get_id_rank_list(limit=10,offset=0):
+ rank_dict = user_rank_dict()
+ rank = [ (i,j) for i,j in rank_dict.iteritems()]
+ rank.sort(key=lambda x:x[1])
+ return rank[offset:offset+limit]
+
+
+def user_rank_dict():
check_count = []
rank = []
for u in User.objects.all():
check_count.append([u.id, u.checkin_set.count()])
if check_count:
- check_count.sort(key=lambda x:x[1],reverse=True)
_rank = set([i[1] for i in check_count ])
_rank = [i for i in _rank]
- _rank = [(i, len(_rank)-1-_rank.index(i)) for i in _rank]
- rank = {}
- for i,j in _rank:
- rank.update({i:j+1})
- rank = [ [i,rank[j]] for i, j in check_count]
- rank.sort(key=lambda x:x[1],reverse=True)
- return rank[offset:offset+limit]
-
+ _rank.sort()
+ _rank = [(i, _rank.index(j)+1) for i,j in check_count]
+ rank = dict(_rank)
+ return dict(rank)
def get_rank_by_id(id):
- rank_list = get_id_rank_list()
- return dict(rank_list).get(id)
+ rank_dict = user_rank_dict()
+ return rank_dict.get(id)

0 comments on commit aaaf87e

Please sign in to comment.