Skip to content

Commit

Permalink
Merge pull request #20 from ukwhatn/develop
Browse files Browse the repository at this point in the history
Release v3.1.0dev3
  • Loading branch information
ukwhatn authored Sep 28, 2024
2 parents 1443937 + c882745 commit ee00236
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "wikidot"
version = "3.1.0dev2"
version = "3.1.0dev3"
authors = [{ name = "ukwhatn", email = "[email protected]" }]
description = "Wikidot Utility Library"
readme = "README.md"
Expand Down
7 changes: 7 additions & 0 deletions src/wikidot/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def __init__(self, message):
super().__init__(message)


class TargetExistsException(WikidotException):
"""対象が既に存在しているときの例外"""

def __init__(self, message):
super().__init__(message)


class TargetErrorException(WikidotException):
"""メソッドの対象としたオブジェクトに操作が適用できないときの例外"""

Expand Down
115 changes: 100 additions & 15 deletions src/wikidot/module/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def _parse(site: "Site", html_body: BeautifulSoup):

# レーティング方式を判定
is_5star_rating = (
page_element.select_one("span.rating span.page-rate-list-pages-start")
is not None
page_element.select_one("span.rating span.page-rate-list-pages-start")
is not None
)

# 各値を取得
Expand Down Expand Up @@ -179,17 +179,17 @@ def search_pages(site: "Site", query: SearchPagesQuery = SearchPagesQuery()):
query_dict = query.as_dict()
query_dict["moduleName"] = "list/ListPagesModule"
query_dict["module_body"] = (
'[[div class="page"]]\n'
+ "".join(
[
f'[[span class="set {key}"]]'
f'[[span class="name"]] {key} [[/span]]'
f'[[span class="value"]] %%{key}%% [[/span]]'
f"[[/span]]"
for key in DEFAULT_MODULE_BODY
]
)
+ "\n[[/div]]"
'[[div class="page"]]\n'
+ "".join(
[
f'[[span class="set {key}"]]'
f'[[span class="name"]] {key} [[/span]]'
f'[[span class="value"]] %%{key}%% [[/span]]'
f"[[/span]]"
for key in DEFAULT_MODULE_BODY
]
)
+ "\n[[/div]]"
)

try:
Expand Down Expand Up @@ -320,7 +320,7 @@ def _acquire_page_revisions(site: "Site", pages: list["Page"]):
revs = []
body_html = BeautifulSoup(body, "lxml")
for rev_element in body_html.select(
"table.page-history > tr[id^=revision-row-]"
"table.page-history > tr[id^=revision-row-]"
):
rev_id = int(rev_element["id"].removeprefix("revision-row-"))

Expand Down Expand Up @@ -545,7 +545,7 @@ def revisions(self) -> PageRevisionCollection["PageRevision"]:

@revisions.setter
def revisions(
self, value: list["PageRevision"] | PageRevisionCollection["PageRevision"]
self, value: list["PageRevision"] | PageRevisionCollection["PageRevision"]
):
self._revisions = value

Expand Down Expand Up @@ -629,3 +629,88 @@ def delete_meta(self, name: str):
}
]
)

@staticmethod
def create_or_edit(
site: "Site",
fullname: str,
page_id: int | None = None,
title: str = "",
source: str = "",
comment: str = "",
force_edit: bool = False,
raise_on_exists: bool = False,
):
site.client.login_check()

# ページロックを取得しにいく
page_lock_request_body = {
"mode": "page",
"wiki_page": fullname,
"moduleName": "edit/PageEditModule"
}
if force_edit:
page_lock_request_body["force_lock"] = "yes"

page_lock_response = site.amc_request([page_lock_request_body])[0]
page_lock_response_data = page_lock_response.json()

if "locked" in page_lock_response_data or "other_locks" in page_lock_response_data:
raise exceptions.TargetErrorException(
f"Page {fullname} is locked or other locks exist",
)

# ページが存在するか(page_revision_idがあるか)確認
is_exist = "page_revision_id" in page_lock_response_data

if raise_on_exists and is_exist:
raise exceptions.TargetExistsException(f"Page {fullname} already exists")

if is_exist and page_id is None:
raise ValueError("page_id must be specified when editing existing page")

# lock_idとlock_secret、page_revision_id(あれば)を取得
lock_id = page_lock_response_data["lock_id"]
lock_secret = page_lock_response_data["lock_secret"]
page_revision_id = page_lock_response_data.get("page_revision_id")

# ページの作成または編集
edit_request_body = {
"action": "WikiPageAction",
"event": "savePage",
"moduleName": "Empty",
"mode": "page",
"lock_id": lock_id,
"lock_secret": lock_secret,
"revision_id": page_revision_id if page_revision_id is not None else "",
"wiki_page": fullname,
"page_id": page_id if page_id is not None else "",
"title": title,
"source": source,
"comments": comment
}
response = site.amc_request([edit_request_body])[0]

return response.json()

def edit(
self,
title: str = None,
source: str = None,
comment: str = None,
force_edit: bool = False,
):
# Noneならそのままにする
title = title or self.title
source = source or self.source.wiki_text
comment = comment or ""

return Page.create_or_edit(
self.site,
self.fullname,
self.id,
title,
source,
comment,
force_edit,
)
33 changes: 33 additions & 0 deletions src/wikidot/module/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ def get(self, fullname: str, raise_when_not_found: bool = True) -> Optional["Pag
return None
return res[0]

def create(
self,
fullname: str,
title: str = "",
source: str = "",
comment: str = "",
force_edit: bool = False,
) -> None:
"""ページを作成する
Parameters
----------
fullname: str
ページのフルネーム
title: str
ページのタイトル
source: str
ページのソース
comment: str
コメント
force_edit: bool
ページが存在する場合に上書きするかどうか
"""
Page.create_or_edit(
site=self.site,
fullname=fullname,
title=title,
source=source,
comment=comment,
force_edit=force_edit,
raise_on_exists=True
)


@dataclass
class Site:
Expand Down

0 comments on commit ee00236

Please sign in to comment.