Beispiel #1
0
def get_navigation_object_from_page(page: Page, current_page_id: int) -> dict:
    page_object = {
        "text": str(page.title),
        "nodes": [],
        "href": page.get_url(),
        "state": {}
    }
    if isinstance(page.specific, PageWithSidebar) or isinstance(page.specific, LessonPage) or isinstance(page.specific,
                                                                                                         ArticlePage):
        menu_title = page.specific.menu_title
        if not isinstance(menu_title, str):
            menu_title = menu_title.decode()
        if menu_title != '':
            page_object["text"] = menu_title
        if not page.specific.is_selectable:
            page_object["selectable"] = False
    if page.id == current_page_id:
        page_object["state"] = {
            "selected": True
        }
        page_object["selectable"] = False
    for child in page.get_children():
        if child.show_in_menus:
            page_object["nodes"].append(get_navigation_object_from_page(child, current_page_id))
    if len(page_object["nodes"]) == 0:
        page_object.pop('nodes', None)
    return page_object
Beispiel #2
0
    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)
Beispiel #3
0
    def clean(self):

        cleaned_data = super().clean()
        if 'slug' in self.cleaned_data:
            if not Page._slug_is_available(
                cleaned_data['slug'], self.parent_page, self.instance
            ):
                self.add_error('slug', forms.ValidationError(_("This slug is already in use")))

        # Check scheduled publishing fields
        go_live_at = cleaned_data.get('go_live_at')
        expire_at = cleaned_data.get('expire_at')

        # Go live must be before expire
        if go_live_at and expire_at:
            if go_live_at > expire_at:
                msg = _('Go live date/time must be before expiry date/time')
                self.add_error('go_live_at', forms.ValidationError(msg))
                self.add_error('expire_at', forms.ValidationError(msg))

        # Expire at must be in the future
        if expire_at and expire_at < timezone.now():
            self.add_error('expire_at', forms.ValidationError(_('Expiry date/time must be in the future')))

        # Don't allow an existing first_published_at to be unset by clearing the field
        if 'first_published_at' in cleaned_data and not cleaned_data['first_published_at']:
            del cleaned_data['first_published_at']

        return cleaned_data
Beispiel #4
0
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)

    # Pagination
    paginator, child_pages = paginate(request, child_pages, per_page=50)

    return render(request, 'wagtailadmin/pages/move_choose_destination.html', {
        'page_to_move': page_to_move,
        'viewed_page': viewed_page,
        'child_pages': child_pages,
    })
def test_rendering(root_page, example_svg_upload, dummy_wagtail_doc):
    page = Page(title="nnep", slug="nnep")
    page.set_url_path(root_page)
    root_page.add_child(instance=page)
    page.save()
    assert page.url

    map = ImageMap.objects.create(svg=example_svg_upload)
    map.regions.create(element_id='green', link_external='/foobar', target='_blank')
    map.regions.create(element_id='blue', link_page=page, target='_top')
    map.regions.create(element_id='red', link_document=dummy_wagtail_doc)

    svg = map.rendered_svg
    assert '/foobar' in svg
    assert '_blank' in svg
    assert 'nnep' in svg
    assert '_top' in svg
    assert ('documents/%s' % dummy_wagtail_doc.pk) in svg
Beispiel #6
0
    def test_page_copy_alias_post_copy_subpages(self):
        post_data = {
            'new_title': "Hello world 2",
            'new_slug': 'hello-world-2',
            'new_parent_page': str(self.root_page.id),
            'copy_subpages': True,
            'publish_copies': False,
            'alias': True,
        }
        response = self.client.post(
            reverse('wagtailadmin_pages:copy', args=(self.test_page.id, )),
            post_data)

        # Check that the user was redirected to the parents explore page
        self.assertRedirects(
            response,
            reverse('wagtailadmin_explore', args=(self.root_page.id, )))

        # Get copy
        page_copy = self.root_page.get_children().get(slug='hello-world-2')

        # Check the copy is an alias of the original
        self.assertEqual(page_copy.alias_of, self.test_page.page_ptr)

        # Check that the copy is live
        # Note: publish_copies is ignored. Alias pages always keep the same state as their original
        self.assertTrue(page_copy.live)
        self.assertFalse(page_copy.has_unpublished_changes)

        # Check that the owner of the page is set correctly
        self.assertEqual(page_copy.owner, self.user)

        # Check that the children were copied
        self.assertEqual(page_copy.get_children().count(), 2)

        # Check the the child pages
        # Neither of them should be live
        child_copy = page_copy.get_children().filter(slug='child-page').first()
        self.assertNotEqual(child_copy, None)
        self.assertEqual(child_copy.alias_of, self.test_child_page.page_ptr)
        self.assertTrue(child_copy.live)
        self.assertFalse(child_copy.has_unpublished_changes)

        unpublished_child_copy = page_copy.get_children().filter(
            slug='unpublished-child-page').first()
        self.assertNotEqual(unpublished_child_copy, None)
        self.assertEqual(unpublished_child_copy.alias_of,
                         self.test_unpublished_child_page.page_ptr)
        self.assertFalse(unpublished_child_copy.live)
        self.assertTrue(unpublished_child_copy.has_unpublished_changes)

        # treebeard should report no consistency problems with the tree
        self.assertFalse(any(Page.find_problems()),
                         'treebeard found consistency problems')
Beispiel #7
0
    def test_can_delete_default_locale(self):
        # The presence of the locale on the root page node (if that's the only thing using the
        # locale) should not prevent deleting it

        for lang in ("fr", "de", "pl", "ja"):
            Locale.objects.create(language_code=lang)

        self.assertTrue(Page.get_first_root_node().locale.language_code, "en")
        Page.objects.filter(depth__gt=1).delete()
        response = self.post()

        # Should redirect back to index
        self.assertRedirects(response, reverse("wagtaillocales:index"))

        # Check that the locale was deleted
        self.assertFalse(Locale.objects.filter(language_code="en").exists())

        # root node's locale should now have been reassigned to the one matching the current
        # LANGUAGE_CODE
        self.assertTrue(Page.get_first_root_node().locale.language_code, "de")
Beispiel #8
0
    def _create(cls, model_class, *args, **kwargs):

        try:
            parent = kwargs.pop('parent')
        except KeyError:
            # no parent, appending page to root
            parent = Page.get_first_root_node()

        page = model_class(*args, **kwargs)
        parent.add_child(instance=page)

        return page
Beispiel #9
0
    def test_empty_bin_removes_pages_older_than_setting(self):
        root_page = Page.objects.get(url_path="/")

        new_page = Page(title="new page")
        root_page.add_child(instance=new_page)
        RecycleBin.objects.create(page=new_page)

        old_page = Page(title="new page oldie")
        root_page.add_child(instance=old_page)
        RecycleBin.objects.create(page=old_page)
        RecycleBin.objects.filter(page__title="new page oldie").update(
            time_recycled=datetime.now() - timedelta(days=31)
        )

        self.assertEqual(RecycleBin.objects.count(), 2)

        call_command("empty_recycle_bin", older_than_days=30)

        self.assertFalse(RecycleBin.objects.filter(page_id=old_page.id).exists())
        self.assertTrue(RecycleBin.objects.filter(page_id=new_page.id).exists())
        self.assertEqual(RecycleBin.objects.count(), 1)
Beispiel #10
0
 def entries_search(self, request, *args, **kwargs):
     """ Generates a page containing all the entries associated with a specific search. """
     self.search_query = request.GET.get('q', None)
     response = None
     if self.search_query:
         self.filter_type = 'search'
         self.filter_value = self.search_query
         self.entries = self.get_entries().search(self.search_query,
                                                  operator='or')
         Query.get(self.search_query).add_hit()
         response = Page.serve(self, request, *args, **kwargs)
     return response or redirect(self.url)
    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/')
Beispiel #12
0
 def post_list(self, request, *args, **kwargs):
     '''
     pagination for posts
     '''
     print('route post list is called!!')
     if self.language.code == 'en':
         self.help_text = 'Recent Posts:'
     else:
         self.help_text = '近期文章:'
     all_post = self.get_recent_posts(999)
     self.posts_this_page = all_post
     return Page.serve(self, request, *args, **kwargs)
Beispiel #13
0
 def post_by_date(self, request, year, month=None, day=None, *args, **kwargs):
     self.posts = self.get_posts().filter(date__year=year)
     self.search_type = 'date'
     self.search_term = year
     if month:
         self.posts = self.posts.filter(date__month=month)
         df = DateFormat(date(int(year), int(month), 1))
         self.search_term = df.format('F Y')
     if day:
         self.posts = self.posts.filter(date__day=day)
         self.search_term = date_format(date(int(year), int(month), int(day)))
     return Page.serve(self, request, *args, **kwargs)
Beispiel #14
0
    def post_search(self, request, *args, **kwargs):
        search_query = request.GET.get('q', None)
        self.posts = self.get_posts()
        if search_query:
            self.posts = self.posts.filter(
                Q(body__icontains=search_query)
                | Q(title__icontains=search_query)
                | Q(excerpt__icontains=search_query))

        self.search_term = search_query
        self.search_type = 'search'
        return Page.serve(self, request, *args, **kwargs)
Beispiel #15
0
    def setUp(self):
        self.login()
        site = Site.objects.get()
        page = Page.get_first_root_node()
        home = HomePage(title='New America')
        home_page = page.add_child(instance=home)

        site.root_page = home
        site.save()

        # People objects to test template tags
        # that create byline and determine author order
        program_page_1 = home_page.add_child(
            instance=Program(title='OTI',
                             name='OTI',
                             slug='oti',
                             description='OTI',
                             location=False,
                             depth=3))
        program_page_1.save()

        our_people_page = home_page.add_child(instance=OurPeoplePage(
            title='Our People',
            depth=3,
        ))
        our_people_page.save()

        self.first_person = Person(
            title='First Person',
            slug='first-person',
            first_name='first',
            last_name='person',
            role='Central Staff',
            depth=4,
        )
        our_people_page.add_child(instance=self.first_person)

        # Using policy papers to test the other post types
        all_policy_papers_home_page = home_page.add_child(
            instance=AllPolicyPapersHomePage(title="Policy Papers"))

        program_policy_papers_page = program_page_1.add_child(
            instance=ProgramPolicyPapersPage(title='OTI Policy Papers',
                                             slug='oti-policy-papers'))
        self.policy_paper = PolicyPaper(title='Policy Paper 1',
                                        slug='policy-paper-1',
                                        date='2016-06-15',
                                        depth=5)
        program_policy_papers_page.add_child(instance=self.policy_paper)
        self.policy_paper.save()
        PostAuthorRelationship(author=self.first_person,
                               post=self.policy_paper).save()
        all_policy_papers_home_page.save()
Beispiel #16
0
    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 test_auto_recache(root_page, example_svg_upload):
    page = Page(title="nnep", slug="nnep")
    page.set_url_path(root_page)
    root_page.add_child(instance=page)
    page.save()
    assert page.url

    map = ImageMap.objects.create(svg=example_svg_upload)
    map.regions.create(element_id='blue', link_page=page)
    map.recache_svg(save=True)
    assert 'nnep' in map.rendered_svg
    page.slug = 'ffflop'
    page.save()  # The `post_save` triggers will get called...
    assert 'ffflop' in ImageMap.objects.get(pk=map.pk).rendered_svg
def import_pages(import_data, parent_page):
    """
    Overwrite of the wagtailimportexport `import_page` function to handle generic csvs.
    The standard `import_pages` assumes that your pages will have a pk from the exported
    json files.  It does not facilitate the idea that the pages you import will be
    new pages.
    """

    pages_by_original_id = {}

    # First create the base Page records; these contain no foreign keys, so this allows us to
    # build a complete mapping from old IDs to new IDs before we go on to importing the
    # specific page models, which may require us to rewrite page IDs within foreign keys / rich
    # text / streamfields.
    page_content_type = ContentType.objects.get_for_model(Page)

    for page_record in import_data['pages']:
        # build a base Page instance from the exported content (so that we pick up its title and other
        # core attributes)
        page = Page.from_serializable_data(page_record['content'])

        # clear id and treebeard-related fields so that they get reassigned when we save via add_child
        page.id = None
        page.path = None
        page.depth = None
        page.numchild = 0
        page.url_path = None
        page.content_type = page_content_type
        parent_page.add_child(instance=page)

        # Custom Code to add the new pk back into the original page record.
        page_record['content']['pk'] = page.pk

        pages_by_original_id[page.id] = page

    for page_record in import_data['pages']:
        # Get the page model of the source page by app_label and model name
        # The content type ID of the source page is not in general the same
        # between the source and destination sites but the page model needs
        # to exist on both.
        # Raises LookupError exception if there is no matching model
        model = apps.get_model(page_record['app_label'], page_record['model'])

        specific_page = model.from_serializable_data(page_record['content'], check_fks=False, strict_fks=False)
        base_page = pages_by_original_id[specific_page.id]
        specific_page.page_ptr = base_page
        specific_page.__dict__.update(base_page.__dict__)
        specific_page.content_type = ContentType.objects.get_for_model(model)
        update_page_references(specific_page, pages_by_original_id)
        specific_page.save()

    return len(import_data['pages'])
Beispiel #19
0
 def post_by_date_slug(self, request, year, month, day, slug, *args,
                       **kwargs):
     post_page = self.get_posts().filter(slug=slug).first()
     if not post_page:
         raise Http404
     if request.method == 'POST':
         form = PostCommentForm(request.POST)
         if form.is_valid():
             PostComment.objects.create(
                 user=request.user,
                 comment=form.cleaned_data['comment'],
                 post=post_page)
     return Page.serve(post_page, request, *args, **kwargs)
Beispiel #20
0
 def post_by_category(self, request, category, *args, **kwargs):
     print('route post_by_category is called!!')
     if self.language.code == 'en':
         self.help_text = 'Posts contain these categories:'
     else:
         self.help_text = '包含這些類別的文章:'
     self.present_method = 1
     self.searched_categories = BlogCategory.objects.filter(slug=category)
     self.posts_this_page = [
         post for post in self.get_recent_posts(50)
         if category in [c.slug for c in post.categories.all()]
     ]
     return Page.serve(self, request, *args, **kwargs)
