61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
from django.shortcuts import resolve_url, redirect
|
|
from django.views.generic import FormView
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
from library.views import CollectionMixin
|
|
from library.models import Work, Document
|
|
from library import forms
|
|
|
|
|
|
class WorkGDriveView(CollectionMixin, SingleObjectMixin, FormView):
|
|
model = Work
|
|
template_name = "library/gdrive.html"
|
|
form_class = forms.DocumentLinkForm
|
|
|
|
@property
|
|
def cancel_url(self):
|
|
return resolve_url("work_detail", self.collection.pk, self.kwargs["pk"])
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
data = super().get_context_data(*args, **kwargs)
|
|
|
|
data["meta"] = dict(self.object.meta_info.values_list("name", "value"))
|
|
print(data["meta"])
|
|
|
|
return data
|
|
|
|
def form_valid(self, form):
|
|
link = form.cleaned_data["link"]
|
|
|
|
storage = self.collection.storage.instance()
|
|
self.object = self.get_object()
|
|
|
|
try:
|
|
folderid = storage.get_folder_id(link)
|
|
self.object.meta_info.update_or_create(
|
|
name="folderid", defaults={"value": folderid}
|
|
)
|
|
return redirect("work_detail", self.collection.pk, self.kwargs["pk"])
|
|
except FileNotFoundError:
|
|
pass # not a folder id
|
|
|
|
try:
|
|
link = self.collection.storage.instance().import_link(link)
|
|
except AttributeError:
|
|
pass
|
|
except FileNotFoundError as e:
|
|
form.add_error("link", str(e))
|
|
return self.form_invalid(form)
|
|
|
|
work = self.collection.works.get(pk=self.kwargs["pk"])
|
|
|
|
doc = Document(
|
|
work=work,
|
|
upload=f"{self.collection.storage.name}:{link}",
|
|
doctype=Document.DOCTYPE_PDF,
|
|
)
|
|
doc.save()
|
|
doc.auto_tag()
|
|
|
|
return redirect("work_detail", self.collection.pk, self.kwargs["pk"])
|