Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
issue #37; more docstrings in management commands
Browse files Browse the repository at this point in the history
  • Loading branch information
verbalhanglider committed May 26, 2017
1 parent 56dfd43 commit 87915c0
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand All @@ -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.\
Expand All @@ -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)
Expand Down
139 changes: 85 additions & 54 deletions operacat/catalogitems/management/commands/load_date_data.py
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))
38 changes: 28 additions & 10 deletions operacat/catalogitems/management/commands/load_items.py
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))

0 comments on commit 87915c0

Please sign in to comment.