Ejemplo n.º 1
0
    def get(self, request, user_id, *args, **kwargs):
        try:

            user = User.objects.get(pk=user_id)
            roles = user.role.all()
            ret = []
            ids = list(roles.values('id'))
            for i in ids:
                ret.append(i['id'])
            fid = Role.objects.filter(~Q(id__in=ret)).values(
                'id', 'code', 'name')

            ser2 = RoleSerializer(instance=fid, many=True)
            # todo 判断查询条件查询数据库是否为空
            if roles.exists():

                ser = RoleSerializer(instance=roles, many=True)

                return restful.result(message="查询成功",
                                      role2=ser.data,
                                      role=ser2.data)
            else:
                rets = self.list(request, *args, **kwargs)
                return restful.result(message="查询成功,当前用户无角色,请为用户添加角色",
                                      role=rets.data)
        except Exception as e:
            ip_username(request)
            return restful.result2(message="查询失败,id传输错误",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        try:
            # eavl 将字符串转换为列表

            if "," in request.data["role_id"]:
                roles = Role.objects.filter(
                    id__in=eval(request.data["role_id"]))
            else:
                roles = Role.objects.filter(id=request.data["role_id"])

            if "," in request.data["menu_id"]:
                menus = Menu.objects.filter(
                    id__in=eval(request.data["menu_id"]))

            else:
                menus = Menu.objects.filter(id=request.data["menu_id"])

            for role in roles:
                # 设置权限之前,先查询当前角色对应的所有权限,进行删除,然后在插入。
                role.menu.set([])  # 删除
                role.menu.set(menus)  # 插入
            return restful.result(message="操作成功")
        except Exception as e:
            ip_username(request)
            return restful.result2(message="操作失败",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        try:
            # eavl 将字符串转换为列表

            if "," in request.data["role_id"]:
                roles = Role.objects.filter(
                    id__in=eval(request.data["role_id"]))
            else:
                roles = Role.objects.filter(id=request.data["role_id"])

            if "," in request.data["user_id"]:
                users = User.objects.filter(
                    id__in=eval(request.data["user_id"]))

            else:
                users = User.objects.filter(id=request.data["user_id"])

            for user in users:
                # 设置角色之前,先查询当前用户对应的所有角色,进行删除,然后在插入。
                user.role.set([])  # 删除
                user.role.set(roles)  # 插入
            return restful.result(message="操作成功")
        except Exception as e:
            ip_username(request)
            return restful.result2(message="操作失败",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 4
0
 def delete(self, request, *args, **kwargs):
     try:
         self.destroy(request, *args, **kwargs)
         return restful.ok()
     except Exception as e:
         ip_username(request)
         return restful.result2(message="操作失败",
                                kwargs=logger.error(e.args),
                                data=e.args)
Ejemplo n.º 5
0
 def get(self, request, *args, **kwargs):
     try:
         ret = self.list(request, *args, **kwargs)
         return restful.result(message="操作成功", data=ret.data)
     except Exception as e:
         ip_username(request)
         return restful.result2(message="操作失败",
                                kwargs=logger.error(e.args),
                                data=e.args)
Ejemplo n.º 6
0
 def patch(self, request, id):
     try:
         ret = Role.objects.filter(pk=id).first()
         ser = RoleSerializer(instance=ret, many=False)
         return restful.result(data=ser.data)
     except Exception as e:
         ip_username(request)
         return restful.result2("操作失败",
                                kwargs=logger.error(e.args),
                                data=e.args)
Ejemplo n.º 7
0
    def post(self, request, *args, **kwargs):

        try:
            ret = self.create(request, *args, **kwargs)
            return restful.result(message="保存成功")
        except Exception as e:
            ip_username(request)
            return restful.result2(message="操作失败",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 8
0
    def patch(self, request, id):
        try:

            ret = User.objects.filter(pk=id).first()
            ser = UserSerializer(instance=ret, many=False)
            return restful.result(message="查询成功", data=ser.data)
        except Exception as e:
            ip_username(request)
            return restful.result2(message="传输id有误",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 9
0
 def get(self, request, *args, **kwargs):
     # todo 创建用户
     # User.objects.create_user("dading","1")
     try:
         ret = self.list(request, *args, **kwargs)
         return restful.result(message="操作成功", data=ret.data)
     except Exception as e:
         ip_username(request)
         return restful.result2(message="操作失败",
                                kwargs=logger.error(e.args),
                                data=e.args)
Ejemplo n.º 10
0
    def get(self, request):
        try:

            user_id = request.GET.get("user_id")
            if not user_id:
                user_id = request.user.id

            menus = Menu.objects.filter(roles__users__id=user_id).distinct()

            if menus.count() == 0:
                print("请为用户添加角色")
                return restful.result2(message="当前用户无角色,请联系管理员")
            else:

                ser = MenuSerializer(instance=menus, many=True)
                return restful.result(message="查询成功", data=ser.data)
        except Exception as e:
            ip_username(request)
            return restful.result2(message="操作失败",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 11
0
    def delete(self, request, *args, id):
        try:
            self.destroy(request, *args, id)
            print(id)
            print(Menu.objects.filter(parent_id=id))
            Menu.objects.filter(parent_id=id).delete()

            return restful.ok()
        except Exception as e:
            ip_username(request)
            return restful.result2(message="操作失败",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 12
0
    def put(self, request):

        try:
            ret = Menu.objects.get(pk=request.data['id'])
            # partial=True 标识局部跟新
            ser = MenuSerializer(instance=ret, data=request.data, partial=True)
            if ser.is_valid():
                ser.save()
            return restful.ok()
        except Exception as e:
            ip_username(request)
            return restful.result2(message="操作失败",
                                   kwargs=logger.error(e.args),
                                   data=e.args)
Ejemplo n.º 13
0
 def put(self, request, *args, **kwargs):
     # print(request.data)
     try:
         # self.partial_update(request, *args, **kwargs)
         ret = Role.objects.filter(id=request.data['id']).first()
         ser = RoleSerializer(instance=ret, data=request.data, partial=True)
         if ser.is_valid():
             ser.save()
         return restful.ok()
     except Exception as e:
         ip_username(request)
         return restful.result2(message="操作失败",
                                kwargs=logger.error(e.args),
                                data=e.args)
Ejemplo n.º 14
0
 def post(self, request, *args, **kwargs):
     print(request.data)
     try:
         # 判断是否为一级菜单,parent_id为0标识一级菜单
         if request.data["parent_id"] == 0:
             # 查询编码值最大的一级菜单,设置以及菜单的编码和排序字段
             firstLevelMenu = Menu.objects.filter(
                 parent_id=0).order_by("-code").first()
             # print(connection.queries)
             if firstLevelMenu:
                 code_num = int(firstLevelMenu.code) + 1
                 code = "%03d" % code_num
                 order_num = firstLevelMenu.order_num + 5
             else:
                 code = "001"
                 order_num = 1
             request.data["code"] = code
             request.data["order_num"] = order_num
             request.data["layer"] = 1
             # print(code)
             # print(order_num)
         else:
             # 查询父级菜单
             parentNenu = Menu.objects.get(id=request.data["parent_id"])
             # 其他层级的菜单,查询同一个父亲的菜单
             brotherMenu = Menu.objects.filter(
                 parent_id=parentNenu.id).order_by("-code").first()
             if brotherMenu:
                 code_num = int(brotherMenu.code[-3:]) + 1
                 code = brotherMenu.code[:-3] + "%03d" % code_num
                 order_num = brotherMenu.order_num + 5
             else:
                 code = parentNenu.code + "001"
                 order_num = 1
             request.data["code"] = code
             request.data["order_num"] = order_num
             request.data["layer"] = parentNenu.layer + 1
         request.data["perms"] = request.data["code"]
         ret = self.create(request, *args, **kwargs)
         # return restful.result(message="保存成功",data=ret.data)
         return restful.result(message="保存成功")
     except Exception as e:
         ip_username(request)
         return restful.result2(message="操作失败",
                                kwargs=logger.error(e.args),
                                data=e.args)