def test_copy_tree(mock_datetime, mock_page_objects, mock_copy_content_and_children): mock_datetime.now().strftime.return_value = 'date' parent = Page() page = Page(title='foo bar', slug='sl-ug', active=True, parent=parent) assert ( utils.copy_tree(page) == mock_copy_content_and_children.return_value) mock_page_objects.create.assert_called_with( title='Copy of foo bar on date', slug='copy-of-sl-ug-on-date', parent=parent, active=False) mock_copy_content_and_children.assert_called_with( page, mock_page_objects.create.return_value)
def import_structure(f, root_id=None): """docstring for import_structure""" parents = {} if root_id: parents[0] = Page.objects.get(id=root_id) else: parents[0] = None for line in f: match = re.match(r"^(#+) (.*)$", line) if match: level = len(match.group(1)) title = match.group(2) parent = parents[level - 1] parent_page = Page.objects.get(pk=parent.pk) if parent else None p = Page(active=True, title=title, slug=slugify(title), in_navigation=True, parent=parent_page) for field in ('navigation_type', ): if parent and parent.__dict__.get(field, None): p.__dict__[field] = parent.__dict__[field] p.save() parents[level] = p print('adding FeinCMS page: %s' % title)
def test_pages_l10n_template(): parent = Page(title='Parent Title', slug='parent-slug') page = Page(title='Page Title', slug='page-slug') page.parent = parent parent_entry = models.RichTextEntry(text='<p>Parent Text</p>') entry = models.RichTextEntry(text='<p>Page Text</p>') parent.content.all_of_type = lambda content_type: [parent_entry] page.content.all_of_type = lambda content_type: [entry] l10n_template = utils.pages_l10n_template([parent, page]) assert 'Page path: /parent-slug/\n' in l10n_template assert 'Page path: /parent-slug/page-slug/\n' in l10n_template assert ('{% blocktrans trimmed %}\nParent Title\n{% endblocktrans %}' in l10n_template) assert ('{% blocktrans trimmed %}\nPage Title\n{% endblocktrans %}' in l10n_template) assert ('{% blocktrans trimmed %}\n<p>Parent Text</p>\n{% endblocktrans %}' in l10n_template) assert ('{% blocktrans trimmed %}\n<p>Page Text</p>\n{% endblocktrans %}' in l10n_template)
def test_copy_page_with_parent(mock_page_objects, mock_copy_content_and_children): page = Page(title='title', slug='slug', active=False) assert (utils.copy_page_with_parent( page, 'parent') == mock_copy_content_and_children.return_value) mock_page_objects.create.assert_called_with(title='title', slug='slug', parent='parent', active=False) mock_copy_content_and_children.assert_called_with( page, mock_page_objects.create.return_value)
def siblings_along_path_to(page_list, page2): """ Filters a list of pages so that only those remain that are either: * An ancestor of the current page * A sibling of an ancestor of the current page A typical use case is building a navigation menu with the active path to the current page expanded:: {% feincms_nav feincms_page level=1 depth=3 as navitems %} {% with navitems|siblings_along_path_to:feincms_page as navtree %} ... whatever ... {% endwith %} """ if page_list: try: # Try to avoid hitting the database: If the current page is in_navigation, # then all relevant pages are already in the incoming list, no need to # fetch ancestors or children. # NOTE: This assumes that the input list actually is complete (ie. comes from # feincms_nav). We'll cope with the fall-out of that assumption # when it happens... ancestors = [ a_page for a_page in page_list if _is_equal_or_parent_of(a_page, page2) ] top_level = min((a_page.level for a_page in page_list)) if not ancestors: # Happens when we sit on a page outside the navigation tree # so fake an active root page to avoid a get_ancestors() db call # which would only give us a non-navigation root page anyway. p = Page(title="dummy", tree_id=-1, parent_id=None, in_navigation=False) ancestors = (p, ) siblings = [ a_page for a_page in page_list if a_page.parent_id == page2.id or a_page.level == top_level or any((_is_sibling_of(a_page, a) for a in ancestors)) ] return siblings except (AttributeError, ValueError), e: logger.warn("siblings_along_path_to caught exception: %s", format_exception(e))
def testPage(self): page = Page() self.assertTrue(hasattr(page, 'language')) self.assertTrue(hasattr(page, 'translation_of')) self.assertEqual(self.page_de.translation_of, self.page_en) self.assertEqual(self.page_de.original_translation, self.page_en)