Forráskód Böngészése

修复查询指定部门下岗位重复BUG,新增岗位与用户关联的增删

liceal 4 éve
szülő
commit
e08585aaaa

BIN
Django_test/__pycache__/settings.cpython-37.pyc


BIN
even_table/__pycache__/urls.cpython-37.pyc


BIN
even_table/__pycache__/views.cpython-37.pyc


+ 3 - 2
even_table/urls.py

@@ -1,10 +1,11 @@
 from django.conf.urls import url
 
-from even_table.views import deptView, jobsView, userView
+from even_table.views import deptView, jobsView, userView, Dept2JobsView
 
 urlpatterns = []
 urlpatterns += [
     url(r'dept/', deptView.as_view()),
     url(r'jobs/', jobsView.as_view()),
-    url(r'user/', userView.as_view())
+    url(r'user/', userView.as_view()),
+    url(r'Dept2Jobs/', Dept2JobsView.as_view()),
 ]

+ 22 - 7
even_table/views.py

@@ -1,7 +1,7 @@
 import json
 
 from django.contrib.auth.models import User
-from django.db.models import Q
+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
@@ -12,6 +12,8 @@ from even_table.models import Dept, Jobs, Dept2Jobs
 
 # Create your views here.
 # 部门增删查改
+
+
 class deptView(APIView):
     """
     request {name名字},parent_id{父级id,可空}
@@ -125,7 +127,8 @@ class jobsView(APIView):
     # 查 {dept_id:父级部门id(可空,空则查全部)}
     def get(self, request):
         # 从关联表里 查到 这个父级的id下的所有岗位id 查到job下面的所有name
-        names = Dept2Jobs.objects.filter(dept=request.GET['dept_id']).values("job", "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)
 
@@ -145,18 +148,18 @@ class jobsView(APIView):
 
 # 用户增删查改
 class userView(APIView):
-    def post(self, request):
-        pass
 
     # 用户查询 {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()
+            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)
-            res = User.objects.filter(id__in=ids).values()
+                ids.append(id['user_id'])
+            if len(ids) != 0:
+                res = User.objects.filter(id__in=ids).values()
         else:
             res = User.objects.values()
 
@@ -179,3 +182,15 @@ class userView(APIView):
 
     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)

BIN
routes/__pycache__/models.cpython-37.pyc


BIN
routes/__pycache__/views.cpython-37.pyc


BIN
routes/migrations/__pycache__/0003_route2user.cpython-37.pyc


+ 1 - 1
routes/views.py

@@ -15,7 +15,7 @@ class RoutesView(ModelViewSet):
     serializer_class = RoutesSerializer
     pagination_class = None
 
-    # 根据用户id
+    # 根据用户id 从关联表里 找它的路由id
     def list(self, request, *args, **kwargs):
         now_user_id = request.user.id
         route_ids = Route2User.objects.filter(user_id=now_user_id).values('route_id')