Update api_views.py In your API while creting user group field is not working i.e user not mapped with the group.
So we added above API for the same.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Group
|
||||
from django.contrib.auth.models import Group, User
|
||||
|
||||
from rest_framework import generics
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from rest_api.filters import MayanObjectPermissionsFilter
|
||||
from rest_api.permissions import MayanPermission
|
||||
@@ -14,6 +16,7 @@ from .permissions import (
|
||||
permission_user_edit, permission_user_view
|
||||
)
|
||||
from .serializers import GroupSerializer, UserSerializer
|
||||
from rest_framework import authentication, permissions
|
||||
|
||||
|
||||
class APIGroupListView(generics.ListCreateAPIView):
|
||||
@@ -174,3 +177,41 @@ class APICurrentUserView(generics.RetrieveUpdateDestroyAPIView):
|
||||
"""
|
||||
|
||||
return super(APICurrentUserView, self).put(*args, **kwargs)
|
||||
|
||||
class APIUserGroupMap(APIView):
|
||||
"""
|
||||
View to map user with groups
|
||||
|
||||
|
||||
**Arguments:**
|
||||
- request: Http request object.
|
||||
- pk:primary key of User
|
||||
|
||||
**Returns:** User Details
|
||||
|
||||
**Raises:** Nothing.
|
||||
|
||||
This methods handles http POST request.
|
||||
|
||||
This method map users with group.
|
||||
|
||||
|
||||
* Requires token authentication.\n
|
||||
* Only admin users are able to access this view.
|
||||
"""
|
||||
authentication_classes = (authentication.TokenAuthentication,)
|
||||
permission_classes = (permissions.IsAdminUser,)
|
||||
|
||||
def post(self, request,pk,format=None):
|
||||
"""
|
||||
Maps user with groups
|
||||
"""
|
||||
groups = request.POST['group_ids'].split(',')
|
||||
userObj = User.objects.get(pk=pk)
|
||||
for group in groups:
|
||||
groupObj = Group.objects.get(pk=group)
|
||||
groupObj.user_set.add(userObj)
|
||||
mapped_group_ids = userObj.groups.all().values_list('id', flat=True)
|
||||
result = { "id":userObj.id,"groups":mapped_group_ids,"username":userObj.username,
|
||||
"fname":userObj.first_name,"lname":userObj.last_name }
|
||||
return Response({ 'data':result })
|
||||
|
||||
Reference in New Issue
Block a user