(custom_bulk_actions)=
# 사용자 지정 대량 작업 추가
이 문서는 다양한 목록에 사용자 지정 대량 작업을 추가하는 방법을 설명합니다.
## 사용자 지정 대량 작업 등록
```python
from wagtail.admin.views.bulk_action import BulkAction
from wagtail import hooks
@hooks.register('register_bulk_action')
class CustomDeleteBulkAction(BulkAction):
display_name = _("삭제")
aria_label = _("선택한 객체 삭제")
action_type = "delete"
template_name = "/path/to/confirm_bulk_delete.html"
models = [...]
@classmethod
def execute_action(cls, objects, **kwargs):
for obj in objects:
do_something(obj)
return num_parent_objects, num_child_objects # 업데이트된 객체 수를 반환합니다.
```
속성은 다음과 같습니다:
- `display_name` - 사용자 인터페이스의 버튼에 표시될 레이블
- `aria_label` - 사용자 인터페이스의 버튼에 적용될 `aria-label` 속성
- `action_type` - 작업에 대한 고유 식별자 (대량 작업의 URL에 필요)
- `template_name` - 확인 템플릿의 경로
- `models` - 대량 작업이 수행될 수 있는 모델 목록
- `action_priority` (선택 사항) - 버튼 목록에서 버튼의 위치를 결정하는 데 사용되는 숫자
- `classes` (선택 사항) - 사용자 인터페이스의 버튼에 사용될 CSS 클래스 이름 집합
확인 템플릿의 예시는 다음과 같습니다:
```html+django
{% extends 'wagtailadmin/bulk_actions/confirmation/base.html' %}
{% load i18n wagtailadmin_tags %}
{% block titletag %}{% blocktranslate trimmed count counter=items|length %}1개 항목 삭제{% plural %}{{ counter }}개 항목 삭제{% endblocktranslate %}{% endblock %}
{% block header %}
{% trans "삭제" as del_str %}
{% include "wagtailadmin/shared/header.html" with title=del_str icon="doc-empty-inverse" %}
{% endblock header %}
{% block items_with_access %}
{% if items %}
{% trans "이 항목들을 삭제하시겠습니까?" %}
{% endif %}
{% endblock items_with_access %}
{% block items_with_no_access %}
{% blocktranslate trimmed asvar no_access_msg count counter=items_with_no_access|length %}이 항목을 삭제할 권한이 없습니다{% plural %}이 항목들을 삭제할 권한이 없습니다{% endblocktranslate %}
{% include './list_items_with_no_access.html' with items=items_with_no_access no_access_msg=no_access_msg %}
{% endblock items_with_no_access %}
{% block form_section %}
{% if items %}
{% trans '예, 삭제합니다' as action_button_text %}
{% trans "아니요, 삭제하지 않습니다" as no_action_button_text %}
{% include 'wagtailadmin/bulk_actions/confirmation/form.html' with action_button_class="serious" %}
{% else %}
{% include 'wagtailadmin/bulk_actions/confirmation/go_back.html' %}
{% endif %}
{% endblock form_section %}
```
```html+django
{% extends 'wagtailadmin/bulk_actions/confirmation/list_items_with_no_access.html' %}
{% load i18n %}
{% block per_item %}
{% if item.can_edit %}
{{ item.item.title }}
{% else %}
{{ item.item.title }}
{% endif %}
{% endblock per_item %}
```
`execute_action` 클래스 메서드는 대량 작업이 제대로 작동하기 위해 재정의해야 하는 유일한 메서드입니다. 이 메서드는 객체 목록을 유일한 필수 인수로 사용하며, `get_execution_context` 메서드를 재정의하여 제공할 수 있는 여러 키워드 인수를 사용합니다. 예를 들어.
```python
@classmethod
def execute_action(cls, objects, **kwargs):
# 여기서 kwargs는 get_execution_context 메서드의 출력입니다.
user = kwargs.get('user', None)
num_parent_objects, num_child_objects = 0, 0
# 객체별로 작업을 실행하거나 Django의 대량 업데이트 및 삭제 메서드를 사용하여 대량으로 실행할 수 있습니다.
for obj in objects:
num_child_objects += obj.get_children().count()
num_parent_objects += 1
obj.delete(user=user)
num_parent_objects += 1
return num_parent_objects, num_child_objects
```
`get_execution_context` 메서드는 `execute_action` 에 컨텍스트를 제공하기 위해 재정의할 수 있습니다.
```python
def get_execution_context(self):
return { 'user': self.request.user }
```
`get_context_data` 메서드는 확인 템플릿에 추가 컨텍스트를 전달하기 위해 재정의할 수 있습니다.
```python
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['new_key'] = some_value
return context
```
`check_perm` 메서드는 객체가 특정 권한을 가지고 있는지 여부를 확인하기 위해 재정의할 수 있습니다. `check_perm` 이 `False` 를 반환하는 객체는 컨텍스트에서 `'items_with_no_access'` 키 아래에서 사용할 수 있습니다.
```python
def check_perm(self, obj):
return obj.has_perm('some_perm') # True 또는 False 반환
```
관리자에 표시되는 성공 메시지는 `get_success_message` 메서드를 재정의하여 사용자 정의할 수 있습니다.
```python
def get_success_message(self, num_parent_objects, num_child_objects):
return _("{}개 객체(하위 객체 {}개 포함)가 업데이트되었습니다.".format(num_parent_objects, num_child_objects))
```
## 페이지 탐색기에 대량 작업 추가
페이지에 대한 사용자 지정 대량 작업 클래스를 만들 때 `wagtail.admin.views.bulk_action.BulkAction` 대신 `wagtail.admin.views.pages.bulk_actions.page_bulk_action.PageBulkAction` 에서 서브클래스화합니다.
### 기본 예시
```python
from wagtail.admin.views.pages.bulk_actions.page_bulk_action import PageBulkAction
from wagtail import hooks
@hooks.register('register_bulk_action')
class CustomPageBulkAction(PageBulkAction):
...
```
## 이미지 목록에 대량 작업 추가
이미지에 대한 사용자 지정 대량 작업 클래스를 만들 때 `wagtail.admin.views.bulk_action.BulkAction` 대신 `wagtail.images.views.bulk_actions.image_bulk_action.ImageBulkAction` 에서 서브클래스화합니다.
### 기본 예시
```python
from wagtail.images.views.bulk_actions.image_bulk_action import ImageBulkAction
from wagtail import hooks
@hooks.register('register_bulk_action')
class CustomImageBulkAction(ImageBulkAction):
...
```
## 문서 목록에 대량 작업 추가
문서에 대한 사용자 지정 대량 작업 클래스를 만들 때 `wagtail.admin.views.bulk_action.BulkAction` 대신 `wagtail.documents.views.bulk_actions.document_bulk_action.DocumentBulkAction` 에서 서브클래스화합니다.
### 기본 예시
```python
from wagtail.documents.views.bulk_actions.document_bulk_action import DocumentBulkAction
from wagtail import hooks
@hooks.register('register_bulk_action')
class CustomDocumentBulkAction(DocumentBulkAction):
...
```
## 사용자 목록에 대량 작업 추가
사용자에 대한 사용자 지정 대량 작업 클래스를 만들 때 `wagtail.admin.views.bulk_action.BulkAction` 대신 `wagtail.users.views.bulk_actions.user_bulk_action.UserBulkAction` 에서 서브클래스화합니다.
### 기본 예시
```python
from wagtail.users.views.bulk_actions.user_bulk_action import UserBulkAction
from wagtail import hooks
@hooks.register('register_bulk_action')
class CustomUserBulkAction(UserBulkAction):
...
```
(wagtailsnippets_custom_bulk_actions)=
## 스니펫 목록에 대량 작업 추가
스니펫에 대한 사용자 지정 대량 작업 클래스를 만들 때 `wagtail.admin.views.bulk_action.BulkAction` 대신 `wagtail.snippets.bulk_actions.snippet_bulk_action.SnippetBulkAction` 에서 서브클래스화합니다.
### 기본 예시
```python
from wagtail.snippets.bulk_actions.snippet_bulk_action import SnippetBulkAction
from wagtail import hooks
@hooks.register('register_bulk_action')
class CustomSnippetBulkAction(SnippetBulkAction):
# ...
```
특정 스니펫에만 작업을 적용하려면 작업 클래스의 `models` 목록을 재정의합니다.
```python
from wagtail.snippets.bulk_actions.snippet_bulk_action import SnippetBulkAction
from wagtail import hooks
@hooks.register('register_bulk_action')
class CustomSnippetBulkAction(SnippetBulkAction):
models = [SnippetA, SnippetB]
# ...
```