GdriveFolder code
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from django.shortcuts import get_object_or_404, redirect, resolve_url
|
||||
from django.http import HttpRequest
|
||||
from django.views.generic import TemplateView
|
||||
from django.views.generic.detail import DetailView, SingleObjectMixin, View
|
||||
from django.views.generic.list import ListView
|
||||
@@ -19,7 +20,7 @@ import string
|
||||
from interface.views import ProjectMixin, AuthorizedResourceMixin
|
||||
from interface.utils import signed_url
|
||||
from library.models import Collection, Work, Document, Section
|
||||
from library.music_tags import MUSIC_TAGS, MusicTag, auto_tag
|
||||
from library.music_tags import MUSIC_TAGS, MusicTag
|
||||
from library import forms, models
|
||||
from library.pdf_utils import extract_pages, extract_and_concat
|
||||
from library.indexer import indexer, model_search
|
||||
@@ -33,14 +34,15 @@ class ProjectItemListView(ProjectMixin, ListView):
|
||||
template_name = "library/item_list.html"
|
||||
model = models.ProjectItem
|
||||
|
||||
def post(self, request, **kwargs):
|
||||
def post(self, request: HttpRequest, **kwargs):
|
||||
|
||||
project_works = self.project.works.all()
|
||||
|
||||
instruments = request.POST.getlist("instruments")
|
||||
works = request.POST.getlist("works")
|
||||
|
||||
self.request.session["part"] = request.POST.get("part", "")
|
||||
self.request.session["instrument"] = request.POST.get("instrument")
|
||||
request.session["part"] = request.POST.get("part", "")
|
||||
request.session["instrument"] = request.POST.get("instrument")
|
||||
|
||||
valid_pks = [x.pk for x in project_works]
|
||||
|
||||
@@ -314,6 +316,18 @@ class WorkAddView(CollectionMixin, FormView):
|
||||
class WorkDetailView(CollectionMixin, DetailView):
|
||||
model = models.Work
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
context = super().get_context_data(*args, **kwargs)
|
||||
|
||||
methods = set("upload")
|
||||
match self.collection.storage.storage:
|
||||
case "library.storage.GDriveLinkStorage":
|
||||
methods.discard("upload")
|
||||
methods.add("gdrive")
|
||||
context["methods"] = methods
|
||||
|
||||
return context
|
||||
|
||||
|
||||
class WorkUpdateView(CollectionMixin, UpdateView):
|
||||
model = models.Work
|
||||
@@ -471,13 +485,10 @@ class WorkAddDocumentView(CollectionMixin, CreateView):
|
||||
# auto tag the document
|
||||
|
||||
# name, ext = os.path.splitext(os.path.basename(doc.upload.name))
|
||||
if doc.doctype == models.Document.DOCTYPE_PDF:
|
||||
inst = auto_tag(orig_name)
|
||||
if inst:
|
||||
doc.sections.create(tag=inst.abbreviate())
|
||||
doc.auto_tag()
|
||||
|
||||
if self.request.headers["Accept"] == "application/json":
|
||||
os.path.basename(doc.upload.name)
|
||||
# filename = os.path.basename(doc.upload.name)
|
||||
return JsonResponse(
|
||||
{
|
||||
"message": "created",
|
||||
@@ -497,6 +508,31 @@ class WorkAddDocumentView(CollectionMixin, CreateView):
|
||||
return redirect("document_annotate", self.collection.pk, doc.pk)
|
||||
|
||||
|
||||
class WorkAddDocumentBulkView(CollectionMixin, FormView):
|
||||
template_name = "interface/default_form.html"
|
||||
form_class = forms.DocumentBulkForm
|
||||
|
||||
@property
|
||||
def cancel_url(self):
|
||||
return resolve_url("work_detail", self.collection.pk, self.kwargs["pk"])
|
||||
|
||||
def form_valid(self, form):
|
||||
folder_link = form.cleaned_data["folder_link"]
|
||||
|
||||
work = self.collection.works.get(pk=self.kwargs["pk"])
|
||||
current = set(work.docs.values_list("upload", flat=True))
|
||||
|
||||
logger.info("Current documents: %r", current)
|
||||
|
||||
for link in self.collection.storage.instance().folder_import(folder_link):
|
||||
uri = f"{self.collection.storage.name}:{link}"
|
||||
if uri not in current:
|
||||
doc = work.docs.create(upload=uri, doctype=Document.DOCTYPE_PDF)
|
||||
doc.auto_tag()
|
||||
|
||||
return redirect("work_detail", self.collection.pk, self.kwargs["pk"])
|
||||
|
||||
|
||||
class DocumentMixin(CollectionMixin):
|
||||
model = models.Document
|
||||
|
||||
|
||||
Reference in New Issue
Block a user