from django.contrib.auth.models import * from django.shortcuts import render # Create your views here. from rest_framework import status, permissions from rest_framework.response import Response # class RolePermissionViews(APIView): # def get(self, request): from rest_framework.viewsets import ModelViewSet from power.models import * from power.serializer import RouteSerializer, UserRouteSerializer class UserRouteViews(ModelViewSet): pagination_class = None queryset = UserRoute.objects.all() serializer_class = UserRouteSerializer def list(self, request): user = request.user userId = user.id userRouteList = UserRoute.objects.filter(user_id = user.id) routeList = list() parentRoute = list() queryRoute = list() routeIdArray = [] for userRoute in userRouteList: route = userRoute.route parentRoute.append(userRoute.route) routeIdArray.append(route.id) while len(parentRoute) > 0: queryRoute = parentRoute parentRoute = list() for route in queryRoute: routeList.append(route) parent = route.parent if parent != None and parent.id not in routeIdArray: parentRoute.append(parent) routeIdArray.append(parent.id) data = RouteSerializer(routeList, many=True) return Response(data=data.data, status=status.HTTP_200_OK) class RouteViews(ModelViewSet): pagination_class = None queryset = Route.objects.all() serializer_class = RouteSerializer