def signed_out(): """Find instruments that haven't been signed back in yet""" found = InstrumentModel.scan((InstrumentModel.assignedTo.exists()) & (InstrumentModel.gifted == False)) instruments_out = api_models.process_instrument_db_list(found) return data_or_404(instruments_out)
def main(id_): """Delete an instrument""" item = InstrumentModel.get(id_) if getattr(item, "photo", None): delete_photos(item.photo) item.delete() return success("Delete successful", 204)
def history_and_assigned(search: api_models.Search): """Find a student's assigned instrument and history""" found_items = InstrumentModel.scan( InstrumentModel.history.contains(search.term) | InstrumentModel.assignedTo.contains(search.term)) instruments_out = api_models.process_instrument_db_list(found_items) return data_or_404(instruments_out)
def main(id_): """Get a single instrument from the database.""" ins = api_models.InstrumentInDB.parse_obj( InstrumentModel.get(id_).attribute_values) photo_urls = generate_photo_urls(ins.photo) if ins.photo else None ins_out = api_models.InstrumentOut(**ins.dict(), photoUrls=photo_urls) return success(ins_out.dict())
def main(instrument_filter: api_models.InstrumentFilter): """Filter instruments by certain values""" filter_string = instrument_filter.generate_filter_string() found = InstrumentModel.scan(eval(filter_string)) instruments_out = api_models.process_instrument_db_list(found) return success(instruments_out)
def full(new_instrument: api_models.Instrument, path_id): """Update a full record""" ins = InstrumentModel.get(path_id) for key, value in new_instrument.dict().items(): setattr(ins, key, value) ins.save() ins_db = api_models.InstrumentInDB.parse_obj(ins.attribute_values) ins_out = api_models.InstrumentOut.parse_obj(ins_db) return success({"message": "Update Successful", "item": ins_out.dict()})
def photo(data, path_id): """Change or add a photo""" if not path_id: return failure("Record ID must be in url", 400) ins = InstrumentModel.get(path_id) if ins.photo: delete_photos(ins.photo) ins.photo = handle_photo(data["photoUrl"]) ins.save() return success({"message": "Photo successfully updated"})
def sign_out_instrument(sign_out): found = list(InstrumentModel.scan(InstrumentModel.number == sign_out.number)) if not found: return None item = found[0] if item.assignedTo: item.history = make_new_history(item.history, item.assignedTo) item.assignedTo = sign_out.assignedTo item.location = sign_out.location item.save() item.refresh() return item
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
def main(sign_out: api_models.SignOut): """Sign out an instrument""" # noinspection PyTypeChecker found = list(InstrumentModel.scan(InstrumentModel.number == sign_out.number)) item = sign_out_instrument(sign_out) if item: return success( { "message": f"Instrument {item.number} signed out to {item.assignedTo}" f" at {item.location}", "id": item.id, } ) else: return not_found()
def single(body: api_models.RetrieveSingle): """Mark an instrument as having been retrieved""" # noinspection PyTypeChecker found = list( InstrumentModel.scan(InstrumentModel.number == body.number.upper())) if not found: return not_found() item = found[0] item.location = "Storage" if item.assignedTo: item.history = make_new_history(item.history, item.assignedTo) item.assignedTo = None item.gifted = False item.save() return success({ "message": f"{item.type} {item.number} retrieved", "id": item.id })
def multiple(body: api_models.RetrieveMultiple): """Mark multiple instruments as having been retrieved""" numbers = [number.upper() for number in body.numbers] response_body = {"instrumentsUpdated": [], "instrumentsFailed": []} scan = InstrumentModel.scan(InstrumentModel.number.is_in(*numbers)) for ins in scan: try: ins.location = "Storage" if ins.assignedTo: ins.history = make_new_history(ins.history, ins.assignedTo) ins.assignedTo = None ins.gifted = False ins.save() response_body["instrumentsUpdated"].append(ins.number) except Exception as err: print(err) response_body["instrumentsFailed"].append(ins.number) for instrument_number in body.numbers: if (instrument_number not in response_body["instrumentsUpdated"] and instrument_number not in response_body["instrumentsFailed"]): response_body["instrumentsFailed"].append(instrument_number) return success(response_body)
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 all_(): """Get all the instruments""" instruments = api_models.process_instrument_db_list(InstrumentModel.scan()) return success(instruments)
def history(search: api_models.Search): """Find an instrument by its history""" found_items = InstrumentModel.scan( InstrumentModel.history.contains(search.term)) instruments_out = api_models.process_instrument_db_list(found_items) return data_or_404(instruments_out)
def number(search: api_models.Search): """Find an instrument by number""" # noinspection PyTypeChecker found_items = InstrumentModel.scan(InstrumentModel.number == search.term) instruments_out = api_models.process_instrument_db_list(found_items) return data_or_404(instruments_out)
def gifted(): """Find instruments that have been given away to students""" found = InstrumentModel.scan(InstrumentModel.gifted == True) instruments_out = api_models.process_instrument_db_list(found) return data_or_404(instruments_out)
def assigned(search: api_models.Search): """Find an instrument by who it's assigned to""" found_items = InstrumentModel.scan( InstrumentModel.assignedTo.contains(search.term)) instruments_out = api_models.process_instrument_db_list(found_items) return data_or_404(instruments_out)