This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #37; more docstrings in management commands
- Loading branch information
verbalhanglider
committed
May 26, 2017
1 parent
56dfd43
commit 87915c0
Showing
4 changed files
with
142 additions
and
69 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
operacat/catalogitems/management/commands/link_images_with_items.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 85 additions & 54 deletions
139
operacat/catalogitems/management/commands/load_date_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
else: | ||
self.stderr.write(str(n_item)) |