from django.shortcuts import get_object_or_404
from rest_framework import generics, permissions as drf_permissions
from rest_framework.exceptions import NotFound, ValidationError, PermissionDenied

from api.base.exceptions import Gone
from api.base import permissions as base_permissions
from api.base.views import JSONAPIBaseView
from api.comments.permissions import (
    CommentDetailPermissions,
    CommentReportsPermissions,
)
from api.comments.serializers import (
    CommentSerializer,
    NodeCommentDetailSerializer,
    RegistrationCommentDetailSerializer,
    CommentReportSerializer,
    CommentReportDetailSerializer,
    CommentReport,
)
from framework.auth.core import Auth
from framework.auth.oauth_scopes import CoreScopes
from framework.exceptions import PermissionsError
from osf.models import AbstractNode, Comment, BaseFileNode
from addons.wiki.models import WikiPage


class CommentMixin(object):
    """Mixin with convenience methods for retrieving the current comment  based on the
    current URL. By default, fetches the comment based on the comment_id kwarg.
    """

    serializer_class = CommentSerializer
    comment_lookup_url_kwarg = 'comment_id'

    def get_comment(self, check_permissions=True):
        pk = self.kwargs[self.comment_lookup_url_kwarg]
        comment = get_object_or_404(Comment, guids___id=pk, root_target__isnull=False, guids___id__isnull=False)