Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
566bb162
Commit
566bb162
authored
Oct 15, 2013
by
Ned Batchelder
Committed by
Calen Pennington
Jan 14, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement InheritanceFieldData
parent
1932c126
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
112 additions
and
4 deletions
+112
-4
common/lib/xmodule/xmodule/modulestore/inheritance.py
+29
-2
common/lib/xmodule/xmodule/tests/test_xml_module.py
+83
-2
No files found.
common/lib/xmodule/xmodule/modulestore/inheritance.py
View file @
566bb162
...
@@ -118,8 +118,35 @@ def own_metadata(module):
...
@@ -118,8 +118,35 @@ def own_metadata(module):
return
module
.
get_explicitly_set_fields_by_scope
(
Scope
.
settings
)
return
module
.
get_explicitly_set_fields_by_scope
(
Scope
.
settings
)
class
InheritanceFieldData
(
KvsFieldData
):
class
InheritingFieldData
(
KvsFieldData
):
pass
"""A `FieldData` implementation that can inherit value from parents to children."""
def
__init__
(
self
,
inheritable_names
,
**
kwargs
):
"""
`inheritable_names` is a list of names that can be inherited from
parents.
"""
super
(
InheritingFieldData
,
self
)
.
__init__
(
**
kwargs
)
self
.
inheritable_names
=
set
(
inheritable_names
)
def
default
(
self
,
block
,
name
):
"""
The default for an inheritable name is found on a parent.
"""
if
name
in
self
.
inheritable_names
and
block
.
parent
is
not
None
:
parent
=
block
.
get_parent
()
if
parent
:
return
getattr
(
parent
,
name
)
super
(
InheritingFieldData
,
self
)
.
default
(
block
,
name
)
def
inheriting_field_data
(
kvs
):
"""Create an InheritanceFieldData that inherits the names in InheritanceMixin."""
return
InheritingFieldData
(
inheritable_names
=
InheritanceMixin
.
fields
.
keys
(),
kvs
=
kvs
,
)
class
InheritanceKeyValueStore
(
KeyValueStore
):
class
InheritanceKeyValueStore
(
KeyValueStore
):
...
...
common/lib/xmodule/xmodule/tests/test_xml_module.py
View file @
566bb162
...
@@ -8,10 +8,10 @@ from nose.tools import assert_equals, assert_not_equals, assert_true, assert_fal
...
@@ -8,10 +8,10 @@ from nose.tools import assert_equals, assert_not_equals, assert_true, assert_fal
from
xblock.field_data
import
DictFieldData
from
xblock.field_data
import
DictFieldData
from
xblock.fields
import
Scope
,
String
,
Dict
,
Boolean
,
Integer
,
Float
,
Any
,
List
from
xblock.fields
import
Scope
,
String
,
Dict
,
Boolean
,
Integer
,
Float
,
Any
,
List
from
xblock.runtime
import
KvsFieldData
from
xblock.runtime
import
KvsFieldData
,
DictKeyValueStore
from
xmodule.fields
import
Date
,
Timedelta
,
RelativeTime
from
xmodule.fields
import
Date
,
Timedelta
,
RelativeTime
from
xmodule.modulestore.inheritance
import
InheritanceKeyValueStore
,
InheritanceMixin
from
xmodule.modulestore.inheritance
import
InheritanceKeyValueStore
,
InheritanceMixin
,
InheritingFieldData
from
xmodule.xml_module
import
XmlDescriptor
,
serialize_field
,
deserialize_field
from
xmodule.xml_module
import
XmlDescriptor
,
serialize_field
,
deserialize_field
from
xmodule.course_module
import
CourseDescriptor
from
xmodule.course_module
import
CourseDescriptor
from
xmodule.seq_module
import
SequenceDescriptor
from
xmodule.seq_module
import
SequenceDescriptor
...
@@ -58,6 +58,87 @@ class TestFields(object):
...
@@ -58,6 +58,87 @@ class TestFields(object):
# Used for testing Lists
# Used for testing Lists
list_field
=
List
(
scope
=
Scope
.
settings
,
default
=
[])
list_field
=
List
(
scope
=
Scope
.
settings
,
default
=
[])
class
InheritingFieldDataTest
(
unittest
.
TestCase
):
"""Tests of InheritingFieldData."""
class
TestableInheritingXBlock
(
XmlDescriptor
):
"""An XBlock we can use in these tests."""
inherited
=
String
(
scope
=
Scope
.
settings
,
default
=
"the default"
)
not_inherited
=
String
(
scope
=
Scope
.
settings
,
default
=
"nothing"
)
def
setUp
(
self
):
self
.
system
=
get_test_descriptor_system
()
self
.
all_blocks
=
{}
self
.
system
.
load_item
=
self
.
all_blocks
.
get
self
.
field_data
=
InheritingFieldData
(
inheritable_names
=
[
'inherited'
],
kvs
=
DictKeyValueStore
({}),
)
def
get_block
(
self
,
usage_id
=
None
):
"""Construct an XBlock for testing with."""
scope_ids
=
Mock
()
if
usage_id
is
None
:
usage_id
=
"_auto
%
d"
%
len
(
self
.
all_blocks
)
scope_ids
.
usage_id
=
usage_id
block
=
self
.
system
.
construct_xblock_from_class
(
self
.
TestableInheritingXBlock
,
field_data
=
self
.
field_data
,
scope_ids
=
scope_ids
,
)
self
.
all_blocks
[
usage_id
]
=
block
return
block
def
test_default_value
(
self
):
# Blocks with nothing set with return the fields' defaults.
block
=
self
.
get_block
()
self
.
assertEqual
(
block
.
inherited
,
"the default"
)
self
.
assertEqual
(
block
.
not_inherited
,
"nothing"
)
def
test_set_value
(
self
):
# If you set a value, that's what you get back.
block
=
self
.
get_block
()
block
.
inherited
=
"Changed!"
block
.
not_inherited
=
"New Value!"
self
.
assertEqual
(
block
.
inherited
,
"Changed!"
)
self
.
assertEqual
(
block
.
not_inherited
,
"New Value!"
)
def
test_inherited
(
self
):
# A child with get a value inherited from the parent.
parent
=
self
.
get_block
(
usage_id
=
"parent"
)
parent
.
inherited
=
"Changed!"
self
.
assertEqual
(
parent
.
inherited
,
"Changed!"
)
child
=
self
.
get_block
(
usage_id
=
"child"
)
child
.
parent
=
"parent"
self
.
assertEqual
(
child
.
inherited
,
"Changed!"
)
def
test_inherited_across_generations
(
self
):
# A child with get a value inherited from a great-grandparent.
parent
=
self
.
get_block
(
usage_id
=
"parent"
)
parent
.
inherited
=
"Changed!"
self
.
assertEqual
(
parent
.
inherited
,
"Changed!"
)
parent_id
=
"parent"
for
child_num
in
range
(
10
):
usage_id
=
"child_{}"
.
format
(
child_num
)
child
=
self
.
get_block
(
usage_id
=
usage_id
)
child
.
parent
=
"parent"
self
.
assertEqual
(
child
.
inherited
,
"Changed!"
)
parent_id
=
usage_id
def
test_not_inherited
(
self
):
# Fields not in the inherited_names list won't be inherited.
parent
=
self
.
get_block
(
usage_id
=
"parent"
)
parent
.
not_inherited
=
"Changed!"
self
.
assertEqual
(
parent
.
not_inherited
,
"Changed!"
)
child
=
self
.
get_block
(
usage_id
=
"child"
)
child
.
parent
=
"parent"
self
.
assertEqual
(
child
.
not_inherited
,
"nothing"
)
class
EditableMetadataFieldsTest
(
unittest
.
TestCase
):
class
EditableMetadataFieldsTest
(
unittest
.
TestCase
):
def
test_display_name_field
(
self
):
def
test_display_name_field
(
self
):
editable_fields
=
self
.
get_xml_editable_fields
(
DictFieldData
({}))
editable_fields
=
self
.
get_xml_editable_fields
(
DictFieldData
({}))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment