def copy(request, page_id): page = Page.objects.get(id=page_id) # Parent page defaults to parent of source page parent_page = page.get_parent() # Check if the user has permission to publish subpages on the parent can_publish = parent_page.permissions_for_user(request.user).can_publish_subpage() # Create the form form = CopyForm(request.POST or None, page=page, can_publish=can_publish) next_url = get_valid_next_url_from_request(request) # Check if user is submitting if request.method == "POST": # Prefill parent_page in case the form is invalid (as prepopulated value for the form field, # because ModelChoiceField seems to not fall back to the user given value) parent_page = Page.objects.get(id=request.POST["new_parent_page"]) if form.is_valid(): # Receive the parent page (this should never be empty) if form.cleaned_data["new_parent_page"]: parent_page = form.cleaned_data["new_parent_page"] # Make sure this user has permission to add subpages on the parent if not parent_page.permissions_for_user(request.user).can_add_subpage(): raise PermissionDenied # Re-check if the user has permission to publish subpages on the new parent can_publish = parent_page.permissions_for_user(request.user).can_publish_subpage() # Copy the page new_page = page.copy( recursive=form.cleaned_data.get("copy_subpages"), to=parent_page, update_attrs={"title": form.cleaned_data["new_title"], "slug": form.cleaned_data["new_slug"]}, keep_live=(can_publish and form.cleaned_data.get("publish_copies")), user=request.user, ) # Give a success message back to the user if form.cleaned_data.get("copy_subpages"): messages.success( request, _("Page '{0}' and {1} subpages copied.").format( page.get_admin_display_title(), new_page.get_descendants().count() ), ) else: messages.success(request, _("Page '{0}' copied.").format(page.get_admin_display_title())) # Redirect to explore of parent page if next_url: return redirect(next_url) return redirect("wagtailadmin_explore", parent_page.id) return render(request, "wagtailadmin/pages/copy.html", {"page": page, "form": form, "next": next_url})
def copy(request, page_id): page = Page.objects.get(id=page_id) # Parent page defaults to parent of source page parent_page = page.get_parent() # Check if the user has permission to publish subpages on the parent can_publish = parent_page.permissions_for_user(request.user).can_publish_subpage() # Create the form form = CopyForm(request.POST or None, page=page, can_publish=can_publish) # Check if user is submitting if request.method == 'POST': # Prefill parent_page in case the form is invalid (as prepopulated value for the form field, # because ModelChoiceField seems to not fall back to the user given value) parent_page = Page.objects.get(id=request.POST['new_parent_page']) if form.is_valid(): # Receive the parent page (this should never be empty) if form.cleaned_data['new_parent_page']: parent_page = form.cleaned_data['new_parent_page'] # Make sure this user has permission to add subpages on the parent if not parent_page.permissions_for_user(request.user).can_add_subpage(): raise PermissionDenied # Re-check if the user has permission to publish subpages on the new parent can_publish = parent_page.permissions_for_user(request.user).can_publish_subpage() # Copy the page new_page = page.copy( recursive=form.cleaned_data.get('copy_subpages'), to=parent_page, update_attrs={ 'title': form.cleaned_data['new_title'], 'slug': form.cleaned_data['new_slug'], }, keep_live=(can_publish and form.cleaned_data.get('publish_copies')), user=request.user, ) # Give a success message back to the user if form.cleaned_data.get('copy_subpages'): messages.success( request, _("Page '{0}' and {1} subpages copied.").format(page.title, new_page.get_descendants().count()) ) else: messages.success(request, _("Page '{0}' copied.").format(page.title)) # Redirect to explore of parent page return redirect('wagtailadmin_explore', parent_page.id) return render(request, 'wagtailadmin/pages/copy.html', { 'page': page, 'form': form, })
def copy(request, page_id): page = Page.objects.get(id=page_id) parent_page = page.get_parent() # Make sure this user has permission to add subpages on the parent if not parent_page.permissions_for_user(request.user).can_add_subpage(): raise PermissionDenied # Check if the user has permission to publish subpages on the parent can_publish = parent_page.permissions_for_user( request.user).can_publish_subpage() # Create the form form = CopyForm(request.POST or None, page=page, can_publish=can_publish) # Check if user is submitting if request.method == 'POST' and form.is_valid(): # Copy the page new_page = page.copy(recursive=form.cleaned_data.get('copy_subpages'), update_attrs={ 'title': form.cleaned_data['new_title'], 'slug': form.cleaned_data['new_slug'], }) # Check if we should keep copied subpages published publish_copies = can_publish and form.cleaned_data.get( 'publish_copies') # Unpublish copied pages if we need to if not publish_copies: new_page.get_descendants(inclusive=True).unpublish() # Assign user of this request as the owner of all the new pages new_page.get_descendants(inclusive=True).update(owner=request.user) # Give a success message back to the user if form.cleaned_data.get('copy_subpages'): messages.success( request, _("Page '{0}' and {1} subpages copied.").format( page.title, new_page.get_descendants().count())) else: messages.success(request, _("Page '{0}' copied.").format(page.title)) # Redirect to explore of parent page return redirect('wagtailadmin_explore', parent_page.id) return render(request, 'wagtailadmin/pages/copy.html', { 'page': page, 'form': form, })
def copy(request, page_id): page = Page.objects.get(id=page_id) parent_page = page.get_parent() # Make sure this user has permission to add subpages on the parent if not parent_page.permissions_for_user(request.user).can_add_subpage(): raise PermissionDenied # Check if the user has permission to publish subpages on the parent can_publish = parent_page.permissions_for_user(request.user).can_publish_subpage() # Create the form form = CopyForm(request.POST or None, page=page, can_publish=can_publish) # Check if user is submitting if request.method == 'POST' and form.is_valid(): # Copy the page new_page = page.copy( recursive=form.cleaned_data.get('copy_subpages'), update_attrs={ 'title': form.cleaned_data['new_title'], 'slug': form.cleaned_data['new_slug'], } ) # Check if we should keep copied subpages published publish_copies = can_publish and form.cleaned_data.get('publish_copies') # Unpublish copied pages if we need to if not publish_copies: new_page.get_descendants(inclusive=True).unpublish() # Assign user of this request as the owner of all the new pages new_page.get_descendants(inclusive=True).update(owner=request.user) # Give a success message back to the user if form.cleaned_data.get('copy_subpages'): messages.success(request, _("Page '{0}' and {1} subpages copied.").format(page.title, new_page.get_descendants().count())) else: messages.success(request, _("Page '{0}' copied.").format(page.title)) # Redirect to explore of parent page return redirect('wagtailadmin_explore', parent_page.id) return render(request, 'wagtailadmin/pages/copy.html', { 'page': page, 'form': form, })