палево

  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
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
=== modified file 'players/views.py'
--- players/views.py 2007-08-22 14:30:26 +0000
+++ players/views.py 2008-07-30 12:19:56 +0000
@@ -9,6 +9,9 @@
from models import *
+from itertools import imap
+from operator import itemgetter
+
CACHE_TIME = 60 * 60 * 24
def search(request):
@@ -37,6 +40,60 @@
damage = InfoTeam.objects.order_by('-tdmg')[:25]
return render_to_response('players/boards/teamplay.html', locals(), RequestContext(request))
+
+def get_keys(model, key):
+ cache_key = "%s-%s-list" % (model._meta.module_name, key)
+ result = cache.get(cache_key+'foo')
+ 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 get_records(model, list_key, sort_key, record_fields):
+ def factory(record, fields=record_fields.items()):
+ return dict( ( field_name, getattr(record, field[:-2])() if field.endswith('()') \
+ else getattr(record, field) )
+ for field_name, field in fields )
+
+ rows = ( model.objects.filter(**{list_key: row_group}).select_related(depth=1).order_by('-%s' % sort_key)[0]
+ for row_group in get_keys(model, list_key) )
+
+ return sorted(imap(factory, rows), key=itemgetter(sort_key), reverse=True)
+
+def make_top(model, name, list_key, sort_key='kills', **record_fields):
+ ctx = {}
+ cache_key = "%s-top-%s" % (model._meta.module_name, name)
+ result = cache.get(cache_key+'foo')
+ if not result:
+ ctx[name] = result = get_records(model, list_key, sort_key, record_fields)
+ cache.set(cache_key, result, CACHE_TIME)
+ return ctx
+
+def weapon_top(request, weapon_id=None):
+ ctx = {}
+ if not weapon_id:
+ ctx.update(make_top( InfoWeapon, 'weapons', 'weapon_id',
+ id='weapon_id', weapon='get_name()', kills='kills', player='player'))
+ 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))
+
+def vehicle_top(request, vehicle_id=None):
+ ctx = {}
+ if not vehicle_id:
+ ctx.update(make_top( InfoVehicle, 'vehicles', 'vehicle_id',
+ id='vehicle_id', vehicle='get_name()',
+ kills='kills', player='player' ))
+ else:
+ ctx['players'] = players = InfoVehicle.objects\
+ .filter(vehicle_id=vehicle_id).order_by('-kills').select_related(depth=1)[:50]
+ ctx['name'] = players[0].get_name()
+
+ return render_to_response('players/boards/vehicles.html', ctx, RequestContext(request))
+
def resort(raw, key):
return sorted([ (row[1][key], row[1]) for row in raw.items() ], reverse=True )
@@ -61,35 +118,6 @@
name = conquest[0].get_name()
return render_to_response('players/boards/maps.html', locals(), RequestContext(request))
-def weapon_top(request, weapon_id=None):
- if not weapon_id:
- weapons = cache.get('top-weapon-list')
- if not weapons:
- raw = dict(
- [(record.weapon_id, {'id':record.weapon_id, 'weapon':record.get_name(), 'kills':record.kills, 'player': record.player})
- for record in InfoWeapon.objects.select_related().order_by('kills')]
- )
- weapons = resort(raw, 'kills')
- cache.set('top-weapon-list', weapons, CACHE_TIME)
- else:
- players = InfoWeapon.objects.filter(weapon_id=weapon_id).order_by('-kills').select_related()[:100]
- name = players[0].get_name()
- return render_to_response('players/boards/weapon_list.html', locals(), RequestContext(request))
-
-def vehicle_top(request, vehicle_id=None):
- if not vehicle_id:
- vehicles = cache.get('top-vehicle-list')
- if not vehicles:
- raw = dict(
- [(record.vehicle_id, {'id': record.vehicle_id, 'vehicle':record.get_name(), 'kills':record.kills, 'player': record.player})
- for record in InfoVehicle.objects.all().order_by('kills').select_related()])
- vehicles = resort(raw, 'kills')
- cache.set('top-vehicle-list', vehicles, CACHE_TIME)
- else:
- players = InfoVehicle.objects.filter(vehicle_id=vehicle_id).order_by('-kills').select_related()[:100]
- name = players[0].get_name()
- return render_to_response('players/boards/vehicles.html', locals(), RequestContext(request))
-
def award_top(request, award_id=None):
def prepare(dataset, key):
data = cache.get('top-awards-%s' % key)