123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import json
- from django.contrib.auth.models import User
- from django.db.models import Q, Sum
- 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").annotate(
- jog_sum=Sum('job'))
- # 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):
- # 用户查询 {job_id:岗位id(可空,有则选这个岗位下的用户)}
- def get(self, request):
- data = request.GET
- res = []
- if 'job_id' in data:
- job_ids = Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__isnull=False).values('user_id')
- ids = []
- for id in job_ids:
- ids.append(id['user_id'])
- if len(ids) != 0:
- 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
- # 部门岗位关联表增删查改
- class Dept2JobsView(APIView):
- # 传入岗位ID,用户ID数组,data{'dept_id': '34', 'job_id': '25', 'user_id': [1, 2]}
- def post(self, request):
- # user_id 不为空的删除
- Dept2Jobs.objects.filter(~Q(user_id=None), dept_id=request.data['dept_id'],
- job_id=request.data['job_id']).delete()
- for user_id in request.data['user_id']:
- Dept2Jobs(dept_id=request.data['dept_id'], job_id=request.data['job_id'], user_id=user_id).save()
- return Response(data=None, status=status.HTTP_200_OK)
|