import json from django.contrib.auth.models import User from django.db.models import Q from django.forms.models import model_to_dict from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView from even_table.models import Dept, Jobs, Dept2Jobs # Create your views here. # 部门增删查改 class deptView(APIView): """ request {name名字},parent_id{父级id,可空} 创建一个节点 """ def post(self, request): data = request.data res = [] if 'parent_id' in data: # try: # 创建部门表,返回创建数据成功 # print(data['parent_id']) # print(model_to_dict(Dept.objects.get(id=data['parent_id']))) res = Dept.objects.create( name=data['name'], parent_id=data['parent_id'] ) res = model_to_dict(res) res = {'node': res, 'message': data['name'] + ' 新增叶子成功'} return Response(data=res, status=status.HTTP_200_OK) # except Exception: # print(json.dump(Exception)) # return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: # try: res = Dept.objects.create( name=data['name'], ) res = model_to_dict(res) res = {'node': res, 'message': data['name'] + ' 新增父级成功'} return Response(data=res, status=status.HTTP_200_OK) # except Exception: # return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) """ 得到节点 request{parent_id(节点id,可空)} """ def get(self, request): data = request.GET res = [] if 'parent_id' in data: # 给parent_id 拿到子集 res = Dept.objects.filter(parent_id=data['parent_id']).values().all() else: # 没有传name 则给第0层 res = Dept.objects.filter(parent_id=None).values().all() return Response(data=res, status=status.HTTP_200_OK) """ 删除节点 {id:部门id} """ def delete(self, request): data = request.data # 岗位 删除这个部门下面的所有岗位 # 这个部门下面的所有岗位 jobs = Dept2Jobs.objects.filter(dept_id=data['id']).values('job_id').all() job_ids = [] for job in jobs: job_ids.append(job['job_id']) Jobs.objects.filter(id__in=job_ids).delete() # 部门 删除这个部门 res = Dept.objects.filter(id=data['id']).delete() # 关联表 删除跟这个部门关联的 Dept2Jobs.objects.filter(dept_id=data['id']).delete() return Response(data="删除成功", status=status.HTTP_200_OK) """ 更新节点 {id:节点id,name:节点名字,parent_id:父节点id(可空)} """ def put(self, request): data = request.data if 'parent_id' in data: res = Dept.objects.filter(id=data['id']).update(name=data['name'], parent_id=data['parent_id']) else: res = Dept.objects.filter(id=data['id']).update(name=data['name']) return Response(data=res, status=status.HTTP_200_OK) # 岗位增删查改 class jobsView(APIView): # 增 {name:岗位名字,dept_id:父级部门id} def post(self, request): data = request.data # 创建岗位 res = Jobs.objects.create( name=data['name'] ) res = model_to_dict(res) if res: # 创建关联 Dept2Jobs.objects.create( dept_id=data['dept_id'], job_id=res['id'] ) return Response(data={'result': res, 'message': '创建岗位成功'}, status=status.HTTP_200_OK) # 查 {dept_id:父级部门id(可空,空则查全部)} def get(self, request): # 从关联表里 查到 这个父级的id下的所有岗位id 查到job下面的所有name names = Dept2Jobs.objects.filter(dept=request.GET['dept_id']).values("job", "job__name") # print(ids) return Response(data={'result': names, 'message': '查询成功'}, status=status.HTTP_200_OK) # 更新 {id:岗位id,name:岗位新名字} def put(self, request): pass # 删 {id:岗位id} def delete(self, request): # 删除关联表数据 data = request.data Dept2Jobs.objects.filter(job_id=data['id']).delete() # 删除岗位 Jobs.objects.filter(id=data['id']).delete() return Response(data={'message': data['id'] + '删除成功'}, status=status.HTTP_200_OK) # 用户增删查改 class userView(APIView): def post(self, request): pass # 用户查询 {job_id:岗位id(可空,有则选这个岗位下的用户)} def get(self, request): data = request.GET if 'job_id' in data: job_ids = Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__isnull=False).values() ids = [] for id in job_ids: ids.append(id) res = User.objects.filter(id__in=ids).values() else: res = User.objects.values() return Response(data={'result': res, 'message': '查询用户基础数据'}, status=status.HTTP_200_OK) # 用户替换部门 {job_id:岗位id,user_ids:用户id数组,type:移除用户还是新增用户(0移除,1新增)} def put(self, request): data = request.data res = {} if data['type'] == 0: # 岗位下删除这个用户 Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__in=data['user_ids']).delete() res = {'message': '删除用户'} elif data['type'] == 1: # 岗位下新增用户 for user_id in data['user_ids']: Dept2Jobs.objects.create(job_id=data['job_id'], user_id=user_id) res = {'message': '新增用户'} return Response(data=res, status=status.HTTP_200_OK) def delete(self, request): pass