Ejemplo n.º 1
0
def update_item_notes():
    # Clear all item notes before re-importing.
    ItemNote.objects.all().delete()

    legacy_items = LegacyItem.objects.filter(Q(notes__isnull=False) | Q(copyingstyle__isnull=False) | \
                                             Q(concordances__isnull=False) | Q(layout__isnull=False))

    for entry in legacy_items:
        print("Updating item {0}".format(entry.pk))
        note_fields = (
            (ItemNote.GENERAL_NOTE, entry.notes),
            (ItemNote.COPYING_STYLE, entry.copyingstyle),
            (ItemNote.CONCORDANCES, entry.concordances),
            (ItemNote.LAYOUT, entry.layout)
        )

        try:
            item = Item.objects.get(pk=entry.pk)
        except Item.DoesNotExist:
            print('This item does not exist')
            continue

        for nt in note_fields:
            if not nt[1]:
                continue
            d = {
                'type': nt[0],
                'note': nt[1],
                'item': item
            }
            itn = ItemNote(**d)
            itn.save()

    for entry in LegacyItemImage.objects.filter(positiononpage__isnull=False):
        print("Updating item {0}".format(entry.pk))
        try:
            item = Item.objects.get(pk=entry.pk)
        except Item.DoesNotExist:
            print('This item does not exist')
            continue

        d = {
            'type': ItemNote.POSITION,
            'note': entry.positiononpage,
            'item': item
        }
        itmn = ItemNote(**d)
        itmn.save()
Ejemplo n.º 2
0
def migrate_item(entry):
    print(term.green("\tMigrating composition {0} in source {1} with Item ID {2}".format(entry.compositionkey,
                                                                                         entry.sourcekey,
                                                                                         entry.pk)))
    source_pk = entry.sourcekey
    source = Source.objects.get(pk=source_pk)

    # This will stop any composition placeholders from being added to the item table.
    # We'll skip adding this composition to the source
    # and simply set a flag on the source that no inventory has been provided.
    if entry.compositionkey in BAD_COMPOSITION_KEYS:
        print(term.red('\t\tMarking the inventory as not provided.'))
        source.inventory_provided = False
        source.save()
        # Prevent the migration from creating a new item for this record.
        return None

    layout = None
    if layout == "score":
        layout = Item.L_SCORE
    elif layout == "parts":
        layout = Item.L_PARTS

    d = {
        'id': entry.pk,
        'source': source,
        'folio_start': entry.folio_start,
        'folio_end': entry.folio_end,
        'source_attribution': entry.composeroriginal,
        'source_incipit': entry.incipittranscription,
        'layout': layout,
        'num_voices': entry.novoices,
        'legacy_position_ms': entry.positionms,
        'page_order': entry.positionpage if entry.positionpage else 0
    }

    it = Item(**d)
    it.save()

    composition = None
    composition_pk = entry.compositionkey
    orig_composition = None

    if composition_pk not in BAD_COMPOSITION_KEYS:
        orig_composition = Composition.objects.get(pk=composition_pk)

    # If the composition is a 'filler' one that meant to stand in for one or more
    # listed but un-titled works in the source, we will instead shift the
    # composer to being an 'item composer entry' and not join the original 'composition'
    # to the source.
    if orig_composition and orig_composition.title in aggregate_titles:
        print(term.magenta('\tCreating non-work composer entries.'))
        # we have an aggregate entry, and iterate through the CompositionComposer objects.
        for composer in orig_composition.composers.all():
            # store the original title ("4 works") in a note.
            note = orig_composition.title
            if composer.notes:
                note = "{0}\n{1}".format(note, composer.notes)

            icd = {
                'item': it,
                'composer': composer.composer,
                'note': orig_composition.title,
                'uncertain': composer.uncertain
            }
            ic = ItemComposer(**icd)
            ic.save()
    else:
        it.composition = orig_composition
        it.save()

    note_fields = (
        (ItemNote.GENERAL_NOTE, entry.notes),
        (ItemNote.COPYING_STYLE, entry.copyingstyle),
        (ItemNote.CONCORDANCES, entry.concordances),
        (ItemNote.LAYOUT, entry.layout)
    )

    for nt in note_fields:
        if not nt[1]:
            continue
        d = {
            'type': nt[0],
            'note': nt[1],
            'item': it
        }
        itn = ItemNote(**d)
        itn.save()

    # Migrate the position note from the item image table.
    itemimages = LegacyItemImage.objects.filter(pk=entry.pk, positiononpage__isnull=False)
    for entry in itemimages:
        d = {
            'type': ItemNote.POSITION,
            'note': entry.positiononpage,
            'item': it
        }
        itmn = ItemNote(**d)
        itmn.save()