コード例 #1
0
ファイル: test_core.py プロジェクト: iatkin/mezzanine
    def test_multisite(self):
        from django.conf import settings

        # setup
        try:
            old_site_id = settings.SITE_ID
        except:  # noqa
            old_site_id = None

        site1 = Site.objects.create(domain="site1.com")
        site2 = Site.objects.create(domain="site2.com")

        # create pages under site1, which should be only accessible
        # when SITE_ID is site1
        with override_current_site_id(site1.pk):
            site1_page = self._create_page("Site1", CONTENT_STATUS_PUBLISHED)
            self._test_site_pages("Site1", CONTENT_STATUS_PUBLISHED, count=1)

        # create pages under site2, which should only be accessible
        # when SITE_ID is site2
        with override_current_site_id(site2.pk):
            self._create_page("Site2", CONTENT_STATUS_PUBLISHED)
            self._test_site_pages("Site2", CONTENT_STATUS_PUBLISHED, count=1)

            # original page should 404
            response = self.client.get(site1_page.get_absolute_url(),
                                       follow=True)
            self.assertEqual(response.status_code, 404)

        # change back to site1, and only the site1 pages should be retrieved
        with override_current_site_id(site1.pk):
            self._test_site_pages("Site1", CONTENT_STATUS_PUBLISHED, count=1)

            # insert a new record, see the count change
            self._create_page("Site1 Draft", CONTENT_STATUS_DRAFT)
            self._test_site_pages("Site1 Draft", CONTENT_STATUS_DRAFT, count=2)
            self._test_site_pages("Site1 Draft",
                                  CONTENT_STATUS_PUBLISHED,
                                  count=2)

        # change back to site2, and only the site2 pages should be retrieved
        with override_current_site_id(site2.pk):
            self._test_site_pages("Site2", CONTENT_STATUS_PUBLISHED, count=1)

            # insert a new record, see the count change
            self._create_page("Site2 Draft", CONTENT_STATUS_DRAFT)
            self._test_site_pages("Site2 Draft", CONTENT_STATUS_DRAFT, count=2)
            self._test_site_pages("Site2 Draft",
                                  CONTENT_STATUS_PUBLISHED,
                                  count=2)

        # tear down
        if old_site_id:
            settings.SITE_ID = old_site_id
        else:
            del settings.SITE_ID

        site1.delete()
        site2.delete()
コード例 #2
0
ファイル: models.py プロジェクト: ctrengove/mezzanine
 def get_ascendants(self, for_user=None):
     """
     Returns the ascendants for the page. Ascendants are cached in
     the ``_ascendants`` attribute, which is populated when the page
     is loaded via ``Page.objects.with_ascendants_for_slug``.
     """
     if not self.parent_id:
         # No parents at all, bail out.
         return []
     if not hasattr(self, "_ascendants"):
         # _ascendants has not been either page.get_ascendants or
         # Page.objects.assigned by with_ascendants_for_slug, so
         # run it to see if we can retrieve all parents in a single
         # query, which will occur if the slugs for each of the pages
         # have not been customised.
         if self.slug:
             kwargs = {"for_user": for_user}
             with override_current_site_id(self.site_id):
                 pages = Page.objects.with_ascendants_for_slug(self.slug,
                                                               **kwargs)
             self._ascendants = pages[0]._ascendants
         else:
             self._ascendants = []
     if not self._ascendants:
         # Page has a parent but with_ascendants_for_slug failed to
         # find them due to custom slugs, so retrieve the parents
         # recursively.
         child = self
         while child.parent_id is not None:
             self._ascendants.append(child.parent)
             child = child.parent
     return self._ascendants
コード例 #3
0
ファイル: models.py プロジェクト: yukiohmiya/mezzanine
 def get_ascendants(self, for_user=None):
     """
     Returns the ascendants for the page. Ascendants are cached in
     the ``_ascendants`` attribute, which is populated when the page
     is loaded via ``Page.objects.with_ascendants_for_slug``.
     """
     if not self.parent_id:
         # No parents at all, bail out.
         return []
     if not hasattr(self, "_ascendants"):
         # _ascendants has not been either page.get_ascendants or
         # Page.objects.assigned by with_ascendants_for_slug, so
         # run it to see if we can retrieve all parents in a single
         # query, which will occur if the slugs for each of the pages
         # have not been customised.
         if self.slug:
             kwargs = {"for_user": for_user}
             with override_current_site_id(self.site_id):
                 pages = Page.objects.with_ascendants_for_slug(
                     self.slug, **kwargs)
             self._ascendants = pages[0]._ascendants
         else:
             self._ascendants = []
     if not self._ascendants:
         # Page has a parent but with_ascendants_for_slug failed to
         # find them due to custom slugs, so retrieve the parents
         # recursively.
         child = self
         while child.parent_id is not None:
             self._ascendants.append(child.parent)
             child = child.parent
     return self._ascendants
コード例 #4
0
ファイル: tests.py プロジェクト: ctrengove/mezzanine
    def test_ascendants_different_site(self):
        site2 = Site.objects.create(domain='site2.example.com', name='Site 2')

        parent = Page.objects.create(title="Parent", site=site2)
        child = parent.children.create(title="Child", site=site2)
        grandchild = child.children.create(title="Grandchild", site=site2)

        # Re-retrieve grandchild so its parent attribute is not cached
        with override_current_site_id(site2.id):
            grandchild = Page.objects.get(pk=grandchild.pk)

        with self.assertNumQueries(1):
            self.assertListEqual(grandchild.get_ascendants(), [child, parent])
コード例 #5
0
ファイル: tests.py プロジェクト: zhouzexufly/mezzanine
    def test_ascendants_different_site(self):
        site2 = Site.objects.create(domain='site2.example.com', name='Site 2')

        parent = Page.objects.create(title="Parent", site=site2)
        child = parent.children.create(title="Child", site=site2)
        grandchild = child.children.create(title="Grandchild", site=site2)

        # Re-retrieve grandchild so its parent attribute is not cached
        with override_current_site_id(site2.id):
            grandchild = Page.objects.get(pk=grandchild.pk)

        with self.assertNumQueries(1):
            self.assertListEqual(grandchild.get_ascendants(), [child, parent])
コード例 #6
0
 def test_override_site_id(self):
     self.assertEqual(current_site_id(), 1)
     with override_current_site_id(2):
         self.assertEqual(current_site_id(), 2)
     self.assertEqual(current_site_id(), 1)
コード例 #7
0
ファイル: test_core.py プロジェクト: iatkin/mezzanine
    def test_update_site(self):
        from django.conf import settings

        # setup
        try:
            old_site_id = settings.SITE_ID
        except:  # noqa
            old_site_id = None

        site1 = Site.objects.create(domain="site1.com")
        site2 = Site.objects.create(domain="site2.com")

        # default behaviour, page gets assigned current site
        with override_current_site_id(site2.pk):
            page = RichTextPage()
            page.save()
            self.assertEqual(page.site_id, site2.pk)

            # Subsequent saves do not update site to current site
            page.site = site1
            page.save()
            self.assertEqual(page.site_id, site1.pk)

        # resave w/ update_site=True, page gets assigned current site
        with override_current_site_id(site1.pk):
            page.site = site2
            page.save(update_site=True)
            self.assertEqual(page.site_id, site1.pk)

        # resave w/ update_site=False, page does not update site
        with override_current_site_id(site2.pk):
            page.save(update_site=False)
            self.assertEqual(page.site_id, site1.pk)

        # When update_site=True, new page gets assigned current site
        with override_current_site_id(site2.pk):
            page = RichTextPage()
            page.site = site1
            page.save(update_site=True)
            self.assertEqual(page.site_id, site2.pk)

        # When update_site=False, new page keeps current site
        with override_current_site_id(site2.pk):
            page = RichTextPage()
            page.site = site1
            page.save(update_site=False)
            self.assertEqual(page.site_id, site1.pk)

        # When site explicitly assigned, new page keeps assigned site
        with override_current_site_id(site2.pk):
            page = RichTextPage()
            page.site = site1
            page.save()
            self.assertEqual(page.site_id, site1.pk)

        # tear down
        if old_site_id:
            settings.SITE_ID = old_site_id
        else:
            del settings.SITE_ID

        site1.delete()
        site2.delete()