From f40e825ae3b87391a0307d0a2b5cbcd220196303 Mon Sep 17 00:00:00 2001 From: David Cain Date: Thu, 9 May 2024 23:51:18 -0600 Subject: [PATCH] Upgrade gspread to latest This newer version of the library requires a `Path` object, not just a `str`, so update accordingly. --- poetry.lock | 25 +++++++++++++++++++++---- pyproject.toml | 1 - ws/utils/member_sheets.py | 18 ++++++++++++------ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/poetry.lock b/poetry.lock index 3c97b229..b1a20fd5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -881,18 +881,19 @@ tool = ["click (>=6.0.0)"] [[package]] name = "gspread" -version = "5.11.0" +version = "6.1.0" description = "Google Spreadsheets Python API" optional = false -python-versions = ">=3.7" +python-versions = ">=3.8" files = [ - {file = "gspread-5.11.0-py3-none-any.whl", hash = "sha256:21110370cb5096c6a528806db16ea9329ad1d31203bd529faf7e4a909bb174bc"}, - {file = "gspread-5.11.0.tar.gz", hash = "sha256:16c0a1eab195318ce56e94325be7d71f061ae6923725928dadf777312fd62370"}, + {file = "gspread-6.1.0-py3-none-any.whl", hash = "sha256:67aa3335cfcbb2625e41b53129433ba779f2890244a4c532624362ce5e022342"}, + {file = "gspread-6.1.0.tar.gz", hash = "sha256:576b72b628b251d2ee41e02b982d3c714d511d2a5aa3a88e587ed9efc4d6e752"}, ] [package.dependencies] google-auth = ">=1.12.0" google-auth-oauthlib = ">=0.4.1" +StrEnum = "0.4.15" [[package]] name = "gunicorn" @@ -1969,6 +1970,22 @@ pure-eval = "*" [package.extras] tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"] +[[package]] +name = "strenum" +version = "0.4.15" +description = "An Enum that inherits from str." +optional = false +python-versions = "*" +files = [ + {file = "StrEnum-0.4.15-py3-none-any.whl", hash = "sha256:a30cda4af7cc6b5bf52c8055bc4bf4b2b6b14a93b574626da33df53cf7740659"}, + {file = "StrEnum-0.4.15.tar.gz", hash = "sha256:878fb5ab705442070e4dd1929bb5e2249511c0bcf2b0eeacf3bcd80875c82eff"}, +] + +[package.extras] +docs = ["myst-parser[linkify]", "sphinx", "sphinx-rtd-theme"] +release = ["twine"] +test = ["pylint", "pytest", "pytest-black", "pytest-cov", "pytest-pylint"] + [[package]] name = "tomli" version = "2.0.1" diff --git a/pyproject.toml b/pyproject.toml index 8db8a75e..fecf3764 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -237,7 +237,6 @@ module = [ "debug_toolbar", "factory.*", "freezegun", - "gspread", "kombu.*", "localflavor.*", "markdown2", diff --git a/ws/utils/member_sheets.py b/ws/utils/member_sheets.py index 0541334d..2d8c6b6b 100644 --- a/ws/utils/member_sheets.py +++ b/ws/utils/member_sheets.py @@ -13,17 +13,21 @@ from collections.abc import Iterable, Iterator from datetime import timedelta from itertools import zip_longest -from typing import Any, NamedTuple +from pathlib import Path +from typing import TYPE_CHECKING, Any, NamedTuple -import gspread import requests from django.utils import timezone +from gspread.auth import service_account +from gspread.cell import Cell from mitoc_const import affiliations from ws import enums, models, settings from ws.utils import membership as membership_utils from ws.utils.perms import is_chair +if TYPE_CHECKING: + from gspread.worksheet import Worksheet logger = logging.getLogger(__name__) MIT_STUDENT_AFFILIATIONS = frozenset( @@ -205,7 +209,7 @@ def get_row(self, participant: models.Participant) -> tuple[str, ...]: return tuple(row_mapper[label] for label in self.header) -def _assign(cells: Iterable[gspread.cell.Cell], values: Iterable[Any]) -> None: +def _assign(cells: Iterable[Cell], values: Iterable[Any]) -> None: """Set the values of cells, but **do not** issue an API call to update.""" for cell, value in zip(cells, values, strict=True): cell.value = value @@ -219,7 +223,8 @@ def update_participant( Much more efficient than updating the entire sheet. """ - client = gspread.service_account(settings.OAUTH_JSON_CREDENTIALS) + assert settings.OAUTH_JSON_CREDENTIALS is not None, "gpread not configured?" + client = service_account(Path(settings.OAUTH_JSON_CREDENTIALS)) wks = client.open_by_key(discount.ga_key).sheet1 writer = SheetWriter(discount, trust_cache=False) @@ -250,8 +255,9 @@ def update_discount_sheet(discount: models.Discount, trust_cache: bool) -> None: For individual updates, this approach should be avoided (instead, opting to update individual cells in the spreadsheet). """ - client = gspread.service_account(settings.OAUTH_JSON_CREDENTIALS) - wks: gspread.worksheet.Worksheet = client.open_by_key(discount.ga_key).sheet1 + assert settings.OAUTH_JSON_CREDENTIALS is not None, "gpread not configured?" + client = service_account(Path(settings.OAUTH_JSON_CREDENTIALS)) + wks: Worksheet = client.open_by_key(discount.ga_key).sheet1 participants = list( discount.participant_set.select_related("membership", "user").order_by("name") )