def groups(request): if request.method == 'POST': from group_actions import add_user_to_group, remove_user_from_group if request.POST['action'] == 'add': group_id = request.POST['group_id'] user_name = request.POST['user_name'] pa_user = PAUser.objects.get(name=user_name) pa_group = PAGroup.objects.get(id=group_id) add_user_to_group(pa_group, pa_user) elif request.POST['action'] == 'remove': group_id = request.POST['group_id'] user_id = request.POST['user_id'] pa_user = PAUser.objects.get(id=user_id) pa_group = PAGroup.objects.get(id=group_id) remove_user_from_group(pa_group, pa_user) else: raise Exception("Unsupported POST action") template = loader.get_template('groups.html') context = RequestContext(request, { 'groups': PAGroup.objects.all(), }) return HttpResponse(template.render(context))
def create(self, request, group_pk): user_pk = request.data["id"] group = self.get_group(group_pk) user = PAUser.objects.get(id=user_pk) if not group.members.filter(id=user_pk).exists(): add_user_to_group(group, user) return Response(None, status.HTTP_204_NO_CONTENT) else: return Response( {u"detail": u"User is already a member of group", u"User": user_pk, u"Group": group_pk}, status.HTTP_400_BAD_REQUEST, )