Skip to content

Commit

Permalink
fix missing sitelinks in item.write() (#764)
Browse files Browse the repository at this point in the history
* fix missing sitelinks in item.write()

* Fixing import order

---------

Co-authored-by: Myst <[email protected]>
  • Loading branch information
dabu1111 and LeMyst authored Sep 6, 2024
1 parent 5e566d9 commit dba6c97
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions wikibaseintegrator/entities/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def get_json(self) -> dict[str, str | dict]:
'labels': self.labels.get_json(),
'descriptions': self.descriptions.get_json(),
'aliases': self.aliases.get_json(),
'sitelinks': self.sitelinks.get_json(),
**super().get_json()
}

Expand Down
10 changes: 10 additions & 0 deletions wikibaseintegrator/models/sitelinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def set(self, site: str, title: str | None = None, badges: list[str] | None = No
self.sitelinks[site] = sitelink
return sitelink

def get_json(self) -> dict[str, dict]:
return {
sitelink:
{
'site': self.sitelinks[sitelink].site,
'title': self.sitelinks[sitelink].title,
'badges': self.sitelinks[sitelink].badges
} for sitelink in self.sitelinks
}

def from_json(self, json_data: dict[str, dict]) -> Sitelinks:
for sitelink in json_data:
self.set(site=json_data[sitelink]['site'], title=json_data[sitelink]['title'], badges=json_data[sitelink]['badges'])
Expand Down
8 changes: 7 additions & 1 deletion wikibaseintegrator/wbi_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from wikibaseintegrator.wbi_backoff import wbi_backoff
from wikibaseintegrator.wbi_config import config
from wikibaseintegrator.wbi_exceptions import MaxRetriesReachedException, ModificationFailed, MWApiError, NonExistentEntityError, SearchError
from wikibaseintegrator.wbi_exceptions import MaxRetriesReachedException, ModificationFailed, MWApiError, NonExistentEntityError, SaveFailed, SearchError

if TYPE_CHECKING:
from wikibaseintegrator.datatypes import BaseDataType
Expand Down Expand Up @@ -125,6 +125,12 @@ def mediawiki_api_call(method: str, mediawiki_api_url: str | None = None, sessio
if 'code' in json_data['error'] and json_data['error']['code'] == 'modification-failed': # pragma: no cover
raise ModificationFailed(json_data['error'])

# sitelink conflict
if ('code' in json_data['error'] and json_data['error']['code'] == 'failed-save' and
any([message.get('name', '') == 'wikibase-validator-sitelink-conflict'
for message in json_data['error'].get('messages', [])])):
raise SaveFailed(json_data['error'])

# others case
raise MWApiError(json_data['error'])

Expand Down

0 comments on commit dba6c97

Please sign in to comment.