Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
code.vt.edu will be down for maintenance from 0530-0630 EDT Wednesday, March 26th
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
c58ec732
Commit
c58ec732
authored
5 years ago
by
DawoudSheraz
Browse files
Options
Downloads
Patches
Plain Diff
create service for CSM
parent
f6258aff
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lms/djangoapps/courseware/services.py
+34
-0
34 additions, 0 deletions
lms/djangoapps/courseware/services.py
lms/djangoapps/courseware/tests/test_services.py
+100
-0
100 additions, 0 deletions
lms/djangoapps/courseware/tests/test_services.py
with
134 additions
and
0 deletions
lms/djangoapps/courseware/services.py
0 → 100644
+
34
−
0
View file @
c58ec732
"""
Courseware services.
"""
from
__future__
import
absolute_import
import
json
from
lms.djangoapps.courseware.models
import
StudentModule
class
UserStateService
(
object
):
"""
User state service to make state accessible in runtime.
"""
def
get_state_as_dict
(
self
,
username
,
block_id
):
"""
Return dict containing user state for a given set of parameters.
Arguments:
username: username of the user for whom the data is being retrieved
block_id: string/object representation of the block whose user state is required
Returns:
Returns a dict containing user state, if present, else empty.
"""
try
:
student_module
=
StudentModule
.
objects
.
get
(
student__username
=
username
,
module_state_key
=
block_id
)
return
json
.
loads
(
student_module
.
state
)
except
StudentModule
.
DoesNotExist
:
return
{}
This diff is collapsed.
Click to expand it.
lms/djangoapps/courseware/tests/test_services.py
0 → 100644
+
100
−
0
View file @
c58ec732
"""
Tests for courseware services.
"""
from
__future__
import
absolute_import
import
json
import
ddt
from
lms.djangoapps.courseware.services
import
UserStateService
from
lms.djangoapps.courseware.tests.factories
import
StudentModuleFactory
,
UserFactory
from
xmodule.modulestore.tests.django_utils
import
ModuleStoreTestCase
from
xmodule.modulestore.tests.factories
import
CourseFactory
,
ItemFactory
@ddt.ddt
class
TestUserStateService
(
ModuleStoreTestCase
):
"""
Test suite for user state service.
"""
def
setUp
(
self
):
"""
Creating pre-requisites for the test cases.
"""
super
(
TestUserStateService
,
self
).
setUp
()
self
.
user
=
UserFactory
.
create
()
self
.
course
=
CourseFactory
.
create
()
chapter
=
ItemFactory
.
create
(
category
=
'
chapter
'
,
parent
=
self
.
course
,
display_name
=
'
Test Chapter
'
)
sequential
=
ItemFactory
.
create
(
category
=
'
sequential
'
,
parent
=
chapter
,
display_name
=
'
Test Sequential
'
)
vertical
=
ItemFactory
.
create
(
category
=
'
vertical
'
,
parent
=
sequential
,
display_name
=
'
Test Vertical
'
)
self
.
problem
=
ItemFactory
.
create
(
category
=
'
problem
'
,
parent
=
vertical
,
display_name
=
'
Test Problem
'
)
def
_create_student_module
(
self
,
state
):
StudentModuleFactory
.
create
(
student
=
self
.
user
,
module_state_key
=
self
.
problem
.
location
,
course_id
=
self
.
course
.
id
,
state
=
json
.
dumps
(
state
)
)
@ddt.data
(
({
'
key_1a
'
:
'
value_1a
'
,
'
key_2a
'
:
'
value_2a
'
}),
({
'
key_1b
'
:
'
value_1b
'
,
'
key_2b
'
:
'
value_2b
'
,
'
key_3b
'
:
'
value_3b
'
})
)
def
test_student_state
(
self
,
expected_state
):
"""
Verify the service gets the correct state from the student module.
Scenario:
Given a user and a problem or block
Then create a student module entry for the user
If the state is obtained from student module service
Then the state is equal to previously created student module state
"""
self
.
_create_student_module
(
expected_state
)
state
=
UserStateService
().
get_state_as_dict
(
self
.
user
.
username
,
self
.
problem
.
location
)
self
.
assertDictEqual
(
state
,
expected_state
)
@ddt.data
(
({
'
username
'
:
'
no_user
'
,
'
block_id
'
:
'
block-v1:myOrg+1234+2030_T2+type@openassessment+block@hash
'
}),
({
'
username
'
:
'
no_user
'
}),
({
'
block_id
'
:
'
block-v1:myOrg+1234+2030_T2+type@openassessment+block@hash
'
})
)
def
test_nonexistent_student_module_state
(
self
,
state_params
):
"""
Verify the user state service returns empty dict for non-existent student module entry.
Scenario:
Given a user and a problem/block
Then create a student module entry for the user
If the state is obtained with incorrect parameters
Then an empty dict is returned
"""
params
=
{
'
username
'
:
self
.
user
.
username
,
'
block_id
'
:
self
.
problem
.
location
}
params
.
update
(
state_params
)
self
.
_create_student_module
({
'
key_1
'
:
'
value_1
'
})
state
=
UserStateService
().
get_state_as_dict
(
**
params
)
self
.
assertFalse
(
state
)
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment