Skip to content

Commit 7975859

Browse files
authored
Merge pull request #6174 from rtibblesbot/issue-6172-298b5a
fix: serve the requested schema version's columns from importmetadata
2 parents d10c2a3 + 5c49d4b commit 7975859

6 files changed

Lines changed: 335 additions & 78 deletions

File tree

‎contentcuration/kolibri_content/constants/schema_versions.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
This is a direct copy of the constants file of the same name in kolibri.core.content
33
https://github.com/learningequality/kolibri/blob/c7417e1d558a1e1e52ac8423927d61a0e44da576/kolibri/core/content/constants/schema_versions.py
4+
EXPORT_SCHEMA_VERSIONS is copied from
5+
https://github.com/learningequality/kolibri/blob/deeec22db9f9e607714bb8ea2c8b3b377f02a5ab/kolibri/core/content/constants/schema_versions.py
46
"""
57

68
V020BETA1 = "v0.2.0-beta1"
@@ -48,3 +50,11 @@
4850
# exceptionally difficult for us to backfill, such as deleting a model field that
4951
# we cannot meaningfully infer the content of from other metadata.
5052
MIN_CONTENT_SCHEMA_VERSION = VERSION_5
53+
54+
# Every version we can provide import metadata for, oldest first.
55+
EXPORT_SCHEMA_VERSIONS = [
56+
str(version)
57+
for version in range(
58+
int(MIN_CONTENT_SCHEMA_VERSION), int(CONTENT_SCHEMA_VERSION) + 1
59+
)
60+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
This is a copy with modifications of the file:
3+
https://github.com/learningequality/kolibri/blob/deeec22db9f9e607714bb8ea2c8b3b377f02a5ab/kolibri/core/content/contentschema/columns/__init__.py
4+
"""
5+
import importlib
6+
7+
from kolibri_content.constants.schema_versions import EXPORT_SCHEMA_VERSIONS
8+
9+
10+
def for_version(version):
11+
"""
12+
A content schema version's table names mapped to its column names, in
13+
declaration order.
14+
"""
15+
if version not in EXPORT_SCHEMA_VERSIONS:
16+
raise ValueError(f"Unknown content schema version {version}")
17+
return importlib.import_module(".content_columns_" + version, __name__).COLUMNS
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
This is a direct copy of the file of the same name in kolibri.core.content.contentschema.columns
3+
https://github.com/learningequality/kolibri/blob/deeec22db9f9e607714bb8ea2c8b3b377f02a5ab/kolibri/core/content/contentschema/columns/content_columns_5.py
4+
"""
5+
6+
COLUMNS = {
7+
"content_assessmentmetadata": (
8+
"id",
9+
"assessment_item_ids",
10+
"number_of_assessments",
11+
"mastery_model",
12+
"randomize",
13+
"is_manipulable",
14+
"contentnode_id",
15+
),
16+
"content_channelmetadata": (
17+
"id",
18+
"name",
19+
"description",
20+
"tagline",
21+
"author",
22+
"version",
23+
"thumbnail",
24+
"last_updated",
25+
"min_schema_version",
26+
"root_id",
27+
),
28+
"content_contentnode": (
29+
"id",
30+
"license_name",
31+
"license_description",
32+
"title",
33+
"coach_content",
34+
"content_id",
35+
"channel_id",
36+
"description",
37+
"sort_order",
38+
"license_owner",
39+
"author",
40+
"kind",
41+
"available",
42+
"options",
43+
"grade_levels",
44+
"resource_types",
45+
"learning_activities",
46+
"accessibility_labels",
47+
"categories",
48+
"learner_needs",
49+
"duration",
50+
"lft",
51+
"rght",
52+
"tree_id",
53+
"level",
54+
"lang_id",
55+
"parent_id",
56+
),
57+
"content_contentnode_has_prerequisite": (
58+
"id",
59+
"from_contentnode_id",
60+
"to_contentnode_id",
61+
),
62+
"content_contentnode_related": ("id", "from_contentnode_id", "to_contentnode_id"),
63+
"content_contentnode_tags": ("id", "contentnode_id", "contenttag_id"),
64+
"content_contenttag": ("id", "tag_name"),
65+
"content_file": (
66+
"id",
67+
"preset",
68+
"supplementary",
69+
"thumbnail",
70+
"priority",
71+
"contentnode_id",
72+
"lang_id",
73+
"local_file_id",
74+
),
75+
"content_language": (
76+
"id",
77+
"lang_code",
78+
"lang_subcode",
79+
"lang_name",
80+
"lang_direction",
81+
),
82+
"content_localfile": ("id", "extension", "available", "file_size"),
83+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
This is a direct copy of the file of the same name in kolibri.core.content.contentschema.columns
3+
https://github.com/learningequality/kolibri/blob/deeec22db9f9e607714bb8ea2c8b3b377f02a5ab/kolibri/core/content/contentschema/columns/content_columns_6.py
4+
"""
5+
6+
COLUMNS = {
7+
"content_assessmentmetadata": (
8+
"id",
9+
"assessment_item_ids",
10+
"number_of_assessments",
11+
"mastery_model",
12+
"randomize",
13+
"is_manipulable",
14+
"contentnode_id",
15+
),
16+
"content_channelmetadata": (
17+
"id",
18+
"name",
19+
"description",
20+
"tagline",
21+
"author",
22+
"version",
23+
"thumbnail",
24+
"last_updated",
25+
"min_schema_version",
26+
"root_id",
27+
),
28+
"content_contentnode": (
29+
"id",
30+
"license_name",
31+
"license_description",
32+
"title",
33+
"coach_content",
34+
"content_id",
35+
"channel_id",
36+
"description",
37+
"sort_order",
38+
"license_owner",
39+
"author",
40+
"kind",
41+
"available",
42+
"options",
43+
"grade_levels",
44+
"resource_types",
45+
"learning_activities",
46+
"accessibility_labels",
47+
"categories",
48+
"learner_needs",
49+
"duration",
50+
"lft",
51+
"rght",
52+
"tree_id",
53+
"level",
54+
"lang_id",
55+
"parent_id",
56+
),
57+
"content_contentnode_has_prerequisite": (
58+
"id",
59+
"from_contentnode_id",
60+
"to_contentnode_id",
61+
),
62+
"content_contentnode_related": ("id", "from_contentnode_id", "to_contentnode_id"),
63+
"content_contentnode_tags": ("id", "contentnode_id", "contenttag_id"),
64+
"content_contenttag": ("id", "tag_name"),
65+
"content_file": (
66+
"id",
67+
"preset",
68+
"supplementary",
69+
"thumbnail",
70+
"priority",
71+
"included_presets",
72+
"contentnode_id",
73+
"lang_id",
74+
"local_file_id",
75+
),
76+
"content_language": (
77+
"id",
78+
"lang_code",
79+
"lang_subcode",
80+
"lang_name",
81+
"lang_direction",
82+
),
83+
"content_localfile": ("id", "extension", "available", "file_size_bigint"),
84+
}

‎contentcuration/kolibri_public/import_metadata_view.py‎

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
from django.db import connection
88
from django.db.models import Q
9-
from django.http import HttpResponseBadRequest
109
from django.utils.decorators import method_decorator
11-
from kolibri_content import base_models
1210
from kolibri_content import models as kolibri_content_models
1311
from kolibri_content.constants.schema_versions import (
1412
CONTENT_SCHEMA_VERSION,
1513
) # Use kolibri_content
14+
from kolibri_content.constants.schema_versions import EXPORT_SCHEMA_VERSIONS
1615
from kolibri_content.constants.schema_versions import (
1716
MIN_CONTENT_SCHEMA_VERSION,
1817
) # Use kolibri_content
18+
from kolibri_content.contentschema.columns import for_version
1919
from kolibri_public import models # Use kolibri_public models
2020
from kolibri_public.views import metadata_cache
2121
from rest_framework import status
22+
from rest_framework.exceptions import ValidationError
2223
from rest_framework.generics import get_object_or_404
2324
from rest_framework.permissions import AllowAny
2425
from rest_framework.response import Response
@@ -27,20 +28,26 @@
2728
from contentcuration.utils.pagination import ValuesViewsetCursorPagination
2829

2930

30-
def _get_kc_and_base_models(model):
31+
# Clients that send no schema_version predate kolibri#15358 and may read any supported version.
32+
DEFAULT_COLUMNS = {
33+
table: tuple(
34+
dict.fromkeys(
35+
column
36+
for version in EXPORT_SCHEMA_VERSIONS
37+
for column in for_version(version)[table]
38+
)
39+
)
40+
for table in for_version(CONTENT_SCHEMA_VERSION)
41+
}
42+
43+
44+
def _get_kc_model(model):
3145
try:
32-
kc_model = getattr(kolibri_content_models, model.__name__)
33-
base_model = getattr(base_models, model.__name__)
46+
return getattr(kolibri_content_models, model.__name__)
3447
except AttributeError:
3548
# This will happen if it's a M2M through model, which only exist on ContentNode
3649
through_model_name = model.__name__.replace("ContentNode_", "")
37-
kc_model = getattr(
38-
kolibri_content_models.ContentNode, through_model_name
39-
).through
40-
# Through models are not defined for the abstract base models, so we just cheat and
41-
# use these instead.
42-
base_model = kc_model
43-
return kc_model, base_model
50+
return getattr(kolibri_content_models.ContentNode, through_model_name).through
4451

4552

