# Path: yawn/task/views.py
# class TaskViewSet(viewsets.GenericViewSet,
#                   viewsets.mixins.RetrieveModelMixin,
#                   viewsets.mixins.UpdateModelMixin):
#     """
#     GET a task, and its executions. PATCH to re-run or terminate.
#     """
#     queryset = Task.objects.all().order_by('id').prefetch_related(
#         'execution_set__worker').select_related('template__workflow__name')
# 
#     serializer_class = TaskDetailSerializer
# 
# class ExecutionViewSet(viewsets.GenericViewSet,
#                        viewsets.mixins.ListModelMixin):
#     """
#     GET a list of Executions
#     """
#     queryset = Execution.objects.all().order_by('-id').select_related(
#         'worker', 'task__template__workflow__name')
# 
#     serializer_class = ExecutionListSerializer
# 
#     def get_queryset(self):
#         """
#         Optionally filter to the executions for a given worker
#         """
#         queryset = self.queryset
#         worker = self.request.query_params.get('worker')
#         if worker is not None:
#             # worker page filters and reverses the order
#             queryset = queryset.filter(worker_id=worker)
#         return queryset
# 
# Path: yawn/worker/views.py
# class QueueViewSet(viewsets.GenericViewSet,
#                    viewsets.mixins.ListModelMixin,
#                    viewsets.mixins.RetrieveModelMixin,
#                    viewsets.mixins.UpdateModelMixin):
#     """
#     Queue endpoint GET(list,detail), PATCH to clear all messages
#     """
#     queryset = Queue.objects.all().annotate(Count('message')).order_by('id')
# 
#     serializer_class = QueueSerializer
# 
# class WorkerViewSet(viewsets.GenericViewSet,
#                     viewsets.mixins.ListModelMixin,
#                     viewsets.mixins.RetrieveModelMixin):
#     """
#     Worker endpoint, GET(list)
#     """
#     queryset = Worker.objects.all().order_by('-id')
# 
#     serializer_class = WorkerSerializer
# 
# Path: yawn/workflow/views.py
# class WorkflowViewSet(viewsets.GenericViewSet,
#                       viewsets.mixins.ListModelMixin,
#                       viewsets.mixins.RetrieveModelMixin,
#                       viewsets.mixins.UpdateModelMixin,
#                       viewsets.mixins.CreateModelMixin):
#     """
#     Workflow endpoint.
# 
#     The POST action is unusual as it checks for an existing, identical
#     workflow and return it if found instead of creating a new object.
#     """
#     queryset = Workflow.objects.select_related('name').order_by('id')
# 
#     serializer_class = WorkflowSerializer
# 
# class WorkflowNameViewSet(viewsets.ReadOnlyModelViewSet):
#     """
#     WorkflowName endpoint. Provides the name, current version number,
#     current_version_id, and the select_version_id if a version was
#     requested in the query string.
# 
#     Used to list the workflows and power version switcher.
#     """
#     queryset = WorkflowName.objects.select_related('current_version').annotate(
#         task_count=Count('current_version__template')).order_by('id')
#     page_size = 100000  # not much pagination
# 
#     serializer_class = WorkflowNameSerializer
# 
# class RunViewSet(viewsets.GenericViewSet,
#                  viewsets.mixins.ListModelMixin,
#                  viewsets.mixins.RetrieveModelMixin,
#                  viewsets.mixins.CreateModelMixin,
#                  viewsets.mixins.UpdateModelMixin):
#     """
#     Run endpoint: GET(list,detail), POST(list) and PATCH(detail)
#     """
# 
#     serializer_class = RunSerializer
# 
#     queryset = Run.objects.prefetch_related('task_set__template').order_by('id')
# 
#     def get_queryset(self):
#         """
#         Optionally filter to the runs for a given workflow
#         """
#         queryset = self.queryset
#         workflow = self.request.query_params.get('workflow', None)
#         if workflow is not None:
#             queryset = queryset.filter(workflow_id=workflow)
#         return queryset
# 
# Path: yawn/user/views.py
# class UserViewSet(viewsets.GenericViewSet,
#                   viewsets.mixins.ListModelMixin,
#                   viewsets.mixins.CreateModelMixin,
#                   viewsets.mixins.RetrieveModelMixin,
#                   viewsets.mixins.UpdateModelMixin):
#     """
#     User endpoint, GET(list, detail), PATCH to change
# 
#     This ViewSet has `permission_classes` set and `/me/` has the default permissions
#     so that the default permission class can be set to IsAuthenticatedOrReadOnly
#     without leaking user API tokens.
#     """
#     queryset = User.objects.all().order_by('id')
# 
#     serializer_class = UserSerializer
#     permission_classes = (ModelPermissions,)
# 
#     @action(methods=['get'], permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES, detail=False)
#     def me(self, request):
#         serializer = self.get_serializer(request.user)
#         return Response(serializer.data)
# 
#     @action(methods=['patch'], permission_classes=[AllowAny], detail=False)
#     def login(self, request):
#         credentials = LoginSerializer(data=request.data)
#         credentials.is_valid(raise_exception=True)
#         user = authenticate(request, **credentials.data)
#         if not user:
#             return Response({'detail': 'Login failed'}, status.HTTP_401_UNAUTHORIZED)
# 
#         login(request, user)
#         return Response({'detail': 'Login succeeded'})
# 
#     @action(methods=['delete'], detail=False)
#     def logout(self, request):
#         logout(request)
#         return Response({'detail': 'Logout succeeded'})


