From 87915c0e8590e09faa915dcc6d6ed0c67e639d70 Mon Sep 17 00:00:00 2001 From: verbalhanglider Date: Fri, 26 May 2017 12:45:19 -0500 Subject: [PATCH] issue #37; more docstrings in management commands --- .../commands/link_images_with_items.py | 4 +- .../commands/load_complicated_snippets.py | 30 +++- .../management/commands/load_date_data.py | 139 +++++++++++------- .../management/commands/load_items.py | 38 +++-- 4 files changed, 142 insertions(+), 69 deletions(-) diff --git a/operacat/catalogitems/management/commands/link_images_with_items.py b/operacat/catalogitems/management/commands/link_images_with_items.py index 049fd37..69595ae 100644 --- a/operacat/catalogitems/management/commands/link_images_with_items.py +++ b/operacat/catalogitems/management/commands/link_images_with_items.py @@ -1,8 +1,8 @@ import json from django.core.management.base import BaseCommand -from operacat.catalogimage.models import CatalogImage -from operacat.catalogitems.models import CatalogItemPage +from catalogimage.models import CatalogImage +from catalogitems.models import CatalogItemPage class Command(BaseCommand): """a management command to interpret legacy data and link images to items diff --git a/operacat/catalogitems/management/commands/load_complicated_snippets.py b/operacat/catalogitems/management/commands/load_complicated_snippets.py index 1d47eee..2c7cb1a 100644 --- a/operacat/catalogitems/management/commands/load_complicated_snippets.py +++ b/operacat/catalogitems/management/commands/load_complicated_snippets.py @@ -15,7 +15,7 @@ class Command(BaseCommand): help = "Add m2m relation snippet information from legacy data to pages" def add_arguments(self, parser): - """the method that gets called to add parameter to the management command + """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 @@ -46,9 +46,10 @@ def _add_recipients(self, rec_list, cur): rec_list = list of dicts cur = an instance of CatalogItemPage - This method iterates over a list of recipient dicts, finds the matching + This method iterates over a list of recipient dicts, finds the matching RecipientOrDedicatee snippet in the system and adds the - RecipientOrDedicatee instance to the item_places attribute of the relevant CatalogItemPage object + RecipientOrDedicatee instance to the item_recipientordedicatee attribute of the + relevant CatalogItemPage object """ for recipient in rec_list: re_name = recipient["name"] @@ -60,6 +61,17 @@ def _add_recipients(self, rec_list, cur): return cur def _add_authors(self, author_list, cur): + """a method to iterate a list of recipients and add them to an item page + + rec_list = list of dicts + cur = an instance of CatalogItemPage + + This method iterates over a list of recipient dicts, finds the matching + AuthorOrResponsible snippet in the system and adds the + AuthorOrResponsible instance to the item_authororresposibles attribute of the + relevant CatalogItemPage object + """ + for author in author_list: au_name = author["name"] au_record = AuthorOrResponsible.objects.\ @@ -70,6 +82,18 @@ def _add_authors(self, author_list, cur): return cur def _add_item_types(self, type_list, cur): + """a method to iterate a list of recipients and add them to an item page + + type_list = list of strings + cur = an instance of CatalogItemPage + + This method iterates over a list of type names, finds the matching + Itemtyupe snippet in the system and adds the + ItemType instance to the item_types attribute of the + relevant CatalogItemPage object + """ + + for a_type in type_list: ty_name = a_type ty_record = ItemType.objects.filter(type_name=ty_name) diff --git a/operacat/catalogitems/management/commands/load_date_data.py b/operacat/catalogitems/management/commands/load_date_data.py index 2af7fc2..9cad64d 100644 --- a/operacat/catalogitems/management/commands/load_date_data.py +++ b/operacat/catalogitems/management/commands/load_date_data.py @@ -1,76 +1,107 @@ import json -from os.path import dirname, join from django.core.management.base import BaseCommand + from catalogitems.models import CatalogItemPage class Command(BaseCommand): + """a management command to migrate date field definition from legacy data + + This class is necessary for adding all dates -- date_labels, date, + start_date, and end_date -- to item pages in the system + """ + help = "Add date information from legacy data to relevant 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 _extract_date_string_parts(self, some_string): + val_parts = some_string.split('/') + if len(val_parts) == 3: + return val_parts + else: + return None + + def _extract_date_parts(self, date_blob): + def _iterate_a_list_of_values(a_list, type_string): + def _build_stream_value(type_string, value): + def _make_date_entry(value): + return {'year': value[2], 'month': value[1], + 'day': value[0] if len(str(value[0])) == 2 else str(value[0]).zfill(1)} + output = {'type': type_string} + if type_string in ['date', 'end_date', 'starrt_date']: + output['value'] = _make_date_entry(value) + else: + output['value'] = value + return output + + output = [] + for an_item in a_list: + output.append(_build_stream_value(type_string, an_item)) + return output + + date_labels = date_blob["date info"].get("date_label", None) + end_dates = date_blob["date info"].get("end date", None) + start_dates = date_blob["date info"].get("start date", None) + dates = date_blob["date info"].get("date", None) + final_output = [] + if date_labels: + final_output += _iterate_a_list_of_values(date_labels, 'date_label') + else: + a_val = {'type': 'date_label', + 'value': {'date_label': 's.d.'}} + final_output.append(a_val) + if dates: + final_output += _iterate_a_list_of_values(dates, 'date') + if end_dates: + final_output += _iterate_a_list_of_values(end_dates, + 'end_date') + if start_dates: + final_output += _iterate_a_list_of_values(start_dates, + 'start_date') + return final_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 date-info key:value. + + It finds the CatalogItemPage that matches the item identifier and + and if a matching CatalogItemPage can be found it continues. If not, it + sends an error message to stderr + + It sends value of author responsible to private method _add_authors, + recipient dedicatee value to _add_recipients, place value + to _add_places, and item types to _add_item_types + + At end, it sets the stream_data to the matching CatalogItemPage + date_information.stream_data attribute and saves the CatalogItemPage. + """ 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: + the_id = n_item["item"] + cur = CatalogItemPage.objects.filter(title=the_id) if cur.count() == 1: cur = cur[0] - if n.get("date info", None): - date_labels = n["date info"].get("date_label", None) - end_dates = n["date info"].get("end date", None) - start_dates = n["date info"].get("start date", None) - dates = n["date info"].get("date", None) - date_values = [] - if date_labels: - for dl in date_labels: - a_val = {'type': 'date_label', - 'value': {'date_label': dl}} - date_values.append(a_val) - else: - a_val = {'type': 'date_label', - 'value': {'date_label': 's.d.'}} - date_values.append(a_val) - if dates: - for d in dates: - if type(d) != type([]): - pulled_val_parts = d.split('/') - if len(pulled_val_parts) == 3: - d_val = {'type': 'end_date', - 'value':{'year': pulled_val_parts[2], - 'day': pulled_val_parts[1], - 'month': pulled_val_parts[0]}} - date_values.append(d_val) - if end_dates: - for ed in end_dates: - pulled_val_parts = ed.split('/') - if len(pulled_val_parts) == 3: - ed_val = {'type': 'end_date', - 'value':{'year': pulled_val_parts[2], - 'day': pulled_val_parts[1], - 'month': pulled_val_parts[0]}} - date_values.append(ed_val) - if start_dates: - for sd in start_dates: - pull_val_parts = sd.split('/') - if len(pulled_val_parts) == 3: - sd_val = {'type': 'start_date', - 'value':{'year': pulled_val_parts[2], - 'day': pulled_val_parts[1], - 'month': pulled_val_parts[0]}} - date_values.append(sd_val) - cur.date_information.stream_data = date_values + if n_item.get("date info", None): + final_output = self._extract_date_parts(n_item["date info"]) + cur.date_information.stream_data = final_output cur.save() else: self.stderr.write("{} has no date information to add".\ - format(n["item"])) + format(the_id)) else: self.stderr.write("{} has no catalog item page".\ - format(n["item"])) + format(the_id)) diff --git a/operacat/catalogitems/management/commands/load_items.py b/operacat/catalogitems/management/commands/load_items.py index 76fd493..d320f8e 100644 --- a/operacat/catalogitems/management/commands/load_items.py +++ b/operacat/catalogitems/management/commands/load_items.py @@ -1,33 +1,51 @@ import json -from django.core.management.base import BaseCommand, CommandError -from os.path import dirname, join +from django.core.management.base import BaseCommand from wagtail.wagtailcore.models import Page from catalogitems.models import CatalogItemPage, Dealer, Place class Command(BaseCommand): + """a management command to create initial migration of items from legacy data + + This class is necessary for a starting the migration of legacy data into the system. + """ help = "Add item pages for every item in legacy data to system" 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 item key:value. + + It then checks if there is a CatalogItemPage already present with that item + in the title, and if ther is not it creates one and adds it to the system + as a child of the home 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: - if n["item"]: - cur = CatalogItemPage.objects.filter(title=n["item"]) + for n_item in data: + if n_item["item"]: + cur = CatalogItemPage.objects.filter(title=n_item["item"]) if cur.count() == 1: cur = cur[0] else: cur = CatalogItemPage() - cur.title = n["item"] + cur.title = n_item["item"] home.add_child(instance=cur) - else: - self.stderr.write(str(n)) \ No newline at end of file + else: + self.stderr.write(str(n_item)) \ No newline at end of file