Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion e2e/e2e_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,34 @@ def test_create_in_folder(page: Page, login):
create.click()
page.wait_for_url("http://localhost:8080/edit/en/")

expect(page.locator("[name=page]")).to_have_value("en/")
expect(page.locator("[name=page-language]")).to_have_value("en")
expect(page.locator("[name=page-name]")).to_have_value("")
expect(page.locator("#create-hint")).to_be_visible()


def test_create_page_with_picker(page: Page, login):
"""Check that the language and name picked on the create form are used."""
page.goto("http://localhost:8080/edit/en/")

page.locator("[name=page-language]").select_option("de")
page.locator("[name=page-name]").fill("Picked")
page.locator("[name=content]").fill("Picked page")
with page.expect_navigation():
page.locator("[name=save]").click()
page.wait_for_url("http://localhost:8080/de/Picked")

expect(page.locator("text=Picked page")).to_be_visible()


def test_create_page_picker_types(page: Page, login):
"""Check that File can only be picked when creating a file."""
page.goto("http://localhost:8080/edit/en/")
expect(page.locator("[name=page-namespace] option")).to_have_text(["Page", "Template", "Category"])

page.goto("http://localhost:8080/edit/File/en/Picker.png")
expect(page.locator("[name=page-namespace] option")).to_have_text(["File"])


