Exemple #1
0
def main(instrument: api_models.InstrumentIn):
    """Create a new instrument"""
    photo_id = None
    if instrument.photo:
        photo_id = handle_photo(instrument.photo)

    new_instrument = InstrumentModel(**instrument.dict(exclude={"photo"}),
                                     photo=photo_id)
    new_instrument.save()
    new_instrument.refresh()
    instrument_in_db = api_models.InstrumentInDB(
        **serialize_item(new_instrument))
    instrument_out = api_models.InstrumentOut(**instrument_in_db.dict(
        exclude={"photo"}))
    return success(
        {
            "message": f"Instrument {new_instrument.number} created",
            "item": instrument_out.dict(),
            "id": instrument_out.id,
        },
        201,
    )
def main():
    items = at.get_all()
    for item in items:
        time.sleep(0.2)
        print("processing item", item["fields"]["Number"])
        photo_key = None
        if item["fields"].get("Photo"):
            photo_key = handle_photo(item["fields"]["Photo"][0]["url"])
        history = None
        type = item["fields"]["Instrument Type"].strip()
        type = type[0].upper() + type[1:]
        location = item["fields"].get("Location", "Unknown").strip()
        location = location[0].upper() + location[1:]
        if item["fields"].get("History"):
            history = json.dumps(item["fields"]["History"].split(", "))
        try:
            new_instrument = api_models.Instrument(
                number=item["fields"]["Number"].strip(),
                size=item["fields"]["Size"].strip(),
                type=type,
                location=location,
                assignedTo=item["fields"].get("Assigned To", None),
                maintenanceNotes=item["fields"].get("Maintenance Notes", None),
                conditionNotes=item["fields"].get("Condition Notes", None),
                quality=item["fields"].get("Quality", None),
                condition=item["fields"].get("Condition", None),
                gifted=item["fields"].get("Gifted to student", False),
            )
            new_item = InstrumentModel(
                **new_instrument.dict(exclude={"photo", "history"}),
                photo=photo_key,
                history=history,
            )
            new_item.save()
        except KeyError as err:
            print(item["fields"].get("Number"), f"not migrated: {err}")
            continue