говнокод

 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
def get_keys(model, key):
cache_key = "%s-%s-list" % (model._meta.module_name, key)
result = cache.get(cache_key)
if not result:
result = frozenset(int(row[0]) for row in model.objects.distinct().values_list(key))
cache.set(cache_key, result)
return result
def weapon_top(request, weapon_id=None):
ctx = {}
if not weapon_id:
ctx['weapons'] = weapons = cache.get('top-weapon-list')
if not weapons:
records = ( InfoWeapon.objects.filter(weapon_id=weapon_id)\
.select_related(depth=1).order_by('-kills')[0]
for weapon_id in get_keys(InfoWeapon, 'weapon_id'))
ctx['weapons'] = stats = sorted(({'id':record.weapon_id, 'weapon':record.get_name(),
'kills':record.kills, 'player': record.player}
for record in records), key=itemgetter('kills'), reverse=True)
cache.set('top-weapon-list', stats, CACHE_TIME)
else:
ctx['players'] = players = InfoWeapon.objects\
.filter(weapon_id=weapon_id).order_by('-kills').select_related(depth=1)[:50]
ctx['name'] = players[0].get_name()
return render_to_response('players/boards/weapon_list.html', ctx, RequestContext(request))