Skip to content

Commit

Permalink
fix: saving upstream settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmccormick committed Jun 21, 2024
1 parent 4369245 commit b44240c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
6 changes: 5 additions & 1 deletion cms/djangoapps/contentstore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,14 @@ def _import_xml_node_to_parent(
if copied_from_block:
# Store a reference to where this block was copied from, in the 'copied_from_block' field (AuthoringMixin)
temp_xblock.copied_from_block = copied_from_block
copied_from_key = UsageKey.from_string(copied_from_block)
from cms.lib.xblock.authoring_mixin import is_block_valid_upstream # @@TODO move
print("1===========================")
print(copied_from_key)
if is_block_valid_upstream(copied_from_key):
print("2===========================")
upstream_link_requested = lambda: True ## @@TODO ask user
if upstream_link_requested()
if upstream_link_requested():
temp_xblock.set_upstream(copied_from_key, user_id)

# Save the XBlock into modulestore. We need to save the block and its parent for this to work:
Expand Down
38 changes: 26 additions & 12 deletions cms/lib/xblock/authoring_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.conf import settings
from web_fragments.fragment import Fragment
from xblock.core import XBlock, XBlockMixin
from xblock.fields import Integer, String, Scope, Dict
from xblock.fields import String, Scope

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,6 +56,8 @@ def visibility_view(self, _context=None):
# BEGIN CONTENT SYNC STUFF
# @@TODO move?
##########################
from xblock.fields import String, Integer, List, Dict
from opaque_keys.edx.keys import UsageKey

upstream = String(
scope=Scope.settings,
Expand Down Expand Up @@ -99,35 +101,43 @@ def visibility_view(self, _context=None):
enforce_type=True,
)

def set_upstream(self, upstream_key: UsageKey, user_id: int):
def set_upstream(self, upstream_key: UsageKey, user_id: int) -> None:
"""
@@TODO
"""
assert is_block_valid_upstream(upstream_key)
self.upstream = str(upstream_key)
self._sync_with_upstream(user_id=user_id, apply_updates=False))
self._sync_with_upstream(user_id=user_id, apply_updates=False)

def _sync_with_upstream(self, *, user_id: int, apply_updates: bool)
def _sync_with_upstream(self, *, user_id: int, apply_updates: bool) -> None:
"""
@@TODO
"""
upstream_key = UsageKey.from_string(self.upstream)
assert is_block_valid_upstream(upstream_key)
from openedx.core.djangoapps.content_libraries.api import get_library_block
from django.contrib.auth import get_user_model
from openedx.core.djangoapps.xblock.api import load_block
self.upstream_settings = {}
try:
upstream = load_block(upstream_key, get_user_model().objects.get(id=user_id)
upstream_version = get_library_block(upstream.usage_key).version_num
print("3==================")
upstream = load_block(upstream_key, get_user_model().objects.get(id=user_id))
upstream_version = get_library_block(upstream_key).version_num
except: # @@TODO handle missing
print("4a=================")
self.upstream_version = None
raise
return
print("4==================")
self.upstream_version = upstream_version
for field_name, field in upstream.fields.items():
if field.scope == Scope.settings:
value = getattr(upstream, field_name)
self.upstream_settings[field_name] = value
if apply_settings and field_name not in self.upstream_overidden:
print(field_name)
if apply_updates and field_name not in self.upstream_overidden:
setattr(self, field_name, value)
print("5==================")
print(self.upstream_settings)

#@XBlock.json_handler
#def upstream_info(self, _data=None, _suffix=None):
Expand All @@ -140,7 +150,7 @@ def _sync_with_upstream(self, *, user_id: int, apply_updates: bool)
# ... update info ...
# }

@XBlock._handler
@XBlock.handler
def update_from_upstream(self, request=None, suffix=None):
user_id = requester.user.id if request and request.user else 0
self._sync_with_upstream(user_id=user_id, apply_updates=True)
Expand All @@ -150,14 +160,18 @@ def save(self, *args, **kwargs):
"""
@@TODO
"""
for field_name, value in self.upstream_settings:
for field_name, value in self.upstream_settings.items():
if field_name not in self.upstream_overridden:
if value != getattr(block, field_name):
if value != getattr(self, field_name):
self.upstream_overridden.append(field_name)
super().save()


def is_block_valid_upstream(usage_key):
from opaque_keys.edx.keys import UsageKey
from opaque_keys.edx.locator import LibraryUsageLocatorV2


def is_block_valid_upstream(usage_key: UsageKey) -> bool:
"""
@@TODO move
"""
Expand Down

0 comments on commit b44240c

Please sign in to comment.