12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- from django.contrib.auth.models import Group
- from django.db.models import Sum
- from rest_framework import status
- from rest_framework.response import Response
- from rest_framework.views import APIView
- from rest_framework.viewsets import ModelViewSet
- from rolepermissions.roles import get_user_roles
- from basic.models import Menu, RouterMenu, GroupRouterMenu
- from basic.serializer import MenuSerializer, RouterMenuSerializer, GroupRouterMenuSerializer
- from payment.models import Payment
- class MenuView(ModelViewSet):
- queryset = Menu.objects.all()
- serializer_class = MenuSerializer
- pagination_class = None
- filter_fields = {'code': ['exact'], 'parent': ['exact']}
- class StatisticsView(APIView):
- def get(self, request):
- data = list(
- Payment.objects.values('payment_type').annotate(price_sum=Sum('index_price')).order_by('-price_sum'))
- return Response(data, status=status.HTTP_200_OK)
- class GroupRouterMenuView(APIView):
- queryset = Menu.objects.all()
- serializer_class = GroupRouterMenuSerializer
- pagination_class = None
- def get(self, request):
- user = request.user
- if user.is_superuser:
- return Response(RouterMenu.objects.all().values(), status=status.HTTP_200_OK)
- role = get_user_roles(user)
- # 根据当前用户的角色获取角色id
- roleArr = Group.objects.filter(name__in=[r.get_name() for r in role]).all().values('id')
- # 根据角色id查到所有相关的角色与菜单对应的记录
- menu = GroupRouterMenu.objects.filter(group_id__in=[arr['id'] for arr in roleArr]).all()
- # 生成菜单列表
- data = [RouterMenuSerializer(m.menu).data for m in menu]
- return Response(data, status=status.HTTP_200_OK)
- class RouterMenuView(ModelViewSet):
- queryset = Menu.objects.all()
- serializer_class = RouterMenuSerializer
- pagination_class = None
|