Skip to content

Commit

Permalink
revert: refactor: fetch_from_upstream -> get_from_upstream
Browse files Browse the repository at this point in the history
This reverts commit 3178fbe.
  • Loading branch information
kdmccormick committed Oct 6, 2024
1 parent 3178fbe commit 6b96952
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def get(self, request: Request, usage_key_string: str):
child_info = modulestore().get_item(child)
user_partition_info = get_visibility_partition_info(child_info, course=course)
user_partitions = get_user_partition_info(child_info, course=course)
upstream_link = UpstreamLink.try_get_for_block(child_info)
upstream_link = UpstreamLink.try_fetch_for_block(child_info)
validation_messages = get_xblock_validation_messages(child_info)
render_error = get_xblock_render_error(request, child_info)

Expand Down
4 changes: 2 additions & 2 deletions cms/djangoapps/contentstore/rest_api/v2/views/downstreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get(self, request: Request, usage_key_string: str) -> Response:
Inspect an XBlock's link to upstream content.
"""
downstream = _load_accessible_downstream_block(request.user, usage_key_string, require_write_access=False)
if link := UpstreamLink.try_get_for_block(downstream):
if link := UpstreamLink.try_fetch_for_block(downstream):
return Response(link.to_json())
raise NotFound(detail=f"Block '{usage_key_string}' is not found")

Expand Down Expand Up @@ -141,7 +141,7 @@ def post(self, request: Request, usage_key_string: str) -> Response:
except (BadUpstream, BadDownstream) as exc:
raise ValidationError(detail=str(exc)) from exc
modulestore().update_item(downstream, request.user.id)
return Response(UpstreamLink.get_for_block(downstream).to_json(), status=200)
return Response(UpstreamLink.fetch_for_block(downstream).to_json(), status=200)

def delete(self, request: Request, usage_key_string: str) -> Response:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
MOCK_UPSTREAM_ERROR = "your LibraryGPT subscription has expired"


def _get_upstream_link_good_and_syncable(downstream):
def _fetch_upstream_link_good_and_syncable(downstream):
return UpstreamLink(
upstream_ref=downstream.upstream,
version_synced=downstream.upstream_version,
Expand All @@ -24,7 +24,7 @@ def _get_upstream_link_good_and_syncable(downstream):
)


def _get_upstream_link_bad(_downstream):
def _fetch_upstream_link_bad(_downstream):
raise BadUpstream(MOCK_UPSTREAM_ERROR)


Expand All @@ -50,7 +50,7 @@ def setUp(self):
self.superuser = UserFactory(username="superuser", password="password", is_staff=True, is_superuser=True)
self.learner = UserFactory(username="learner", password="password")

@patch.object(UpstreamLink, "get_for_block", _get_upstream_link_good_and_syncable)
@patch.object(UpstreamLink, "fetch_for_block", _fetch_upstream_link_good_and_syncable)
def test_200_good_upstream(self):
"""
Does the happy path work?
Expand All @@ -62,7 +62,7 @@ def test_200_good_upstream(self):
assert response.data['error_message'] is None
assert response.data['ready_to_sync'] is True

@patch.object(UpstreamLink, "get_for_block", _get_upstream_link_bad)
@patch.object(UpstreamLink, "fetch_for_block", _fetch_upstream_link_bad)
def test_200_bad_upstream(self):
"""
If the upstream link is broken, do we still return 200, but with an error message in body?
Expand Down
10 changes: 5 additions & 5 deletions cms/lib/xblock/test/test_upstream_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ def test_prompt_and_decline_sync(self):
"""
# Initial conditions (pre-sync)
downstream = BlockFactory.create(category='html', parent=self.unit, upstream=str(self.upstream_key))
link = UpstreamLink.get_for_block(downstream)
link = UpstreamLink.fetch_for_block(downstream)
assert link.version_synced is None
assert link.version_declined is None
assert link.version_available == 2 # Library block with content starts at version 2
assert link.ready_to_sync is True

