diff --git a/operacat/catalogitems/management/commands/load_items.py b/operacat/catalogitems/management/commands/load_items.py index d320f8e..ba1ae68 100644 --- a/operacat/catalogitems/management/commands/load_items.py +++ b/operacat/catalogitems/management/commands/load_items.py @@ -48,4 +48,4 @@ def handle(self, *args, **options): cur.title = n_item["item"] home.add_child(instance=cur) else: - self.stderr.write(str(n_item)) \ No newline at end of file + self.stderr.write(str(n_item)) diff --git a/operacat/catalogitems/management/commands/load_lot_info.py b/operacat/catalogitems/management/commands/load_lot_info.py index 6036623..24b9958 100644 --- a/operacat/catalogitems/management/commands/load_lot_info.py +++ b/operacat/catalogitems/management/commands/load_lot_info.py @@ -1,24 +1,42 @@ import json -from django.core.management.base import BaseCommand, CommandError -from os.path import dirname, join -from wagtail.wagtailcore.models import Page +from django.core.management.base import BaseCommand from catalogitems.models import CatalogItemPage, Dealer, Place class Command(BaseCommand): - help = "Add related item info from legacy data to new OperaCat website" + """a management command to set lot information from legacy data + + This class will retrieve lot information for each item in legacy data and add + it to the lot attribute of the corresponding item page + """ + help = "Add lot information from legacy data to item pages" def add_arguments(self, parser): + """the method that gets called to add parameter to the management command + + It takes a parser object and adds a string type argument called + legacy_data_filepath + """ parser.add_argument("legacy_data_filepath", help="Path to legacy data JSON", type=str) def handle(self, *args, **options): + """the method that gets called to actually run the management command + + It opens the legacy_data_filepath parameter and loads it into a JSON + object + + Then it iterates through the list of dicts in the data and selects + out the lot key:value. + + It then checks if there is a CatalogItemPage already present with that item + in the title, and if there is it defines the lot attribute of that CatalogItemPage + to be the value of the lot key:value pair from legacy data and saves the item page. + """ + data = json.load(open(options["legacy_data_filepath"], "r", encoding="utf-8")) - successful = open(join(dirname(options["legacy_data_filepath"]), - 'successful.txt'), "w") - home = Page.objects.filter(title="Home")[0] for n in data: cur = CatalogItemPage.objects.filter(title=n["item"])[0] if n.get("lot", None): diff --git a/operacat/catalogitems/management/commands/load_related_items.py b/operacat/catalogitems/management/commands/load_related_items.py index 1011537..5671de7 100644 --- a/operacat/catalogitems/management/commands/load_related_items.py +++ b/operacat/catalogitems/management/commands/load_related_items.py @@ -1,43 +1,72 @@ +import json from django.core.management.base import BaseCommand + from catalogitems.models import CatalogItemPage -import json -from os.path import dirname, join class Command(BaseCommand): + """a management command to set relationship information between items from legacy data + + This class will retrieve relationship information for each item in legacy data and add + it to the related_items attribute of the corresponding item page + """ + help = "Add related item info from legacy data to new OperaCat website" def add_arguments(self, parser): + """the method that gets called to add parameter to the management command + + It takes a parser object and adds a string type argument called + legacy_data_filepath + """ parser.add_argument("legacy_data_filepath", help="Path to legacy data JSON", type=str) + def _bundle_relation_statements(self, list_of_relations): + output = [] + for a_relation in list_of_relations: + related_item = CatalogItemPage.objects.filter(title=a_relation) + if related_item.count() == 1: + stream_value = {'type': 'related_item', 'value': related_item[0].id} + output.append(stream_value) + return output + def handle(self, *args, **options): + """the method that gets called to actually run the management command + + It opens the legacy_data_filepath parameter and loads it into a JSON + object + + Then it iterates through the list of dicts in the data and selects + out the 'related item' key:value. + + It then checks if there is a CatalogItemPage already present with that item + in the title, and if there is it defines the related_items.stream_data attribute of + that CatalogItemPage to be the value of the related_item key:value pair from + legacy data and saves the item page. + """ + data = json.load(open(options["legacy_data_filepath"], "r", encoding="utf-8")) - successful = open(join(dirname(options["legacy_data_filepath"]), 'successful.txt'), "w") counter = 0 total = 0 all_available = 0 - for n in data: + for n_item in data: all_available += 1 - current = CatalogItemPage.objects.filter(title=n["item"]) + current = CatalogItemPage.objects.filter(title=n_item["item"]) if current.count() == 1: - total += 1 - current = current[0] - if n.get("related items", None): - rels = [] - for g in n["related items"]: - match = CatalogItemPage.objects.filter(title=g) - if match.count() == 1: - match = match[0] - val = {'type': 'related_item', 'value': match.id} - rels.append(val) - - successful.write("{}\n".format(current.title)) - current.related_items.stream_data = rels - current.save() - counter += 1 - else: - self.stderr.write("{} has no related items".format(current.title)) + total += 1 + current = current[0] + if n_item.get("related items", None): + stream_data = self._bundle_relation_statements(n_item["related items"]) + current.related_items.stream_data = stream_data + current.save() + counter += 1 + else: + self.stderr.write("{} has no related items".format(current.title)) else: - self.stderr.write("{} has no corresponding catalog item page.".format(n["item"])) - self.stdout.write("{} records modified out of {} total potentially modifiable records from {} total records in legacy data".format(counter, total, all_available)) + self.stderr.write("{} has no corresponding catalog item page.".\ + format(n_item["item"])) + conclusion = "{} records modified out of {} total potentially".format(counter, total) +\ + "modifiable records from {}".format(all_available) +\ + "total records in legacy data\n" + self.stdout.write(conclusion) diff --git a/operacat/catalogitems/management/commands/load_simple_snippet_data.py b/operacat/catalogitems/management/commands/load_simple_snippet_data.py index da6907f..8669c39 100644 --- a/operacat/catalogitems/management/commands/load_simple_snippet_data.py +++ b/operacat/catalogitems/management/commands/load_simple_snippet_data.py @@ -1,50 +1,78 @@ -from django.core.management.base import BaseCommand, CommandError -from catalogitems.models import CatalogItemPage, Catalog, Dealer, Composer, PieceTitle import json -from os.path import dirname, join +from django.core.management.base import BaseCommand + +from catalogitems.models import CatalogItemPage, Catalog, Dealer, Composer, PieceTitle + class Command(BaseCommand): + """a management command to set relationship information between items from legacy data + + This class will retrieve composer, catalog, dealer and optional piece title info + for each item in legacy data and add each piece of info to the appropriate attribute + on the corresponding item page + """ help = "Add simple snippet info from legacy data to item pages" def add_arguments(self, parser): + """the method that gets called to add parameter to the management command + + It takes a parser object and adds a string type argument called + legacy_data_filepath + """ parser.add_argument("legacy_data_filepath", help="Path to legacy data JSON", type=str) + def _switch_case(self, condition, query_string, current_object): + def _build_title_query(query_param): + return PieceTitle.objects.filter(name=query_param) + + def _build_composer_query(query_param): + return Composer.objects.filter(last_name=query_param) + + def _build_catalog_query(query_param): + return Catalog.objects.filter(catalog_name=query_param) + + def _build_dealer_query(query_param): + return Dealer.objects.filter(dealer_name=query_param) + + if condition == 'composer': + query = _build_composer_query(query_string) + elif condition == 'title': + query = _build_title_query(query_string) + elif condition == 'dealer': + query = _build_dealer_query(query_string) + elif condition == 'catalog': + query = _build_catalog_query(query_string) + else: + raise ValueError("invalid condition in _switch_case method") + if query.count() == 1: + if condition != 'title': + setattr(current_object, condition, query[0]) + current_object.save() + else: + current_object.item_titles.create(a_title=query[0]) + def handle(self, *args, **options): + """the method that gets called to actually run the management command + + It opens the legacy_data_filepath parameter and loads it into a JSON + object + + Then it iterates through the list of dicts in the data and selects + out the composer, dealer, catalog, and title key:value pairs. + + It then checks if there is a CatalogItemPage already present with that item + in the title, and if there is it defines the relevant attributes for + that CatalogItemPage with the appropriate key:value pair values. + """ + data = json.load(open(options["legacy_data_filepath"], "r", encoding="utf-8")) - successful = open(join(dirname(options["legacy_data_filepath"]), - 'successful.txt'), "w") - counter = 0 - total = 0 - all_available = 0 - for n in data: - cur = CatalogItemPage.objects.filter(title=n["item"]) + for n_item in data: + cur = CatalogItemPage.objects.filter(title=n_item["item"]) if cur.count() == 1: - cur = cur[0] - if n.get("title"): - try: - matching_title = PieceTitle.objects.filter(name=n["title"]) - if matching_title.count() == 1: - cur.item_titles.create(a_title=matching_title[0]) - else: - self.stderr("{} could not be found in system.".format(n.get('title'))) - except KeyError: - self.stderr.write("{} piece title is not in system.".format(n["title"])) - matching_catalog = Catalog.objects.filter(catalog_name=n["catalog"]) - matching_dealer = Dealer.objects.filter(dealer_name=n["dealer"]) - try: - matching_composer = Composer.objects.filter(last_name=n["composer"]) - except KeyError: - self.stderr.write("{} has no composer".format(n["item"])) - if matching_catalog.count() == 1: - cur.item_catalog = matching_catalog[0] - if matching_dealer.count() == 1: - cur.item_dealer = matching_dealer[0] - if matching_composer.count() == 1: - cur.item_composer = matching_composer[0] - cur.save() + for n_condition in ['title', 'composer', 'catalog', 'dealer']: + self._switch_case(n_condition, n_item.get(n_condition, 'None'), cur[0]) else: - self.stderr.write("{} has no catalog item page".format(n["item"])) - \ No newline at end of file + self.stderr.write("{} has no catalog item page".format(n_item["item"])) diff --git a/operacat/catalogitems/management/commands/load_summary_info.py b/operacat/catalogitems/management/commands/load_summary_info.py index 27b523a..3c3fbc5 100644 --- a/operacat/catalogitems/management/commands/load_summary_info.py +++ b/operacat/catalogitems/management/commands/load_summary_info.py @@ -1,31 +1,55 @@ -from django.core.management.base import BaseCommand, CommandError +import json +from django.core.management.base import BaseCommand + from catalogitems.models import CatalogItemPage, Place, Dealer,\ Composer, AuthorOrResponsible, RecipientOrDedicatee -import json -from os.path import dirname, join + class Command(BaseCommand): + """a management command to set relationship information between items from legacy data + + This class will retrieve item descriptoiun, and optional field note info + for each item in legacy data and add each piece of info to the appropriate attribute + on the corresponding item page + """ + help = "Add item description and field notes from legacy data to item pages" def add_arguments(self, parser): + """the method that gets called to add parameter to the management command + + It takes a parser object and adds a string type argument called + legacy_data_filepath + """ parser.add_argument("legacy_data_filepath", help="Path to legacy data JSON", type=str) def handle(self, *args, **options): + """the method that gets called to actually run the management command + + It opens the legacy_data_filepath parameter and loads it into a JSON + object + + Then it iterates through the list of dicts in the data and selects + out the field note, and item description key:value pairs. + + It then checks if there is a CatalogItemPage already present with that item + in the title, and if there is it defines the relevant attributes for + that CatalogItemPage with the appropriate key:value pair values. + """ data = json.load(open(options["legacy_data_filepath"], "r", encoding="utf-8")) - for n in data: - cur = CatalogItemPage.objects.filter(title=n["item"]) + for n_item in data: + cur = CatalogItemPage.objects.filter(title=n_item["item"]) if cur.count() == 1: - cur = cur[0] - if n.get("item description", None): - val = n["item description"] - cur.item_description = "
" + val.strip() + "
" - else: - self.stderr.write("{} has no item description".format(n["item"])) - if n.get("item notes", None): - - val = n["item notes"] - cur.field_notes = "" + val.strip() + "
" - cur.save() + cur = cur[0] + if n_item.get("item description", None): + val = n_item["item description"] + cur.item_description = "" + val.strip() + "
" + else: + self.stderr.write("{} has no item description".format(n_item["item"])) + if n_item.get("item notes", None): + val = n_item["item notes"] + cur.field_notes = "" + val.strip() + "
" + cur.save()