def test_construct_queryset_hook(self): page = SimplePage(title="Test shown", content="hello") Page.get_first_root_node().add_child(instance=page) page_not_shown = SimplePage(title="Test not shown", content="hello") Page.get_first_root_node().add_child(instance=page_not_shown) def filter_pages(pages, request): return pages.filter(id=page.id) with self.register_hook('construct_page_chooser_queryset', filter_pages): response = self.get() self.assertEqual(len(response.context['pages']), 1) self.assertEqual(response.context['pages'][0].specific, page)
def create_test(title, lead, parent=None): if (parent is None): parent = Page.get_first_root_node() home = HomePage(heading=title, title=title, lead=lead, slug=text.slugify(title)) parent.add_child(instance=home) return home
def browse(request, parent_page_id=None): # Find parent page if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() # Get children of parent page pages = parent_page.get_children() # Filter them by page type # A missing or empty page_type parameter indicates 'all page types' (i.e. descendants of wagtailcore.page) page_type_string = request.GET.get('page_type') or 'wagtailcore.page' if page_type_string != 'wagtailcore.page': try: desired_classes = page_models_from_string(page_type_string) except (ValueError, LookupError): raise Http404 # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) choosable_pages = filter_page_type(pages, desired_classes) descendable_pages = pages.filter(numchild__gt=0) pages = choosable_pages | descendable_pages else: desired_classes = (Page, ) can_choose_root = request.GET.get('can_choose_root', False) # Parent page can be chosen if it is a instance of desired_classes parent_page.can_choose = issubclass(parent_page.specific_class or Page, desired_classes) and (can_choose_root or not parent_page.is_root()) # Pagination # We apply pagination first so we don't need to walk the entire list # in the block below paginator, pages = paginate(request, pages, per_page=25) # Annotate each page with can_choose/can_decend flags for page in pages: if desired_classes == (Page, ): page.can_choose = True else: page.can_choose = issubclass(page.specific_class or Page, desired_classes) page.can_descend = page.get_children_count() # Render return render_modal_workflow( request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', shared_context(request, { 'parent_page': parent_page, 'pages': pages, 'search_form': SearchForm(), 'page_type_string': page_type_string, 'page_type_names': [desired_class.get_verbose_name() for desired_class in desired_classes], 'page_types_restricted': (page_type_string != 'wagtailcore.page') }) )
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in ['title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord']: ordering = '-latest_revision_created_at' # Pagination if ordering != 'ord': ordering_no_minus = ordering if ordering_no_minus.startswith('-'): ordering_no_minus = ordering[1:] pages = pages.order_by(ordering).annotate(null_position=Count(ordering_no_minus)).order_by('-null_position', ordering) p = request.GET.get('p', 1) paginator = Paginator(pages, 50) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pages': pages, })
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering if 'ordering' in request.GET: ordering = request.GET['ordering'] if ordering in [ 'title', '-title', 'content_type', '-content_type', 'live', '-live' ]: pages = pages.order_by(ordering) else: ordering = 'title' return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pages': pages, })
def create_test(title, intro, parent=None): if (parent is None): parent = Page.get_first_root_node() contact = ContactFormPage(title=title, intro=intro, thank_you_head='f', thank_you_text='f', slug=text.slugify(title)) parent.add_child(instance=contact) return contact
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related("content_type") # Get page ordering ordering = request.GET.get("ordering", "title") if ordering not in ["title", "-title", "content_type", "-content_type", "live", "-live", "ord"]: ordering = "title" # Pagination if ordering != "ord": pages = pages.order_by(ordering) p = request.GET.get("p", 1) paginator = Paginator(pages, 50) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) return render( request, "wagtailadmin/pages/index.html", {"parent_page": parent_page, "ordering": ordering, "pages": pages} )
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', 'title') if ordering not in ['title', '-title', 'content_type', '-content_type', 'live', '-live', 'ord']: ordering = 'title' # Pagination if ordering != 'ord': pages = pages.order_by(ordering) p = request.GET.get('p', 1) paginator = Paginator(pages, 50) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pages': pages, })
def create_test(title, intro, parent=None): if (parent is None): parent = Page.get_first_root_node() project_idx = ProjectIndexPage(title=title, intro=intro, slug=text.slugify(title)) parent.add_child(instance=project_idx) return project_idx
def move_choose_destination(request, page_to_move_id, viewed_page_id=None): page_to_move = get_object_or_404(Page, id=page_to_move_id) page_perms = page_to_move.permissions_for_user(request.user) if not page_perms.can_move(): raise PermissionDenied if viewed_page_id: viewed_page = get_object_or_404(Page, id=viewed_page_id) else: viewed_page = Page.get_first_root_node() viewed_page.can_choose = page_perms.can_move_to(viewed_page) child_pages = [] for target in viewed_page.get_children(): # can't move the page into itself or its descendants target.can_choose = page_perms.can_move_to(target) target.can_descend = not (target == page_to_move or target.is_child_of( page_to_move)) and target.get_children_count() child_pages.append(target) return render( request, 'wagtailadmin/pages/move_choose_destination.html', { 'page_to_move': page_to_move, 'viewed_page': viewed_page, 'child_pages': child_pages, })
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in ['title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord']: ordering = '-latest_revision_created_at' # Pagination if ordering != 'ord': ordering_no_minus = ordering if ordering_no_minus.startswith('-'): ordering_no_minus = ordering[1:] pages = pages.order_by(ordering).annotate(null_position=Count(ordering_no_minus)).order_by('-null_position', ordering) p = request.GET.get('p', 1) paginator = Paginator(pages, 50) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'pages': pages, })
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in ['title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord']: ordering = '-latest_revision_created_at' # Pagination # Don't paginate if sorting by page order - all pages must be shown to # allow drag-and-drop reordering do_paginate = ordering != 'ord' if do_paginate: ordering_no_minus = ordering.lstrip('-') pages = pages.order_by(ordering).annotate(null_position=Count(ordering_no_minus)).order_by('-null_position', ordering) paginator, pages = paginate(request, pages, per_page=50) return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'pages': pages, 'do_paginate': do_paginate, })
def move_choose_destination(request, page_to_move_id, viewed_page_id=None): page_to_move = get_object_or_404(Page, id=page_to_move_id) page_perms = page_to_move.permissions_for_user(request.user) if not page_perms.can_move(): raise PermissionDenied if viewed_page_id: viewed_page = get_object_or_404(Page, id=viewed_page_id) else: viewed_page = Page.get_first_root_node() viewed_page.can_choose = page_perms.can_move_to(viewed_page) child_pages = [] for target in viewed_page.get_children(): # can't move the page into itself or its descendants target.can_choose = page_perms.can_move_to(target) target.can_descend = not(target == page_to_move or target.is_child_of(page_to_move)) and target.get_children_count() child_pages.append(target) return render(request, 'wagtailadmin/pages/move_choose_destination.html', { 'page_to_move': page_to_move, 'viewed_page': viewed_page, 'child_pages': child_pages, })
def browse(request, parent_page_id=None): page_type = request.GET.get('page_type') or 'wagtailcore.page' content_type_app_name, content_type_model_name = page_type.split('.') is_searching = False try: content_type = ContentType.objects.get_by_natural_key(content_type_app_name, content_type_model_name) except ContentType.DoesNotExist: raise Http404 desired_class = content_type.model_class() if 'q' in request.GET: search_form = SearchForm(request.GET) if search_form.is_valid() and search_form.cleaned_data['q']: pages = desired_class.objects.exclude( depth=1 # never include root ).filter(title__icontains=search_form.cleaned_data['q'])[:10] is_searching = True if not is_searching: if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() parent_page.can_choose = issubclass(parent_page.specific_class, desired_class) search_form = SearchForm() pages = parent_page.get_children() # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) shown_pages = [] for page in pages: page.can_choose = issubclass(page.specific_class, desired_class) page.can_descend = page.get_children_count() if page.can_choose or page.can_descend: shown_pages.append(page) if is_searching: return render(request, 'wagtailadmin/chooser/_search_results.html', { 'querystring': get_querystring(request), 'searchform': search_form, 'pages': pages, 'is_searching': is_searching }) return render_modal_workflow(request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', { 'allow_external_link': request.GET.get('allow_external_link'), 'allow_email_link': request.GET.get('allow_email_link'), 'querystring': get_querystring(request), 'parent_page': parent_page, 'pages': shown_pages, 'search_form': search_form, 'is_searching': False })
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in [ 'title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord' ]: ordering = '-latest_revision_created_at' if ordering == 'ord': # preserve the native ordering from get_children() pass elif ordering == 'latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the top of the list. # Do this by annotating with Count('latest_revision_created_at'), # which returns 0 for these pages = pages.annotate( null_position=Count('latest_revision_created_at')).order_by( 'null_position', 'latest_revision_created_at') elif ordering == '-latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the end of the list. pages = pages.annotate( null_position=Count('latest_revision_created_at')).order_by( '-null_position', '-latest_revision_created_at') else: pages = pages.order_by(ordering) # Don't paginate if sorting by page order - all pages must be shown to # allow drag-and-drop reordering do_paginate = ordering != 'ord' # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_explorer_page_queryset'): pages = hook(parent_page, pages, request) # Pagination if do_paginate: paginator, pages = paginate(request, pages, per_page=50) return render( request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'pages': pages, 'do_paginate': do_paginate, })
def handle(self, *args, **options): if options['content_path']: content_path = options['content_path'] elif settings.BOOTSTRAP_CONTENT_DIR: content_path = settings.BOOTSTRAP_CONTENT_DIR else: raise CommandError("Pass --content <content dir>, where <content dir>/pages contain .yml files") if options['owner']: owner_user = User.objects.get(username=options['owner']) else: owner_user = None #raise CommandError("Pass --owner <username>, where <username> will be the content owner") dry_run = options['dry'] contents = load_content(os.path.join(content_path, 'pages')) for site in Site.objects.all(): site.delete() for page in Page.objects.filter(id__gt=1): page.delete() root = Page.get_first_root_node() content_root = RootNode('/', page_properties={}, parent_page=root) for page_attrs in contents: new_node = SiteNode(full_path=page_attrs['path'], page_properties=page_attrs) content_root.add_node(new_node) page_property_defaults = get_page_defaults(content_path) relation_mappings = get_relation_mappings(content_path) content_root.instantiate_page(owner_user=owner_user, page_property_defaults=page_property_defaults, relation_mappings=relation_mappings, dry_run=dry_run) sites = [] for site in get_sites(content_path): sites.append(Site.objects.create(hostname=site['hostname'], port=int(site['port']), root_page=page_for_path(site['root_page']))) default_site = sites[0] default_site.is_default_site = True default_site.save() if dry_run: self.stdout.write("Dry run, exiting without making changes") return content_root.instantiate_deferred_models(owner_user=owner_user, page_property_defaults=page_property_defaults, relation_mappings=relation_mappings, dry_run=dry_run)
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in [ 'title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord' ]: ordering = '-latest_revision_created_at' if ordering == 'ord': # preserve the native ordering from get_children() pass elif ordering == 'latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the top of the list. # Do this by annotating with Count('latest_revision_created_at'), # which returns 0 for these pages = pages.annotate( null_position=Count('latest_revision_created_at') ).order_by('null_position', 'latest_revision_created_at') elif ordering == '-latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the end of the list. pages = pages.annotate( null_position=Count('latest_revision_created_at') ).order_by('-null_position', '-latest_revision_created_at') else: pages = pages.order_by(ordering) # Pagination # Don't paginate if sorting by page order - all pages must be shown to # allow drag-and-drop reordering do_paginate = ordering != 'ord' if do_paginate: paginator, pages = paginate(request, pages, per_page=50) return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'pages': pages, 'do_paginate': do_paginate, })
def setUp(self): self.site_2_page = SimplePage( title="Site 2 page", slug="site_2_page", content="Hello", ) Page.get_first_root_node().add_child(instance=self.site_2_page) self.site_2_subpage = SimplePage( title="Site 2 subpage", slug="site_2_subpage", content="Hello again", ) self.site_2_page.add_child(instance=self.site_2_subpage) self.site_2 = Site.objects.create( hostname='example.com', port=8080, root_page=Page.objects.get(pk=self.site_2_page.pk), is_default_site=False) self.about_us_page = SimplePage.objects.get(url_path='/home/about-us/')
def setUp(self): self.site_2_page = SimplePage( title="Site 2 page", slug="site_2_page", content="Hello", ) Page.get_first_root_node().add_child(instance=self.site_2_page) self.site_2_subpage = SimplePage( title="Site 2 subpage", slug="site_2_subpage", content="Hello again", ) self.site_2_page.add_child(instance=self.site_2_subpage) self.site_2 = Site.objects.create( hostname='example.com', port=8080, root_page=Page.objects.get(pk=self.site_2_page.pk), is_default_site=False ) self.about_us_page = SimplePage.objects.get(url_path='/home/about-us/')
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related("content_type") # Get page ordering if "ordering" in request.GET: ordering = request.GET["ordering"] if ordering in ["title", "-title", "content_type", "-content_type", "live", "-live"]: pages = pages.order_by(ordering) else: ordering = "title" return render( request, "wagtailadmin/pages/index.html", {"parent_page": parent_page, "ordering": ordering, "pages": pages} )
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in [ 'title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord' ]: ordering = '-latest_revision_created_at' # Pagination # Don't paginate if sorting by page order - all pages must be shown to # allow drag-and-drop reordering do_paginate = ordering != 'ord' if do_paginate: ordering_no_minus = ordering.lstrip('-') pages = pages.order_by(ordering).annotate( null_position=Count(ordering_no_minus)).order_by('-null_position', ordering) paginator, pages = paginate(request, pages, per_page=50) return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'pages': pages, 'do_paginate': do_paginate, })
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering if 'ordering' in request.GET: ordering = request.GET['ordering'] if ordering in ['title', '-title', 'content_type', '-content_type', 'live', '-live']: pages = pages.order_by(ordering) else: ordering = 'title' return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pages': pages, })
def test_all_pages_include_self_strict(self): self.assertEqual( Page.get_first_root_node(), Page.objects.first_common_ancestor(include_self=True, strict=True))
def browse(request, parent_page_id=None): page_type = request.GET.get('page_type') or 'wagtailcore.page' content_type_app_name, content_type_model_name = page_type.split('.') is_searching = False try: content_type = ContentType.objects.get_by_natural_key( content_type_app_name, content_type_model_name) except ContentType.DoesNotExist: raise Http404 desired_class = content_type.model_class() if 'q' in request.GET: search_form = SearchForm(request.GET) if search_form.is_valid() and search_form.cleaned_data['q']: pages = desired_class.objects.exclude( depth=1 # never include root ).filter(title__icontains=search_form.cleaned_data['q'])[:10] is_searching = True if not is_searching: if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() parent_page.can_choose = issubclass(parent_page.specific_class, desired_class) search_form = SearchForm() pages = parent_page.get_children() # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) shown_pages = [] for page in pages: page.can_choose = issubclass(page.specific_class, desired_class) page.can_descend = page.get_children_count() if page.can_choose or page.can_descend: shown_pages.append(page) if is_searching: return render( request, 'wagtailadmin/chooser/_search_results.html', { 'querystring': get_querystring(request), 'searchform': search_form, 'pages': pages, 'is_searching': is_searching }) return render_modal_workflow( request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', { 'allow_external_link': request.GET.get('allow_external_link'), 'allow_email_link': request.GET.get('allow_email_link'), 'querystring': get_querystring(request), 'parent_page': parent_page, 'pages': shown_pages, 'search_form': search_form, 'is_searching': False })
def browse(request, parent_page_id=None): ITEMS_PER_PAGE = 25 page_type = request.GET.get('page_type') or 'wagtailcore.page' content_type_app_name, content_type_model_name = page_type.split('.') try: content_type = ContentType.objects.get_by_natural_key(content_type_app_name, content_type_model_name) except ContentType.DoesNotExist: raise Http404 desired_class = content_type.model_class() if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() parent_page.can_choose = issubclass(parent_page.specific_class, desired_class) search_form = SearchForm() pages = parent_page.get_children() if desired_class == Page: # apply pagination first, since we know that the page listing won't # have to be filtered, and that saves us walking the entire list p = request.GET.get('p', 1) paginator = Paginator(pages, ITEMS_PER_PAGE) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) for page in pages: page.can_choose = True page.can_descend = page.get_children_count() else: # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) shown_pages = [] for page in pages: page.can_choose = issubclass(page.specific_class or Page, desired_class) page.can_descend = page.get_children_count() if page.can_choose or page.can_descend: shown_pages.append(page) # Apply pagination p = request.GET.get('p', 1) paginator = Paginator(shown_pages, ITEMS_PER_PAGE) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) return render_modal_workflow(request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', { 'allow_external_link': request.GET.get('allow_external_link'), 'allow_email_link': request.GET.get('allow_email_link'), 'querystring': get_querystring(request), 'parent_page': parent_page, 'pages': pages, 'search_form': search_form, 'page_type_string': page_type, 'page_type_name': desired_class.get_verbose_name(), 'page_types_restricted': (page_type != 'wagtailcore.page') })
def handle(self, *args, **options): if options['content_path']: content_path = options['content_path'] elif settings.BOOTSTRAP_CONTENT_DIR: content_path = settings.BOOTSTRAP_CONTENT_DIR else: raise CommandError( "Pass --content <content dir>, where <content dir>/pages contain .yml files" ) if options['owner']: owner_user = User.objects.get(username=options['owner']) else: owner_user = None #raise CommandError("Pass --owner <username>, where <username> will be the content owner") dry_run = options['dry'] contents = load_content(os.path.join(content_path, 'pages')) for site in Site.objects.all(): site.delete() for page in Page.objects.filter(id__gt=1): page.delete() root = Page.get_first_root_node() content_root = RootNode('/', page_properties={}, parent_page=root) for page_attrs in contents: new_node = SiteNode(full_path=page_attrs['path'], page_properties=page_attrs) content_root.add_node(new_node) page_property_defaults = get_page_defaults(content_path) relation_mappings = get_relation_mappings(content_path) content_root.instantiate_page( owner_user=owner_user, page_property_defaults=page_property_defaults, relation_mappings=relation_mappings, dry_run=dry_run) sites = [] for site in get_sites(content_path): sites.append( Site.objects.create(hostname=site['hostname'], port=int(site['port']), root_page=page_for_path( site['root_page']))) default_site = sites[0] default_site.is_default_site = True default_site.save() if dry_run: self.stdout.write("Dry run, exiting without making changes") return content_root.instantiate_deferred_models( owner_user=owner_user, page_property_defaults=page_property_defaults, relation_mappings=relation_mappings, dry_run=dry_run)
def get_root_page(self, request): return Page.get_first_root_node()
def browse(request, parent_page_id=None): # A missing or empty page_type parameter indicates 'all page types' # (i.e. descendants of wagtailcore.page) page_type_string = request.GET.get('page_type') or 'wagtailcore.page' user_perm = request.GET.get('user_perms', False) try: desired_classes = page_models_from_string(page_type_string) except (ValueError, LookupError): raise Http404 # Find parent page if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) elif desired_classes == (Page,): # Just use the root page parent_page = Page.get_first_root_node() else: # Find the highest common ancestor for the specific classes passed in # In many cases, such as selecting an EventPage under an EventIndex, # this will help the administrator find their page quicker. all_desired_pages = filter_page_type(Page.objects.all(), desired_classes) parent_page = all_desired_pages.first_common_ancestor() # Get children of parent page pages = parent_page.get_children().specific() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_page_chooser_queryset'): pages = hook(pages, request) # Filter them by page type if desired_classes != (Page,): # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) choosable_pages = filter_page_type(pages, desired_classes) descendable_pages = pages.filter(numchild__gt=0) pages = choosable_pages | descendable_pages can_choose_root = request.GET.get('can_choose_root', False) # Do permission lookups for this user now, instead of for every page. permission_proxy = UserPagePermissionsProxy(request.user) # Parent page can be chosen if it is a instance of desired_classes parent_page.can_choose = can_choose_page( parent_page, permission_proxy, desired_classes, can_choose_root, user_perm) # Pagination # We apply pagination first so we don't need to walk the entire list # in the block below paginator, pages = paginate(request, pages, per_page=25) # Annotate each page with can_choose/can_decend flags for page in pages: page.can_choose = can_choose_page(page, permission_proxy, desired_classes, can_choose_root, user_perm) page.can_descend = page.get_children_count() # Render return render_modal_workflow( request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', shared_context(request, { 'parent_page': parent_page, 'parent_page_id': parent_page.pk, 'pages': pages, 'search_form': SearchForm(), 'page_type_string': page_type_string, 'page_type_names': [desired_class.get_verbose_name() for desired_class in desired_classes], 'page_types_restricted': (page_type_string != 'wagtailcore.page') }) )
def test_empty_queryset(self): self.assertEqual( Page.get_first_root_node(), Page.objects.none().first_common_ancestor())
def browse(request, parent_page_id=None): ITEMS_PER_PAGE = 25 page_types = request.GET.get('page_types', 'wagtailcore.page').split(',') desired_classes = [] for page_type in page_types: try: content_type = resolve_model_string(page_type) except LookupError: raise Http404 desired_classes.append(content_type) if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() parent_page.can_choose = issubclass(parent_page.specific_class, tuple(desired_classes)) search_form = SearchForm() pages = parent_page.get_children() if desired_classes == [Page]: # apply pagination first, since we know that the page listing won't # have to be filtered, and that saves us walking the entire list p = request.GET.get('p', 1) paginator = Paginator(pages, ITEMS_PER_PAGE) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) for page in pages: page.can_choose = True page.can_descend = page.get_children_count() else: # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) shown_pages = [] for page in pages: page.can_choose = issubclass(page.specific_class or Page, tuple(desired_classes)) page.can_descend = page.get_children_count() if page.can_choose or page.can_descend: shown_pages.append(page) # Apply pagination p = request.GET.get('p', 1) paginator = Paginator(shown_pages, ITEMS_PER_PAGE) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) return render_modal_workflow( request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', shared_context(request, { 'parent_page': parent_page, 'pages': pages, 'search_form': search_form, 'page_type_string': ','.join(page_types), 'page_type_names': [desired_class.get_verbose_name() for desired_class in desired_classes], 'page_types_restricted': (page_type != 'wagtailcore.page') }) )
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() pages = parent_page.get_children().prefetch_related('content_type') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in [ 'title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord' ]: ordering = '-latest_revision_created_at' if ordering == 'ord': # preserve the native ordering from get_children() pass elif ordering == 'latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the top of the list. # Do this by annotating with Count('latest_revision_created_at'), # which returns 0 for these pages = pages.annotate( null_position=Count('latest_revision_created_at') ).order_by('null_position', 'latest_revision_created_at') elif ordering == '-latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the end of the list. pages = pages.annotate( null_position=Count('latest_revision_created_at') ).order_by('-null_position', '-latest_revision_created_at') else: pages = pages.order_by(ordering) # Don't paginate if sorting by page order - all pages must be shown to # allow drag-and-drop reordering do_paginate = ordering != 'ord' if do_paginate: # Retrieve pages in their most specific form. # Only do this for paginated listings, as this could potentially be a # very expensive operation when performed on a large queryset. pages = pages.specific() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_explorer_page_queryset'): pages = hook(parent_page, pages, request) # Pagination if do_paginate: paginator, pages = paginate(request, pages, per_page=50) return render(request, 'wagtailadmin/pages/index.html', { 'parent_page': parent_page, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'pages': pages, 'do_paginate': do_paginate, })
def test_empty_queryset(self): self.assertEqual(Page.get_first_root_node(), Page.objects.none().first_common_ancestor())
def test_all_pages(self): self.assertEqual( Page.get_first_root_node(), Page.objects.first_common_ancestor())
def browse(request, parent_page_id=None): # A missing or empty page_type parameter indicates 'all page types' # (i.e. descendants of wagtailcore.page) page_type_string = request.GET.get('page_type') or 'wagtailcore.page' try: desired_classes = page_models_from_string(page_type_string) except (ValueError, LookupError): raise Http404 # Find parent page if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) elif desired_classes == (Page,): # Just use the root page parent_page = Page.get_first_root_node() else: # Find the highest common ancestor for the specific classes passed in # In many cases, such as selecting an EventPage under an EventIndex, # this will help the administrator find their page quicker. all_desired_pages = filter_page_type(Page.objects.all(), desired_classes) parent_page = all_desired_pages.first_common_ancestor() # Get children of parent page pages = parent_page.get_children().specific() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_page_chooser_queryset'): pages = hook(pages, request) # Filter them by page type if desired_classes != (Page,): # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) choosable_pages = filter_page_type(pages, desired_classes) descendable_pages = pages.filter(numchild__gt=0) pages = choosable_pages | descendable_pages can_choose_root = request.GET.get('can_choose_root', False) # Parent page can be chosen if it is a instance of desired_classes parent_page.can_choose = ( issubclass(parent_page.specific_class or Page, desired_classes) and (can_choose_root or not parent_page.is_root()) ) # Pagination # We apply pagination first so we don't need to walk the entire list # in the block below paginator, pages = paginate(request, pages, per_page=25) # Annotate each page with can_choose/can_decend flags for page in pages: if desired_classes == (Page, ): page.can_choose = True else: page.can_choose = issubclass(page.specific_class or Page, desired_classes) page.can_descend = page.get_children_count() # Render return render_modal_workflow( request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', shared_context(request, { 'parent_page': parent_page, 'parent_page_id': parent_page.pk, 'pages': pages, 'search_form': SearchForm(), 'page_type_string': page_type_string, 'page_type_names': [desired_class.get_verbose_name() for desired_class in desired_classes], 'page_types_restricted': (page_type_string != 'wagtailcore.page') }) )
def browse(request, parent_page_id=None): ITEMS_PER_PAGE = 25 page_type = request.GET.get('page_type') or 'wagtailcore.page' content_type_app_name, content_type_model_name = page_type.split('.') try: content_type = ContentType.objects.get_by_natural_key( content_type_app_name, content_type_model_name) except ContentType.DoesNotExist: raise Http404 desired_class = content_type.model_class() if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) else: parent_page = Page.get_first_root_node() parent_page.can_choose = issubclass(parent_page.specific_class, desired_class) search_form = SearchForm() pages = parent_page.get_children() if desired_class == Page: # apply pagination first, since we know that the page listing won't # have to be filtered, and that saves us walking the entire list p = request.GET.get('p', 1) paginator = Paginator(pages, ITEMS_PER_PAGE) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) for page in pages: page.can_choose = True page.can_descend = page.get_children_count() else: # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) shown_pages = [] for page in pages: page.can_choose = issubclass(page.specific_class or Page, desired_class) page.can_descend = page.get_children_count() if page.can_choose or page.can_descend: shown_pages.append(page) # Apply pagination p = request.GET.get('p', 1) paginator = Paginator(shown_pages, ITEMS_PER_PAGE) try: pages = paginator.page(p) except PageNotAnInteger: pages = paginator.page(1) except EmptyPage: pages = paginator.page(paginator.num_pages) return render_modal_workflow( request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', shared_context( request, { 'parent_page': parent_page, 'pages': pages, 'search_form': search_form, 'page_type_string': page_type, 'page_type_name': desired_class.get_verbose_name(), 'page_types_restricted': (page_type != 'wagtailcore.page') }))
def index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id).specific else: parent_page = Page.get_first_root_node().specific pages = parent_page.get_children().prefetch_related("content_type") # Get page ordering ordering = request.GET.get("ordering", "-latest_revision_created_at") if ordering not in [ "title", "-title", "content_type", "-content_type", "live", "-live", "latest_revision_created_at", "-latest_revision_created_at", "ord", ]: ordering = "-latest_revision_created_at" if ordering == "ord": # preserve the native ordering from get_children() pass elif ordering == "latest_revision_created_at": # order by oldest revision first. # Special case NULL entries - these should go at the top of the list. # Do this by annotating with Count('latest_revision_created_at'), # which returns 0 for these pages = pages.annotate(null_position=Count("latest_revision_created_at")).order_by( "null_position", "latest_revision_created_at" ) elif ordering == "-latest_revision_created_at": # order by oldest revision first. # Special case NULL entries - these should go at the end of the list. pages = pages.annotate(null_position=Count("latest_revision_created_at")).order_by( "-null_position", "-latest_revision_created_at" ) else: pages = pages.order_by(ordering) # Don't paginate if sorting by page order - all pages must be shown to # allow drag-and-drop reordering do_paginate = ordering != "ord" if do_paginate: # Retrieve pages in their most specific form. # Only do this for paginated listings, as this could potentially be a # very expensive operation when performed on a large queryset. pages = pages.specific() # allow hooks to modify the queryset for hook in hooks.get_hooks("construct_explorer_page_queryset"): pages = hook(parent_page, pages, request) # Pagination if do_paginate: paginator, pages = paginate(request, pages, per_page=50) return render( request, "wagtailadmin/pages/index.html", { "parent_page": parent_page.specific, "ordering": ordering, "pagination_query_params": "ordering=%s" % ordering, "pages": pages, "do_paginate": do_paginate, }, )
def test_all_pages(self): self.assertEqual(Page.get_first_root_node(), Page.objects.first_common_ancestor())