def create(todo: api_models.Todo, identity_id): """Create a new to do in the database""" new_todo = TodoModel(identity_id, **todo.dict()) new_todo.save() todo_db = api_models.TodoInDB.parse_obj(new_todo.attribute_values) todo_out = api_models.TodoOut.parse_obj(todo_db) return success({"message": "Todo Created", "item": todo_out.dict()}, 201)
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 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 multiple(assignments: api_models.SignOutMultiple): successes = [] failures = [] for asg in assignments.instruments: item = sign_out_instrument(asg) if item: successes.append(asg.number) else: failures.append(asg.number) return success({"updated": successes, "failed": failures})
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 update(user_id, todo_id, event): """Update the content or instrument of a to do item""" data = ujson.loads(event["body"]) item = TodoModel.get(user_id, todo_id) if data.get("content"): item.content = data["content"] if data.get("relevantInstrument"): item.relevantInstrument = data["relevantInstrument"] item.save() item.refresh() todo_db = api_models.TodoInDB.parse_obj(item.attribute_values) todo_out = api_models.TodoOut.parse_obj(todo_db) return success({"message": "Updated", "item": todo_out.dict()})
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 main(): model_schema = schema( [ api_models.Todo, api_models.TodoOut, api_models.Instrument, api_models.InstrumentOut, api_models.InstrumentFilter, api_models.RetrieveSingle, api_models.RetrieveMultiple, api_models.Search, api_models.SignOut, ] )["definitions"] schema_body = { "openapi": "3.0.2", "info": {"title": "Instrument Inventory", "version": os.getenv("VERSION", "1")}, "components": {"schemas": model_schema}, } return success(schema_body)
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_(): return success("Hello World!")
def main(): return success("Hello World!")
def all_(): """Get all the instruments""" instruments = api_models.process_instrument_db_list(InstrumentModel.scan()) return success(instruments)
def single(): return success("Hello World!")
def delete(user_id, todo_id, _): """Delete a to do item""" item = TodoModel.get(user_id, todo_id) item.delete() return success({"message": "deleted"}, 204)
def read_single(user_id, todo_id, _): """Get a single to do item""" todo = TodoModel.get(user_id, todo_id) todo_db = api_models.TodoInDB.parse_obj(todo.attribute_values) todo_out = api_models.TodoOut.parse_obj(todo_db) return success(todo_out.dict())
def _modify_completed(user_id, todo_id, set_to): item = TodoModel.get(user_id, todo_id) item.completed = set_to item.save() item.refresh() return success({"message": "Updated", "item": item.attribute_values})
def read_completed(event, _context): """Get all completed to do items""" user_id = event["requestContext"]["identity"]["cognitoIdentityId"] items = TodoModel.query(user_id, TodoModel.completed == True) response_body = api_models.process_todo_db_list(items) return success(response_body)