From c2c436ead5ff9731c921a7b4674dcb5a4f637337 Mon Sep 17 00:00:00 2001 From: Ciara Goetze Date: Tue, 17 Aug 2021 13:58:10 -0700 Subject: [PATCH 1/3] Alphabetize after data load --- data_loader.py | 3 ++- drive_webhook_receiver.py | 22 +--------------------- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/data_loader.py b/data_loader.py index 8c7f277..051c07e 100644 --- a/data_loader.py +++ b/data_loader.py @@ -2,7 +2,7 @@ import json from googleapiclient.discovery import build from google.oauth2 import service_account -from utils import _export_file +from utils import _export_file, _alphabetize_all_items from builder_client import BuilderClient from constants import PDF_PATH, SCOPES @@ -69,6 +69,7 @@ def load_file_data(): } response = builder_client.post(link_post_url, link_post_data) + _alphabetize_all_items(builder_client, guide_id, customlist_id) if __name__ == "__main__": load_file_data() diff --git a/drive_webhook_receiver.py b/drive_webhook_receiver.py index 93a8efb..7856675 100644 --- a/drive_webhook_receiver.py +++ b/drive_webhook_receiver.py @@ -3,7 +3,7 @@ import traceback from googleapiclient.discovery import build from google.oauth2 import service_account -from utils import fetch_ssm_params, _export_file +from utils import fetch_ssm_params, _export_file, _alphabetize_all_items from builder_client import BuilderClient from constants import PDF_PATH, SCOPES @@ -153,23 +153,3 @@ def _update_custom_list_item(builder_client, guide_id, custom_list_item, changed pdf_patch_url = "https://builder.guidebook.com/open-api/v1/pdfs/%7B%7D/".format(link['target_object_id']) with open(PDF_PATH, 'rb') as f: pdf_response = builder_client.patch(pdf_patch_url, data={'pdf_view_type': 'pdf'}, files={'pdf_file': f}) - - -def _alphabetize_all_items(builder_client, guide_id, customlist_id): - """ - Pull all items from the custom list, alphabetize, and adjust ranks accordingly. - """ - items_url = f"https://builder.guidebook.com/open-api/v1/custom-list-items?guide={guide_id}&custom_list={customlist_id}" - response = builder_client.get(items_url) - sorted_list = sorted(response.json()["results"], key=lambda x: x['name']) - - rank = 0 - for item in sorted_list: - item_patch_url = "https://builder.guidebook.com/open-api/v1/custom-list-items/%7B%7D/".format(item['id']) - builder_client.patch(item_patch_url, data={'rank': rank}) - rank += 1 - - - - - From 7f3a9cfa230ce518c023504d74096d2d8c63be7d Mon Sep 17 00:00:00 2001 From: Ciara Goetze Date: Wed, 18 Aug 2021 09:10:02 -0700 Subject: [PATCH 2/3] add utils --- utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/utils.py b/utils.py index fd597f4..7dccad3 100644 --- a/utils.py +++ b/utils.py @@ -59,3 +59,18 @@ def _export_file(service, file_id): fh.seek(0) with open(PDF_PATH, 'wb') as f: shutil.copyfileobj(fh, f, length=131072) + + +def _alphabetize_all_items(builder_client, guide_id, customlist_id): + """ + Pull all items from the custom list, alphabetize, and adjust ranks accordingly. + """ + items_url = f"https://builder.guidebook.com/open-api/v1/custom-list-items?guide={guide_id}&custom_list={customlist_id}" + response = builder_client.get(items_url) + sorted_list = sorted(response.json()["results"], key=lambda x: x['name']) + + rank = 0 + for item in sorted_list: + item_patch_url = "https://builder.guidebook.com/open-api/v1/custom-list-items/%7B%7D/".format(item['id']) + builder_client.patch(item_patch_url, data={'rank': rank}) + rank += 1 From 1f4d7046ae1fc03e79f39ee52ea2c350e5b5cefa Mon Sep 17 00:00:00 2001 From: Ciara Goetze Date: Wed, 18 Aug 2021 09:47:44 -0700 Subject: [PATCH 3/3] Update alphabetize to use custom list item relations --- utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utils.py b/utils.py index 7dccad3..91070a0 100644 --- a/utils.py +++ b/utils.py @@ -71,6 +71,8 @@ def _alphabetize_all_items(builder_client, guide_id, customlist_id): rank = 0 for item in sorted_list: - item_patch_url = "https://builder.guidebook.com/open-api/v1/custom-list-items/%7B%7D/".format(item['id']) - builder_client.patch(item_patch_url, data={'rank': rank}) + relation_url = f"https://builder.guidebook.com/open-api/v1/custom-list-item-relations/?custom_list_item={item['id']}&custom_list={customlist_id}" + cutom_list_item_relation = builder_client.get(relation_url).json()['results'][0] + relation_patch_url = f"https://builder.guidebook.com/open-api/v1/custom-list-item-relations/%7Bcutom_list_item_relation['id']}/" + builder_client.patch(relation_patch_url, data={'rank': rank}) rank += 1