From 8a35f6b54c89106aaeb75c9050f7c3a239c6fa2e Mon Sep 17 00:00:00 2001 From: Vladyslav Sitalo Date: Sun, 5 Jul 2020 16:20:01 -0700 Subject: [PATCH] Deck save_to_collection iterate on naming --- crowd_anki/representation/deck.py | 38 ++++++++++++++++--------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/crowd_anki/representation/deck.py b/crowd_anki/representation/deck.py index 77bbf5c..02570a9 100644 --- a/crowd_anki/representation/deck.py +++ b/crowd_anki/representation/deck.py @@ -1,5 +1,4 @@ from collections import namedtuple, defaultdict - from typing import Callable, Any, Iterable from .deck_config import DeckConfig @@ -127,52 +126,55 @@ def _load_metadata_from_json(self, json_dict): self.metadata = DeckMetadata(new_deck_configs, new_models) def save_to_collection(self, collection, import_config: ImportConfig): + self.save_metadata(collection) + + self.save_decks_and_notes(collection=collection, + parent_name="", + model_map_cache=defaultdict(dict), + import_config=import_config) + + def save_metadata(self, collection): for config in self.metadata.deck_configs.values(): config.save_to_collection(collection) for note_model in self.metadata.models.values(): note_model.save_to_collection(collection) - self.save_children_and_notes(collection=collection, - import_config=import_config, - parent_name="", - model_map_cache=defaultdict(dict)) - - def save_children_and_notes(self, collection, import_config: ImportConfig, parent_name, model_map_cache): - name = self._save_deck(collection, parent_name) + def save_decks_and_notes(self, collection, parent_name, model_map_cache, import_config: ImportConfig): + full_name = self._save_deck(collection, parent_name) for child in self.children: - child.save_children_and_notes(collection=collection, - import_config=import_config, - parent_name=name, - model_map_cache=model_map_cache) + child.save_decks_and_notes(collection=collection, + parent_name=full_name, + model_map_cache=model_map_cache, + import_config=import_config) if import_config.use_notes: for note in self.notes: note.save_to_collection(collection, self, model_map_cache, import_config=import_config) def _save_deck(self, collection, parent_name): - name = (parent_name + self.DECK_NAME_DELIMITER if parent_name else "") + self.anki_dict["name"] + full_name = (parent_name + self.DECK_NAME_DELIMITER if parent_name else "") + self.anki_dict["name"] deck_dict = UuidFetcher(collection).get_deck(self.get_uuid()) - deck_id = collection.decks.id(name, create=False) + deck_id = collection.decks.id(full_name, create=False) if deck_id and (not deck_dict or deck_dict["id"] != deck_id): - name = self._rename_deck(name, collection) + full_name = self._rename_deck(full_name, collection) if not deck_dict: - new_deck_id = collection.decks.id(name) + new_deck_id = collection.decks.id(full_name) deck_dict = collection.decks.get(new_deck_id) deck_dict.update(self.anki_dict) self.anki_dict = deck_dict - self.anki_dict["name"] = name + self.anki_dict["name"] = full_name self.anki_dict["conf"] = self.metadata.deck_configs[self.deck_config_uuid].anki_dict["id"] collection.decks.save() collection.decks.flush() - return name + return full_name @staticmethod def _rename_deck(initial_name, collection):