user stats

This commit is contained in:
j 2011-10-03 13:52:16 +02:00
parent a7cc9acc63
commit 72db2e188d
5 changed files with 65 additions and 17 deletions

View file

@ -13,7 +13,7 @@ from django.db.models import Max, Sum
from ox.django.shortcuts import render_to_json_response, json_response from ox.django.shortcuts import render_to_json_response, json_response
from ox.utils import json from ox.utils import json
from user.models import get_user_json from user.models import init_user
from item.models import ItemSort from item.models import ItemSort
from actions import actions from actions import actions
@ -53,7 +53,6 @@ def init(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {user: object}} 'data': {user: object}}
''' '''
#data = json.loads(request.POST['data'])
response = json_response({}) response = json_response({})
config = copy.deepcopy(settings.CONFIG) config = copy.deepcopy(settings.CONFIG)
del config['keys'] #is this needed? del config['keys'] #is this needed?
@ -71,7 +70,7 @@ def init(request):
response['data']['site'] = config response['data']['site'] = config
if request.user.is_authenticated(): if request.user.is_authenticated():
response['data']['user'] = get_user_json(request.user) response['data']['user'] = init_user(request.user, request)
else: else:
response['data']['user'] = response['data']['site']['user'] response['data']['user'] = response['data']['site']['user']

View file

@ -81,7 +81,6 @@ def parseCondition(condition, user):
}.get(op, '__icontains')) }.get(op, '__icontains'))
key = str(key) key = str(key)
print key, v, exclude
if exclude: if exclude:
q = ~Q(**{key: v}) q = ~Q(**{key: v})
else: else:

View file

@ -7,6 +7,9 @@ from django.db.models import Q
def parseCondition(condition, user): def parseCondition(condition, user):
k = condition.get('key', 'name') k = condition.get('key', 'name')
k = { k = {
'email': 'user__email',
'firstseen': 'user__created',
'lastseen': 'user__last_login',
'user': 'user__username', 'user': 'user__username',
}.get(k, k) }.get(k, k)
v = condition['value'] v = condition['value']

View file

@ -9,6 +9,7 @@ from django.db.models import Max
from django.conf import settings from django.conf import settings
from ox.django.fields import DictField from ox.django.fields import DictField
from ox.utils import json
from itemlist.models import List, Position from itemlist.models import List, Position
@ -23,6 +24,12 @@ class UserProfile(models.Model):
ui = DictField(default={}) ui = DictField(default={})
preferences = DictField(default={}) preferences = DictField(default={})
timesseen = models.IntegerField(default=0)
ip = models.CharField(default='', max_length=255)
useragent = models.CharField(default='', max_length=255)
windowsize = models.CharField(default='', max_length=255)
screensize = models.CharField(default='', max_length=255)
def get_preferences(self): def get_preferences(self):
prefs = self.preferences prefs = self.preferences
prefs['email'] = self.user.email prefs['email'] = self.user.email
@ -91,15 +98,43 @@ def user_post_save(sender, instance, **kwargs):
models.signals.post_save.connect(user_post_save, sender=User) models.signals.post_save.connect(user_post_save, sender=User)
#FIXME: this should be one function
def user_json(user, keys, request_user=None): def user_json(user, keys, request_user=None):
return { p = user.get_profile()
j = {
'email': user.email,
'firstseen': user.date_joined,
'ip': p.ip,
'lastseen': user.last_login,
'level': p.get_level(),
'numberoflists': user.lists.count(),
'screensize': p.screensize,
'timesseen': p.timesseen,
'username': user.username, 'username': user.username,
'level': user.get_profile().get_level() 'useragent': p.useragent,
'windowsize': p.windowsize,
} }
if keys:
for key in j.keys():
if key not in keys:
del j[key]
return j
def get_user_json(user): def init_user(user, request=None):
profile = user.get_profile() profile = user.get_profile()
if request:
data = json.loads(request.POST.get('data', '{}'))
screen = data.get('screen', {})
if 'height' in screen and 'width' in screen:
profile.screensize = '%sx%s' % (screen['width'], screen['height'])
window = data.get('window', {})
if 'outerHeight' in window and 'outerWidth' in window:
profile.windowsize = '%sx%s' % (window['outerWidth'], window['outerHeight'])
profile.ip = request.META['REMOTE_ADDR']
profile.useragent = request.META['HTTP_USER_AGENT']
if not profile.timesseen:
profile.timesseen = 0
profile.timesseen += 1
profile.save()
result = {} result = {}
for key in ('username', ): for key in ('username', ):
result[key] = getattr(user, key) result[key] = getattr(user, key)

View file

@ -11,6 +11,7 @@ from django.template import RequestContext, loader
from django.utils import simplejson as json from django.utils import simplejson as json
from django.conf import settings from django.conf import settings
from django.core.mail import send_mail, BadHeaderError from django.core.mail import send_mail, BadHeaderError
from django.db.models import Sum
from ox.django.shortcuts import render_to_json_response, json_response, get_object_or_404_json from ox.django.shortcuts import render_to_json_response, json_response, get_object_or_404_json
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
@ -64,7 +65,7 @@ def signin(request):
if user is not None: if user is not None:
if user.is_active: if user.is_active:
login(request, user) login(request, user)
user_json = models.get_user_json(user) user_json = models.init_user(user, request)
response = json_response({ response = json_response({
'user': user_json 'user': user_json
}) })
@ -327,9 +328,10 @@ def findUser(request):
#FIXME: support other operators and keys #FIXME: support other operators and keys
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
response = json_response(status=200, text='ok') response = json_response(status=200, text='ok')
keys = data.get('keys') #keys = data.get('keys')
if not keys: #if not keys:
keys = ['username', 'level'] # keys = ['username', 'level']
keys = ['username', 'level']
if data['key'] == 'email': if data['key'] == 'email':
response['data']['users'] = [models.user_json(u, keys) response['data']['users'] = [models.user_json(u, keys)
@ -358,11 +360,21 @@ def order_query(qs, sort):
if operator != '-': if operator != '-':
operator = '' operator = ''
key = { key = {
}.get(e['key'], e['key']) 'email': 'email',
'firstseen': 'date_joined',
'lastseen': 'last_login',
'username': 'username',
}.get(e['key'], 'profile__%s'%e['key'])
if key == 'profile__numberoflists':
qs = qs.annotate(numberoflists=Sum('lists'))
key = 'numberoffiles'
order = '%s%s' % (operator, key) order = '%s%s' % (operator, key)
order_by.append(order) order_by.append(order)
if order_by: if order_by:
qs = qs.order_by(*order_by, nulls_last=True) print order_by
#user table does not support this
#qs = qs.order_by(*order_by, nulls_last=True)
qs = qs.order_by(*order_by)
return qs return qs
def findUsers(request): def findUsers(request):
@ -429,12 +441,12 @@ Positions
''' '''
if request.user.is_anonymous() or request.user.get_profile().get_level() != 'admin': if request.user.is_anonymous() or request.user.get_profile().get_level() != 'admin':
response = json_response(status=403, text='permission denied') response = json_response(status=403, text='permission denied')
return response return render_to_json_response(response)
response = json_response(status=200, text='ok')
response = json_response(status=200, text='ok')
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
query = parse_query(data, request.user) query = parse_query(data, request.user)
qs = query['qs'] qs = order_query(query['qs'], query['sort'])
if 'keys' in data: if 'keys' in data:
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [models.user_json(p, data['keys'], request.user) for p in qs] response['data']['items'] = [models.user_json(p, data['keys'], request.user) for p in qs]