Initial commit

This commit is contained in:
2024-08-27 20:33:44 +02:00
commit 1f1832267d
14794 changed files with 1599592 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
from .add_tags import AddTagsBulkAction
from .add_to_collection import AddToCollectionBulkAction
from .delete import DeleteBulkAction
__all__ = ["AddToCollectionBulkAction", "AddTagsBulkAction", "DeleteBulkAction"]

View File

@@ -0,0 +1,44 @@
from django import forms
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext
from wagtail.admin import widgets
from wagtail.documents.views.bulk_actions.document_bulk_action import DocumentBulkAction
class TagForm(forms.Form):
tags = forms.Field(label=_("Tags"), widget=widgets.AdminTagWidget)
class AddTagsBulkAction(DocumentBulkAction):
display_name = _("Tag")
action_type = "add_tags"
aria_label = _("Add tags to the selected documents")
template_name = "wagtaildocs/bulk_actions/confirm_bulk_add_tags.html"
action_priority = 20
form_class = TagForm
def check_perm(self, document):
return self.permission_policy.user_has_permission_for_instance(
self.request.user, "change", document
)
def get_execution_context(self):
return {"tags": self.cleaned_form.cleaned_data["tags"].split(",")}
@classmethod
def execute_action(cls, objects, tags=[], **kwargs):
num_parent_objects = 0
if not tags:
return
for document in objects:
num_parent_objects += 1
document.tags.add(*tags)
return num_parent_objects, 0
def get_success_message(self, num_parent_objects, num_child_objects):
return ngettext(
"New tags have been added to %(num_parent_objects)d document",
"New tags have been added to %(num_parent_objects)d documents",
num_parent_objects,
) % {"num_parent_objects": num_parent_objects}

View File

@@ -0,0 +1,57 @@
from django import forms
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext
from wagtail.documents.views.bulk_actions.document_bulk_action import DocumentBulkAction
class CollectionForm(forms.Form):
def __init__(self, *args, **kwargs):
user = kwargs.pop("user", None)
super().__init__(*args, **kwargs)
self.fields["collection"] = forms.ModelChoiceField(
label=_("Collection"),
queryset=DocumentBulkAction.permission_policy.collections_user_has_permission_for(
user, "add"
),
)
class AddToCollectionBulkAction(DocumentBulkAction):
display_name = _("Add to collection")
action_type = "add_to_collection"
aria_label = _("Add selected documents to collection")
template_name = "wagtaildocs/bulk_actions/confirm_bulk_add_to_collection.html"
action_priority = 30
form_class = CollectionForm
collection = None
def check_perm(self, document):
return self.permission_policy.user_has_permission_for_instance(
self.request.user, "change", document
)
def get_form_kwargs(self):
return {**super().get_form_kwargs(), "user": self.request.user}
def get_execution_context(self):
return {"collection": self.cleaned_form.cleaned_data["collection"]}
@classmethod
def execute_action(cls, objects, collection=None, **kwargs):
if collection is None:
return
num_parent_objects = (
cls.get_default_model()
.objects.filter(pk__in=[obj.pk for obj in objects])
.update(collection=collection)
)
return num_parent_objects, 0
def get_success_message(self, num_parent_objects, num_child_objects):
collection = self.cleaned_form.cleaned_data["collection"]
return ngettext(
"%(num_parent_objects)d document has been added to %(collection)s",
"%(num_parent_objects)d documents have been added to %(collection)s",
num_parent_objects,
) % {"num_parent_objects": num_parent_objects, "collection": collection}

View File

@@ -0,0 +1,33 @@
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext
from wagtail.documents.views.bulk_actions.document_bulk_action import DocumentBulkAction
class DeleteBulkAction(DocumentBulkAction):
display_name = _("Delete")
action_type = "delete"
aria_label = _("Delete selected documents")
template_name = "wagtaildocs/bulk_actions/confirm_bulk_delete.html"
action_priority = 100
classes = {"serious"}
def check_perm(self, document):
return self.permission_policy.user_has_permission_for_instance(
self.request.user, "delete", document
)
@classmethod
def execute_action(cls, objects, **kwargs):
num_parent_objects = len(objects)
cls.get_default_model().objects.filter(
pk__in=[obj.pk for obj in objects]
).delete()
return num_parent_objects, 0
def get_success_message(self, num_parent_objects, num_child_objects):
return ngettext(
"%(num_parent_objects)d document has been deleted",
"%(num_parent_objects)d documents have been deleted",
num_parent_objects,
) % {"num_parent_objects": num_parent_objects}

View File

@@ -0,0 +1,36 @@
from wagtail.admin.views.bulk_action import BulkAction
from wagtail.documents import get_document_model
from wagtail.documents.permissions import (
permission_policy as documents_permission_policy,
)
class DocumentBulkAction(BulkAction):
permission_policy = documents_permission_policy
models = [get_document_model()]
def get_all_objects_in_listing_query(self, parent_id):
listing_objects = self.model.objects.all()
if parent_id is not None:
listing_objects = listing_objects.filter(collection_id=parent_id)
listing_objects = listing_objects.values_list("pk", flat=True)
if "q" in self.request.GET:
query_string = self.request.GET.get("q", "")
listing_objects = listing_objects.search(query_string).results()
return listing_objects
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["items_with_no_access"] = [
{
"item": document,
"can_edit": self.permission_policy.user_has_permission_for_instance(
self.request.user, "change", document
),
}
for document in context["items_with_no_access"]
]
return context