from rest_framework import generics
from rest_framework import permissions as drf_permissions

from modularodm import Q

from framework.auth.oauth_scopes import CoreScopes

from website.models import Node

from api.base import permissions as base_permissions
from api.base.views import JSONAPIBaseView
from api.base.filters import ODMFilterMixin
from .serializers import PreprintSerializer, PreprintDetailSerializer
from api.nodes.views import NodeMixin, WaterButlerMixin, NodeContributorsList
from api.base.utils import get_user_auth, get_object_or_error
from website.exceptions import NodeStateError
from rest_framework.exceptions import ValidationError, NotFound
from api.nodes.serializers import (
    NodeContributorsSerializer
)


class PreprintMixin(NodeMixin):
    serializer_class = PreprintSerializer
    node_lookup_url_kwarg = 'node_id'

    def get_node(self):
        node = get_object_or_error(
            Node,
            self.kwargs[self.node_lookup_url_kwarg],
            display_name='preprint'
        )
        if not node.is_preprint:
            raise NotFound

        return node


class PreprintList(JSONAPIBaseView, generics.ListAPIView, ODMFilterMixin):
    permission_classes = (
        drf_permissions.IsAuthenticatedOrReadOnly,
        base_permissions.TokenHasScope,
    )

    required_read_scopes = [CoreScopes.USERS_READ]
    required_write_scopes = [CoreScopes.USERS_WRITE]

    serializer_class = PreprintSerializer

    ordering = ('-preprint_created')
    view_category = 'preprints'
    view_name = 'preprint-list'

    # overrides ODMFilterMixin
    def get_default_odm_query(self):
        return (
            Q('preprint_file', 'neq', None) &
            Q('_is_preprint_orphan', 'eq', False) &
            Q('is_public', 'eq', True)
        )

    # overrides ListAPIView
    def get_queryset(self):
        query = self.get_query_from_request()
        nodes = Node.find(query)

        return nodes


class PreprintDetail(JSONAPIBaseView, generics.RetrieveUpdateDestroyAPIView, PreprintMixin, WaterButlerMixin):
    permission_classes = (
        drf_permissions.IsAuthenticatedOrReadOnly,
        base_permissions.TokenHasScope,
    )

    required_read_scopes = [CoreScopes.NODE_BASE_READ]
    required_write_scopes = [CoreScopes.NODE_BASE_WRITE]

    serializer_class = PreprintDetailSerializer
    view_category = 'preprints'
    view_name = 'preprint-detail'

    # overrides RetrieveUpdateDestroyAPIView
    def get_object(self):
        return self.get_node()

    # overrides RetrieveUpdateDestroyAPIView
    def perform_destroy(self, instance):
        auth = get_user_auth(self.request)
        node = self.get_object()
        try:
            node.remove_node(auth=auth)
        except NodeStateError as err:
            raise ValidationError(err.message)
        node.save()


class PreprintAuthorsList(NodeContributorsList, PreprintMixin):

    view_category = 'preprint'
    view_name = 'preprint-authors'

    def get_serializer_class(self):
        return NodeContributorsSerializer

@@ -1130,32 +1130,56 @@
 JSONAPIBaseView,
+ generics.CreateAPIView,
  generics.ListAP
@@ -1366,21 +1366,25 @@
 eScopes.
-USERS
+NODE_BASE
 _READ%5D%0A 
@@ -1426,13 +1426,17 @@
 pes.
-USERS
+NODE_BASE
 _WRI
@@ -1704,17 +1704,16 @@
 le', 'ne
-q
 ', None)
@@ -1719,60 +1719,8 @@
 ) &%0A
-            Q('_is_preprint_orphan', 'eq', False) &%0A
     
@@ -1916,21 +1916,60 @@
  return 
-nodes
+%5Bnode for node in nodes if node.is_preprint%5D
 %0A%0A%0Aclass
@@ -2020,31 +2020,24 @@
 trieveUpdate
-Destroy
 APIView, Pre
@@ -2424,53 +2424,8 @@
 l'%0A%0A
-    # overrides RetrieveUpdateDestroyAPIView%0A
     
@@ -2482,329 +2482,8 @@
 ()%0A%0A
-    # overrides RetrieveUpdateDestroyAPIView%0A    def perform_destroy(self, instance):%0A        auth = get_user_auth(self.request)%0A        node = self.get_object()%0A        try:%0A            node.remove_node(auth=auth)%0A        except NodeStateError as err:%0A            raise ValidationError(err.message)%0A        node.save()%0A%0A
 %0Acla

Update scopes, view inheritence, update db query for orphaned files