Skip to content

Commit

Permalink
Upgrade gspread to latest
Browse files Browse the repository at this point in the history
This newer version of the library requires a `Path` object, not just a
`str`, so update accordingly.
  • Loading branch information
DavidCain committed May 10, 2024
1 parent 1ac1b12 commit f40e825
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
25 changes: 21 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ module = [
"debug_toolbar",
"factory.*",
"freezegun",
"gspread",
"kombu.*",
"localflavor.*",
"markdown2",
Expand Down
18 changes: 12 additions & 6 deletions ws/utils/member_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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")
)
Expand Down

0 comments on commit f40e825

Please sign in to comment.