# Initial sync to V2
sync_from_upstream(downstream, self.user, apply_updates=True)
link = UpstreamLink.get_for_block(downstream)
link = UpstreamLink.fetch_for_block(downstream)
assert link.version_synced == 2
assert link.version_declined is None
assert link.version_available == 2
Expand All @@ -245,15 +245,15 @@ def test_prompt_and_decline_sync(self):
upstream = xblock.load_block(self.upstream_key, self.user)
upstream.data = "<html><body>Upstream content V3</body></html>"
upstream.save()
link = UpstreamLink.get_for_block(downstream)
link = UpstreamLink.fetch_for_block(downstream)
assert link.version_synced == 2
assert link.version_declined is None
assert link.version_available == 3
assert link.ready_to_sync is True

# Decline to sync to V3 -- ready_to_sync becomes False.
decline_sync(downstream)
link = UpstreamLink.get_for_block(downstream)
link = UpstreamLink.fetch_for_block(downstream)
assert link.version_synced == 2
assert link.version_declined == 3
assert link.version_available == 3
Expand All @@ -263,7 +263,7 @@ def test_prompt_and_decline_sync(self):
upstream = xblock.load_block(self.upstream_key, self.user)
upstream.data = "<html><body>Upstream content V4</body></html>"
upstream.save()
link = UpstreamLink.get_for_block(downstream)
link = UpstreamLink.fetch_for_block(downstream)
assert link.version_synced == 2
assert link.version_declined == 3
assert link.version_available == 4
Expand Down
14 changes: 7 additions & 7 deletions cms/lib/xblock/upstream_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def to_json(self) -> dict[str, t.Any]:
}

@classmethod
def try_get_for_block(cls, downstream: XBlock) -> t.Self | None:
def try_fetch_for_block(cls, downstream: XBlock) -> t.Self | None:
"""
Same as `get_for_block`, but upon failure, sets `.error_message` instead of raising an exception.
Same as `fetch_for_block`, but upon failure, sets `.error_message` instead of raising an exception.
"""
try:
return cls.get_for_block(downstream)
return cls.fetch_for_block(downstream)
except (BadDownstream, BadUpstream) as exc:
return cls(
upstream_ref=downstream.upstream,
Expand All @@ -91,7 +91,7 @@ def try_get_for_block(cls, downstream: XBlock) -> t.Self | None:
)

@classmethod
def get_for_block(cls, downstream: XBlock) -> t.Self | None:
def fetch_for_block(cls, downstream: XBlock) -> t.Self | None:
"""
Get info on a block's relationship with its linked upstream content (without actually loading the content).
Expand Down Expand Up @@ -143,7 +143,7 @@ def get_for_block(cls, downstream: XBlock) -> t.Self | None:
)


def sync_from_upstream(downstream: XBlock, user: AbstractUser) -> None:
def sync_from_upstream(downstream: XBlock, user: AbstractUser, *, apply_updates: bool) -> None:
"""
Given an XBlock that is linked to upstream content, fetch latest upstream fields and optionally apply them.
Expand All @@ -154,7 +154,7 @@ def sync_from_upstream(downstream: XBlock, user: AbstractUser) -> None:
If `downstream` is not linked at all, does nothing.
"""
# Try to load the upstream.
upstream_link = UpstreamLink.get_for_block(downstream) # Can raise BadUpstream or BadUpstream
upstream_link = UpstreamLink.fetch_for_block(downstream) # Can raise BadUpstream or BadUpstream
if not upstream_link:
return # No upstream -> nothing to sync.
try:
Expand Down Expand Up @@ -218,7 +218,7 @@ def decline_sync(downstream: XBlock) -> None:
If `downstream` is not linked at all, does nothing.
"""
# Try to load the upstream.
upstream_link = UpstreamLink.get_for_block(downstream) # Can raise BadUpstream or BadUpstream
upstream_link = UpstreamLink.fetch_for_block(downstream) # Can raise BadUpstream or BadUpstream
if not upstream_link:
return # No upstream -> nothing to sync.
downstream.upstream_version_declined = upstream_link.version_available
Expand Down

0 comments on commit 6b96952

Please sign in to comment.