From 3cdb0e23cc87db3161e351a3d4175ce4d65973f7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 30 Sep 2014 14:11:55 +0200 Subject: [PATCH] new api calls: getGroups/addGroup/removeGroup --- pandora/item/models.py | 3 +- pandora/user/models.py | 2 +- pandora/user/views.py | 69 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/pandora/item/models.py b/pandora/item/models.py index 02f5ce8d..ef86550d 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -230,7 +230,8 @@ class Item(models.Model): if isinstance(groups, list): groups = filter(lambda g: g.strip(), groups) groups = [ox.escape_html(g) for g in groups] - self.groups.exclude(name__in=groups).delete() + for g in self.groups.exclude(name__in=groups): + self.groups.remove(g) current_groups = [g.name for g in self.groups.all()] for g in filter(lambda g: g not in current_groups, groups): group, created = Group.objects.get_or_create(name=g) diff --git a/pandora/user/models.py b/pandora/user/models.py index 9fdedb94..32c2c522 100644 --- a/pandora/user/models.py +++ b/pandora/user/models.py @@ -3,7 +3,7 @@ import copy from datetime import datetime -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.db import models from django.db.models import Max from django.conf import settings diff --git a/pandora/user/views.py b/pandora/user/views.py index 002d075e..6516318d 100644 --- a/pandora/user/views.py +++ b/pandora/user/views.py @@ -351,7 +351,8 @@ def editUser(request): if isinstance(groups, list): groups = filter(lambda g: g.strip(), groups) groups = [ox.escape_html(g) for g in groups] - user.groups.exclude(name__in=groups).delete() + for g in user.groups.exclude(name__in=groups): + user.groups.remove(g) current_groups = [g.name for g in user.groups.all()] for g in filter(lambda g: g not in current_groups, groups): group, created = Group.objects.get_or_create(name=g) @@ -814,3 +815,69 @@ def statistics(request): response['data'] = stats return render_to_json_response(response) actions.register(statistics, cache=False) + +@capability_required_json('canManageUsers') +def getGroups(request): + ''' + takes {} + returns { + groups: [ + {name:, users...} + ] + } + + ''' + response = json_response(status=200, text='ok') + data = json.loads(request.POST['data']) + response['data']['groups'] = [] + for g in Group.objects.all().order_by('name'): + response['data']['groups'].append({ + 'name': g.name, + 'users': g.user_set.count() + }) + return render_to_json_response(response) +actions.register(getGroups) + +@capability_required_json('canManageUsers') +def addGroup(request): + ''' + takes { + name: string + } + returns { + name: string + users: int + } + + ''' + response = json_response(status=200, text='ok') + data = json.loads(request.POST['data']) + g, created = Group.objects.get_or_create(name=data['name']) + response['data'] = { + 'name': g.name, + 'users': g.user_set.count() + } + return render_to_json_response(response) +actions.register(addGroup) + +@capability_required_json('canManageUsers') +def removeGroup(request): + ''' + takes { + name: string + } + returns { + } + + ''' + response = json_response(status=200, text='ok') + data = json.loads(request.POST['data']) + g = Group.objects.get(name=data['name']) + for i in g.items.all(): + i.groups.remove(g) + for u in g.user_set.all(): + u.groups.remove(g) + g.delete() + return render_to_json_response(response) +actions.register(removeGroup) +