From d064522d60ea8b0d2127f99a7f4500b7fb1c9254 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Mon, 2 Oct 2023 19:04:51 +1100 Subject: [PATCH] sheetsync: Move edit url management into Sheets middleware As streamlog doesn't require it. --- sheetsync/sheetsync/main.py | 20 +++----------------- sheetsync/sheetsync/sheets.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/sheetsync/sheetsync/main.py b/sheetsync/sheetsync/main.py index a948688..007ad9d 100644 --- a/sheetsync/sheetsync/main.py +++ b/sheetsync/sheetsync/main.py @@ -69,11 +69,10 @@ class SheetSync(object): # Time to wait after getting an error ERROR_RETRY_INTERVAL = 10 - def __init__(self, middleware, stop, dbmanager, edit_url): + def __init__(self, middleware, stop, dbmanager): self.middleware = middleware self.stop = stop self.dbmanager = dbmanager - self.edit_url = edit_url # List of input columns self.input_columns = [ 'event_start', @@ -223,19 +222,6 @@ class SheetSync(object): rows_changed.labels('output', worksheet).inc() self.middleware.mark_modified(worksheet) - # Set edit link if marked for editing and start/end set. - # This prevents accidents / clicking the wrong row and provides - # feedback that sheet sync is still working. - # Also clear it if it shouldn't be set. - edit_link = self.edit_url.format(row['id']) if row['marked_for_edit'] == '[+] Marked' else '' - if row['edit_link'] != edit_link: - logging.info("Updating sheet row {} with edit link {}".format(row['id'], edit_link)) - self.middleware.write_value( - worksheet, row, - "edit_link", edit_link, - ) - self.middleware.mark_modified(worksheet) - class PlaylistSync: @@ -382,10 +368,10 @@ def main(dbconnect, sheets_creds_file, edit_url, bustime_start, sheet_id, worksh client_secret=sheets_creds['client_secret'], refresh_token=sheets_creds['refresh_token'], ) - sheets_middleware = SheetsMiddleware(sheets_client, sheet_id, worksheet_names, bustime_start, allocate_ids) + sheets_middleware = SheetsMiddleware(sheets_client, sheet_id, worksheet_names, bustime_start, edit_url, allocate_ids) workers = [ - SheetSync(sheets_middleware, stop, dbmanager, edit_url), + SheetSync(sheets_middleware, stop, dbmanager), ] if playlist_worksheet: workers.append(PlaylistSync(stop, dbmanager, sheets_client, sheet_id, playlist_worksheet)) diff --git a/sheetsync/sheetsync/sheets.py b/sheetsync/sheetsync/sheets.py index 7252296..a262749 100644 --- a/sheetsync/sheetsync/sheets.py +++ b/sheetsync/sheetsync/sheets.py @@ -85,11 +85,12 @@ class SheetsMiddleware(): # If playlist_worksheet is defined, add 1 to len(worksheets). # For current values, this is 100/5 * 2 + 100/5/4 * 7 = 75 - def __init__(self, client, sheet_id, worksheets, bustime_start, allocate_ids=False): + def __init__(self, client, sheet_id, worksheets, bustime_start, edit_url, allocate_ids=False): self.client = client self.sheet_id = sheet_id # map {worksheet: last modify time} self.worksheets = {w: 0 for w in worksheets} + self.edit_url = edit_url self.allocate_ids = allocate_ids # Maps DB column names (or general identifier, for non-DB columns) to sheet column indexes. # Hard-coded for now, future work: determine this from column headers in sheet @@ -183,6 +184,18 @@ class SheetsMiddleware(): str(row['id']), ) + # Set edit link if marked for editing and start/end set. + # This prevents accidents / clicking the wrong row and provides + # feedback that sheet sync is still working. + # Also clear it if it shouldn't be set. + # We do this here instead of in sync_row() because it's Sheets-specific logic + # that doesn't depend on the DB event in any way. + edit_link = self.edit_url.format(row['id']) if row['marked_for_edit'] == '[+] Marked' else '' + if row['edit_link'] != edit_link: + logging.info("Updating sheet row {} with edit link {}".format(row['id'], edit_link)) + self.write_value(worksheet, row, "edit_link", edit_link) + self.mark_modified(worksheet) + yield row def parse_row(self, worksheet, row_index, row):