Пример #1
0
 def obtain_page_menus(self, params: MenusRequestParam):
     """
     获取菜单分页数据
     :param params: 请求参数对象
     :return: 菜单分页数据
     """
     menus = Menu.objects.filter(
         params.get_search_q()).order_by(*params.get_order())
     paginator = Paginator(menus, params.page_size)
     return utils.format_page_data(
         paginator, params, convert_func=lambda obj: MenuVO(menu=obj))
Пример #2
0
 def obtain_menus(self, params: MenusRequestParam):
     """
     获取所有菜单
     :return: 菜单列表
     """
     menus = Menu.objects.all(**params.obtain_dict())
     return utils.objs_to_dicts(menus,
                                convert_func=lambda obj: MenuVO(menu=obj))
Пример #3
0
    def add_menu(self, params: MenusRequestParam):
        """
        添加菜单
        :param params:
        :return:
        """
        params.validate()
        if Role.objects.filter(name=params.title).exists():
            raise ParamError(CODE_10020)

        if Role.objects.filter(name=params.url).exists():
            raise ParamError(CODE_10021)

        now = timezone.now()
        create_dict: dict = params.obtain_dict()
        create_dict['create_time'] = now
        create_dict['update_time'] = now
        menu = Menu.objects.create(**create_dict)
        menu.save()
Пример #4
0
def ajax_update_menu(request: HttpRequest):
    """更新菜单"""
    resp = Resp()
    try:
        result = BdMenuService().update_menu(params=MenusRequestParam(request))
        resp.success(result)
    except BaseError as e:
        logger.error('update menu fail: %s', e)
        resp.fail_error(e)
    except Exception as e:
        logger.error('update menu error: %s', e)
        resp.fail(CODE_99999)
    return JsonResponse(utils.obj_to_dict(resp), encoder=CustomerEncoder)
Пример #5
0
def ajax_obtain_page_menus(request: HttpRequest):
    """获取菜单分页数据"""
    resp = Resp()
    try:
        result = BdMenuService().obtain_page_menus(params=MenusRequestParam(request))
        resp.success(data=result)
    except BaseError as e:
        logger.error('obtain page menus fail: %s', e)
        resp.fail_error(e)
    except Exception as e:
        logger.error('obtain page menus error: %s', e)
        resp.fail(CODE_99999)
    return JsonResponse(utils.obj_to_dict(resp), encoder=CustomerEncoder)
Пример #6
0
    def obtain_menu(self, params: MenusRequestParam):
        """
        获取菜单
        :param params:
        :return:
        """
        query_dict: dict = params.obtain_dict()
        if params.id:
            query_dict['id'] = params.id
        menu = Menu.objects.filter(**query_dict).first()
        if not menu:
            raise ParamError(CODE_10012)

        menu_vo = MenuVO(menu=menu)
        if menu_vo.parent_id:
            pm = Menu.objects.filter(id=menu_vo.parent_id)
            menu_vo.parent_menu = utils.obj_to_dict(
                pm, lambda obj: MenuVO(menu=obj))

        return utils.obj_to_dict(menu, lambda obj: MenuVO(menu=obj))
Пример #7
0
    def update_menu(self, params: MenusRequestParam):
        """
        更新菜单
        :param params:
        :return:
        """
        update_dict: dict = params.obtain_dict()

        if not params.id or not update_dict:
            raise ParamError(CODE_10001)

        if Role.objects.filter(name=params.title).exclude(
                id=params.id).exists():
            raise ParamError(CODE_10020)

        if Role.objects.filter(name=params.url).exclude(id=params.id).exists():
            raise ParamError(CODE_10021)

        now = timezone.now()
        update_dict['update_time'] = now
        return Menu.objects.filter(id=params.id).update(**update_dict)