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
75a2355b
Commit
75a2355b
authored
Oct 28, 2015
by
Nimisha Asthagiri
Committed by
J. Cliff Dyer
Nov 05, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Transformer: BlockCountsTransformer
parent
ae4bc5d0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
0 deletions
+101
-0
lms/djangoapps/course_api/blocks/transformers/block_counts.py
+51
-0
lms/djangoapps/course_api/blocks/transformers/tests/test_block_counts.py
+50
-0
No files found.
lms/djangoapps/course_api/blocks/transformers/block_counts.py
0 → 100644
View file @
75a2355b
"""
Block Counts Transformer
"""
from
openedx.core.lib.block_cache.transformer
import
BlockStructureTransformer
class
BlockCountsTransformer
(
BlockStructureTransformer
):
"""
Keep a count of descendant blocks of the requested types
"""
VERSION
=
1
BLOCK_COUNTS
=
'block_counts'
def
__init__
(
self
,
block_types_to_count
):
self
.
block_types_to_count
=
block_types_to_count
@classmethod
def
name
(
cls
):
return
"blocks_api:block_counts"
@classmethod
def
collect
(
cls
,
block_structure
):
"""
Collects any information that's necessary to execute this transformer's
transform method.
"""
# collect basic xblock fields
block_structure
.
request_xblock_fields
(
'category'
)
def
transform
(
self
,
usage_info
,
block_structure
):
# pylint: disable=unused-argument
"""
Mutates block_structure based on the given usage_info.
"""
if
not
self
.
block_types_to_count
:
return
for
block_key
in
block_structure
.
post_order_traversal
():
for
block_type
in
self
.
block_types_to_count
:
descendants_type_count
=
sum
([
block_structure
.
get_transformer_block_field
(
child_key
,
self
,
block_type
,
0
)
for
child_key
in
block_structure
.
get_children
(
block_key
)
])
block_structure
.
set_transformer_block_field
(
block_key
,
self
,
block_type
,
(
descendants_type_count
+
(
1
if
(
block_structure
.
get_xblock_field
(
block_key
,
'category'
)
==
block_type
)
else
0
)
)
)
lms/djangoapps/course_api/blocks/transformers/tests/test_block_counts.py
0 → 100644
View file @
75a2355b
"""
Tests for BlockCountsTransformer.
"""
# pylint: disable=protected-access
from
openedx.core.lib.block_cache.block_structure_factory
import
BlockStructureFactory
from
xmodule.modulestore.tests.django_utils
import
ModuleStoreTestCase
from
xmodule.modulestore.tests.factories
import
SampleCourseFactory
from
..block_counts
import
BlockCountsTransformer
class
TestBlockCountsTransformer
(
ModuleStoreTestCase
):
"""
Test behavior of BlockCountsTransformer
"""
def
setUp
(
self
):
super
(
TestBlockCountsTransformer
,
self
)
.
setUp
()
self
.
course_key
=
SampleCourseFactory
.
create
()
.
id
self
.
course_usage_key
=
self
.
store
.
make_course_usage_key
(
self
.
course_key
)
self
.
block_structure
=
BlockStructureFactory
.
create_from_modulestore
(
self
.
course_usage_key
,
self
.
store
)
def
test_transform
(
self
):
# collect phase
BlockCountsTransformer
.
collect
(
self
.
block_structure
)
self
.
block_structure
.
_collect_requested_xblock_fields
()
# transform phase
BlockCountsTransformer
([
'problem'
,
'chapter'
])
.
transform
(
usage_info
=
None
,
block_structure
=
self
.
block_structure
)
# block_counts
chapter_x_key
=
self
.
course_key
.
make_usage_key
(
'chapter'
,
'chapter_x'
)
block_counts_for_chapter_x
=
self
.
block_structure
.
get_transformer_block_data
(
chapter_x_key
,
BlockCountsTransformer
,
)
block_counts_for_course
=
self
.
block_structure
.
get_transformer_block_data
(
self
.
course_usage_key
,
BlockCountsTransformer
,
)
# verify count of chapters
self
.
assertEquals
(
block_counts_for_course
[
'chapter'
],
2
)
# verify count of problems
self
.
assertEquals
(
block_counts_for_course
[
'problem'
],
6
)
self
.
assertEquals
(
block_counts_for_chapter_x
[
'problem'
],
3
)
# verify other block types are not counted
for
block_type
in
[
'course'
,
'html'
,
'video'
]:
self
.
assertNotIn
(
block_type
,
block_counts_for_course
)
self
.
assertNotIn
(
block_type
,
block_counts_for_chapter_x
)
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