from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import View
from django.views import generic
from books.models import Book, Author, Paper

from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView


class BookListView(ScrobbleableListView):
    model = Book


class BookDetailView(ScrobbleableDetailView):
    model = Book


class AuthorDetailView(generic.DetailView):
    model = Author
    slug_field = "uuid"


class PaperListView(ScrobbleableListView):
    model = Paper


class PaperDetailView(ScrobbleableDetailView):
    model = Paper


class PaperUploadPdfView(View):
    def post(self, request, slug):
        paper = Paper.objects.filter(uuid=slug).first()
        if not paper or not request.user.is_authenticated:
            return HttpResponseRedirect(reverse("books:paper_detail", args=[slug]))

        pdf_file = request.FILES.get("pdf_file")
        if pdf_file:
            paper.pdf_file.save(pdf_file.name, pdf_file)

        return HttpResponseRedirect(reverse("books:paper_detail", args=[slug]))
