Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance tests and add special handling for mptt fields. #4251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions contentcuration/kolibri_public/import_metadata_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _error_message(self, low):
)
return error

def retrieve(self, request, pk=None):
def retrieve(self, request, pk=None): # noqa: C901
"""
An endpoint to retrieve all content metadata required for importing a content node
all of its ancestors, and any relevant needed metadata.
Expand Down Expand Up @@ -139,8 +139,18 @@ def retrieve(self, request, pk=None):
# Map the table name from the kolibri_public table name to the equivalent Kolibri table name
table_name = kc_model._meta.db_table
# Tweak our introspection here to rely on Django model meta instead of SQLAlchemy
# Read from the base_models here, as those are the ones required for this schema version
raw_fields = [field.column for field in base_model._meta.fields]
# Read valid field names from the combination of the base model, and the mptt tree fields
# of the kc_model - because the base model is abstract, it does not get the mptt fields applied
# to its meta fields attribute, so we need to read the actual fields from the kc_model, but filter
# them only to names valid for the base model.
field_names = {field.column for field in base_model._meta.fields}
if hasattr(base_model, "_mptt_meta"):
field_names.add(base_model._mptt_meta.parent_attr)
field_names.add(base_model._mptt_meta.tree_id_attr)
field_names.add(base_model._mptt_meta.left_attr)
field_names.add(base_model._mptt_meta.right_attr)
field_names.add(base_model._mptt_meta.level_attr)
raw_fields = [field.column for field in kc_model._meta.fields if field.column in field_names]
if qs.model is models.Language:
raw_fields = [rf for rf in raw_fields if rf != "lang_name"] + ["native_name"]
qs = qs.values(*raw_fields)
Expand Down
14 changes: 12 additions & 2 deletions contentcuration/kolibri_public/tests/test_importmetadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.db.models import Q
from django.urls import reverse
from django.utils.http import http_date
from kolibri_content import base_models
from kolibri_content import models as content
from kolibri_content.constants.schema_versions import CONTENT_SCHEMA_VERSION
from kolibri_public import models as public
Expand Down Expand Up @@ -44,11 +45,20 @@ def _assert_data(self, Model, ContentModel, queryset):
response = self.client.get(
reverse("publicimportmetadata-detail", kwargs={"pk": self.node.id})
)
fields = Model._meta.fields
BaseModel = getattr(base_models, Model.__name__, Model)
field_names = {field.column for field in BaseModel._meta.fields}
if hasattr(BaseModel, "_mptt_meta"):
field_names.add(BaseModel._mptt_meta.parent_attr)
field_names.add(BaseModel._mptt_meta.tree_id_attr)
field_names.add(BaseModel._mptt_meta.left_attr)
field_names.add(BaseModel._mptt_meta.right_attr)
field_names.add(BaseModel._mptt_meta.level_attr)
for response_data, obj in zip(response.data[ContentModel._meta.db_table], queryset):
# Ensure that we are not returning any empty objects
self.assertNotEqual(response_data, {})
for field in Model._meta.fields:
if field.column in response_data:
for field in fields:
if field.column in field_names:
value = response_data[field.column]
if hasattr(field, "from_db_value"):
value = field.from_db_value(value, None, connection)
Expand Down