from django.conf.urls import include, url
from django.http import HttpResponse
from rest_framework import routers
from yawn.task.views import TaskViewSet, ExecutionViewSet
from yawn.worker.views import QueueViewSet, WorkerViewSet
from yawn.workflow.views import WorkflowViewSet, WorkflowNameViewSet, RunViewSet
from yawn.user.views import UserViewSet


router = routers.DefaultRouter()
router.include_format_suffixes = False
router.register(r'names', WorkflowNameViewSet)
router.register(r'workflows', WorkflowViewSet)
router.register(r'runs', RunViewSet)
router.register(r'tasks', TaskViewSet)

# Path: yawn/task/views.py
# class TaskViewSet(viewsets.GenericViewSet,
#                   viewsets.mixins.RetrieveModelMixin,
#                   viewsets.mixins.UpdateModelMixin):
#     """
#     GET a task, and its executions. PATCH to re-run or terminate.
#     """
#     queryset = Task.objects.all().order_by('id').prefetch_related(
#         'execution_set__worker').select_related('template__workflow__name')
# 
#     serializer_class = TaskDetailSerializer
# 
# class ExecutionViewSet(viewsets.GenericViewSet,
#                        viewsets.mixins.ListModelMixin):
#     """
#     GET a list of Executions
#     """
#     queryset = Execution.objects.all().order_by('-id').select_related(
#         'worker', 'task__template__workflow__name')
# 
#     serializer_class = ExecutionListSerializer
# 
#     def get_queryset(self):
#         """
#         Optionally filter to the executions for a given worker
#         """
#         queryset = self.queryset
#         worker = self.request.query_params.get('worker')
#         if worker is not None:
#             # worker page filters and reverses the order
#             queryset = queryset.filter(worker_id=worker)
#         return queryset
# 
# Path: yawn/worker/views.py
# class QueueViewSet(viewsets.GenericViewSet,
#                    viewsets.mixins.ListModelMixin,
#                    viewsets.mixins.RetrieveModelMixin,
#                    viewsets.mixins.UpdateModelMixin):
#     """
#     Queue endpoint GET(list,detail), PATCH to clear all messages
#     """
#     queryset = Queue.objects.all().annotate(Count('message')).order_by('id')
# 
#     serializer_class = QueueSerializer
# 
# class WorkerViewSet(viewsets.GenericViewSet,
#                     viewsets.mixins.ListModelMixin,
#                     viewsets.mixins.RetrieveModelMixin):
#     """
#     Worker endpoint, GET(list)
#     """
#     queryset = Worker.objects.all().order_by('-id')
# 
#     serializer_class = WorkerSerializer
# 
# Path: yawn/workflow/views.py
# class WorkflowViewSet(viewsets.GenericViewSet,
#                       viewsets.mixins.ListModelMixin,
#                       viewsets.mixins.RetrieveModelMixin,
#                       viewsets.mixins.UpdateModelMixin,
#                       viewsets.mixins.CreateModelMixin):
#     """
#     Workflow endpoint.
# 
#     The POST action is unusual as it checks for an existing, identical
#     workflow and return it if found instead of creating a new object.
#     """
#     queryset = Workflow.objects.select_related('name').order_by('id')
# 
#     serializer_class = WorkflowSerializer
# 
# class WorkflowNameViewSet(viewsets.ReadOnlyModelViewSet):
#     """
#     WorkflowName endpoint. Provides the name, current version number,
#     current_version_id, and the select_version_id if a version was
#     requested in the query string.
# 
#     Used to list the workflows and power version switcher.
#     """
#     queryset = WorkflowName.objects.select_related('current_version').annotate(
#         task_count=Count('current_version__template')).order_by('id')
#     page_size = 100000  # not much pagination
# 
#     serializer_class = WorkflowNameSerializer
# 
# class RunViewSet(viewsets.GenericViewSet,
#                  viewsets.mixins.ListModelMixin,
#                  viewsets.mixins.RetrieveModelMixin,
#                  viewsets.mixins.CreateModelMixin,
#                  viewsets.mixins.UpdateModelMixin):
#     """
#     Run endpoint: GET(list,detail), POST(list) and PATCH(detail)
#     """
# 
#     serializer_class = RunSerializer
# 
#     queryset = Run.objects.prefetch_related('task_set__template').order_by('id')
# 
#     def get_queryset(self):
#         """
#         Optionally filter to the runs for a given workflow
#         """
#         queryset = self.queryset
#         workflow = self.request.query_params.get('workflow', None)
#         if workflow is not None:
#             queryset = queryset.filter(workflow_id=workflow)
#         return queryset
# 
# Path: yawn/user/views.py
# class UserViewSet(viewsets.GenericViewSet,
#                   viewsets.mixins.ListModelMixin,
#                   viewsets.mixins.CreateModelMixin,
#                   viewsets.mixins.RetrieveModelMixin,
#                   viewsets.mixins.UpdateModelMixin):
#     """
#     User endpoint, GET(list, detail), PATCH to change
# 
#     This ViewSet has `permission_classes` set and `/me/` has the default permissions
#     so that the default permission class can be set to IsAuthenticatedOrReadOnly
#     without leaking user API tokens.
#     """
#     queryset = User.objects.all().order_by('id')
# 
#     serializer_class = UserSerializer
#     permission_classes = (ModelPermissions,)
# 
#     @action(methods=['get'], permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES, detail=False)
#     def me(self, request):
#         serializer = self.get_serializer(request.user)
#         return Response(serializer.data)
# 
#     @action(methods=['patch'], permission_classes=[AllowAny], detail=False)
#     def login(self, request):
#         credentials = LoginSerializer(data=request.data)
#         credentials.is_valid(raise_exception=True)
#         user = authenticate(request, **credentials.data)
#         if not user:
#             return Response({'detail': 'Login failed'}, status.HTTP_401_UNAUTHORIZED)
# 
#         login(request, user)
#         return Response({'detail': 'Login succeeded'})
# 
#     @action(methods=['delete'], detail=False)
#     def logout(self, request):
#         logout(request)
#         return Response({'detail': 'Logout succeeded'})


# Path: yawn/urls.py
from django.conf.urls import include, url
from django.http import HttpResponse
from rest_framework import routers
from yawn.task.views import TaskViewSet, ExecutionViewSet
from yawn.worker.views import QueueViewSet, WorkerViewSet
from yawn.workflow.views import WorkflowViewSet, WorkflowNameViewSet, RunViewSet
from yawn.user.views import UserViewSet



router = routers.DefaultRouter()
router.include_format_suffixes = False
router.register(r'names', WorkflowNameViewSet)
router.register(r'workflows', WorkflowViewSet)
router.register(r'runs', RunViewSet)
router.register(r'tasks', TaskViewSet)

router.register(r'queues', QueueViewSet)