|
3 | 3 | """ |
4 | 4 |
|
5 | 5 |
|
6 | | -import ast |
7 | | -import re |
8 | 6 | import json |
9 | 7 | from collections import OrderedDict |
10 | 8 | from datetime import timedelta |
@@ -444,17 +442,27 @@ def get_expiration_banner_text(user, course, language='en'): # lint-amnesty, py |
444 | 442 | return bannerText |
445 | 443 |
|
446 | 444 |
|
447 | | -def get_context_dict_from_string(data): |
| 445 | +def get_context_from_dict(data): |
448 | 446 | """ |
449 | | - Retrieve dictionary from string. |
| 447 | + Retrieve validated dictionary from template's contextual data. |
| 448 | +
|
| 449 | + Args: |
| 450 | + data: The context dictionary to validate |
| 451 | +
|
| 452 | + Returns: |
| 453 | + dict: context dictionary |
450 | 454 | """ |
451 | | - # Replace tuple and un-necessary info from inside string and get the dictionary. |
452 | | - cleaned_data = data.split('((\'video.html\',')[1].replace("),\n {})", '').strip() |
| 455 | + # Make a copy to avoid modifying the original dict |
| 456 | + validated_data = data.copy() |
| 457 | + |
453 | 458 | # Omit user_id validation |
454 | | - cleaned_data_without_user = re.sub(".*user_id.*\n?", '', cleaned_data) |
| 459 | + validated_data.pop('user_id', None) |
| 460 | + |
| 461 | + # Handle metadata field - parse and sort to ensure consistent ordering |
| 462 | + if 'metadata' in validated_data and validated_data['metadata'] is not None: |
| 463 | + metadata_dict = json.loads(validated_data['metadata']) |
| 464 | + validated_data['metadata'] = OrderedDict( |
| 465 | + sorted(metadata_dict.items(), key=lambda t: t[0]) |
| 466 | + ) |
455 | 467 |
|
456 | | - validated_data = ast.literal_eval(cleaned_data_without_user) |
457 | | - validated_data['metadata'] = OrderedDict( |
458 | | - sorted(json.loads(validated_data['metadata']).items(), key=lambda t: t[0]) |
459 | | - ) |
460 | 468 | return validated_data |
0 commit comments