Skip to content
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
30 changes: 19 additions & 11 deletions lms/djangoapps/courseware/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""


import ast
import re
import json
from collections import OrderedDict
from datetime import timedelta
Expand Down Expand Up @@ -444,17 +442,27 @@ def get_expiration_banner_text(user, course, language='en'): # lint-amnesty, py
return bannerText


def get_context_dict_from_string(data):
def get_context_from_dict(data):
"""
Retrieve dictionary from string.
Retrieve validated dictionary from template's contextual data.

Args:
data: The context dictionary to validate

Returns:
dict: context dictionary
"""
# Replace tuple and un-necessary info from inside string and get the dictionary.
cleaned_data = data.split('((\'video.html\',')[1].replace("),\n {})", '').strip()
# Make a copy to avoid modifying the original dict
validated_data = data.copy()

# Omit user_id validation
cleaned_data_without_user = re.sub(".*user_id.*\n?", '', cleaned_data)
validated_data.pop('user_id', None)

# Handle metadata field - parse and sort to ensure consistent ordering
if 'metadata' in validated_data and validated_data['metadata'] is not None:
metadata_dict = json.loads(validated_data['metadata'])
validated_data['metadata'] = OrderedDict(
sorted(metadata_dict.items(), key=lambda t: t[0])
)

validated_data = ast.literal_eval(cleaned_data_without_user)
validated_data['metadata'] = OrderedDict(
sorted(json.loads(validated_data['metadata']).items(), key=lambda t: t[0])
)
return validated_data
Loading
Loading