Skip to content

Commit f12ff12

Browse files
committed
fix: linter and tests
1 parent a730dad commit f12ff12

File tree

9 files changed

+58
-38
lines changed

9 files changed

+58
-38
lines changed

cms/djangoapps/contentstore/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def import_staged_content_from_user_clipboard(parent_key: UsageKey, request) ->
323323

324324
return new_xblock, notices
325325

326+
326327
def import_staged_content_for_library_sync(new_xblock: XBlock, lib_block: XBlock, request) -> StaticFileNotices:
327328
"""
328329
Import a block (along with its children and any required static assets) from
@@ -355,7 +356,7 @@ def import_staged_content_for_library_sync(new_xblock: XBlock, lib_block: XBlock
355356
# Rewrite the OLX's static asset references to point to the new
356357
# locations for those assets. See _import_files_into_course for more
357358
# info on why this is necessary.
358-
if hasattr(new_xblock, 'data') and substitutions:
359+
if hasattr(new_xblock, "data") and substitutions:
359360
data_with_substitutions = new_xblock.data
360361
for old_static_ref, new_static_ref in substitutions.items():
361362
data_with_substitutions = data_with_substitutions.replace(

cms/djangoapps/contentstore/rest_api/v2/views/downstreams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"ready_to_sync": Boolean
5757
}
5858
"""
59-
from dataclasses import asdict
59+
6060
import logging
6161

6262
from attrs import asdict as attrs_asdict

cms/djangoapps/contentstore/rest_api/v2/views/tests/test_downstreams.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest.mock import patch
55
from django.conf import settings
66

7+
from cms.djangoapps.contentstore.helpers import StaticFileNotices
78
from cms.lib.xblock.upstream_sync import UpstreamLink, BadUpstream
89
from common.djangoapps.student.tests.factories import UserFactory
910
from xmodule.modulestore.django import modulestore
@@ -247,14 +248,16 @@ def call_api(self, usage_key_string):
247248

248249
@patch.object(UpstreamLink, "get_for_block", _get_upstream_link_good_and_syncable)
249250
@patch.object(downstreams_views, "sync_from_upstream")
250-
def test_200(self, mock_sync_from_upstream):
251+
@patch.object(downstreams_views, "import_staged_content_for_library_sync", return_value=StaticFileNotices())
252+
def test_200(self, mock_sync_from_upstream, mock_import_staged_content):
251253
"""
252254
Does the happy path work?
253255
"""
254256
self.client.login(username="superuser", password="password")
255257
response = self.call_api(self.downstream_video_key)
256258
assert response.status_code == 200
257259
assert mock_sync_from_upstream.call_count == 1
260+
assert mock_import_staged_content.call_count == 1
258261

259262

260263
class DeleteDownstreamSyncViewtest(_DownstreamSyncViewTestMixin, SharedModuleStoreTestCase):

cms/djangoapps/contentstore/xblock_storage_handlers/view_handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ def _create_block(request):
609609
return JsonResponse({"error": str(exc)}, status=400)
610610
static_file_notices = import_staged_content_for_library_sync(created_block, lib_block, request)
611611
modulestore().update_item(created_block, request.user.id)
612-
response['upstreamRef'] = upstream_ref
613-
response['static_file_notices'] = asdict(static_file_notices)
612+
response["upstreamRef"] = upstream_ref
613+
response["static_file_notices"] = asdict(static_file_notices)
614614

615615
return JsonResponse(response)
616616

openedx/core/djangoapps/content_staging/api.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
from xblock.core import XBlock
1515

1616
from openedx.core.lib.xblock_serializer.api import StaticFile, XBlockSerializer
17-
from openedx.core.djangoapps.content.course_overviews.api import get_course_overview_or_none
1817
from xmodule import block_metadata_utils
1918
from xmodule.contentstore.content import StaticContent
2019
from xmodule.contentstore.django import contentstore
2120

2221
from .data import (
2322
CLIPBOARD_PURPOSE,
2423
LIBRARY_SYNC_PURPOSE,
25-
StagedContentData, StagedContentFileData, StagedContentStatus, UserClipboardData, UserLibrarySyncData,
24+
StagedContentData,
25+
StagedContentFileData,
26+
StagedContentStatus,
27+
UserClipboardData,
28+
UserLibrarySyncData,
2629
)
2730
from .models import (
2831
UserClipboard as _UserClipboard,
@@ -40,10 +43,7 @@
4043

4144

4245
def _save_xblock_to_staged_content(
43-
block: XBlock,
44-
user_id: int,
45-
purpose: str,
46-
version_num: int | None = None
46+
block: XBlock, user_id: int, purpose: str, version_num: int | None = None
4747
) -> _StagedContent:
4848
"""
4949
Generic function to save an XBlock's OLX to staged content.
@@ -82,7 +82,7 @@ def _save_xblock_to_staged_content(
8282
)
8383

8484
# Log an event so we can analyze how this feature is used:
85-
log.info(f"Saved {usage_key.block_type} component \"{usage_key}\" to staged content for {purpose}.")
85+
log.info(f'Saved {usage_key.block_type} component "{usage_key}" to staged content for {purpose}.')
8686

8787
# Try to copy the static files. If this fails, we still consider the overall save attempt to have succeeded,
8888
# because intra-course operations will still work fine, and users can manually resolve file issues.
@@ -162,15 +162,14 @@ def save_xblock_to_user_clipboard(block: XBlock, user_id: int, version_num: int
162162
defaults={
163163
"content": staged_content,
164164
"source_usage_key": usage_key,
165-
}
165+
},
166166
)
167167

168168
return _user_clipboard_model_to_data(clipboard)
169169

170+
170171
def save_xblock_to_user_library_sync(
171-
block: XBlock,
172-
user_id: int,
173-
version_num: int | None = None
172+
block: XBlock, user_id: int, version_num: int | None = None
174173
) -> UserLibrarySyncData:
175174
"""
176175
Save an XBlock's OLX for library sync.
@@ -184,11 +183,12 @@ def save_xblock_to_user_library_sync(
184183
defaults={
185184
"content": staged_content,
186185
"source_usage_key": usage_key,
187-
}
186+
},
188187
)
189188

190189
return _user_library_sync_model_to_data(sync)
191190

191+
192192
def get_user_clipboard(user_id: int, only_ready: bool = True) -> UserClipboardData | None:
193193
"""
194194
Get the details of the user's clipboard.
@@ -251,16 +251,11 @@ def get_user_library_sync_json(user_id: int, request: HttpRequest | None = None)
251251
sync = _UserLibrarySync.objects.get(user_id=user_id)
252252
except _UserLibrarySync.DoesNotExist:
253253
# This user does not have any library sync content.
254-
return {
255-
"content": None,
256-
"source_usage_key": "",
257-
"source_context_title": "",
258-
"source_edit_url": ""
259-
}
254+
return {"content": None, "source_usage_key": "", "source_context_title": "", "source_edit_url": ""}
260255

261256
serializer = _UserLibrarySyncSerializer(
262257
_user_library_sync_model_to_data(sync),
263-
context={'request': request},
258+
context={"request": request},
264259
)
265260
return serializer.data
266261

@@ -281,6 +276,7 @@ def _staged_content_to_data(content: _StagedContent) -> StagedContentData:
281276
version_num=content.version_num,
282277
)
283278

279+
284280
def _user_clipboard_model_to_data(clipboard: _UserClipboard) -> UserClipboardData:
285281
"""
286282
Convert a UserClipboard model instance to an immutable data object.
@@ -291,6 +287,7 @@ def _user_clipboard_model_to_data(clipboard: _UserClipboard) -> UserClipboardDat
291287
source_context_title=clipboard.get_source_context_title(),
292288
)
293289

290+
294291
def _user_library_sync_model_to_data(sync: _UserLibrarySync) -> UserLibrarySyncData:
295292
"""
296293
Convert a UserLibrarySync model instance to an immutable data object.

openedx/core/djangoapps/content_staging/data.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ def source_context_key(self) -> LearningContextKey:
7777
""" Get the context (course/library) that this was copied from """
7878
return self.source_usage_key.context_key
7979

80+
8081
@frozen
8182
class UserLibrarySyncData:
82-
""" Read-only data model for User Library Sync data """
83+
"""Read-only data model for User Library Sync data"""
84+
8385
content: StagedContentData = field(validator=validators.instance_of(StagedContentData))
84-
source_usage_key: UsageKey = field(validator=validators.instance_of(UsageKey))
86+
source_usage_key: UsageKey = field(validator=validators.instance_of(UsageKey)) # type: ignore[type-abstract]
8587
source_context_title: str
8688

8789
@property
8890
def source_context_key(self) -> LearningContextKey:
89-
""" Get the context (course/library) that this was copied from """
91+
"""Get the context (course/library) that this was copied from"""
9092
return self.source_usage_key.context_key

openedx/core/djangoapps/content_staging/migrations/0006_userlibrarysync.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,33 @@
99
class Migration(migrations.Migration):
1010

1111
dependencies = [
12-
('auth', '0012_alter_user_first_name_max_length'),
13-
('content_staging', '0005_stagedcontent_version_num'),
12+
("auth", "0012_alter_user_first_name_max_length"),
13+
("content_staging", "0005_stagedcontent_version_num"),
1414
]
1515

1616
operations = [
1717
migrations.CreateModel(
18-
name='UserLibrarySync',
18+
name="UserLibrarySync",
1919
fields=[
20-
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
21-
('source_usage_key', opaque_keys.edx.django.models.UsageKeyField(help_text='Original usage key/ID of the thing that is being synced.', max_length=255)),
22-
('content', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='content_staging.stagedcontent')),
20+
(
21+
"user",
22+
models.OneToOneField(
23+
on_delete=django.db.models.deletion.CASCADE,
24+
primary_key=True,
25+
serialize=False,
26+
to=settings.AUTH_USER_MODEL,
27+
),
28+
),
29+
(
30+
"source_usage_key",
31+
opaque_keys.edx.django.models.UsageKeyField(
32+
help_text="Original usage key/ID of the thing that is being synced.", max_length=255
33+
),
34+
),
35+
(
36+
"content",
37+
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="content_staging.stagedcontent"),
38+
),
2339
],
2440
),
2541
]

openedx/core/djangoapps/content_staging/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,26 +151,26 @@ class UserLibrarySync(models.Model):
151151
Each user can trigger a sync from a library component to that component in a course.
152152
This model is used to facilitate that and to ease tracking.
153153
"""
154+
154155
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
155156
content = models.ForeignKey(StagedContent, on_delete=models.CASCADE)
156157
source_usage_key = UsageKeyField(
157158
max_length=255,
158159
help_text=_("Original usage key/ID of the thing that is being synced."),
159160
)
160161

161-
162162
@property
163163
def source_context_key(self) -> LearningContextKey:
164-
""" Get the context (library) that this was copied from """
164+
"""Get the context (library) that this was copied from"""
165165
return self.source_usage_key.context_key
166166

167167
def get_source_context_title(self) -> str:
168-
""" Get the title of the source context, if any """
168+
"""Get the title of the source context, if any"""
169169
# Just return the ID as the name, since it can only be a library
170170
return str(self.source_context_key)
171171

172172
def clean(self):
173-
""" Check that this model is being used correctly. """
173+
"""Check that this model is being used correctly."""
174174
# These could probably be replaced with constraints in Django 4.1+
175175
if self.user.id != self.content.user.id:
176176
raise ValidationError("User ID mismatch.")
@@ -180,7 +180,7 @@ def clean(self):
180180
)
181181

182182
def save(self, *args, **kwargs):
183-
""" Save this model instance """
183+
"""Save this model instance"""
184184
# Enforce checks on save:
185185
self.full_clean()
186186
return super().save(*args, **kwargs)

openedx/core/djangoapps/content_staging/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class UserLibrarySyncSerializer(serializers.Serializer):
8383
"""
8484
Serializer for the status of the user's library sync (a UserLibrarySyncData instance)
8585
"""
86+
8687
content = StagedContentSerializer(allow_null=True)
8788
source_usage_key = serializers.CharField(allow_blank=True)
8889
# The title of the course that the content came from originally, if relevant

0 commit comments

Comments
 (0)