forked from 0x2620/pandora
user stats
This commit is contained in:
parent
a7cc9acc63
commit
72db2e188d
5 changed files with 65 additions and 17 deletions
|
@ -13,7 +13,7 @@ from django.db.models import Max, Sum
|
|||
from ox.django.shortcuts import render_to_json_response, json_response
|
||||
from ox.utils import json
|
||||
|
||||
from user.models import get_user_json
|
||||
from user.models import init_user
|
||||
from item.models import ItemSort
|
||||
|
||||
from actions import actions
|
||||
|
@ -53,7 +53,6 @@ def init(request):
|
|||
return {'status': {'code': int, 'text': string},
|
||||
'data': {user: object}}
|
||||
'''
|
||||
#data = json.loads(request.POST['data'])
|
||||
response = json_response({})
|
||||
config = copy.deepcopy(settings.CONFIG)
|
||||
del config['keys'] #is this needed?
|
||||
|
@ -71,7 +70,7 @@ def init(request):
|
|||
|
||||
response['data']['site'] = config
|
||||
if request.user.is_authenticated():
|
||||
response['data']['user'] = get_user_json(request.user)
|
||||
response['data']['user'] = init_user(request.user, request)
|
||||
else:
|
||||
response['data']['user'] = response['data']['site']['user']
|
||||
|
||||
|
|
|
@ -81,7 +81,6 @@ def parseCondition(condition, user):
|
|||
}.get(op, '__icontains'))
|
||||
|
||||
key = str(key)
|
||||
print key, v, exclude
|
||||
if exclude:
|
||||
q = ~Q(**{key: v})
|
||||
else:
|
||||
|
|
|
@ -7,6 +7,9 @@ from django.db.models import Q
|
|||
def parseCondition(condition, user):
|
||||
k = condition.get('key', 'name')
|
||||
k = {
|
||||
'email': 'user__email',
|
||||
'firstseen': 'user__created',
|
||||
'lastseen': 'user__last_login',
|
||||
'user': 'user__username',
|
||||
}.get(k, k)
|
||||
v = condition['value']
|
||||
|
|
|
@ -9,6 +9,7 @@ from django.db.models import Max
|
|||
from django.conf import settings
|
||||
|
||||
from ox.django.fields import DictField
|
||||
from ox.utils import json
|
||||
|
||||
from itemlist.models import List, Position
|
||||
|
||||
|
@ -23,6 +24,12 @@ class UserProfile(models.Model):
|
|||
ui = 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):
|
||||
prefs = self.preferences
|
||||
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)
|
||||
|
||||
#FIXME: this should be one function
|
||||
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,
|
||||
'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()
|
||||
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 = {}
|
||||
for key in ('username', ):
|
||||
result[key] = getattr(user, key)
|
||||
|
|
|
@ -11,6 +11,7 @@ from django.template import RequestContext, loader
|
|||
from django.utils import simplejson as json
|
||||
from django.conf import settings
|
||||
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.decorators import login_required_json
|
||||
|
@ -64,7 +65,7 @@ def signin(request):
|
|||
if user is not None:
|
||||
if user.is_active:
|
||||
login(request, user)
|
||||
user_json = models.get_user_json(user)
|
||||
user_json = models.init_user(user, request)
|
||||
response = json_response({
|
||||
'user': user_json
|
||||
})
|
||||
|
@ -327,9 +328,10 @@ def findUser(request):
|
|||
#FIXME: support other operators and keys
|
||||
data = json.loads(request.POST['data'])
|
||||
response = json_response(status=200, text='ok')
|
||||
keys = data.get('keys')
|
||||
if not keys:
|
||||
keys = ['username', 'level']
|
||||
#keys = data.get('keys')
|
||||
#if not keys:
|
||||
# keys = ['username', 'level']
|
||||
keys = ['username', 'level']
|
||||
|
||||
if data['key'] == 'email':
|
||||
response['data']['users'] = [models.user_json(u, keys)
|
||||
|
@ -358,11 +360,21 @@ def order_query(qs, sort):
|
|||
if operator != '-':
|
||||
operator = ''
|
||||
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_by.append(order)
|
||||
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
|
||||
|
||||
def findUsers(request):
|
||||
|
@ -429,12 +441,12 @@ Positions
|
|||
'''
|
||||
if request.user.is_anonymous() or request.user.get_profile().get_level() != 'admin':
|
||||
response = json_response(status=403, text='permission denied')
|
||||
return response
|
||||
response = json_response(status=200, text='ok')
|
||||
return render_to_json_response(response)
|
||||
|
||||
response = json_response(status=200, text='ok')
|
||||
data = json.loads(request.POST['data'])
|
||||
query = parse_query(data, request.user)
|
||||
qs = query['qs']
|
||||
qs = order_query(query['qs'], query['sort'])
|
||||
if 'keys' in data:
|
||||
qs = qs[query['range'][0]:query['range'][1]]
|
||||
response['data']['items'] = [models.user_json(p, data['keys'], request.user) for p in qs]
|
||||
|
|
Loading…
Reference in a new issue