views.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from django.contrib.auth.models import *
  2. from django.shortcuts import render
  3. # Create your views here.
  4. from rest_framework import status, permissions
  5. from rest_framework.response import Response
  6. # class RolePermissionViews(APIView):
  7. # def get(self, request):
  8. from rest_framework.viewsets import ModelViewSet
  9. from power.models import *
  10. from power.serializer import RouteSerializer, UserRouteSerializer
  11. class UserRouteViews(ModelViewSet):
  12. pagination_class = None
  13. queryset = UserRoute.objects.all()
  14. serializer_class = UserRouteSerializer
  15. def list(self, request):
  16. user = request.user
  17. userId = user.id
  18. userRouteList = UserRoute.objects.filter(user_id = user.id)
  19. routeList = list()
  20. parentRoute = list()
  21. queryRoute = list()
  22. routeIdArray = []
  23. for userRoute in userRouteList:
  24. route = userRoute.route
  25. parentRoute.append(userRoute.route)
  26. routeIdArray.append(route.id)
  27. while len(parentRoute) > 0:
  28. queryRoute = parentRoute
  29. parentRoute = list()
  30. for route in queryRoute:
  31. routeList.append(route)
  32. parent = route.parent
  33. if parent != None and parent.id not in routeIdArray:
  34. parentRoute.append(parent)
  35. routeIdArray.append(parent.id)
  36. data = RouteSerializer(routeList, many=True)
  37. return Response(data=data.data, status=status.HTTP_200_OK)
  38. class RouteViews(ModelViewSet):
  39. pagination_class = None
  40. queryset = Route.objects.all()
  41. serializer_class = RouteSerializer