Skip to content

Instantly share code, notes, and snippets.

@meg-codes
Last active May 6, 2019 20:46
Show Gist options
  • Save meg-codes/4fa645382e4801597d8d0d5aa5f1daa1 to your computer and use it in GitHub Desktop.
Save meg-codes/4fa645382e4801597d8d0d5aa5f1daa1 to your computer and use it in GitHub Desktop.
Updated version of rlskoeser, Export people data for Shakespeare & co lending card holders (fields needed for Finding Aid enhancement and image import into Figgy)
# use env to set django settings module
# env DJANGO_SETTINGS_MODULE=mep.settings python mep_cardholders.py
import csv
import codecs
import django
from django.db import models
import progressbar
django.setup()
from mep.accounts.partial_date import DatePrecision
from mep.people.models import Person
from mep.accounts.models import Borrow, Event
cardholders = Person.objects.filter(account__card__isnull=False)
fields = ['name', 'sort name', 'person/organization', 'viaf', 'dates', 'images']
image_files = []
with open('/tmp/sylviabeach-cardholders.csv', 'w') as csvfile:
# write utf-8 byte order mark at the beginning of the file
csvfile.write(codecs.BOM_UTF8.decode())
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
for person in progressbar.progressbar(cardholders):
dates = ''
account = person.account_set.filter(card__isnull=False).first()
# find any event with footnotes for this account and order by
# start date
# footnote could be on event, borrow, or purchase
events = Event.objects.filter(account=account) \
.filter(models.Q(event_footnotes__isnull=False) |
models.Q(borrow__footnotes__isnull=False) |
models.Q(purchase__footnotes__isnull=False)) \
.exclude(models.Q(event_footnotes__location='') &
models.Q(borrow__footnotes__location='') &
models.Q(purchase__footnotes__location='')) \
.order_by('start_date')
# split on newline, drop first (label "Images:")
card_images = account.card.notes.split()[1:]
entity = 'organization' if person.is_organization else 'person'
if events:
# get earliest date and latest date, combine them if they aren't the same date
dates = events.first().start_date or events.first().end_date
end_date = events.last().end_date or events.last().start_date
if end_date and end_date != dates:
dates = '%s/%s' % (dates, end_date)
card_images = ['%s.jp2' % img if not img.endswith('.jp2')
and img[-1].isnumeric() else img for img in card_images]
# a number of image paths have an extra slash in the url; remove it
card_images = [img.replace('//c/', '/c/').replace('.jpg', '.jp2')
for img in card_images]
# add to list of all card images
image_files.extend(card_images)
csvwriter.writerow([person.name, person.sort_name, entity,
person.viaf.uri if person.viaf else '',
dates, ';'.join([str(img) for img in card_images])])
with open('/tmp/sylviabeach-card-images.txt', 'w') as cardimgfile:
if len(image_files) != len(set(image_files)):
print('Warning: duplicate images (unique count off by %d)' % \
(len(image_files) - len(set(image_files)), ))
cardimgfile.write('\n'.join(image_files))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment