more user backend

This commit is contained in:
j 2011-10-03 18:09:11 +02:00
parent 72d3a8f403
commit 26e9bb90a2
5 changed files with 72 additions and 14 deletions

View file

@ -20,12 +20,13 @@ def parseCondition(condition, user):
''' '''
k = condition.get('key', 'name') k = condition.get('key', 'name')
k = { k = {
'user': 'annotations__user__username',
'place': 'annotations__places__id',
'event': 'annotations__events__id', 'event': 'annotations__events__id',
'id': 'public_id',
'in': 'start', 'in': 'start',
'out': 'end', 'out': 'end',
'id': 'public_id', 'place': 'annotations__places__id',
'text': 'annotations__value',
'user': 'annotations__user__username',
}.get(k, k) }.get(k, k)
if not k: if not k:
k = 'name' k = 'name'

View file

@ -36,6 +36,7 @@ def _order_query(qs, sort):
qs = qs.annotate(subscribers=Sum('subscribed_users')) qs = qs.annotate(subscribers=Sum('subscribed_users'))
if order_by: if order_by:
qs = qs.order_by(*order_by) qs = qs.order_by(*order_by)
qs = qs.distinct()
return qs return qs
def parse_query(data, user): def parse_query(data, user):

View file

@ -6,16 +6,19 @@ import models
def padma_video(request, url): def padma_video(request, url):
url = url.split('/') url = url.split('/')
hid = url[0] hid = url[0]
view = None
layer = None
if len(url) > 1: if len(url) > 1:
view = url[1] view = url[1]
else: if len(url) > 2:
view = None layer = url[2]
alias = get_object_or_404(models.IDAlias, old=hid) alias = get_object_or_404(models.IDAlias, old=hid)
url = '/%s' % alias.new url = '/%s' % alias.new
if view: if view:
url += '/' + { url += '/' + {
'editor': 'timeline', 'editor': 'timeline',
}.get(view, view) }.get(view, view)
#FIXME: reqire layer urls, reqrite timerange urls #FIXME: reqrite layer urls
#FIXME: rewrite timerange urls
return redirect(url) return redirect(url)

View file

@ -29,6 +29,8 @@ class UserProfile(models.Model):
useragent = models.CharField(default='', max_length=255) useragent = models.CharField(default='', max_length=255)
windowsize = models.CharField(default='', max_length=255) windowsize = models.CharField(default='', max_length=255)
screensize = models.CharField(default='', max_length=255) screensize = models.CharField(default='', max_length=255)
info = DictField(default={})
note = models.TextField(default='')
def get_preferences(self): def get_preferences(self):
prefs = self.preferences prefs = self.preferences
@ -90,8 +92,11 @@ class UserProfile(models.Model):
del ui['lists'][i] del ui['lists'][i]
return ui return ui
def set_level(self, level):
self.level = settings.CONFIG['userLevels'].index(level)
def get_level(self): def get_level(self):
return ['guest', 'member', 'staff', 'admin'][self.level] return settings.CONFIG['userLevels'][self.level]
def user_post_save(sender, instance, **kwargs): def user_post_save(sender, instance, **kwargs):
profile, new = UserProfile.objects.get_or_create(user=instance) profile, new = UserProfile.objects.get_or_create(user=instance)
@ -106,6 +111,7 @@ def user_json(user, keys, request_user=None):
'ip': p.ip, 'ip': p.ip,
'lastseen': user.last_login, 'lastseen': user.last_login,
'level': p.get_level(), 'level': p.get_level(),
'note': p.note,
'numberoflists': user.lists.count(), 'numberoflists': user.lists.count(),
'screensize': p.screensize, 'screensize': p.screensize,
'timesseen': p.timesseen, 'timesseen': p.timesseen,
@ -123,6 +129,7 @@ def init_user(user, request=None):
profile = user.get_profile() profile = user.get_profile()
if request: if request:
data = json.loads(request.POST.get('data', '{}')) data = json.loads(request.POST.get('data', '{}'))
profile.info = data
screen = data.get('screen', {}) screen = data.get('screen', {})
if 'height' in screen and 'width' in screen: if 'height' in screen and 'width' in screen:
profile.screensize = '%sx%s' % (screen['width'], screen['height']) profile.screensize = '%sx%s' % (screen['width'], screen['height'])

View file

@ -14,7 +14,7 @@ from django.core.mail import send_mail, BadHeaderError
from django.db.models import Sum 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 admin_required_json, login_required_json
import ox import ox
@ -309,6 +309,57 @@ def requestToken(request):
actions.register(requestToken, cache=False) actions.register(requestToken, cache=False)
@admin_required_json
def editUser(request):
'''
param data {
key: value
}
required key: username
optional keys: newUsername, email, level, note
return {
'status': {'code': int, 'text': string}
'data': {
}
}
'''
response = json_response()
data = json.load(request.POST['data'])
user = get_object_or_404_json(models.User, username=data['username'])
profile = user.get_profile()
if 'email' in data:
user.email = data['email']
if 'level' in data:
profile.set_level(data['level'])
if 'note' in data:
profile.note = data['note']
if 'newUsername' in data:
user.username = data['newUsername']
user.save()
profile.save()
return render_to_json_response(response)
actions.register(editUser, cache=False)
@admin_required_json
def removeUser(request):
'''
param data {
username: username
}
return {
'status': {'code': int, 'text': string}
'data': {
}
}
'''
response = json_response()
data = json.load(request.POST['data'])
user = get_object_or_404_json(models.User, username=data['username'])
user.delete()
return render_to_json_response(response)
actions.register(removeUser, cache=False)
def findUser(request): def findUser(request):
''' '''
param data { param data {
@ -324,8 +375,6 @@ def findUser(request):
} }
} }
''' '''
#admins should be able to find all users, other users only exact matches
#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')
@ -377,6 +426,7 @@ def order_query(qs, sort):
qs = qs.order_by(*order_by) qs = qs.order_by(*order_by)
return qs return qs
@admin_required_json
def findUsers(request): def findUsers(request):
''' '''
param data { param data {
@ -439,10 +489,6 @@ Positions
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
positions: ids of places for which positions are required positions: ids of places for which positions are required
''' '''
if request.user.is_anonymous() or request.user.get_profile().get_level() != 'admin':
response = json_response(status=403, text='permission denied')
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)