Skip to content

Commit

Permalink
try to make smart-ish API calls for Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelizimm committed Apr 16, 2024
1 parent 84bfa79 commit 75c49f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
54 changes: 28 additions & 26 deletions pins/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ def pin_write(
* Modifies content item to include any title and description changes.
* Adds access_type argument to specify who can see content. Defaults to "acl".
"""

f_super = super().pin_write

# bind the original signature to get pin name
Expand All @@ -918,6 +919,28 @@ def pin_write(
" can write to it."
)

# attempt to make the least number of API calls possible
if versioned or versioned is None and self.versioned:
# arbitrary number greater than 1
n_versions_before = 100
else:
try:
versions_df = self.pin_versions(pin_name, as_df=True)
versions = versions_df["version"].to_list()
n_versions_before = len(versions)
except PinsError:
# pin does not exist
n_versions_before = 0

if versioned is None:
versioned = True if n_versions_before > 1 else self.versioned

if versioned is False and n_versions_before > 1:
raise PinsVersionError(
"Pin is versioned, but you have requested a write without versions."
"To un-version a pin, you must delete it"
)

# run parent function ---
meta = f_super(*args, **kwargs)

Expand All @@ -931,32 +954,11 @@ def pin_write(
access_type=access_type or content["access_type"],
)

# return before more API calls if we already know it is versioned
if versioned or versioned is None and self.versioned:
return meta

versions_df = self.pin_versions(pin_name, as_df=True)
versions = versions_df["version"].to_list()

if versioned is None:
versioned = True if len(versions) > 2 else self.versioned

if versioned is False:
# clean up non-active pins in the case of an unversioned board
# a pin existed before the latest pin
if len(versions) == 2:
_log.info(
f"Replacing version '{versions}' with '{meta.version.version}'"
)
self.pin_version_delete(pin_name, versions[0])
if len(versions) > 2:
# delete what was just pinned-- we'd rather have this take a little longer
# write + delete erroneous pins than do all the API calls for a pin that doesn't need it
self.pin_version_delete(pin_name, versions[-1])
raise PinsVersionError(
"Pin is versioned, but you have requested a write without versions."
"To un-version a pin, you must delete it"
)
# clean up non-active pins in the case of an unversioned board
# a pin existed before the latest pin
if versioned is False and n_versions_before == 1:
_log.info(f"Replacing version '{versions}' with '{meta.version.version}'")
self.pin_version_delete(pin_name, versions[0])

return meta

Expand Down
4 changes: 2 additions & 2 deletions pins/tests/test_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ def test_board_versioned_pin_write_unversioned(board):
with pytest.raises(PinsVersionError) as e:
board.pin_write({"a": 3}, "test_pin", type="json", versioned=False)

assert len(board_unversioned.pin_versions("test_pin")) == 2
assert len(board.pin_versions("test_pin")) == 2
assert "Pin is versioned, but you have requested a write without versions." in e
assert board_unversioned.pin_read("test_pin") == {"a": 2}
assert board.pin_read("test_pin") == {"a": 2}


# pin_delete ==================================================================
Expand Down

0 comments on commit 75c49f0

Please sign in to comment.