more groups api

This commit is contained in:
j 2014-10-01 15:56:16 +02:00
parent 3eedf8e243
commit a894a39c4d

View file

@ -2,6 +2,7 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import random import random
random.seed() random.seed()
import re
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.template import RequestContext, loader from django.template import RequestContext, loader
@ -25,6 +26,23 @@ import models
from decorators import capability_required_json from decorators import capability_required_json
import persona import persona
def get_user_or_404(data):
if 'id' in data:
u = get_object_or_404_json(User, id=ox.fromAZ(data['id']))
else:
u = get_object_or_404_json(User, username=data['username'])
return u
def get_group_or_404(data):
if 'id' in data:
g = get_object_or_404_json(Group, id=ox.fromAZ(data['id']))
else:
g = get_object_or_404_json(Group, name=data['name'])
return g
def signin(request): def signin(request):
''' '''
takes { takes {
@ -330,6 +348,7 @@ def editUser(request):
response = json_response() response = json_response()
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
user = get_object_or_404_json(User, pk=ox.fromAZ(data['id'])) user = get_object_or_404_json(User, pk=ox.fromAZ(data['id']))
profile = user.get_profile() profile = user.get_profile()
if 'disabled' in data: if 'disabled' in data:
user.is_active = not data['disabled'] user.is_active = not data['disabled']
@ -369,6 +388,7 @@ def editUser(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editUser, cache=False) actions.register(editUser, cache=False)
@capability_required_json('canManageUsers') @capability_required_json('canManageUsers')
def removeUser(request): def removeUser(request):
''' '''
@ -379,11 +399,12 @@ def removeUser(request):
''' '''
response = json_response() response = json_response()
data = json.load(request.POST['data']) data = json.load(request.POST['data'])
user = get_object_or_404_json(User, username=data['username']) u = get_user_or_404(data)
user.delete() u.delete()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeUser, cache=False) actions.register(removeUser, cache=False)
def findUser(request): def findUser(request):
''' '''
takes { takes {
@ -427,6 +448,7 @@ def parse_query(data, user):
query['qs'] = models.SessionData.objects.find(query, user) query['qs'] = models.SessionData.objects.find(query, user)
return query return query
def order_query(qs, sort): def order_query(qs, sort):
order_by = [] order_by = []
for e in sort: for e in sort:
@ -457,6 +479,7 @@ def order_query(qs, sort):
qs = qs.order_by(*order_by, nulls_last=True) qs = qs.order_by(*order_by, nulls_last=True)
return qs return qs
@capability_required_json('canManageUsers') @capability_required_json('canManageUsers')
def findUsers(request): def findUsers(request):
''' '''
@ -550,6 +573,26 @@ Positions
return render_to_json_response(response) return render_to_json_response(response)
actions.register(findUsers) actions.register(findUsers)
@capability_required_json('canManageUsers')
def getUser(request):
'''
takes {
id: string or username: string,
keys: []
}
returns {
id: string,
...
}
'''
response = json_response()
data = json.loads(request.POST['data'])
u = get_user_or_404(data)
response['data'] = u.data.get().json(data.get('keys', []), request.user)
return render_to_json_response(response)
actions.register(getUser)
@login_required_json @login_required_json
def mail(request): def mail(request):
''' '''
@ -731,6 +774,7 @@ def reset_ui(request):
request.session['ui'] = '{}' request.session['ui'] = '{}'
return redirect('/') return redirect('/')
def resetUI(request): def resetUI(request):
''' '''
reset user ui settings to defaults reset user ui settings to defaults
@ -750,6 +794,7 @@ def resetUI(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(resetUI, cache=False) actions.register(resetUI, cache=False)
def setUI(request): def setUI(request):
''' '''
takes { takes {
@ -801,6 +846,7 @@ def setUI(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(setUI, cache=False) actions.register(setUI, cache=False)
@capability_required_json('canManageUsers') @capability_required_json('canManageUsers')
def statistics(request): def statistics(request):
''' '''
@ -816,13 +862,23 @@ def statistics(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(statistics, cache=False) actions.register(statistics, cache=False)
def group_json(g):
return {
'id': ox.toAZ(g.id),
'name': g.name,
'users': g.user_set.count(),
'items': g.items.count(),
}
@capability_required_json('canManageUsers') @capability_required_json('canManageUsers')
def getGroups(request): def getGroups(request):
''' '''
takes {} takes {}
returns { returns {
groups: [ groups: [
{id:, name:, users:...} {id: string, name: string, users: int, items: int}
] ]
} }
@ -831,14 +887,35 @@ def getGroups(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
response['data']['groups'] = [] response['data']['groups'] = []
for g in Group.objects.all().order_by('name'): for g in Group.objects.all().order_by('name'):
response['data']['groups'].append({ response['data']['groups'].append(group_json(g))
'id': ox.toAZ(g.id),
'name': g.name,
'users': g.user_set.count()
})
return render_to_json_response(response) return render_to_json_response(response)
actions.register(getGroups) actions.register(getGroups)
@capability_required_json('canManageUsers')
def getGroup(request):
'''
takes {
id: string
or
name: string
}
returns {
id: string,
name: string
users: int
items: int
}
'''
response = json_response(status=200, text='ok')
data = json.loads(request.POST['data'])
g = get_group_or_404(data)
response['data'] = group_json(g)
return render_to_json_response(response)
actions.register(getGroup, cache=False)
@capability_required_json('canManageUsers') @capability_required_json('canManageUsers')
def addGroup(request): def addGroup(request):
''' '''
@ -849,19 +926,24 @@ def addGroup(request):
id: string, id: string,
name: string name: string
users: int users: int
items: int
} }
''' '''
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'])
g, created = Group.objects.get_or_create(name=data['name']) created = False
response['data'] = { n = 1
'id': ox.toAZ(g.id), name = data['name']
'name': g.name, _name = re.sub(' \[\d+\]$', '', name).strip()
'users': g.user_set.count() while not created:
} g, created = Group.objects.get_or_create(name=name)
n += 1
name = u'%s [%d]' % (_name, n)
response['data'] = group_json(g)
return render_to_json_response(response) return render_to_json_response(response)
actions.register(addGroup) actions.register(addGroup, cache=False)
@capability_required_json('canManageUsers') @capability_required_json('canManageUsers')
def editGroup(request): def editGroup(request):
@ -882,13 +964,10 @@ def editGroup(request):
g = Group.objects.get(id=ox.fromAZ(data['id'])) g = Group.objects.get(id=ox.fromAZ(data['id']))
g.name = data['name'] g.name = data['name']
g.save() g.save()
response['data'] = { response['data'] = group_json(g)
'id': ox.toAZ(g.id),
'name': g.name,
'users': g.user_set.count()
}
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editGroup) actions.register(editGroup, cache=False)
@capability_required_json('canManageUsers') @capability_required_json('canManageUsers')
def removeGroup(request): def removeGroup(request):
@ -902,12 +981,11 @@ def removeGroup(request):
''' '''
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'])
g = Group.objects.get(id=ox.fromAZ(data['id'])) g = get_group_or_404(data)
for i in g.items.all(): for i in g.items.all():
i.groups.remove(g) i.groups.remove(g)
for u in g.user_set.all(): for u in g.user_set.all():
u.groups.remove(g) u.groups.remove(g)
g.delete() g.delete()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(removeGroup) actions.register(removeGroup, cache=False)