Skip to content

Commit e3823d7

Browse files
authored
Merge pull request #3407 from rtibbles/internal_metadata
Add support to ricecooker endpoint for metadata labels.
2 parents 392e218 + 3340265 commit e3823d7

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

contentcuration/contentcuration/tests/views/test_views_internal.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
from django.db import connections
99
from django.urls import reverse_lazy
1010
from le_utils.constants import format_presets
11+
from le_utils.constants.labels.accessibility_categories import ACCESSIBILITYCATEGORIESLIST
12+
from le_utils.constants.labels.learning_activities import LEARNINGACTIVITIESLIST
13+
from le_utils.constants.labels.levels import LEVELSLIST
14+
from le_utils.constants.labels.needs import NEEDSLIST
15+
from le_utils.constants.labels.resource_type import RESOURCETYPELIST
16+
from le_utils.constants.labels.subjects import SUBJECTSLIST
1117
from mixer.main import mixer
1218
from mock import patch
1319
from rest_framework.test import APIClient
@@ -44,6 +50,16 @@ class SampleContentNodeDataSchema:
4450
copyright_holder = str
4551

4652

53+
METADATA = {
54+
"grade_levels": LEVELSLIST,
55+
"resource_types": RESOURCETYPELIST,
56+
"learning_activities": LEARNINGACTIVITIESLIST,
57+
"accessibility_labels": ACCESSIBILITYCATEGORIESLIST,
58+
"categories": SUBJECTSLIST,
59+
"learner_needs": NEEDSLIST,
60+
}
61+
62+
4763
class ApiAddNodesToTreeTestCase(StudioTestCase):
4864
"""
4965
Tests for contentcuration.views.internal.api_add_nodes_to_tree function.
@@ -110,13 +126,19 @@ def setUp(self):
110126
invalid_copyright_holder["title"] = "invalid_copyright_holder"
111127
invalid_copyright_holder["copyright_holder"] = ""
112128

129+
valid_metadata_labels = self._make_node_data()
130+
valid_metadata_labels["title"] = "valid_metadata_labels"
131+
for label, values in METADATA.items():
132+
valid_metadata_labels[label] = [values[0]]
133+
113134
self.sample_data = {
114135
"root_id": self.root_node.id,
115136
"content_data": [
116137
valid_node,
117138
invalid_title_node,
118139
invalid_license_description,
119140
invalid_copyright_holder,
141+
valid_metadata_labels,
120142
],
121143
}
122144
self.resp = self.admin_client().post(
@@ -166,6 +188,13 @@ def test_associates_file_with_created_node(self):
166188
# our original file object
167189
assert f.file_on_disk.read() == self.fileobj.file_on_disk.read()
168190

191+
def test_metadata_properly_created(self):
192+
node = ContentNode.objects.get(title="valid_metadata_labels")
193+
for label, values in METADATA.items():
194+
self.assertEqual(getattr(node, label), {
195+
values[0]: True
196+
})
197+
169198
def test_invalid_nodes_are_not_complete(self):
170199
node_0 = ContentNode.objects.get(title=self.title)
171200
node_1 = ContentNode.objects.get(description="invalid_title_node")
@@ -196,6 +225,24 @@ def test_tag_greater_than_30_chars_excluded(self):
196225

197226
self.assertEqual(response.status_code, 400, response.content)
198227

228+
def test_invalid_metadata_label_excluded(self):
229+
invalid_metadata_labels = self._make_node_data()
230+
invalid_metadata_labels["title"] = "invalid_metadata_labels"
231+
invalid_metadata_labels["categories"] = ["not a label!"]
232+
233+
test_data = {
234+
"root_id": self.root_node.id,
235+
"content_data": [
236+
invalid_metadata_labels,
237+
],
238+
}
239+
240+
response = self.admin_client().post(
241+
reverse_lazy("api_add_nodes_to_tree"), data=test_data, format="json"
242+
)
243+
244+
self.assertEqual(response.status_code, 400, response.content)
245+
199246

200247
class ApiAddExerciseNodesToTreeTestCase(StudioTestCase):
201248
"""

contentcuration/contentcuration/views/internal.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
from django.http import JsonResponse
1717
from le_utils.constants import content_kinds
1818
from le_utils.constants import roles
19+
from le_utils.constants.labels.accessibility_categories import ACCESSIBILITYCATEGORIESLIST
20+
from le_utils.constants.labels.learning_activities import LEARNINGACTIVITIESLIST
21+
from le_utils.constants.labels.levels import LEVELSLIST
22+
from le_utils.constants.labels.needs import NEEDSLIST
23+
from le_utils.constants.labels.resource_type import RESOURCETYPELIST
24+
from le_utils.constants.labels.subjects import SUBJECTSLIST
1925
from past.builtins import basestring
2026
from raven.contrib.django.raven_compat.models import client
2127
from rest_framework import status
@@ -66,7 +72,7 @@
6672
)
6773

6874

69-
class NodeValidationError(Exception):
75+
class NodeValidationError(ValidationError):
7076
pass
7177

7278

@@ -598,6 +604,16 @@ def convert_data_to_nodes(user, content_data, parent_node):
598604
raise ObjectDoesNotExist("Error creating node: {0}".format(e))
599605

600606

607+
METADATA = {
608+
"grade_levels": set(LEVELSLIST),
609+
"resource_types": set(RESOURCETYPELIST),
610+
"learning_activities": set(LEARNINGACTIVITIESLIST),
611+
"accessibility_labels": set(ACCESSIBILITYCATEGORIESLIST),
612+
"categories": set(SUBJECTSLIST),
613+
"learner_needs": set(NEEDSLIST),
614+
}
615+
616+
601617
def create_node(node_data, parent_node, sort_order): # noqa: C901
602618
""" Generate node based on node dict """
603619
# Make sure license is valid
@@ -632,6 +648,18 @@ def create_node(node_data, parent_node, sort_order): # noqa: C901
632648
if license.copyright_holder_required:
633649
is_complete &= copyright_holder != ""
634650

651+
metadata_labels = {}
652+
653+
for label, valid_values in METADATA.items():
654+
if label in node_data:
655+
if type(node_data[label]) is not list:
656+
raise NodeValidationError("{} must pass a list of values".format(label))
657+
metadata_labels[label] = {}
658+
for value in node_data[label]:
659+
if value not in valid_values:
660+
raise NodeValidationError("{} is not a valid value for {}".format(value, label))
661+
metadata_labels[label][value] = True
662+
635663
node = ContentNode.objects.create(
636664
title=title,
637665
tree_id=parent_node.tree_id,
@@ -654,6 +682,7 @@ def create_node(node_data, parent_node, sort_order): # noqa: C901
654682
freeze_authoring_data=True,
655683
role_visibility=node_data.get('role') or roles.LEARNER,
656684
complete=is_complete,
685+
**metadata_labels
657686
)
658687

659688
tags = []

0 commit comments

Comments
 (0)