Beispiel #21
0
    def test_create_sets_locale_to_parent_locale(self):
        # We need to make sure the page's locale it set to the parent in the create view so that any customisations
        # for that language will take effect.
        fr_locale = Locale.objects.create(language_code="fr")
        fr_homepage = self.root_page.add_child(instance=Page(
            title="Home",
            slug="home-fr",
            locale=fr_locale,
        ))

        response = self.client.get(reverse('wagtailadmin_pages:add', args=('tests', 'simplepage', fr_homepage.id)))

        self.assertEqual(response.context['page'].locale, fr_locale)
    def setUpClass(cls):
        super().setUpClass()

        cls.test_page = SimplePage(title="test", slug='test', content="test")
        cls.wagtail_root = Page.get_first_root_node()
        cls.wagtail_root.add_child(instance=cls.test_page)

        cls.test_page_group = Group.objects.create(name="Test page")
        GroupPagePermission.objects.create(
            group=cls.test_page_group,
            page=cls.test_page,
            permission_type='edit'
        )
    def setUp(self):
        self.login()

        page = Page(title="Test Page")

        root = WagtailSite.objects.get().root_page
        root.add_child(instance=page)

        self.comment = XtdComment.objects.create(
            content_type=ContentType.objects.get_for_model(Page),
            object_pk=page.pk,
            site=Site.objects.get_current(),
        )
Beispiel #24
0
 def test_slugurl_tag_returns_url_for_current_site(self):
     home_page = Page.objects.get(url_path='/home/')
     new_home_page = home_page.copy(update_attrs={'title': "New home page", 'slug': 'new-home'})
     second_site = Site.objects.create(hostname='site2.example.com', root_page=new_home_page)
     # Add a page to the new site that has a slug that is the same as one on
     # the first site, but is in a different position in the treeself.
     new_christmas_page = Page(title='Christmas', slug='christmas')
     new_home_page.add_child(instance=new_christmas_page)
     request = HttpRequest()
     request.META['HTTP_HOST'] = second_site.hostname
     request.META['SERVER_PORT'] = second_site.port
     url = slugurl(context=template.Context({'request': request}), slug='christmas')
     self.assertEqual(url, '/christmas/')
Beispiel #25
0
    def test_issue_2599(self):
        homepage = Page.objects.get(id=2)

        child1 = Page(title="child1")
        homepage.add_child(instance=child1)
        child2 = Page(title="child2")
        homepage.add_child(instance=child2)

        child1.delete()

        self.login()
        post_data = {
            "title": "New page!",
            "content": "Some content",
            "slug": "hello-world",
            "action-submit": "Submit",
        }
        preview_url = reverse(
            "wagtailadmin_pages:preview_on_add",
            args=("tests", "simplepage", homepage.id),
        )
        response = self.client.post(preview_url, post_data)

        # Check the JSON response
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(response.content.decode(), {"is_valid": True})

        response = self.client.get(preview_url)

        # Check the HTML response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "tests/simple_page.html")
        self.assertContains(response, "New page!")

        # Check that the treebeard attributes were set correctly on the page object
        self.assertEqual(response.context["self"].depth, homepage.depth + 1)
        self.assertTrue(response.context["self"].path.startswith(
            homepage.path))
        self.assertEqual(response.context["self"].get_parent(), homepage)
Beispiel #26
0
    def test_page_copy_post_copy_subpages_publish_copies(self):
        post_data = {
            "new_title": "Hello world 2",
            "new_slug": "hello-world-2",
            "new_parent_page": str(self.root_page.id),
            "copy_subpages": True,
            "publish_copies": True,
            "alias": False,
        }
        response = self.client.post(
            reverse("wagtailadmin_pages:copy", args=(self.test_page.id, )),
            post_data)

        # Check that the user was redirected to the parents explore page
        self.assertRedirects(
            response,
            reverse("wagtailadmin_explore", args=(self.root_page.id, )))

        # Get copy
        page_copy = self.root_page.get_children().filter(
            slug="hello-world-2").first()

        # Check that the copy exists
        self.assertIsNotNone(page_copy)

        # Check that the copy is live
        self.assertTrue(page_copy.live)
        self.assertFalse(page_copy.has_unpublished_changes)

        # Check that the owner of the page is set correctly
        self.assertEqual(page_copy.owner, self.user)

        # Check that the children were copied
        self.assertEqual(page_copy.get_children().count(), 2)

        # Check the the child pages
        # The child_copy should be live but the unpublished_child_copy shouldn't
        child_copy = page_copy.get_children().filter(slug="child-page").first()
        self.assertIsNotNone(child_copy)
        self.assertTrue(child_copy.live)
        self.assertTrue(child_copy.has_unpublished_changes)

        unpublished_child_copy = (page_copy.get_children().filter(
            slug="unpublished-child-page").first())
        self.assertIsNotNone(unpublished_child_copy)
        self.assertFalse(unpublished_child_copy.live)
        self.assertTrue(unpublished_child_copy.has_unpublished_changes)

        # treebeard should report no consistency problems with the tree
        self.assertFalse(any(Page.find_problems()),
                         "treebeard found consistency problems")
Beispiel #27
0
    def _setup_home(self):
        """Creates the language specific home pages."""
        # LanguageRedirectionPage = apps.get_model("cms.LanguageRedirectionPage")
        # parent_page = LanguageRedirectionPage.objects.first()
        Page.objects.filter(id=2).delete()
        root = Page.get_first_root_node()

        Site = apps.get_model("wagtailcore.Site")
        # ContentType = apps.get_model("contenttypes.ContentType")
        HomePage = apps.get_model("home.HomePage")
        # homepage_content_type = ContentType.objects.get(
        #     model="homepage", app_label="home"
        # )
        # For each supported language, create a new homepage
        # for language_code, label in settings.LANGUAGES:
        #     if language_code == "de":
        #         hero_title = "Wir sind Codista."
        #         hero_intro = "Eine Software-Agentur, die sich auf die partnerschaftliche Entwicklung von hochqualitativen, digitalen Produkten spezialisiert hat."
        #         title = "Home - Deutsch"
        #         services_teaser = "Wir sind spezialisiert auf die Entwicklung von innovativen Software-Lösungen.  Mit langjähriger Erfahrung in Entwicklung und Design von Websites, Online-Plattformen,  Web-Applikationen und SaaS Lösungen unterstützen wir Ihr Unternehmen in der Realisierung neuer Software-Produkte und der Verbesserung Ihrer Unternehmens-Prozesse."
        #         services_title = "Wir denken digital."
        #         team_teaser = "Wir sind ein eingespieltes Team aus Software-Entwicklungs Experten und Design Profis welches komplexe Web-Projekte für unsere Kunden realisiert. Wir stehen für Qualität, Innovation, Umsetzungsgeschwindigkeit, Zuverlässigkeit und lösungsorientiertes Handeln."
        #         team_title = "Wir sind ein eingespieltes Team aus Software-Entwicklungs Experten und Design Profis."
        #         team_teaser = "Wir stehen für Qualität, Innovation, Umsetzungsgeschwindigkeit, Zuverlässigkeit und lösungsorientiertes Handeln."
        # elif language_code == "en":
        title = "Home - English"
        hero_title = "We are Codista."
        hero_intro = "A software agency specializing in the partnership development of high quality digital products."
        services_teaser = "We specialize in the development of innovative software solutions. With many years of experience in the development and design of websites, online platforms, web applications and SaaS solutions, we support your company in the realization of new software products and the improvement of your business processes."
        services_title = "We think digitally."
        team_title = "We are a well-established team of software development experts and design professionals."
        team_teaser = "We stand for quality, innovation, speed of implementation, reliability and solution-oriented action."
        # else:
        #     raise RuntimeError(f"unsupported language encountered: {language_code}")
        homepage = HomePage(
            # language=language_code,
            title="St Louis DSA",
            banner_title="We are the St. Louis Democratic Socialists of America!",
            body="Lorem Ipsum blah blah blah",
            # draft_title=title,
            # slug=language_code,
            # hero_title=hero_title,
            # hero_intro=hero_intro,
            # services_teaser=services_teaser,
            # services_title=services_title,
            # team_title=team_title,
            # team_teaser=team_teaser,
            # show_in_menus=True,
            # content_type=homepage_content_type,
        )
        root.add_child(instance=homepage)
    def test_page_copy_no_publish_permission_post_copy_subpages_publish_copies(self):
        # This tests that unprivileged users cannot publish copied pages even if they hack their browser

        # Turn user into an editor who can add pages but not publish them
        self.user.is_superuser = False
        self.user.groups.add(
            Group.objects.get(name="Editors"),
        )
        self.user.save()

        # Post
        post_data = {
            'new_title': "Hello world 2",
            'new_slug': 'hello-world-2',
            'new_parent_page': str(self.root_page.id),
            'copy_subpages': True,
            'publish_copies': True,
            'alias': False,
        }
        response = self.client.post(reverse('wagtailadmin_pages:copy', args=(self.test_page.id, )), post_data)

        # Check that the user was redirected to the parents explore page
        self.assertRedirects(response, reverse('wagtailadmin_explore', args=(self.root_page.id, )))

        # Get copy
        page_copy = self.root_page.get_children().filter(slug='hello-world-2').first()

        # Check that the copy exists
        self.assertNotEqual(page_copy, None)

        # Check that the copy is not live
        self.assertFalse(page_copy.live)

        # Check that the owner of the page is set correctly
        self.assertEqual(page_copy.owner, self.user)

        # Check that the children were copied
        self.assertEqual(page_copy.get_children().count(), 2)

        # Check the the child pages
        # Neither of them should be live
        child_copy = page_copy.get_children().filter(slug='child-page').first()
        self.assertNotEqual(child_copy, None)
        self.assertFalse(child_copy.live)

        unpublished_child_copy = page_copy.get_children().filter(slug='unpublished-child-page').first()
        self.assertNotEqual(unpublished_child_copy, None)
        self.assertFalse(unpublished_child_copy.live)

        # treebeard should report no consistency problems with the tree
        self.assertFalse(any(Page.find_problems()), 'treebeard found consistency problems')
Beispiel #29
0
    def setUp(self):
        self.root_page = Page.objects.get(id=2)

        # Add child pages
        self.child_pages = [
            Page(title=f"Hello world!-{i}",
                 slug=f"hello-world-{i}",
                 live=False) for i in range(1, 5)
        ]
        self.pages_to_be_published = self.child_pages[:3]
        self.pages_not_to_be_published = self.child_pages[3:]

        for child_page in self.child_pages:
            self.root_page.add_child(instance=child_page)

        # map of the form { page: [child_pages] } to be added
        self.grandchildren_pages = {
            self.pages_to_be_published[0]:
            [Page(title="Hello world!-a", slug="hello-world-a", live=False)],
            self.pages_to_be_published[1]: [
                Page(title="Hello world!-b", slug="hello-world-b", live=False),
                Page(title="Hello world!-c", slug="hello-world-c", live=False)
            ]
        }
        for child_page, grandchild_pages in self.grandchildren_pages.items():
            for grandchild_page in grandchild_pages:
                child_page.add_child(instance=grandchild_page)

        self.url = reverse('wagtail_bulk_action',
                           args=(
                               'wagtailcore',
                               'page',
                               'publish',
                           )) + '?'
        for child_page in self.pages_to_be_published:
            self.url += f'&id={child_page.id}'

        self.user = self.login()
Beispiel #30
0
    def create_cms(self):
        Page.objects.last().delete()
        root = Page.get_first_root_node()
        home = root.add_child(
            instance=HomePage(**{"title": "The Hedera Project", "slug": ""})
        )

        Site.objects.all().delete()
        Site.objects.create(
            hostname='localhost',
            site_name='Hedera',
            root_page=home,
            is_default_site=True
        )
Beispiel #31
0
 def post_by_date_slug(
     self,
     request,
     year,
     month,
     day,
     slug,
     *args,
     **kwargs
 ):
     post_page = self.get_posts().filter(slug=slug).first()
     if not post_page:
         raise Http404
     return Page.serve(post_page, request, *args, **kwargs)
Beispiel #32
0
    def get_queryset(self):
        q = Q(page__in=UserPagePermissionsProxy(
            self.request.user).explorable_pages().values_list('pk', flat=True))

        root_page_permissions = Page.get_first_root_node(
        ).permissions_for_user(self.request.user)
        if (self.request.user.is_superuser
                or root_page_permissions.can_add_subpage()
                or root_page_permissions.can_edit()):
            # Include deleted entries
            q = q | Q(page_id__in=Subquery(
                PageLogEntry.objects.filter(deleted=True).values('page_id')))

        return PageLogEntry.objects.filter(q)
    def test_non_editable_fields_are_silently_ignored(self):
        page = Page(title='Test Page', locked=False)
        home = Page.objects.get(slug='home')
        home.add_child(instance=page)

        csv_data = StringIO('id,locked\r\n' f'{page.pk},True\r\n')
        successes, errors = import_pages(csv_data, Page)
        self.assertEqual(successes, [f'Updated page Test Page with id 3'],
                         f'Errors: {errors}')
        self.assertEqual(errors, [])

        page = Page.objects.get(pk=page.pk)
        # non-editable fields have not been changed
        self.assertIs(page.locked, False)
Beispiel #34
0
    def setUp(self) -> None:
        self.user = get_user_model().objects.create_user(
            'Test User', '*****@*****.**', 'password')
        self.user.groups.add(Group.objects.get(name="Moderators"))
        self.client.force_login(self.user)

        self.default_site = Site.objects.get(id=1)
        self.root_page = Page.objects.get(id=1)
        self.training_root_page = Page(owner=self.user,
                                       slug='Test',
                                       title='Test')
        self.root_page.add_sibling(instance=self.training_root_page)
        PageViewRestriction.objects.create(page=self.training_root_page,
                                           restriction_type='password',
                                           password='******')
        self.training_root_page.save_revision().publish()
        self.training_site = Site(hostname='localhost',
                                  port='8000',
                                  site_name='Training',
                                  root_page=self.training_root_page)
        self.training_site.save()

        self.factory = RequestFactory()
Beispiel #35
0
    def post_list(self, request, *args, **kwargs):
        #page_n = request.GET.get('page')
        #if not page_n:
        #    page_n = "1"
        #post_t = self.get_posts()
        #paginator = Paginator(post_t, 3)
        #posts_page = paginator.get_page(page_n)
        #self.posts = posts_page
        #self.posts = self.get_posts()
        self.showing_all = False
        posts_p = self.get_posts()
        self.posts = self.use_paginator(request, posts_p)

        return Page.serve(self, request, *args, **kwargs)
Beispiel #36
0
    def test_issue_2599(self):
        homepage = Page.objects.get(id=2)

        child1 = Page(title='child1')
        homepage.add_child(instance=child1)
        child2 = Page(title='child2')
        homepage.add_child(instance=child2)

        child1.delete()

        self.login()
        post_data = {
            'title': "New page!",
            'content': "Some content",
            'slug': 'hello-world',
            'action-submit': "Submit",
        }
        preview_url = reverse('wagtailadmin_pages:preview_on_add',
                              args=('tests', 'simplepage', homepage.id))
        response = self.client.post(preview_url, post_data)

        # Check the JSON response
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(response.content.decode(), {'is_valid': True})

        response = self.client.get(preview_url)

        # Check the HTML response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'tests/simple_page.html')
        self.assertContains(response, "New page!")

        # Check that the treebeard attributes were set correctly on the page object
        self.assertEqual(response.context['self'].depth, homepage.depth + 1)
        self.assertTrue(response.context['self'].path.startswith(
            homepage.path))
        self.assertEqual(response.context['self'].get_parent(), homepage)
Beispiel #37
0
    def test_homepage(self):

        root = Page.get_first_root_node()

        home = HomePage(
            slug='homepageslug',
            title='home page title',
            heading='home page heading',
            introduction='home page introduction',
            request_for_comments_section_body='rfc section body',
            working_groups_section_body='wg section body',
        )

        root.add_child(instance=home)

        self.assertEqual(HomePage.objects.count(), 1)

        Site.objects.all().delete()

        Site.objects.create(
            hostname='localhost',
            root_page=home,
            is_default_site=True,
            site_name='testingsitename',
        )

        blogindex = BlogIndexPage(
            slug='blog',
            title='blog index title',
        )
        home.add_child(instance=blogindex)

        blog = BlogPage(slug='blogpost',
                        title='blog title',
                        introduction='blog introduction',
                        body='blog body')
        blogindex.add_child(instance=blog)

        home.button_text = 'blog button text'
        home.button_link = blog
        home.save()

        r = self.client.get(path=home.url)
        self.assertEqual(r.status_code, 200)
        self.assertIn(home.title.encode(), r.content)
        self.assertIn(home.heading.encode(), r.content)
        self.assertIn(home.introduction.encode(), r.content)
        self.assertIn(home.button_text.encode(), r.content)
        self.assertIn(('href="%s"' % blog.url).encode(), r.content)
Beispiel #38
0
def move_confirm(request, page_to_move_id, destination_id):
    page_to_move = get_object_or_404(Page, id=page_to_move_id).specific
    destination = get_object_or_404(Page, id=destination_id)
    if not page_to_move.permissions_for_user(request.user).can_move_to(destination):
        raise PermissionDenied

    if not Page._slug_is_available(page_to_move.slug, destination, page=page_to_move):
        messages.error(
            request,
            _("The slug '{0}' is already in use at the selected parent page. Make sure the slug is unique and try again".format(page_to_move.slug))
        )
        return redirect('wagtailadmin_pages:move_choose_destination', page_to_move.id, destination.id)

    for fn in hooks.get_hooks('before_move_page'):
        result = fn(request, page_to_move, destination)
        if hasattr(result, 'status_code'):
            return result

    if request.method == 'POST':
        # any invalid moves *should* be caught by the permission check above,
        # so don't bother to catch InvalidMoveToDescendant
        page_to_move.move(destination, pos='last-child')

        messages.success(request, _("Page '{0}' moved.").format(page_to_move.get_admin_display_title()), buttons=[
            messages.button(reverse('wagtailadmin_pages:edit', args=(page_to_move.id,)), _('Edit'))
        ])

        for fn in hooks.get_hooks('after_move_page'):
            result = fn(request, page_to_move)
            if hasattr(result, 'status_code'):
                return result

        return redirect('wagtailadmin_explore', destination.id)

    return render(request, 'wagtailadmin/pages/confirm_move.html', {
        'page_to_move': page_to_move,
        'destination': destination,
    })
Beispiel #39
0
    def get_page(self):
        (content_type_app_name, content_type_model_name,
         parent_page_id) = self.args
        try:
            content_type = ContentType.objects.get_by_natural_key(
                content_type_app_name, content_type_model_name)
        except ContentType.DoesNotExist:
            raise Http404

        page = content_type.model_class()()
        parent_page = get_object_or_404(Page, id=parent_page_id).specific
        # We need to populate treebeard's path / depth fields in order to
        # pass validation. We can't make these 100% consistent with the rest
        # of the tree without making actual database changes (such as
        # incrementing the parent's numchild field), but by calling treebeard's
        # internal _get_path method, we can set a 'realistic' value that will
        # hopefully enable tree traversal operations
        # to at least partially work.
        page.depth = parent_page.depth + 1
        # Puts the page at the maximum possible path
        # for a child of `parent_page`.
        page.path = Page._get_children_path_interval(parent_page.path)[1]
        return page
 def normal_page(self, request):
     return Page.serve(self, request)
Beispiel #41
0
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()

    # This will always succeed because of the @user_passes_test above.
    root_page = get_explorable_root_page(request.user)

    # If this page isn't a descendant of the user's explorable root page,
    # then redirect to that explorable root page instead.
    if not (
        parent_page.pk == root_page.pk or
        parent_page.is_descendant_of(root_page)
    ):
        return redirect('wagtailadmin_explore', root_page.pk)

    parent_page = parent_page.specific

    pages = parent_page.get_children().prefetch_related('content_type', 'sites_rooted_here')

    # 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 or pages.count() < 100:
        # Retrieve pages in their most specific form, so that custom
        # get_admin_display_title and get_url_parts methods on subclasses are respected.
        # However, skip this on unpaginated listings with >100 child pages as this could
        # be a significant performance hit. (This should only happen on the reorder view,
        # and hopefully no-one is having to do manual reordering on listings that large...)
        pages = pages.specific(defer=True)

    # 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_include_self_strict(self):
     self.assertEqual(
         Page.get_first_root_node(),
         Page.objects.first_common_ancestor(include_self=True, strict=True))
 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())
Beispiel #45
0
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', 'sites_rooted_here')

    # 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 handle(self, **options):
     with use_language(mt_settings.DEFAULT_LANGUAGE):
         for node in Page.get_root_nodes():
             self.set_subtree(node, None)
 def handle(self, *args, **options):
     for node in Page.get_root_nodes():
         self.set_subtree(node)
Beispiel #48
0
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
    context = 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')
    })

    return render_modal_workflow(
        request,
        'wagtailadmin/chooser/browse.html', None,
        context,
        json_data={'step': 'browse', 'parent_page_id': context['parent_page_id']},
    )
Beispiel #49
0
 def get_root_page(self, request):
     return Page.get_first_root_node()
Beispiel #50
0
    def handle(self, **options):
        any_problems_fixed = False

        for page in Page.objects.all():
            try:
                page.specific
            except page.specific_class.DoesNotExist:
                self.stdout.write("Page %d (%s) is missing a subclass record; deleting." % (page.id, page.title))
                any_problems_fixed = True
                page.delete()

        (bad_alpha, bad_path, orphans, bad_depth, bad_numchild) = Page.find_problems()

        if bad_depth:
            self.stdout.write("Incorrect depth value found for pages: %s" % self.numberlist_to_string(bad_depth))
        if bad_numchild:
            self.stdout.write("Incorrect numchild value found for pages: %s" % self.numberlist_to_string(bad_numchild))

        if bad_depth or bad_numchild:
            Page.fix_tree(destructive=False)
            any_problems_fixed = True

        if orphans:
            # The 'orphans' list as returned by treebeard only includes pages that are
            # missing an immediate parent; descendants of orphans are not included.
            # Deleting only the *actual* orphans is a bit silly (since it'll just create
            # more orphans), so generate a queryset that contains descendants as well.
            orphan_paths = Page.objects.filter(id__in=orphans).values_list('path', flat=True)
            filter_conditions = []
            for path in orphan_paths:
                filter_conditions.append(Q(path__startswith=path))

            # combine filter_conditions into a single ORed condition
            final_filter = functools.reduce(operator.or_, filter_conditions)

            # build a queryset of all pages to be removed; this must be a vanilla Django
            # queryset rather than a treebeard MP_NodeQuerySet, so that we bypass treebeard's
            # custom delete() logic that would trip up on the very same corruption that we're
            # trying to fix here.
            pages_to_delete = models.query.QuerySet(Page).filter(final_filter)

            self.stdout.write("Orphaned pages found:")
            for page in pages_to_delete:
                self.stdout.write("ID %d: %s" % (page.id, page.title))
            self.stdout.write('')

            if options.get('interactive', True):
                yes_or_no = input("Delete these pages? [y/N] ")
                delete_orphans = yes_or_no.lower().startswith('y')
                self.stdout.write('')
            else:
                # Running tests, check for the "delete_orphans" option
                delete_orphans = options.get('delete_orphans', False)

            if delete_orphans:
                deletion_count = len(pages_to_delete)
                pages_to_delete.delete()
                self.stdout.write(
                    "%d orphaned page%s deleted." % (deletion_count, "s" if deletion_count != 1 else "")
                )
                any_problems_fixed = True

        if any_problems_fixed:
            # re-run find_problems to see if any new ones have surfaced
            (bad_alpha, bad_path, orphans, bad_depth, bad_numchild) = Page.find_problems()

        if any((bad_alpha, bad_path, orphans, bad_depth, bad_numchild)):
            self.stdout.write("Remaining problems (cannot fix automatically):")
            if bad_alpha:
                self.stdout.write(
                    "Invalid characters found in path for pages: %s" % self.numberlist_to_string(bad_alpha)
                )
            if bad_path:
                self.stdout.write("Invalid path length found for pages: %s" % self.numberlist_to_string(bad_path))
            if orphans:
                self.stdout.write("Orphaned pages found: %s" % self.numberlist_to_string(orphans))
            if bad_depth:
                self.stdout.write("Incorrect depth value found for pages: %s" % self.numberlist_to_string(bad_depth))
            if bad_numchild:
                self.stdout.write(
                    "Incorrect numchild value found for pages: %s" % self.numberlist_to_string(bad_numchild)
                )

        elif any_problems_fixed:
            self.stdout.write("All problems fixed.")
        else:
            self.stdout.write("No problems found.")