-
Notifications
You must be signed in to change notification settings - Fork 29
AnkiEDN maintener request: add an auto-protect feature for learned cards #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dev-Zaius
wants to merge
3
commits into
CravingCrates:main
Choose a base branch
from
Dev-Zaius:feature/auto-protect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from aqt import mw | ||
| from typing import Literal | ||
| import aqt.utils | ||
| from anki.cards import Card | ||
|
|
||
| from .var_defs import PREFIX_PROTECTED_FIELDS | ||
| from .utils import get_logger | ||
|
|
||
| logger = get_logger("ankicollab.auto_protect") | ||
|
|
||
| AUTO_PROTECT_TAG = f"{PREFIX_PROTECTED_FIELDS}::All" | ||
|
|
||
|
|
||
| def _is_auto_protect_on_review_enabled() -> bool: | ||
| config = mw.addonManager.getConfig(__name__) | ||
| if not config: | ||
| return False | ||
| return bool(config.get("settings", {}).get("auto_protect_on_review", False)) | ||
|
|
||
|
|
||
| def _is_auto_protect_learned_enabled() -> bool: | ||
| config = mw.addonManager.getConfig(__name__) | ||
| if not config: | ||
| return False | ||
| return bool(config.get("settings", {}).get("auto_protect_learned", False)) | ||
|
|
||
|
|
||
| def on_card_reviewed(reviewer, card: Card, ease: Literal[1, 2, 3, 4]) -> None: | ||
| if not _is_auto_protect_on_review_enabled(): | ||
| return | ||
| if card.type != 2: | ||
| return | ||
|
|
||
| note = card.note() | ||
| if any(t.lower() == AUTO_PROTECT_TAG.lower() for t in note.tags): | ||
| return | ||
|
|
||
| note.tags.append(AUTO_PROTECT_TAG) | ||
| try: | ||
| undo_id = mw.col.add_custom_undo_entry("Auto-protect note") | ||
| mw.col.update_note(note) | ||
| mw.col.merge_undo_entries(undo_id) | ||
| logger.info(f"Auto-protected note {note.id}") | ||
| except Exception as e: | ||
| logger.error(f"Failed to auto-protect note {note.id}: {e}") | ||
|
|
||
|
|
||
| def protect_all_learned() -> None: | ||
| if not mw.col: | ||
| aqt.utils.showInfo("Collection not available.") | ||
| return | ||
|
|
||
| def _task(): | ||
| card_ids = mw.col.find_cards("is:review") | ||
| note_ids = set() | ||
| for cid in card_ids: | ||
| card = mw.col.get_card(cid) | ||
| note_ids.add(card.nid) | ||
|
|
||
| notes_to_update = [] | ||
| for nid in note_ids: | ||
| note = mw.col.get_note(nid) | ||
| if not any(t.lower() == AUTO_PROTECT_TAG.lower() for t in note.tags): | ||
| note.tags.append(AUTO_PROTECT_TAG) | ||
| notes_to_update.append(note) | ||
|
|
||
| if notes_to_update: | ||
| undo_id = mw.col.add_custom_undo_entry("Protect all learned notes") | ||
| mw.col.update_notes(notes_to_update) | ||
| mw.col.merge_undo_entries(undo_id) | ||
|
|
||
| return len(notes_to_update) | ||
|
|
||
| def _on_done(future): | ||
| try: | ||
| count = future.result() | ||
| aqt.utils.showInfo( | ||
| f"{count} notes protected with {AUTO_PROTECT_TAG}.\n" | ||
| f"the notes won't be erased with new card update." | ||
| ) | ||
| except Exception as e: | ||
| aqt.utils.showInfo(f"Error : {e}") | ||
| logger.exception("Error in protect_all_learned") | ||
|
|
||
| mw.taskman.with_progress( | ||
| task=_task, | ||
| on_done=_on_done, | ||
| label="Protecting learned card", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.