Skip to content

Commit

Permalink
Merge pull request #135 from aplaice/disambiguate_cloned_deckconf
Browse files Browse the repository at this point in the history
Avoid duplicating crowdanki_uuid when cloning deck config
  • Loading branch information
aplaice authored Dec 11, 2021
2 parents 5f79ca9 + 4306b59 commit 19cfef2
Show file tree
Hide file tree
Showing 2 changed files with 21 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)
14 changes: 14 additions & 0 deletions crowd_anki/utils/deckconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Note that this only works for Anki ≤ 2.1.45, or when using the old
# deck options popup with Anki 2.1.46+. Anki 2.1.46+ has a new
# JavaScript-based options popup (with no hooks), by default.

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 19cfef2

Please sign in to comment.