forked from wagtail/wagtail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CopyPageAction and admin API for copying pages
* Moved Page.copy implementation into an external function * Convert copy_page function into a class * Implement PageCopyAction.execute() * Implement CopyPageAction.check() To allow checking the action ahead-of-time * Make page copy view use PageCopyAction * Add page copy Admin API
- Loading branch information
Showing
10 changed files
with
559 additions
and
190 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class APIAction: | ||
serializer = None | ||
|
||
def __init__(self, view, request): | ||
self.view = view | ||
self.request = request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from django.core.exceptions import ValidationError as DjangoValidationError | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import fields, status | ||
from rest_framework.exceptions import ValidationError | ||
from rest_framework.response import Response | ||
from rest_framework.serializers import Serializer | ||
|
||
from wagtail.api.v2.utils import BadRequestError | ||
from wagtail.core.actions.copy_page import CopyPageAction, CopyPageIntegrityError | ||
from wagtail.core.models import Page | ||
from wagtail.core.utils import find_available_slug | ||
|
||
from .base import APIAction | ||
|
||
|
||
class CopyPageAPIActionSerializer(Serializer): | ||
# Note: CopyPageAction will validate the destination page | ||
destination_page_id = fields.IntegerField(required=False) | ||
recursive = fields.BooleanField(default=False, required=False) | ||
keep_live = fields.BooleanField(default=True, required=False) | ||
slug = fields.CharField(required=False) | ||
title = fields.CharField(required=False) | ||
|
||
|
||
class CopyPageAPIAction(APIAction): | ||
serializer = CopyPageAPIActionSerializer | ||
|
||
def _action_from_data(self, instance, data): | ||
destination_page_id = data.get('destination_page_id') | ||
if destination_page_id is None: | ||
destination = instance.get_parent() | ||
else: | ||
destination = get_object_or_404(Page, id=destination_page_id) | ||
|
||
update_attrs = {} | ||
if 'slug' in data: | ||
update_attrs['slug'] = data['slug'] | ||
else: | ||
# If user didn't specify a particular slug, find an available one | ||
available_slug = find_available_slug(destination, instance.slug) | ||
if available_slug != instance.slug: | ||
update_attrs['slug'] = available_slug | ||
|
||
if 'title' in data: | ||
update_attrs['title'] = data['title'] | ||
|
||
return CopyPageAction( | ||
page=instance, | ||
to=destination, | ||
recursive=data['recursive'], | ||
keep_live=data['keep_live'], | ||
update_attrs=update_attrs, | ||
user=self.request.user | ||
) | ||
|
||
def execute(self, instance, data): | ||
action = self._action_from_data(instance, data) | ||
|
||
try: | ||
new_page = action.execute() | ||
except DjangoValidationError as e: | ||
raise ValidationError(e.message_dict) | ||
except CopyPageIntegrityError as e: | ||
raise BadRequestError(e.args[0]) | ||
|
||
serializer = self.view.get_serializer(new_page) | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
|
||
class AdminAPITestCase(TestCase, WagtailTestUtils): | ||
def setUp(self): | ||
self.login() | ||
self.user = self.login() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.