4653
class ImportMetadataPagination(ValuesViewsetCursorPagination):
@@ -75,7 +82,20 @@ def _error_message(self, low):
7582
)
7683
return error
7784

78-
def retrieve(self, request, pk=None): # noqa: C901
85+
def _validate_content_schema(self, content_schema):
86+
try:
87+
version = int(content_schema)
88+
except ValueError as e:
89+
raise ValidationError(
90+
"Schema version is not parseable by this version of Kolibri"
91+
) from e
92+
if version > int(self.default_content_schema):
93+
raise ValidationError(self._error_message(False))
94+
if version < int(self.min_content_schema):
95+
raise ValidationError(self._error_message(True))
96+
return str(version)
97+
98+
def retrieve(self, request, pk=None):
7999
"""
80100
An endpoint to retrieve all content metadata required for importing a content node
81101
all of its ancestors, and any relevant needed metadata.
@@ -92,24 +112,13 @@ def retrieve(self, request, pk=None): # noqa: C901
92112
{"error": "Invalid UUID format."}, status=status.HTTP_400_BAD_REQUEST
93113
)
94114

95-
content_schema = request.query_params.get(
96-
"schema_version", self.default_content_schema
97-
)
98-
99-
try:
100-
if int(content_schema) > int(self.default_content_schema):
101-
return HttpResponseBadRequest(self._error_message(False))
102-
if int(content_schema) < int(self.min_content_schema):
103-
return HttpResponseBadRequest(self._error_message(True))
104-
# Remove reference to SQLAlchemy schema bases
105-
except ValueError:
106-
return HttpResponseBadRequest(
107-
"Schema version is not parseable by this version of Kolibri"
108-
)
109-
except AttributeError:
110-
return HttpResponseBadRequest(
111-
"Schema version is not known by this version of Kolibri"
112-
)
115+
content_schema = request.query_params.get("schema_version")
116+
if content_schema is None:
117+
content_schema = self.default_content_schema
118+
columns = DEFAULT_COLUMNS
119+
else:
120+
content_schema = self._validate_content_schema(content_schema)
121+
columns = for_version(content_schema)
113122

114123
# Get the model for the target node here - we do this so that we trigger a 404 immediately if the node
115124
# does not exist.
@@ -163,32 +172,17 @@ def retrieve(self, request, pk=None): # noqa: C901
163172
related,
164173
channel_metadata,
165174
]:
166-
# First get the kolibri_content model and base model to which this is equivalent
167-
kc_model, base_model = _get_kc_and_base_models(qs.model)
168175
# Map the table name from the kolibri_public table name to the equivalent Kolibri table name
169-
table_name = kc_model._meta.db_table
170-
# Tweak our introspection here to rely on Django model meta instead of SQLAlchemy
171-
# Read valid field names from the combination of the base model, and the mptt tree fields
172-
# of the kc_model - because the base model is abstract, it does not get the mptt fields applied
173-
# to its meta fields attribute, so we need to read the actual fields from the kc_model, but filter
174-
# them only to names valid for the base model.
175-
field_names = {field.column for field in base_model._meta.fields}
176-
if hasattr(base_model, "_mptt_meta"):
177-
field_names.add(base_model._mptt_meta.parent_attr)
178-
field_names.add(base_model._mptt_meta.tree_id_attr)
179-
field_names.add(base_model._mptt_meta.left_attr)
180-
field_names.add(base_model._mptt_meta.right_attr)
181-
field_names.add(base_model._mptt_meta.level_attr)
182-
raw_fields = [
183-
field.column
184-
for field in kc_model._meta.fields
185-
if field.column in field_names
186-
]
176+
table_name = _get_kc_model(qs.model)._meta.db_table
177+
raw_fields = columns[table_name]
178+
value_fields = raw_fields
187179
if qs.model is models.Language:
188-
raw_fields = [rf for rf in raw_fields if rf != "lang_name"] + [
189-
"native_name"
180+
# Studio's Language keeps the name Kolibri calls lang_name in native_name
181+
value_fields = [
182+
"native_name" if field == "lang_name" else field
183+
for field in raw_fields
190184
]
191-
qs = qs.values(*raw_fields)
185+
qs = qs.values(*value_fields)
192186
# Avoid using the Django queryset directly, as it will coerce the database values
193187
# via its field 'from_db_value' transformers, whereas import metadata is read
194188
# directly from the database.
@@ -208,10 +202,6 @@ def retrieve(self, request, pk=None): # noqa: C901
208202
)
209203
for row in cursor
210204
]
211-
if qs.model is models.Language:
212-
for lang in data[table_name]:
213-
lang["lang_name"] = lang["native_name"]
214-
del lang["native_name"]
215205

216206
data["schema_version"] = content_schema
217207

0 commit comments

Comments
 (0)