from django.shortcuts import render
from django.http import Http404

from django.views.generic.edit import UpdateView
from django.views.generic import ListView, View
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.utils.decorators import method_decorator


import logging

from ..models.projects import Project
from ..models.authors import Author
from ..forms import AuthorForm
from .permission_helpers import PermissionOnObjectViewMixin

# logger for this file
logger = logging.getLogger(__name__)


class AuthorListView(ListView):
    """A generic view of the authors in a list"""

    paginate_by = 10
    template_name = "code_doc/authors/author_list.html"
    context_object_name = "authors"
    model = Author


def detail_author(request, author_id):
    try:
        author = Author.objects.get(pk=author_id)
    except Author.DoesNotExist:
        raise Http404

    project_list = Project.objects.filter(authors=author)
    coauthor_list = (
        Author.objects.filter(project__in=project_list).distinct().exclude(pk=author_id)
    )

    return render(
        request,
        "code_doc/authors/author_details.html",
        {
            "project_list": project_list,
            "author": author,
            "user": request.user,
            "coauthor_list": coauthor_list,
        },
    )


class AuthorUpdateView(PermissionOnObjectViewMixin, UpdateView):
    """View for editing information about an Author

      .. note:: in order to be able to edit an Author, the user should have the
                'code_doc.author_edit' permission on the Author object.
    """

    form_class = AuthorForm
    model = Author

    permissions_on_object = ("code_doc.author_edit",)
    permissions_object_getter = "get_author_from_request"

    template_name = "code_doc/authors/author_edit.html"

    pk_url_kwarg = "author_id"

    def get_author_from_request(self, request, *args, **kwargs):
        # TODO check if needed
        try:
            return Author.objects.get(pk=kwargs["author_id"])
        except Author.DoesNotExist:
            logger.warning(
                "[AuthorUpdateView] non existent Author with id %s", kwargs["author_id"]
            )
            return None


class MaintainerProfileView(View):
    """Manages the views associated to the maintainers"""

    @method_decorator(login_required)
    def get(self, request, maintainer_id):
        try:
            maintainer = User.objects.get(pk=maintainer_id)
        except Project.DoesNotExist:
            raise Http404

        projects = Project.objects.filter(administrators=maintainer)
        return render(
            request,
            "code_doc/maintainer_details.html",
            {"projects": projects, "maintainer": maintainer},
        )

    @method_decorator(login_required)
    def post(self, request):
        pass

[((19, 9, 19, 36), 'logging.getLogger', 'logging.getLogger', ({(19, 27, 19, 35): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((42, 11, 51, 5), 'django.shortcuts.render', 'render', ({(43, 8, 43, 15): 'request', (44, 8, 44, 46): '"""code_doc/authors/author_details.html"""', (45, 8, 50, 9): "{'project_list': project_list, 'author': author, 'user': request.user,\n    'coauthor_list': coauthor_list}"}, {}), "(request, 'code_doc/authors/author_details.html', {'project_list':\n    project_list, 'author': author, 'user': request.user, 'coauthor_list':\n    coauthor_list})", False, 'from django.shortcuts import render\n'), ((85, 5, 85, 37), 'django.utils.decorators.method_decorator', 'method_decorator', ({(85, 22, 85, 36): 'login_required'}, {}), '(login_required)', False, 'from django.utils.decorators import method_decorator\n'), ((99, 5, 99, 37), 'django.utils.decorators.method_decorator', 'method_decorator', ({(99, 22, 99, 36): 'login_required'}, {}), '(login_required)', False, 'from django.utils.decorators import method_decorator\n'), ((93, 15, 97, 9), 'django.shortcuts.render', 'render', ({(94, 12, 94, 19): 'request', (95, 12, 95, 46): '"""code_doc/maintainer_details.html"""', (96, 12, 96, 60): "{'projects': projects, 'maintainer': maintainer}"}, {}), "(request, 'code_doc/maintainer_details.html', {'projects': projects,\n    'maintainer': maintainer})", False, 'from django.shortcuts import render\n'), ((88, 25, 88, 59), 'django.contrib.auth.models.User.objects.get', 'User.objects.get', (), '', False, 'from django.contrib.auth.models import User\n')]