Catch DeckIsFilteredError directly on frontend

This commit is contained in:
RumovZ 2021-02-26 11:32:40 +01:00
parent ef925a88d6
commit 92cbf168f6
6 changed files with 23 additions and 35 deletions

View file

@ -12,7 +12,7 @@ from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
import anki # pylint: disable=unused-import import anki # pylint: disable=unused-import
import anki._backend.backend_pb2 as _pb import anki._backend.backend_pb2 as _pb
from anki.consts import * from anki.consts import *
from anki.errors import DeckIsFilteredError, DeckRenameError, NotFoundError from anki.errors import NotFoundError
from anki.utils import from_json_bytes, ids2str, intTime, to_json_bytes from anki.utils import from_json_bytes, ids2str, intTime, to_json_bytes
# public exports # public exports
@ -246,12 +246,9 @@ class DeckManager:
def update(self, g: Deck, preserve_usn: bool = True) -> None: def update(self, g: Deck, preserve_usn: bool = True) -> None:
"Add or update an existing deck. Used for syncing and merging." "Add or update an existing deck. Used for syncing and merging."
try:
g["id"] = self.col._backend.add_or_update_deck_legacy( g["id"] = self.col._backend.add_or_update_deck_legacy(
deck=to_json_bytes(g), preserve_usn_and_mtime=preserve_usn deck=to_json_bytes(g), preserve_usn_and_mtime=preserve_usn
) )
except DeckIsFilteredError as exc:
raise DeckRenameError("deck was filtered") from exc
def rename(self, g: Deck, newName: str) -> None: def rename(self, g: Deck, newName: str) -> None:
"Rename deck prefix to NAME if not exists. Updates children." "Rename deck prefix to NAME if not exists. Updates children."

View file

@ -9,7 +9,7 @@ from typing import Any
import aqt import aqt
from anki.decks import DeckTreeNode from anki.decks import DeckTreeNode
from anki.errors import DeckRenameError from anki.errors import DeckIsFilteredError
from anki.utils import intTime from anki.utils import intTime
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
@ -21,8 +21,8 @@ from aqt.utils import (
getOnlyText, getOnlyText,
openLink, openLink,
shortcut, shortcut,
show_rename_deck_error,
showInfo, showInfo,
showWarning,
tr, tr,
) )
@ -268,8 +268,8 @@ class DeckBrowser:
try: try:
self.mw.col.decks.rename(deck, newName) self.mw.col.decks.rename(deck, newName)
gui_hooks.sidebar_should_refresh_decks() gui_hooks.sidebar_should_refresh_decks()
except DeckRenameError as err: except DeckIsFilteredError as err:
show_rename_deck_error(err) showWarning(str(err))
return return
self.show() self.show()
@ -353,8 +353,8 @@ class DeckBrowser:
if deck: if deck:
try: try:
self.mw.col.decks.id(deck) self.mw.col.decks.id(deck)
except DeckRenameError as err: except DeckIsFilteredError as err:
show_rename_deck_error(err) showWarning(str(err))
return return
gui_hooks.sidebar_should_refresh_decks() gui_hooks.sidebar_should_refresh_decks()
self.refresh() self.refresh()

View file

@ -4,8 +4,8 @@ from typing import Callable, List, Optional
import aqt import aqt
from anki.collection import SearchNode from anki.collection import SearchNode
from anki.decks import Deck, DeckRenameError from anki.decks import Deck
from anki.errors import InvalidInput from anki.errors import DeckIsFilteredError, InvalidInput
from anki.lang import without_unicode_isolation from anki.lang import without_unicode_isolation
from aqt import AnkiQt, colors, gui_hooks from aqt import AnkiQt, colors, gui_hooks
from aqt.qt import * from aqt.qt import *
@ -19,7 +19,6 @@ from aqt.utils import (
restoreGeom, restoreGeom,
saveGeom, saveGeom,
show_invalid_search_error, show_invalid_search_error,
show_rename_deck_error,
showWarning, showWarning,
tr, tr,
) )
@ -244,8 +243,8 @@ class DeckConf(QDialog):
self.saveConf() self.saveConf()
except InvalidInput as err: except InvalidInput as err:
show_invalid_search_error(err) show_invalid_search_error(err)
except DeckRenameError as err: except DeckIsFilteredError as err:
show_rename_deck_error(err) showWarning(str(err))
else: else:
if not self.mw.col.sched.rebuild_filtered_deck(self.deck["id"]): if not self.mw.col.sched.rebuild_filtered_deck(self.deck["id"]):
if askUser(tr(TR.DECKS_THE_PROVIDED_SEARCH_DID_NOT_MATCH)): if askUser(tr(TR.DECKS_THE_PROVIDED_SEARCH_DID_NOT_MATCH)):

View file

@ -10,7 +10,7 @@ from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, cast
import aqt import aqt
from anki.collection import Config, SearchNode from anki.collection import Config, SearchNode
from anki.decks import DeckTreeNode from anki.decks import DeckTreeNode
from anki.errors import DeckRenameError, InvalidInput from anki.errors import DeckIsFilteredError, InvalidInput
from anki.tags import TagTreeNode from anki.tags import TagTreeNode
from anki.types import assert_exhaustive from anki.types import assert_exhaustive
from aqt import colors, gui_hooks from aqt import colors, gui_hooks
@ -23,8 +23,8 @@ from aqt.utils import (
askUser, askUser,
getOnlyText, getOnlyText,
show_invalid_search_error, show_invalid_search_error,
show_rename_deck_error,
showInfo, showInfo,
showWarning,
tr, tr,
) )
@ -993,8 +993,8 @@ class SidebarTreeView(QTreeView):
self.mw.checkpoint(tr(TR.ACTIONS_RENAME_DECK)) self.mw.checkpoint(tr(TR.ACTIONS_RENAME_DECK))
try: try:
self.mw.col.decks.rename(deck, new_name) self.mw.col.decks.rename(deck, new_name)
except DeckRenameError as err: except DeckIsFilteredError as err:
show_rename_deck_error(err) showWarning(str(err))
return return
self.refresh() self.refresh()
self.mw.deckBrowser.refresh() self.mw.deckBrowser.refresh()

View file

@ -4,7 +4,7 @@
from typing import List, Optional from typing import List, Optional
import aqt import aqt
from anki.decks import DeckRenameError from anki.errors import DeckIsFilteredError
from aqt import gui_hooks from aqt import gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
@ -17,8 +17,8 @@ from aqt.utils import (
restoreGeom, restoreGeom,
saveGeom, saveGeom,
shortcut, shortcut,
show_rename_deck_error,
showInfo, showInfo,
showWarning,
tr, tr,
) )
@ -168,8 +168,8 @@ class StudyDeck(QDialog):
if n: if n:
try: try:
did = self.mw.col.decks.id(n) did = self.mw.col.decks.id(n)
except DeckRenameError as err: except DeckIsFilteredError as err:
show_rename_deck_error(err) showWarning(str(err))
return return
# deck name may not be the same as user input. ex: ", :: # deck name may not be the same as user input. ex: ", ::
self.name = self.mw.col.decks.name(did) self.name = self.mw.col.decks.name(did)

View file

@ -36,7 +36,7 @@ from PyQt5.QtWidgets import (
import anki import anki
import aqt import aqt
from anki import Collection from anki import Collection
from anki.errors import DeckRenameError, InvalidInput from anki.errors import InvalidInput
from anki.lang import TR # pylint: disable=unused-import from anki.lang import TR # pylint: disable=unused-import
from anki.utils import invalidFilename, isMac, isWin, noBundledLibs, versionWithBuild from anki.utils import invalidFilename, isMac, isWin, noBundledLibs, versionWithBuild
from aqt.qt import * from aqt.qt import *
@ -146,14 +146,6 @@ def show_invalid_search_error(err: Exception) -> None:
showWarning(text) showWarning(text)
def show_rename_deck_error(err: DeckRenameError) -> None:
if err.description == "deck was filtered":
reason = tr(TR.ERRORS_REASON_FILTERED_PARENT)
else:
reason = "unknown reason."
showWarning(tr(TR.ERRORS_INVALID_DECK_NAME, reason=reason))
def showInfo( def showInfo(
text: str, text: str,
parent: Union[Literal[False], QDialog] = False, parent: Union[Literal[False], QDialog] = False,