def test_create_in_folder_without_login(page: Page):
"""Check that the create-in-folder link is hidden when not logged in."""
page.goto("http://localhost:8080/en/Main%20Page")
Expand Down
8 changes: 5 additions & 3 deletions e2e/e2e_namespace_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_category_cannot_edit_root(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/Category/en/")

page.locator("[name=page]").fill("Category/en/Main Page")
page.locator("[name=page-name]").fill("Main Page")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand All @@ -99,7 +99,8 @@ def test_category_edit_missing_language(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/Category/en/")

page.locator("[name=page]").fill("Category/test")
page.locator("[name=page-language]").evaluate("(el, v) => { el.add(new Option(v, v)); el.value = v; }", "")
page.locator("[name=page-name]").fill("test")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand All @@ -118,7 +119,8 @@ def test_category_edit_invalid_language(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/Category/en/")

page.locator("[name=page]").fill("Category/zz/test")
page.locator("[name=page-language]").evaluate("(el, v) => { el.add(new Option(v, v)); el.value = v; }", "zz")
page.locator("[name=page-name]").fill("test")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand Down
10 changes: 6 additions & 4 deletions e2e/e2e_namespace_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def test_file_cannot_edit_root(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/File/en/Test2.png")

page.locator("[name=page]").fill("File/en/Testing/Main Page")
page.locator("[name=page-name]").fill("Testing/Main Page")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand All @@ -440,7 +440,7 @@ def test_file_cannot_edit_language(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/File/en/Test2.png")

page.locator("[name=page]").fill("File/en/Main Page")
page.locator("[name=page-name]").fill("Main Page")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand All @@ -461,7 +461,8 @@ def test_file_edit_missing_language(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/File/en/Test2.png")

page.locator("[name=page]").fill("File/test")
page.locator("[name=page-language]").evaluate("(el, v) => { el.add(new Option(v, v)); el.value = v; }", "")
page.locator("[name=page-name]").fill("test")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand All @@ -480,7 +481,8 @@ def test_file_edit_invalid_language(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/File/en/Test2.png")

page.locator("[name=page]").fill("File/zz/test")
page.locator("[name=page-language]").evaluate("(el, v) => { el.add(new Option(v, v)); el.value = v; }", "zz")
page.locator("[name=page-name]").fill("test")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand Down
8 changes: 5 additions & 3 deletions e2e/e2e_namespace_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_template_cannot_edit_root(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/Template/en/")

page.locator("[name=page]").fill("Template/en/Main Page")
page.locator("[name=page-name]").fill("Main Page")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand All @@ -161,7 +161,8 @@ def test_template_edit_missing_language(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/Template/en/")

page.locator("[name=page]").fill("Template/test")
page.locator("[name=page-language]").evaluate("(el, v) => { el.add(new Option(v, v)); el.value = v; }", "")
page.locator("[name=page-name]").fill("test")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand All @@ -180,7 +181,8 @@ def test_template_edit_invalid_language(page: Page, login):
edit.click()
page.wait_for_url("http://localhost:8080/edit/Template/en/")

page.locator("[name=page]").fill("Template/zz/test")
page.locator("[name=page-language]").evaluate("(el, v) => { el.add(new Option(v, v)); el.value = v; }", "zz")
page.locator("[name=page-name]").fill("test")
page.locator("[name=content]").fill("Some text")
with page.expect_navigation():
page.locator("[name=save]").click()
Expand Down
21 changes: 21 additions & 0 deletions static/truewiki/truewiki.css
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,27 @@ h6:hover .anchor {
font-size: 80%;
margin: 0 0 4px 0;
}
#page-picker {
align-items: flex-end;
display: flex;
flex-wrap: wrap;
gap: 4px 6px;
margin-bottom: 4px;
}
#page-picker label {
color: #555;
display: flex;
flex-direction: column;
font-size: 12px;
}
#page-picker select, #page-picker input {
box-sizing: border-box;
font-size: 14px;
height: 26px;
}
#page-picker-name {
flex: 1 1 240px;
}
#categories {
float: left;
}
Expand Down
6 changes: 5 additions & 1 deletion templates/Edit.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,21 @@
<h3>Edit page</h3>
|
<h3>Create new page</h3>
<p id="create-hint">Tip: to add sub-pages to this page later, end its name with "/Main Page", like "en/Bridges/Main Page".</p>
<p id="create-hint">Tip: to add sub-pages to this page later, end its name with "/Main Page", like "Bridges/Main Page".</p>
}}

<form method="post" accept-charset="utf-8" enctype="multipart/form-data">

{{#if:{{{does_exist|}}}|
{{#if:{{{has_used_on_pages|}}}|
<input type="text" value="{{{new_page}}}" style="width: 100%;" disabled />
<span id="no-rename">Pages cannot be renamed if they are used by other pages. Still want to rename? Create a <a href="{{{repository_url}}}/pulls" target="_new">Pull Request</a> or <a href="{{{repository_url}}}/issues" target="_new">Issue</a>.</span>
|
<input type="text" name="page" value="{{{new_page}}}" style="width: 100%;" />
}}
|
{{page_picker}}
}}

<textarea name="content" style="width: 100%;" rows=30>{{page}}</textarea>
<input type="text" name="summary" maxlength="500" placeholder="Edit Summary (Optional)..." value="{{summary_text}}" style="width: 100%;" />
Expand Down
61 changes: 61 additions & 0 deletions truewiki/page_picker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import html

from . import metadata

NAMESPACES = ("Page", "Template", "Category", "File")


def split(page: str) -> tuple:
"""Split a page name into its namespace, language and name."""
parts = page.split("/")
namespace = "Page"
if parts[0] in NAMESPACES[1:]:
namespace = parts.pop(0)

language = parts[0] if parts else ""
name = "/".join(parts[1:])
return namespace, language, name


def join(namespace: str, language: str, name: str) -> str:
"""Join a namespace, language and name into a page name."""
if namespace not in NAMESPACES[1:]:
return f"{language}/{name}"
return f"{namespace}/{language}/{name}"


def _options(values, selected: str) -> str:
options = []
for value in values:
value = html.escape(value)
attribute = ' selected="selected"' if value == selected else ""
options.append(f'<option value="{value}"{attribute}>{value}</option>')
return "".join(options)


def render(page: str, is_file: bool) -> str:
"""HTML form fields to pick the namespace, language and name of a new page."""
namespace, language, name = split(page)
choices = ("File",) if is_file else NAMESPACES[:3]

languages = set(metadata.LANGUAGES)
if language:
languages.add(language)

namespaces = _options(choices, namespace)
languages = _options(sorted(languages), html.escape(language))
name = html.escape(name)

return (
'<div id="page-picker">'
'<label for="page-namespace">Type '
f'<select id="page-namespace" name="page-namespace">{namespaces}</select>'
"</label>"
'<label for="page-language">Language '
f'<select id="page-language" name="page-language">{languages}</select>'
"</label>"
'<label for="page-name" id="page-picker-name">'
f'Name <input type="text" id="page-name" name="page-name" value="{name}" />'
"</label>"
"</div>"
)
6 changes: 5 additions & 1 deletion truewiki/views/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from wikitexthtml.render import wikilink

from . import error
from .. import singleton
from .. import (
page_picker,
singleton,
)
from ..content import breadcrumb
from ..wiki_page import (
NAMESPACE_MAPPING,
Expand Down Expand Up @@ -75,6 +78,7 @@ def create_body(
"errors": "\n".join(errors),
"breadcrumbs": breadcrumb.create(wiki_page.page),
"namespace_edit": wiki_page.add_edit_content() if new_page else "",
"page_picker": page_picker.render(new_page, wiki_page.page.startswith("File/")) if new_page else "",
"summary_text": summary if summary else "",
"edit_nonce": singleton.STORAGE.get_file_nonce(ondisk_name),
}
Expand Down
8 changes: 7 additions & 1 deletion truewiki/web_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from . import (
config,
page_picker,
singleton,
)
from .views import (
Expand Down Expand Up @@ -215,7 +216,12 @@ async def edit_page_post(request):
raise web.HTTPBadRequest(reason="Summary exceeds maximum length of 500 characters.")

# Make sure that page names don't contain /../ or anything silly.
new_page = payload.get("page", page)
if "page-language" in payload:
new_page = page_picker.join(
payload.get("page-namespace", ""), payload["page-language"], payload.get("page-name", "")
)
else:
new_page = payload.get("page", page)
filename = posixpath.basename(new_page)
path = posixpath.normpath(posixpath.dirname(new_page))
new_page = f"{path}/{filename}"
Expand Down