Skip to content

Commit eb9e0e1

Browse files
committed
fix(file): write the metadata onto the page the upload created
- store_entity saw the page the upload had just made and kept its empty content - pass overwrite='replace remote' when the file page did not exist beforehand - an already existing page keeps whatever policy the caller asked for
1 parent 4a09bf5 commit eb9e0e1

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/osw/controller/file/wiki.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ def assert_upload_success(result: Any, title: str, host: str) -> None:
6464
)
6565

6666

67+
def store_params_for_upload(
68+
se_params: Dict[str, Any], page_existed: bool
69+
) -> Dict[str, Any]:
70+
"""Picks the overwrite policy for the entity that accompanies an upload
71+
72+
An upload creates the file page when it was not there yet. store_entity
73+
would then see an existing page and, under the default 'keep existing',
74+
leave the metadata unwritten. So the entity has to replace what the upload
75+
put there. A page that was already there keeps whatever the caller asked
76+
for.
77+
"""
78+
if page_existed:
79+
return se_params
80+
return {**se_params, "overwrite": "replace remote"}
81+
82+
6783
class WikiFileController(model.WikiFile, RemoteFileController):
6884
"""File controller for wiki files"""
6985

@@ -190,6 +206,7 @@ def put(self, file: IO, **kwargs: Dict[str, Any]):
190206
# across the two writes, so one of them can be left standing. A file
191207
# page without metadata is visibly incomplete, while metadata without a
192208
# file looks like a valid entity until someone tries to download it.
209+
page_existed = self.osw.mw_site.pages[f"{self.namespace}:{self.title}"].exists
193210
try:
194211
result = self.osw.mw_site.upload(
195212
file=file,
@@ -205,7 +222,7 @@ def put(self, file: IO, **kwargs: Dict[str, Any]):
205222
OSW.StoreEntityParam(
206223
entities=[self.cast(model.WikiFile, **wf_params)],
207224
namespace=self.namespace,
208-
**se_params,
225+
**store_params_for_upload(se_params, page_existed),
209226
)
210227
)
211228

‎tests/test_wiki_file_upload_errors.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
assert_upload_success,
1616
get_allowed_file_extensions,
1717
reraise_upload_error,
18+
store_params_for_upload,
1819
)
1920

2021

@@ -109,6 +110,27 @@ def test_upload_without_success_raises(result):
109110
assert "wiki.example.org" in str(exc_info.value)
110111

111112

113+
def test_a_page_the_upload_created_gets_its_metadata_written():
114+
"""Otherwise store_entity keeps the empty page the upload just left behind."""
115+
assert store_params_for_upload({}, page_existed=False) == {
116+
"overwrite": "replace remote"
117+
}
118+
119+
120+
def test_an_existing_page_keeps_the_callers_overwrite_policy():
121+
se_params = {"overwrite": "keep existing", "edit_comment": "hi"}
122+
123+
assert store_params_for_upload(se_params, page_existed=True) == se_params
124+
125+
126+
def test_store_params_are_not_mutated():
127+
se_params = {"edit_comment": "hi"}
128+
129+
store_params_for_upload(se_params, page_existed=False)
130+
131+
assert se_params == {"edit_comment": "hi"}
132+
133+
112134
def test_warned_upload_reports_the_warnings():
113135
result = {"result": "Warning", "warnings": {"badfilename": "a_png"}}
114136

0 commit comments

Comments
 (0)