def create(self, request, *args, **kwargs): role_id = request.data["role_id"] req_menus = request.data["permissions"] role = Role.objects.filter(id=role_id).first() all_menus = Menu.objects.all().values("id", "parent_id") ret_menus = self._relyon_all(id_lists=req_menus, all_data=all_menus, abc=[]) try: role.menus.set(ret_menus) return JsonResponse(code=status.HTTP_200_OK) except Exception as e: return JsonResponse(code=status.HTTP_403_FORBIDDEN, data=e)
def info(self, request): """ 用户已经登陆后,get时获取的数据 :param request: :return: """ username = request.user.name user_code = request.user.is_superuser role_list = self._user_role(username) permissions_list, access_menus = self._menus(role_list, user_code) # access_routes = self._routes(role_list, user_code) access_routes = self._routes() access_interfaces = self._interfaces(role_list, user_code) data = { "userName": username, "isAdmin": user_code, "userRoles": role_list, "userPermissions": permissions_list, "accessMenus": access_menus, "accessRoutes": access_routes, "accessInterfaces": access_interfaces, "avatarUrl": "https://api.adorable.io/avatars/85/[email protected]" } return JsonResponse(code=status.HTTP_200_OK, data=data)
def retrieve(self, request, *args, **kwargs): pk = request.path.split("/")[-2] data = Role.objects.filter(id=pk, menus__type=2).annotate( role_id=F("id"), functionId=F("menus__id")).values("role_id", "functionId") return JsonResponse(code=status.HTTP_200_OK, ret_status=status.HTTP_200_OK, data=data)
def batch_del(request): """ 根据传入的ids列表,批量删除msk_interface内对应id的数据 :param request: :return: """ id_list = Common._json(request.GET.get("ids")) Interface.objects.filter(pk__in=id_list).delete() return JsonResponse(code=status.HTTP_200_OK)
def paged(self, request): # 过滤 queryset = Role.objects.all().order_by("id") data = Common._paged(self, serializer_class=self.serializer_class, request=request, queryset=queryset) return JsonResponse(code=status.HTTP_200_OK, ret_status=status.HTTP_200_OK, data=data)
def list(self, request, *args, **kwargs): """ :param request: :param args: :param kwargs: :return: """ data = Common._list_data(self, self.queryset) return JsonResponse(code=status.HTTP_200_OK, data=data, ret_status=status.HTTP_200_OK)
def post(request): """ 用于用户登录,账号密码效验 :param request: :return: """ if not request.data: return Response(status=status.HTTP_400_BAD_REQUEST) user_info = User.objects.filter( name=request.data["username"], password=request.data["password"]).values("id", "name", "true_name").first() if user_info: token_str = Token.objects.get(user=user_info['id']).key request.user = user_info["name"] user_info['name'] = user_info.pop('true_name') user_info["id"] = str(user_info["id"]) return JsonResponse(code=status.HTTP_200_OK, token=token_str, data=user_info) else: return JsonResponse(code=499, msg="用户名或密码错误", data=user_info)
def create(self, request, *args, **kwargs): """ 新增用户时,新增token :param request: :param args: :param kwargs: :return: """ request.data["create_time"] = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M") request.data["last_login"] = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M") serializer_class = UserAddSerializer(data=request.data) if serializer_class.is_valid(): name = serializer_class.save() if name: Token.objects.create(user=name) return JsonResponse(code=status.HTTP_200_OK) else: data = serializer_class.errors return JsonResponse(code=status.HTTP_200_OK, data=data)
def paged(self, request): """ 分页方法 :param request: :return: """ queryset = User.objects.all().order_by("id") data = Common._paged(self, serializer_class=self.serializer_class, request=request, queryset=queryset) return JsonResponse(code=status.HTTP_200_OK, ret_status=status.HTTP_200_OK, data=data)
def api_to_menu(self, request): """ 角色与用户关系绑定 :param request: :return: """ action = request.data["action"] menu_id = request.data["functionId"] interface = self.queryset.filter( id=request.data["interfaceId"]).first() if action: interface.menu_set.add(menu_id) else: interface.menu_set.remove(menu_id) return JsonResponse(code=status.HTTP_200_OK)
def role_to_user(self, request): """ 角色与用户关系绑定 :param request: :return: """ action = request.data["action"] user_id = request.data["user_id"] role_id = request.data["role_id"] user = self.queryset.filter(id=user_id).first() if action: user.user_role.add(role_id) else: user.user_role.remove(role_id) return JsonResponse(code=status.HTTP_200_OK)
def batch_del(request): id_list = Common._json(request.GET.get("ids")) Role.objects.filter(pk__in=id_list).delete() return JsonResponse(code=status.HTTP_200_OK)