Templating a Project

Or, Day in my Lifeimage.png

I have twice been asked, "Can you demonstrate a script that creates container records from a CSV?"

So, in preparation for this workshop, I decided to work on that specifically to show you the steps I take when templating a new project. Obviously, I have experience doing this, but even with that experience, I start with some basics each and every time. This is truly how I do it.

Let's couch this task in a scenario that I have actually faced:

You work in an institution that attaches containers to accession records. 40 boxes of a new physical collection have just come in. Those boxes need to be barcoded and then top containers need to be created and linked to the appropriate accession record.

Creating and linking 40 top containers by hand is a chore, so staff want to be able to do this via a CSV instead. What would it take to do this through the API?

Goal: Write a script that {sources from a CSV and creates containers through the API} and then {links those containers to an accession record by populating that accession's instances array}.

*Note that this is a two-stage process. Even if I only write one script to do it, {creating containers} and {linking containers} are two discrete tasks with different endpoints.

From my API Playbook

- If you are creating something from scratch, create it in the staff interface first, then GET it out using Postman. This is your template.

Set the stage: Templating

We'll be flipping back and forth between multiple windows as we go through stages.

  1. Go to the staff interface and create a top container linked to an accession. Leave out the barcode for now.
    • Note how it is a two-step process: {create} and {link}

Task 1: Create containers

  1. GET the top container out through Postman and explore the record
    • Note how the barcode field isn't present if it's empty
    • Go back and add a barcode and demonstrate how it appears
  1. Attempt to POST a new top container

    • Explore record requirements
    • In the end, all I needed was

      ``` { "barcode": "425346342f63426", "indicator": "3", "type": "box" }

If those are the only fields I need to create a top container with a barcode, those are the three columns I'll start with in my CSV.

  1. Create a CSV template based on the testing in Postman
    • Let's look at containers-for-accession.csv (link)
  1. Map the CSV columns to the fields in my JSON template (script)

Restart kernel shift + R

  1. Once mapped, create the new containers and temporarily hold onto their URIs

Now explore how the instance array in the accession record looks. And let me acknowledge that we have already zoomed past Beginner here:

```"instances": [ { "lock_version": 0, "instance_type": "mixed_materials", "jsonmodel_type": "instance", "is_representative": false, "sub_container": { "lock_version": 0, "created_by": "admin", "last_modified_by": "admin", "create_time": "2021-03-23T18:23:38Z", "system_mtime": "2021-03-23T18:23:38Z", "user_mtime": "2021-03-23T18:23:38Z", "jsonmodel_type": "sub_container", "top_container": { "ref": "/repositories/101/top_containers/214" } } } ]

That was our end goal, but to confirm, we are starting with an empty instance array in our accession:

  1. Populate the empty accession array with our new containers
  1. Post the accession with the new populated instances array, which will finish the linking part of our script: