Skip to content

Commit

Permalink
Avoid duplicating crowdanki_uuid when cloning deck config
Browse files Browse the repository at this point in the history
Fix #118.

Testing, it seems that I somehow haven't broken the tests despite
fiddling with anki/hook_vendor without adding to the overrides.

Note that deck_conf_did_add_config is not available as a "legacy"
hook, so the new hook mechanism has to be used.  It might (or might
not) be worth converting the existing hooks to the new mechanism.
  • Loading branch information
aplaice committed Jul 3, 2021
1 parent b25eace commit 87d4864
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crowd_anki/anki/hook_vendor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from aqt import gui_hooks

from dataclasses import dataclass
from typing import Any

from ..config.config_settings import ConfigSettings
from ..anki.adapters.hook_manager import AnkiHookManager
from ..export.anki_exporter_wrapper import exporters_hook
from ..history.archiver_vendor import ArchiverVendor
from ..utils.deckconf import disambiguate_crowdanki_uuid


@dataclass
Expand All @@ -16,6 +19,7 @@ class HookVendor:
def setup_hooks(self):
self.setup_exporter_hook()
self.setup_snapshot_hooks()
self.setup_add_config_hook()

def setup_exporter_hook(self):
self.hook_manager.hook("exportersList", exporters_hook)
Expand All @@ -24,3 +28,6 @@ def setup_snapshot_hooks(self):
snapshot_handler = ArchiverVendor(self.window, self.config).snapshot_on_sync
self.hook_manager.hook('profileLoaded', snapshot_handler)
self.hook_manager.hook('unloadProfile', snapshot_handler)

def setup_add_config_hook(self):
gui_hooks.deck_conf_did_add_config.append(disambiguate_crowdanki_uuid)
10 changes: 10 additions & 0 deletions crowd_anki/utils/deckconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .constants import UUID_FIELD_NAME

def disambiguate_crowdanki_uuid(deck_conf, deck,
config, new_name,
new_conf_id):
new_deck_conf = deck_conf.mw.col.decks.get_config(new_conf_id)
if (new_deck_conf and (UUID_FIELD_NAME in new_deck_conf)):
# Delete rather than generating anew, (with uuid1()) to avoid code duplication.
del new_deck_conf[UUID_FIELD_NAME]
deck_conf.mw.col.decks.update_config(new_deck_conf)

0 comments on commit 87d4864

Please sign in to comment.