实战小练习
需求
- users
- userGroups
- groups
- groupUsers
—- get : 获取指定组下的user列表
—–put:往用户组添加用户
—–delete:从组中删除用户 - permissions
- userPermissions
- groupPermissions
需求分析
groupUsers
---- get : 获取指定组下的user列表
-----create:网用户组添加用户
-----delete:从组中删除用户
GET: /api/groupUser/1
POST: r = request.post(“http://192.168.33.10:8080/groupUsers/”,data=json) 【data={‘uid’:1,’gid’:1}】
DELETE: r = request.delete(“http://192.168.33.10:8080/groupUsers/”,data=json) 【data={‘uid’:1,’gid’:1}】 #走界面不行
项目结构
[vagrant@localhost day5]$ tree devops/
devops/
├── apps
│ ├── groupUsers
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ ├── router.py
│ │ ├── serializers.py
│ │ └── views.py
│ └── idc
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ ├── models.py
│ ├── __pycache__
│ ├── router.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── devops
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
- devops/urls.py #主路由配置文件
from django.conf.urls import url,include
from rest_framework.routers import DefaultRouter
from rest_framework.documentation import include_docs_urls
from groupUsers.router import group_users_router
from idc.router import idc_router
router = DefaultRouter()
router.registry.extend(group_users_router.registry)
router.registry.extend(idc_router.registry)
urlpatterns = [
url(r'^',include(router.urls)),
url(r'^docs/', include_docs_urls("开源运维平台"))
]
- devops/groupUsers/router.py #子路由配置文件
from rest_framework.routers import DefaultRouter
from .views import GroupUsersViewset
group_users_router = DefaultRouter()
group_users_router.register(r'group-users',GroupUsersViewset,base_name='group-users')
- devops/groupUsers/serializers.py #序列化文件
from rest_framework import serializers
from django.contrib.auth.models import Group
class GroupSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(required=False)
class UserSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
username = serializers.CharField(required=False)
- devops/groupUsers/views.py #视图文件
from rest_framework import viewsets,mixins,response,status
from django.contrib.auth.models import Group,User
from django.http import QueryDict
from .serializers import GroupSerializer,UserSerializer
class GroupUsersViewset(viewsets.GenericViewSet):
serializer_class = UserSerializer
def get_group_object(self):
# Perform the lookup filtering.
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
#lookup_field=pk,kwargs[lookup_url_kwarg]=传入的pk值
filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
return Group.objects.get(**filter_kwargs)
def get_queryset(self):
groupObj = self.get_group_object()
return groupObj.user_set.all()
def retrieve(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = self.get_serializer(queryset,many=True)
return response.Response(serializer.data)
def create(self, request, *args, **kwargs):
'''
int异常补货
:param request:
:param args:
:param kwargs:
:return:
'''
groupObj = Group.objects.get(pk=request.data['gid'])
userObj = User.objects.get(pk=request.data['uid'])
groupObj.user_set.add(userObj)
return response.Response(status=status.HTTP_204_NO_CONTENT)
def destroy(self,request,*args,**kwargs):
groupObj = self.get_group_object() #获取
userObj = User.objects.get(pk=request.data['uid'])
groupObj.user_set.reverse(userObj)
return response.Response(status=status)
其余实例详细请看: