def test_edit_save(self): old_revision = URLPath.root().article.current_revision self.get_url('wiki:edit', path='') self.fill({ '#id_content': 'Something 2', '#id_summary': 'why edited', '#id_title': 'wiki test' }) self.submit('#id_save') self.assertTextPresent('Something 2') self.assertTextPresent('successfully added') new_revision = URLPath.root().article.current_revision self.assertIn('Something 2', new_revision.content) self.assertEqual(new_revision.revision_number, old_revision.revision_number + 1)
def test_edit_save(self): old_revision = URLPath.root().article.current_revision self.get_url("wiki:edit", path="") self.fill({ "#id_content": "Something 2", "#id_summary": "why edited", "#id_title": "wiki test", }) self.submit("#id_save") self.assertTextPresent("Something 2") self.assertTextPresent("successfully added") new_revision = URLPath.root().article.current_revision self.assertIn("Something 2", new_revision.content) self.assertEqual(new_revision.revision_number, old_revision.revision_number + 1)
def get_or_create_root(): """ Returns the root article, or creates it if it doesn't exist. """ try: root = URLPath.root() if not root.article: root.delete() raise NoRootURL return root except NoRootURL: pass starting_content = "\n".join(( _("Welcome to the {platform_name} Wiki").format( platform_name=configuration_helpers.get_value( 'PLATFORM_NAME', settings.PLATFORM_NAME), ), "===", _("Visit a course wiki to add an article."), )) root = URLPath.create_root(title=_("Wiki"), content=starting_content) article = root.article article.group = None article.group_read = True article.group_write = False article.other_read = True article.other_write = False article.save() return root
def test_revision_conflict(self): """ Test the warning if the same article is being edited concurrently. """ example_data = { 'content': 'More modifications', 'current_revision': str(URLPath.root().article.current_revision.id), 'preview': '0', 'save': '1', 'summary': 'why edited', 'title': 'wiki test' } response = self.client.post( resolve_url('wiki:edit', path=''), example_data ) self.assertRedirects(response, resolve_url('wiki:root')) response = self.client.post( resolve_url('wiki:edit', path=''), example_data ) self.assertContains( response, 'While you were editing, someone else changed the revision.' )
def test_revision_conflict(self): """ Test the warning if the same article is being edited concurrently. """ example_data = { 'content': 'More modifications', 'current_revision': str(URLPath.root().article.current_revision.id), 'preview': '0', 'save': '1', 'summary': 'why edited', 'title': 'wiki test' } response = self.client.post(resolve_url('wiki:edit', path=''), example_data) self.assertRedirects(response, resolve_url('wiki:root')) response = self.client.post(resolve_url('wiki:edit', path=''), example_data) self.assertContains( response, 'While you were editing, someone else changed the revision.')
def get_or_create_root(): """ Returns the root article, or creates it if it doesn't exist. """ try: root = URLPath.root() if not root.article: root.delete() raise NoRootURL return root except NoRootURL: pass starting_content = "\n".join(( "Welcome to the edX Wiki", "===", "Visit a course wiki to add an article.")) root = URLPath.create_root(title="Wiki", content=starting_content) article = root.article article.group = None article.group_read = True article.group_write = False article.other_read = True article.other_write = False article.save() return root
def test_editsection_edit(self): urlpath = URLPath.create_urlpath(URLPath.root(), "testedit", title="TestEdit", content=TEST_CONTENT) old_number = urlpath.article.current_revision.revision_number self.get_literal_url( reverse( "wiki:editsection", kwargs={ "path": "testedit/", "location": "1-2-0", "header": "T3" }, )) self.fill({"#id_content": "# Header 1\nContent of the new section"}) self.submit("#id_save") expected = ( r"(?s)" r'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*' r'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*' r'Header 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/H1/">\[edit\]</a>.*' r"Content of the new section.*" r'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-1-0/header/T5/">\[edit\]</a>.*' r'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/3-0-0/header/T6/">\[edit\]</a>.*' ) self.assertRegex(self.last_response.content.decode("utf-8"), expected) new_number = URLPath.objects.get( slug="testedit").article.current_revision.revision_number self.assertEqual(new_number, old_number + 1)
def test_revision_conflict(self): """ Test the warning if the same article is being edited concurrently. """ example_data = { "content": "More modifications", "current_revision": str(URLPath.root().article.current_revision.id), "preview": "0", "save": "1", "summary": "why edited", "title": "wiki test", } response = self.client.post(resolve_url("wiki:edit", path=""), example_data) self.assertRedirects(response, resolve_url("wiki:root")) response = self.client.post(resolve_url("wiki:edit", path=""), example_data) self.assertContains( response, "While you were editing, someone else changed the revision.")
def get_article(self, cont, image): urlpath = URLPath.create_urlpath( URLPath.root(), "html_image", title="TestImage", content=cont ) if image: self._create_test_image(urlpath.path) return urlpath.article.render()
def get_or_create_root(): """ Returns the root article, or creates it if it doesn't exist. """ try: root = URLPath.root() if not root.article: root.delete() raise NoRootURL return root except NoRootURL: pass starting_content = "\n".join(( _("Welcome to the edX Wiki"), "===", _("Visit a course wiki to add an article."), )) root = URLPath.create_root(title=_("Wiki"), content=starting_content) article = root.article article.group = None article.group_read = True article.group_write = False article.other_read = True article.other_write = False article.save() return root
def get_article(self, cont): urlpath = URLPath.create_urlpath(URLPath.root(), "html_attach", title="TestAttach", content=cont) self._create_test_attachment(urlpath.path) return urlpath.article.render()
def get_or_create_root(): """ Returns the root article, or creates it if it doesn't exist. """ try: root = URLPath.root() if not root.article: root.delete() raise NoRootURL return root except NoRootURL: pass starting_content = "\n".join(( _("Welcome to the {platform_name} Wiki").format(platform_name=get_themed_value('PLATFORM_NAME', settings.PLATFORM_NAME)), "===", _("Visit a course wiki to add an article."), )) root = URLPath.create_root(title=_("Wiki"), content=starting_content) article = root.article article.group = None article.group_read = True article.group_write = False article.other_read = True article.other_write = False article.save() return root
def form_valid(self, form): clean_data = form.cleaned_data print(clean_data) slug = clean_data['slug'] title = clean_data['name'] content = clean_data['description'] # creates an article for the category and then associates them by having equaling titles and slugs self.landing_article_urlpath = URLPath.create_article( URLPath.root(), slug, title=title, content=content, user_message=" ", user=self.request.user, article_kwargs={ 'owner': self.request.user, 'group': self.article.group, 'group_read': self.article.group_read, 'group_write': self.article.group_write, 'other_read': self.article.other_read, 'other_write': self.article.other_write, }) landing_article = Article.objects.get( urlpath=self.landing_article_urlpath) form.instance.article = landing_article form.save() category = Category.objects.get(slug=slug) return redirect("wiki:get", path=self.landing_article_urlpath.path, article_id=self.article.id)
def test_editsection_edit(self): urlpath = URLPath.create_urlpath(URLPath.root(), "testedit", title="TestEdit", content=TEST_CONTENT) old_number = urlpath.article.current_revision.revision_number self.get_literal_url( reverse('wiki:editsection', kwargs={ 'path': 'testedit/', 'location': '1-2-0', 'header': 'T3' })) self.fill({'#id_content': '# Header 1\nContent of the new section'}) self.submit('#id_save') expected = ( r'(?s)' r'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*' r'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*' r'Header 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/H1/">\[edit\]</a>.*' r'Content of the new section.*' r'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-1-0/header/T5/">\[edit\]</a>.*' r'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/3-0-0/header/T6/">\[edit\]</a>.*' ) self.assertRegex(self.last_response.content.decode('utf-8'), expected) new_number = URLPath.objects.get( slug='testedit').article.current_revision.revision_number self.assertEqual(new_number, old_number + 1)
def setUp(self): super().setUp() self.root = URLPath.create_root() self.root_article = URLPath.root().article rev = self.root_article.current_revision rev.title = "Root Article" rev.content = "root article content" rev.save()
def test_broken_content(self): # Regression test for https://github.com/django-wiki/django-wiki/issues/1094 TEST_CONTENT = "### [Here we go](#anchor)" urlpath = URLPath.create_urlpath( URLPath.root(), "testedit", title="TestEdit", content=TEST_CONTENT ) output = urlpath.article.render() print(output)
def setUp(self): super(RequireRootArticleMixin, self).setUp() self.root = URLPath.create_root() self.root_article = URLPath.root().article rev = self.root_article.current_revision rev.title = "Root Article" rev.content = "root article content" rev.save()
def get_article(self, cont): urlpath = URLPath.create_urlpath( URLPath.root(), "html_attach", title="TestAttach", content=cont ) self._create_test_attachment(urlpath.path) return urlpath.article.render()
def test_article_list_update(self): """ Test automatic adding and removing the new article to/from article_list. """ root_data = { "content": "[article_list depth:2]", "current_revision": str(URLPath.root().article.current_revision.id), "preview": "1", "title": "Root Article", } response = self.client.post(resolve_url("wiki:edit", path=""), root_data) self.assertRedirects(response, resolve_url("wiki:root")) # verify the new article is added to article_list response = self.client.post( resolve_url("wiki:create", path=""), { "title": "Sub Article 1", "slug": "SubArticle1" }, ) self.assertRedirects(response, resolve_url("wiki:get", path="subarticle1/")) self.assertContains(self.get_by_path(""), "Sub Article 1") self.assertContains(self.get_by_path(""), "subarticle1/") # verify the deleted article is removed from article_list response = self.client.post( resolve_url("wiki:delete", path="SubArticle1/"), { "confirm": "on", "purge": "on", "revision": str( URLPath.objects.get( slug="subarticle1").article.current_revision.id), }, ) message = getattr(self.client.cookies["messages"], "value") self.assertRedirects(response, resolve_url("wiki:get", path="")) self.assertIn( "This article together with all " "its contents are now completely gone", message, ) self.assertNotContains(self.get_by_path(""), "Sub Article 1")
def test_editsection(self): # Test creating links to allow editing all sections individually urlpath = URLPath.create_urlpath(URLPath.root(), "testedit", title="TestEdit", content=TEST_CONTENT) output = urlpath.article.render() expected = ( r'(?s)' r'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*' r'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*' r'Title 3<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-0/header/T3/">\[edit\]</a>.*' r'Title 4<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-1/header/T4/">\[edit\]</a>.*' r'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-3-0/header/T5/">\[edit\]</a>.*' r'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/T6/">\[edit\]</a>.*' ) self.assertRegex(output, expected) # Test wrong header text. Editing should fail with a redirect. url = reverse('wiki:editsection', kwargs={ 'path': 'testedit/', 'location': '1-2-1', 'header': 'Test' }) response = self.client.get(url) self.assertRedirects(response, reverse('wiki:get', kwargs={'path': 'testedit/'})) # Test extracting sections for editing url = reverse('wiki:editsection', kwargs={ 'path': 'testedit/', 'location': '1-2-1', 'header': 'T4' }) response = self.client.get(url) expected = ('>### Title 4[\r\n]*' '<') self.assertRegex(response.rendered_content, expected) url = reverse('wiki:editsection', kwargs={ 'path': 'testedit/', 'location': '1-2-0', 'header': 'T3' }) response = self.client.get(url) expected = ('>Title 3[\r\n]*' '-------[\r\n]*' 'a[\r\n]*' 'Paragraph[\r\n]*' '-------[\r\n]*' '### Title 4[\r\n]*' '<') self.assertRegex(response.rendered_content, expected)
def test_preview_and_save(self): self.get_url("wiki:edit", path="") self.fill({ "#id_content": "Some changed stuff", "#id_summary": "why edited", "#id_title": "wiki test", }) self.click("#id_preview") self.submit("#id_preview_save_changes") new_revision = URLPath.root().article.current_revision self.assertIn("Some changed stuff", new_revision.content)
def test_preview_and_save(self): self.get_url('wiki:edit', path='') self.fill({ '#id_content': 'Some changed stuff', '#id_summary': 'why edited', '#id_title': 'wiki test' }) self.click('#id_preview') self.submit('#id_preview_save_changes') new_revision = URLPath.root().article.current_revision self.assertIn("Some changed stuff", new_revision.content)
def test_history(self): url = reverse('wiki:globalhistory') response = self.c.get(url) expected = ( '(?s)<title>Global history.*' '>Global history</.*' 'List of all <strong>1 changes</strong>.*' 'Root Article.*no log message.*' '</table>' ) self._assertRegex(response.rendered_content, expected) URLPath.create_article(URLPath.root(), "testhistory1", title="TestHistory1", content="a page", user_message="Comment 1") response = self.c.get(url) expected = ( '(?s)<title>Global history.*' '>Global history</.*' 'List of all <strong>2 changes</strong>.*' 'TestHistory1.*Comment 1.*' 'Root Article.*no log message.*' '</table>' ) self._assertRegex(response.rendered_content, expected) URLPath.create_article(URLPath.root(), "testhistory2", title="TestHistory2", content="a page", user_message="Comment 2") response = self.c.get(url) expected = ( '(?s)<title>Global history.*' '>Global history</.*' 'List of all <strong>3 changes</strong>.*' 'TestHistory2.*Comment 2.*' 'TestHistory1.*Comment 1.*' 'Root Article.*no log message.*' '</table>' ) self._assertRegex(response.rendered_content, expected)
def setUp(self): super(ArticleWebTestBase, self).setUp() response = self.c.post( reverse('wiki:root_create'), {'content': 'root article content', 'title': 'Root Article'}, follow=True ) self.assertEqual(response.status_code, 200) # sanity check self.root_article = URLPath.root().article
def setUp(self): super(ArticleWebTestBase, self).setUp() response = self.c.post(reverse('wiki:root_create'), { 'content': 'root article content', 'title': 'Root Article' }, follow=True) self.assertEqual(response.status_code, 200) # sanity check self.root_article = URLPath.root().article
def test_works_with_lazy_functions(self): URLPath.create_root() config = (("base_url", reverse_lazy("wiki:get", kwargs={"path": ""})), ) md = markdown.Markdown(extensions=["extra", WikiPathExtension(config)]) text = "[Français](wiki:/fr)" self.assertEqual( md.convert(text), '<p><a class="wikipath linknotfound" href="/fr">Français</a></p>', ) URLPath.create_urlpath( URLPath.root(), "linktest", title="LinkTest", content="A page\n#A section\nA line", user_message="Comment1", ) # Link to an existing page text = "[Test link](wiki:/linktest)" self.assertEqual( md.convert(text), '<p><a class="wikipath" href="/linktest/">Test link</a></p>', ) # Link with an empty fragment text = "[Test link](wiki:/linktest#)" self.assertEqual( md.convert(text), '<p><a class="wikipath" href="/linktest/#">Test link</a></p>', ) # Link to a header in an existing page text = "[Test head](wiki:/linktest#wiki-toc-a-section)" self.assertEqual( md.convert(text), '<p><a class="wikipath" href="/linktest/#wiki-toc-a-section">Test head</a></p>', ) # Link to a header in a non existing page text = "[Test head nonExist](wiki:/linktesterr#wiki-toc-a-section)" self.assertEqual( md.convert(text), '<p><a class="wikipath linknotfound" href="/linktesterr#wiki-toc-a-section">Test head nonExist</a></p>', ) # Invalid Wiki link: The default markdown link parser takes over text = "[Test head err](wiki:/linktest#wiki-toc-a-section#err)" self.assertEqual( md.convert(text), '<p><a href="wiki:/linktest#wiki-toc-a-section#err">Test head err</a></p>', )
def setUp(self): config = (("base_url", reverse_lazy("wiki:get", kwargs={"path": ""})), ) self.md = markdown.Markdown( extensions=["extra", WikiPathExtension(config)]) URLPath.create_root() URLPath.create_urlpath( URLPath.root(), "linktest", title="LinkTest", content="A page\n#A section\nA line", user_message="Comment1", )
def test_editsection(self): # Test creating links to allow editing all sections individually urlpath = URLPath.create_urlpath( URLPath.root(), "testedit", title="TestEdit", content=TEST_CONTENT ) output = urlpath.article.render() expected = ( r"(?s)" r'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*' r'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*' r'Title 3<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-0/header/T3/">\[edit\]</a>.*' r'Title 4<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-1/header/T4/">\[edit\]</a>.*' r'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-3-0/header/T5/">\[edit\]</a>.*' r'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/T6/">\[edit\]</a>.*' ) self.assertRegex(output, expected) # Test wrong header text. Editing should fail with a redirect. url = reverse( "wiki:editsection", kwargs={"path": "testedit/", "location": "1-2-1", "header": "Test"}, ) response = self.client.get(url) self.assertRedirects( response, reverse("wiki:get", kwargs={"path": "testedit/"}) ) # Test extracting sections for editing url = reverse( "wiki:editsection", kwargs={"path": "testedit/", "location": "1-2-1", "header": "T4"}, ) response = self.client.get(url) expected = ">### Title 4[\r\n]*" "<" self.assertRegex(response.rendered_content, expected) url = reverse( "wiki:editsection", kwargs={"path": "testedit/", "location": "1-2-0", "header": "T3"}, ) response = self.client.get(url) expected = ( ">Title 3[\r\n]*" "-------[\r\n]*" "a[\r\n]*" "Paragraph[\r\n]*" "-------[\r\n]*" "### Title 4[\r\n]*" "<" ) self.assertRegex(response.rendered_content, expected)
def test_preview_xframe_options_sameorigin(self): """Ensure that preview response has X-Frame-Options: SAMEORIGIN""" example_data = { "content": "The modified text", "current_revision": str(URLPath.root().article.current_revision.id), "preview": "1", "summary": "why edited", "title": "wiki test", } response = self.client.post(resolve_url("wiki:preview", path=""), example_data) self.assertEquals(response.get("X-Frame-Options"), "SAMEORIGIN")
def test_article_list_update(self): """ Test automatic adding and removing the new article to/from article_list. """ c = self.c root_data = { 'content': '[article_list depth:2]', 'current_revision': str(URLPath.root().article.current_revision.id), 'preview': '1', 'title': 'Root Article' } response = c.post(reverse('wiki:edit', kwargs={'path': ''}), root_data) self.assertRedirects(response, reverse('wiki:root')) # verify the new article is added to article_list response = c.post(reverse('wiki:create', kwargs={'path': ''}), { 'title': 'Sub Article 1', 'slug': 'SubArticle1' }) self.assertRedirects( response, reverse('wiki:get', kwargs={'path': 'subarticle1/'})) self.assertContains(self.get_by_path(''), 'Sub Article 1') self.assertContains(self.get_by_path(''), 'subarticle1/') # verify the deleted article is removed from article_list response = c.post( reverse('wiki:delete', kwargs={'path': 'SubArticle1/'}), { 'confirm': 'on', 'purge': 'on', 'revision': str( URLPath.objects.get( slug='subarticle1').article.current_revision.id), }) message = getattr(c.cookies['messages'], 'value') self.assertRedirects(response, reverse('wiki:get', kwargs={'path': ''})) self.assertIn( 'This article together with all ' 'its contents are now completely gone', message) self.assertNotContains(self.get_by_path(''), 'Sub Article 1')
def test_root_article(self): """ Test redirecting to /create-root/, creating the root article and a simple markup. """ self.get_url("wiki:root") self.assertUrlsEqual(resolve_url("wiki:root_create")) self.fill( {"#id_content": "test heading h1\n====\n", "#id_title": "Wiki Test",} ) self.submit('button[name="save_changes"]') self.assertUrlsEqual("/") self.assertTextPresent("test heading h1") article = URLPath.root().article self.assertIn("test heading h1", article.current_revision.content)
def setUp(self): super(ArticleTestBase, self).setUp() response = self.c.post( reverse('wiki:root_create'), {'content': 'root article content', 'title': 'Root Article'}, follow=True) self.assertEqual(response.status_code, 200) # sanity check self.root_article = URLPath.root().article self.example_data = { 'content': 'The modified text', 'current_revision': '1', 'preview': '1', # 'save': '1', # probably not too important 'summary': 'why edited', 'title': 'wiki test'}
def article_toc(article_slug=None): article = None if not article_slug: root = URLPath.root() article = root.article else: urlpath = URLPath.get_by_path(article_slug, select_related=True) article = urlpath.article toc_tree = article.get_children( article__current_revision__deleted=False) return { 'article_children': toc_tree, 'article': article, 'article_path': article_slug + "/", }
def test_article_list_update(self): """ Test automatic adding and removing the new article to/from article_list. """ root_data = { 'content': '[article_list depth:2]', 'current_revision': str(URLPath.root().article.current_revision.id), 'preview': '1', 'title': 'Root Article' } response = self.client.post(resolve_url('wiki:edit', path=''), root_data) self.assertRedirects(response, resolve_url('wiki:root')) # verify the new article is added to article_list response = self.client.post( resolve_url('wiki:create', path=''), {'title': 'Sub Article 1', 'slug': 'SubArticle1'} ) self.assertRedirects( response, resolve_url('wiki:get', path='subarticle1/') ) self.assertContains(self.get_by_path(''), 'Sub Article 1') self.assertContains(self.get_by_path(''), 'subarticle1/') # verify the deleted article is removed from article_list response = self.client.post( resolve_url('wiki:delete', path='SubArticle1/'), {'confirm': 'on', 'purge': 'on', 'revision': str(URLPath.objects.get(slug='subarticle1').article.current_revision.id), } ) message = getattr(self.client.cookies['messages'], 'value') self.assertRedirects( response, resolve_url('wiki:get', path='') ) self.assertIn( 'This article together with all ' 'its contents are now completely gone', message) self.assertNotContains(self.get_by_path(''), 'Sub Article 1')
def test_root_article(self): """ Test redirecting to /create-root/, creating the root article and a simple markup. """ self.get_url('wiki:root') self.assertUrlsEqual(resolve_url('wiki:root_create')) self.fill({ '#id_content': 'test heading h1\n====\n', '#id_title': 'Wiki Test', }) self.submit('input[name="save_changes"]') self.assertUrlsEqual('/') self.assertTextPresent('test heading h1') article = URLPath.root().article self.assertIn('test heading h1', article.current_revision.content)
def test_preview_xframe_options_sameorigin(self): """Ensure that preview response has X-Frame-Options: SAMEORIGIN""" example_data = { 'content': 'The modified text', 'current_revision': str(URLPath.root().article.current_revision.id), 'preview': '1', 'summary': 'why edited', 'title': 'wiki test' } response = self.client.post(resolve_url('wiki:preview', path=''), example_data) self.assertEquals(response.get('X-Frame-Options'), 'SAMEORIGIN')
def inner_article_toc(article_slug=None, dropdown=True, current_article=None): article = None if not article_slug: root = URLPath.root() article = root.article else: urlpath = URLPath.get_by_path(article_slug, select_related=True) article = urlpath.article toc_tree = get_article_children(article, article__current_revision__deleted=False) return { 'article_children': toc_tree, 'article_parent': article, 'article_path': article_slug + "/", 'is_dropdown': dropdown, 'current_article': current_article, }
def test_preview_xframe_options_sameorigin(self): """Ensure that preview response has X-Frame-Options: SAMEORIGIN""" example_data = { 'content': 'The modified text', 'current_revision': str(URLPath.root().article.current_revision.id), 'preview': '1', 'summary': 'why edited', 'title': 'wiki test' } response = self.client.post( resolve_url('wiki:preview', path=''), example_data ) self.assertEquals(response.get('X-Frame-Options'), 'SAMEORIGIN')
def newArticle(self, ex_article=None, name=None, slug=None, parent=None): article_urlpath = URLPath.create_article( parent=parent or URLPath.root(), slug=slug, title=name, content=name, user_message=" ", user=self.user, article_kwargs={'owner': self.user, 'group': ex_article.group, 'group_read': ex_article.group_read, 'group_write': ex_article.group_write, 'other_read': ex_article.other_read, 'other_write': ex_article.other_write, }) newarticle = models.Article.objects.get(urlpath=article_urlpath) return newarticle
def wiki_articles_in_menu(request): wiki_root = URLPath.root() urls = [wiki_root] for subroot in wiki_root.children.all(): urls.append(subroot) items = [] for url in urls: if url.article.can_read(request.user): items.append({ 'url_regex': r'^' + str(url) + ('$' if url.parent is None else ''), 'text': url.article.current_revision.title, 'link': url.article.get_absolute_url(), }) return items
def test_preview_save(self): """Test edit preview, edit save and messages.""" example_data = { 'content': 'The modified text', 'current_revision': str(URLPath.root().article.current_revision.id), 'preview': '1', # 'save': '1', # probably not too important 'summary': 'why edited', 'title': 'wiki test' } # test preview response = self.client.post( resolve_url('wiki:preview', path=''), # url: '/_preview/' example_data ) self.assertContains(response, 'The modified text')
def get_context_data(self, tag, **kwargs): context = super(TagPageView, self).get_context_data(**kwargs) current_site = Site.objects.get_current() current_language_code = translation.get_language() tag_instance = get_tag(tag) if tag_instance is None: raise Http404(_('No Tag found matching "%s".') % tag) try: article = Article.get_for_object(tag_instance) except ArticleForObject.DoesNotExist: # Get or create root try: root_path = URLPath.root() except NoRootURL: root_path = URLPath.create_root(site=current_site) # Get current language namespace. E.g. "/fr" try: language_ns_path = URLPath.get_by_path("/%s" % current_language_code) except URLPath.DoesNotExist: language_ns_path = URLPath.create_article( parent=root_path, slug=current_language_code, site=current_site, title=current_language_code ) # Get or create the article from django.template.defaultfilters import slugify tag_slug = slugify(tag_instance.name) try: article_path = URLPath.get_by_path("/%s/%s" % (current_language_code, tag_slug)) except URLPath.DoesNotExist: article_path = URLPath.create_article( parent=language_ns_path, slug=tag_slug, site=current_site, title=tag_instance.name ) # Get the wiki article itself article = article_path.article article.add_object_relation(tag_instance) context["article"] = article # XXX: site not taken in account context["tag"] = tag_instance context["related_tags"] = list( reversed( sorted(Tag.objects.related_for_model(tag_instance, I4pProject, counts=True), key=attrgetter("count")) ) )[:15] # Get project sheets tagged with this tag XXX: site=site may # not be correct 4 Random projects with at least one picture. # It's not possible to mix distinct and order by random, so # use a trick hilighted_projects = TaggedItem.objects.get_by_model( I4pProject.objects.using_translations() .filter(language_code=current_language_code, master__site=current_site, master__pictures__isnull=False) .distinct(), tag_instance, ).distinct() context["picture_projects"] = random.sample(hilighted_projects, min(4, len(hilighted_projects))) # Mature projects mature_projects = TaggedItem.objects.get_by_model( I4pProject.objects.using_translations() .filter(master__site=current_site, master__status__in=("WIP", "END")) .distinct(), tag_instance, ).distinct() context["num_mature_projects_projects_with_tag"] = len(mature_projects) context["mature_projects"] = random.sample(mature_projects, min(4, len(mature_projects))) # Starting projects starting_projects = TaggedItem.objects.get_by_model( I4pProject.objects.using_translations() .filter(master__site=current_site, master__status__in=("IDEA", "BEGIN")) .distinct(), tag_instance, ).distinct() context["num_starting_projects_projects_with_tag"] = len(starting_projects) context["starting_projects"] = random.sample(starting_projects, min(4, len(starting_projects))) # New projects context["new_projects"] = TaggedItem.objects.get_by_model( I4pProject.objects.using_translations().filter(master__site=current_site).distinct(), tag_instance ).order_by("-master__created")[:4] # Latest modifications context["modified_projects"] = TaggedItem.objects.get_by_model( I4pProject.objects.using_translations().filter(master__site=current_site).distinct(), tag_instance ).order_by("-modified")[:4] # Related people # List is to force evaluation to avoid a sql bug in queryset combining later (project__in=projects) projects = list( TaggedItem.objects.get_by_model( I4pProject.objects.using_translations().filter(master__site=current_site).distinct(), tag_instance ).all() ) # While we're at it, let's count them for free context["num_projects_with_tag"] = len(projects) context["people"] = ProjectMember.objects.filter(project__in=projects).order_by("?")[:6] return context
def get_context_data(self, tag, **kwargs): context = super(TagPageView, self).get_context_data(**kwargs) current_site = Site.objects.get_current() current_language_code = translation.get_language() tag_instance = get_tag(tag) if tag_instance is None: raise Http404(_('No Tag found matching "%s".') % tag) try: article = Article.get_for_object(tag_instance) except ArticleForObject.DoesNotExist: # Get or create root try: root_path = URLPath.root() except NoRootURL: root_path = URLPath.create_root(site=current_site) # Get current language namespace. E.g. "/fr" try: language_ns_path = URLPath.get_by_path("/%s" % current_language_code) except URLPath.DoesNotExist: language_ns_path = URLPath.create_article( parent=root_path, slug=current_language_code, site=current_site, title=current_language_code ) # Get or create the article from django.template.defaultfilters import slugify tag_slug = slugify(tag_instance.name) try: article_path = URLPath.get_by_path("/%s/%s" % (current_language_code, tag_slug) ) except URLPath.DoesNotExist: article_path = URLPath.create_article( parent=language_ns_path, slug=tag_slug, site=current_site, title=tag_instance.name ) # Get the wiki article itself article = article_path.article article.add_object_relation(tag_instance) context['article'] = article # XXX: site not taken in account context['tag'] = tag_instance context['related_tags'] = list( reversed( sorted(Tag.objects.related_for_model(tag_instance, I4pProjectTranslation, counts=True), key=attrgetter('count'), ) ) )[:15] # Get project sheets tagged with this tag XXX: site=site may # not be correct 4 Random projects with at least one picture. # It's not possible to mix distinct and order by random, so # use a trick hilighted_projects= TaggedItem.objects.get_by_model(I4pProjectTranslation.objects.filter( language_code=current_language_code, project__site=current_site, project__pictures__isnull=False ).distinct(), tag_instance).distinct() context['picture_project_translations'] = random.sample(hilighted_projects, min(4, len(hilighted_projects))) # Mature projects mature_project_translations = TaggedItem.objects.get_by_model(I4pProjectTranslation.objects.filter( language_code=current_language_code, project__site=current_site, project__status__in=('WIP', 'END') ).distinct(), tag_instance).distinct() context['mature_project_translations'] = random.sample(mature_project_translations, min(4, len(mature_project_translations))) # Starting projects starting_project_translations = TaggedItem.objects.get_by_model(I4pProjectTranslation.objects.filter( language_code=current_language_code, project__site=current_site, project__status__in=('IDEA', 'BEGIN') ).distinct(), tag_instance).distinct() context['starting_project_translations'] = random.sample(starting_project_translations, min(4, len(starting_project_translations))) # New projects context['new_project_translations'] = TaggedItem.objects.get_by_model(I4pProjectTranslation.objects.filter( language_code=current_language_code, project__site=current_site, ).distinct(), tag_instance).order_by('-project__created')[:4] # Latest modifications context['modified_project_translations'] = TaggedItem.objects.get_by_model(I4pProjectTranslation.objects.filter( language_code=current_language_code, project__site=current_site, ).distinct(), tag_instance).order_by('-modified')[:4] # Related people project_translations = ModelTaggedItemManager().with_any([tag_instance.name], I4pProjectTranslation.objects.filter( language_code=current_language_code, project__site=current_site, ) ).distinct() projects = [p.project for p in project_translations] context['people'] = ProjectMember.objects.filter( project__in=projects ).order_by('?')[:6] return context
def test_related_manager_works_with_filters(self): root = URLPath.root() self.assertNotIn(root.id, [p.id for p in root.children.active()])
def test_move(self): # Create a hierarchy of pages self.client.post( resolve_url('wiki:create', path=''), {'title': 'Test', 'slug': 'test0', 'content': 'Content .0.'} ) self.client.post( resolve_url('wiki:create', path='test0/'), {'title': 'Test00', 'slug': 'test00', 'content': 'Content .00.'} ) self.client.post( resolve_url('wiki:create', path=''), {'title': 'Test1', 'slug': 'test1', 'content': 'Content .1.'} ) self.client.post( resolve_url('wiki:create', path='test1/'), {'title': 'Tes10', 'slug': 'test10', 'content': 'Content .10.'} ) self.client.post( resolve_url('wiki:create', path='test1/test10/'), {'title': 'Test100', 'slug': 'test100', 'content': 'Content .100.'} ) # Move /test1 => /test0 (an already existing destination slug!) response = self.client.post( resolve_url('wiki:move', path='test1/'), { 'destination': str(URLPath.root().article.current_revision.id), 'slug': 'test0', 'redirect': '' } ) self.assertContains(response, 'A slug named') self.assertContains(response, 'already exists.') # Move /test1 >= /test2 (valid slug), no redirect test0_id = URLPath.objects.get(slug='test0').article.current_revision.id response = self.client.post( resolve_url('wiki:move', path='test1/'), {'destination': str(test0_id), 'slug': 'test2', 'redirect': ''} ) self.assertRedirects( response, resolve_url('wiki:get', path='test0/test2/') ) # Check that there is no article displayed in this path anymore response = self.get_by_path('test1/') self.assertRedirects(response, '/_create/?slug=test1') # Create /test0/test2/test020 response = self.client.post( resolve_url('wiki:create', path='test0/test2/'), {'title': 'Test020', 'slug': 'test020', 'content': 'Content .020.'} ) # Move /test0/test2 => /test1new + create redirect response = self.client.post( resolve_url('wiki:move', path='test0/test2/'), { 'destination': str(URLPath.root().article.current_revision.id), 'slug': 'test1new', 'redirect': 'true' } ) self.assertRedirects( response, resolve_url('wiki:get', path='test1new/') ) # Check that /test1new is a valid path response = self.get_by_path('test1new/') self.assertContains(response, 'Content .1.') # Check that the child article test0/test2/test020 was also moved response = self.get_by_path('test1new/test020/') self.assertContains(response, 'Content .020.') response = self.get_by_path('test0/test2/') self.assertContains(response, 'Moved: Test1') self.assertRegex(response.rendered_content, r'moved to <a[^>]*>wiki:/test1new/') response = self.get_by_path('test0/test2/test020/') self.assertContains(response, 'Moved: Test020') self.assertRegex(response.rendered_content, r'moved to <a[^>]*>wiki:/test1new/test020') # Check that moved_to was correctly set urlsrc = URLPath.get_by_path('/test0/test2/') urldst = URLPath.get_by_path('/test1new/') self.assertEqual(urlsrc.moved_to, urldst) # Check that moved_to was correctly set on the child's previous path urlsrc = URLPath.get_by_path('/test0/test2/test020/') urldst = URLPath.get_by_path('/test1new/test020/') self.assertEqual(urlsrc.moved_to, urldst)
def test_history(self): url = reverse('wiki:globalhistory') url0 = reverse('wiki:globalhistory', kwargs={'only_last': '0'}) url1 = reverse('wiki:globalhistory', kwargs={'only_last': '1'}) response = self.client.get(url) expected = ( '(?s).*Root Article.*no log message.*' ) self.assertRegexpMatches(response.rendered_content, expected) URLPath.create_urlpath(URLPath.root(), "testhistory1", title="TestHistory1", content="a page", user_message="Comment 1") response = self.client.get(url) expected = ( '(?s).*TestHistory1.*Comment 1.*' 'Root Article.*no log message.*' ) self.assertRegexpMatches(response.rendered_content, expected) urlpath = URLPath.create_urlpath( URLPath.root(), "testhistory2", title="TestHistory2", content="a page", user_message="Comment 2" ) expected = ( '(?s).*TestHistory2.*Comment 2.*' 'TestHistory1.*Comment 1.*' 'Root Article.*no log message.*' ) response = self.client.get(url) self.assertRegexpMatches(response.rendered_content, expected) response = self.client.get(url0) self.assertRegexpMatches(response.rendered_content, expected) response = self.client.get(url1) self.assertRegexpMatches(response.rendered_content, expected) response = self.client.post( reverse('wiki:edit', kwargs={'path': 'testhistory2/'}), {'content': 'a page modified', 'current_revision': str(urlpath.article.current_revision.id), 'preview': '0', 'save': '1', 'summary': 'Testing Revision', 'title': 'TestHistory2Mod'} ) expected = ( '(?s).*TestHistory2Mod.*Testing Revision.*' 'TestHistory2.*Comment 2.*' 'TestHistory1.*Comment 1.*' 'Root Article.*no log message.*' ) response = self.client.get(url) self.assertRegexpMatches(response.rendered_content, expected) response = self.client.get(url0) self.assertRegexpMatches(response.rendered_content, expected) expected = ( '(?s).*TestHistory2Mod.*Testing Revision.*' 'TestHistory1.*Comment 1.*' 'Root Article.*no log message.*' ) response = self.client.get(url1) self.assertRegexpMatches(response.rendered_content, expected)
def get(self, request, *args, **kwargs): auth_logout(request) messages.info(request, _(u"You are no longer logged in. Bye bye!")) return redirect("wiki:get", URLPath.root().path)