def payments(api): """Lists available payment methods""" methods = api.payment_methods() click.echo('{:^50}'.format("Method") + '|' + '{:^20}'.format("Expiry") + '|' + '{:^20}'.format("Id")) click.echo('{:-^90}'.format('')) if len(methods) == 0: print_stderr("No payment methods found.") return for method in methods: if method['type'] == "CreditCard": description = "{} ending with {}".format( method["credit_card_type"], method["credit_card_last_4"] ) elif method['type'] == "PurchaseOrder": description = "Purchase Order \"{}\"".format( method["description"] ) else: description = method["description"] if method['is_default?']: description += " (Default)" if not method['is_valid']: description += " (Invalid)" click.echo('{:^50}'.format(ascii_encode(description)) + '|' + '{:^20}'.format(ascii_encode(method['expiry'])) + '|' + '{:^20}'.format(ascii_encode(method['id'])))
def resources(api, query): """Search catalog of provisionable resources""" resource_req = api.resources(query) if resource_req["results"]: kit_req = api.kits(query) if not kit_req["results"]: common_name = resource_req["results"][0]["name"] kit_req = api.kits(common_name) flat_items = list( flatmap( lambda x: [{ "name": y["resource"]["name"], "id": y["resource"]["id"], "vendor": x["vendor"]["name"] if "vendor" in list(x.keys()) else '' } for y in x["kit_items"] if (y["provisionable"] and not y["reservable"])], kit_req["results"])) rs_id_list = [rs["id"] for rs in resource_req["results"]] matched_resources = [] for item in flat_items: if item["id"] in rs_id_list and item not in matched_resources: matched_resources.append(item) if matched_resources: click.echo("Results for '%s':" % query) click.echo('{:^40}'.format("Resource Name") + '|' + '{:^40}'.format("Vendor") + '|' + '{:^40}'.format("Resource ID")) click.echo('{:-^120}'.format('')) for resource in matched_resources: click.echo('{:^40}'.format(ascii_encode(resource["name"])) + '|' + '{:^40}'.format(ascii_encode(resource["vendor"])) + '|' + '{:^40}'.format(ascii_encode(resource["id"]))) click.echo('{:-^120}'.format('')) else: click.echo("No usable resource for '{}'.".format(query)) else: click.echo("No results for '{}'.".format(query))
def resources(ctx, query): """Search catalog of provisionable resources""" resource_req = ctx.obj.api.resources(query) if resource_req["results"]: kit_req = ctx.obj.api.kits(query) if not kit_req["results"]: common_name = resource_req["results"][0]["name"] kit_req = ctx.obj.api.kits(common_name) flat_items = list(flatmap(lambda x: [{"name": y["resource"]["name"], "id": y["resource"]["id"], "vendor": x["vendor"]["name"] if "vendor" in list(x.keys()) else ''} for y in x["kit_items"] if (y["provisionable"] and not y["reservable"])], kit_req["results"])) rs_id_list = [rs["id"] for rs in resource_req["results"]] matched_resources = [] for item in flat_items: if item["id"] in rs_id_list and item not in matched_resources: matched_resources.append(item) if matched_resources: click.echo("Results for '%s':" % query) click.echo('{:^40}'.format("Resource Name") + '|' + '{:^40}'.format("Vendor") + '|' + '{:^40}'.format("Resource ID")) click.echo('{:-^120}'.format('')) for resource in matched_resources: click.echo('{:^40}'.format(ascii_encode(resource["name"])) + '|' + '{:^40}'.format(ascii_encode(resource["vendor"])) + '|' + '{:^40}'.format(ascii_encode(resource["id"]))) click.echo('{:-^120}'.format('')) else: click.echo("No usable resource for '{}'.".format(query)) else: click.echo("No results for '{}'.".format(query))
def inventory(api, include_aliquots, show_status, retrieve_all, query): """Search organization for inventory""" inventory_req = api.inventory(query) num_pages = inventory_req["num_pages"] per_page = inventory_req["per_page"] results = inventory_req["results"] max_results_bound = num_pages * per_page num_prefiltered = len(results) if retrieve_all: for i in range(1, num_pages): click.echo("Retrieved {} records" " out of {} total for '{}'...\r". format(i * per_page, max_results_bound, query), nl=False) inventory_req = api.inventory(query, page=i) results.extend(inventory_req["results"]) click.echo() if include_aliquots: results = [c if "label" in c else c["container"] for c in results] else: results = [c for c in results if "label" in c] results = [i for n, i in enumerate(results) if i not in results[n + 1:]] if results: def truncate_time(d, k): old_time = d[k] d[k] = old_time.split("T")[0] return d results = [truncate_time(c, "created_at") for c in results] barcode_present = any(c["barcode"] for c in results) keys = ["label", "id", "container_type_id", "storage_condition", "created_at"] if barcode_present: keys.insert(2, "barcode") if show_status: keys.append("status") friendly_keys = {k: k.split("_")[0] for k in keys} spacing = {k: max(len(friendly_keys[k]), max([len(str(c[k])) for c in results])) for k in keys} spacing = {k: (v // 2 + 1) * 2 + 1 for k, v in spacing.items()} sum_spacing = sum(spacing.values()) + (len(keys) - 1) * 3 + 1 spacing = {k: "{:^%s}" % v for k, v in spacing.items()} sum_spacing = "{:-^%s}" % sum_spacing click.echo("Results for '%s':" % query) click.echo(' | '.join([spacing[k]. format(friendly_keys[k]) for k in keys])) click.echo(sum_spacing.format('')) for c in results: click.echo(' | '.join([spacing[k]. format(ascii_encode(c[k])) for k in keys])) click.echo(sum_spacing.format('')) if not retrieve_all: if num_pages > 1: click.echo("Retrieved {} records out of " "{} total (use the --retrieve_all flag " "to request all records).". format(num_prefiltered, max_results_bound)) else: if retrieve_all: click.echo("No results for '{}'.".format(query)) else: if num_pages > 1: click.echo("Retrieved {} records out of " "{} total but all were filtered out. " "Use the --retrieve_all flag " "to request all records.". format(num_prefiltered, max_results_bound)) else: click.echo("All records were filtered out. " "Use flags to modify your search")
def inventory(ctx, include_aliquots, show_status, retrieve_all, query): """Search organization for inventory""" inventory_req = ctx.obj.api.inventory(query) num_pages = inventory_req["num_pages"] per_page = inventory_req["per_page"] results = inventory_req["results"] max_results_bound = num_pages * per_page num_prefiltered = len(results) if retrieve_all: for i in range(1, num_pages): click.echo("Retrieved {} records" " out of {} total for '{}'...\r". format(i * per_page, max_results_bound, query), nl=False) inventory_req = ctx.obj.api.inventory(query, page=i) results.extend(inventory_req["results"]) click.echo() if include_aliquots: results = [c if "label" in c else c["container"] for c in results] else: results = [c for c in results if "label" in c] results = [i for n, i in enumerate(results) if i not in results[n + 1:]] if results: def truncate_time(d, k): old_time = d[k] d[k] = old_time.split("T")[0] return d results = [truncate_time(c, "created_at") for c in results] barcode_present = any(c["barcode"] for c in results) keys = ["label", "id", "container_type_id", "storage_condition", "created_at"] if barcode_present: keys.insert(2, "barcode") if show_status: keys.append("status") friendly_keys = {k: k.split("_")[0] for k in keys} spacing = {k: max(len(friendly_keys[k]), max([len(str(c[k])) for c in results])) for k in keys} spacing = {k: (v // 2 + 1) * 2 + 1 for k, v in spacing.items()} sum_spacing = sum(spacing.values()) + (len(keys) - 1) * 3 + 1 spacing = {k: "{:^%s}" % v for k, v in spacing.items()} sum_spacing = "{:-^%s}" % sum_spacing click.echo("Results for '%s':" % query) click.echo(' | '.join([spacing[k]. format(friendly_keys[k]) for k in keys])) click.echo(sum_spacing.format('')) for c in results: click.echo(' | '.join([spacing[k]. format(ascii_encode(c[k])) for k in keys])) click.echo(sum_spacing.format('')) if not retrieve_all: if num_pages > 1: click.echo("Retrieved {} records out of " "{} total (use the --retrieve_all flag " "to request all records).". format(num_prefiltered, max_results_bound)) else: if retrieve_all: click.echo("No results for '{}'.".format(query)) else: if num_pages > 1: click.echo("Retrieved {} records out of " "{} total but all were filtered out. " "Use the --retrieve_all flag " "to request all records.". format(num_prefiltered, max_results_bound)) else: click.echo("All records were filtered out. " "Use flags to modify your search")