<%page expression_filter="h"/>
<%!
from openedx.core.djangolib.js_utils import dump_js_escaped_json
from eventtracking import tracker
from opaque_keys.edx.keys import CourseKey
%>
<%
user_metadata = {
    key: context.get(key)
    for key in (
        'username',
        'user_id',
        'course_id',
        'enrollment_mode',
        'upgrade_link',
        'upgrade_price',
        'pacing_type',
    )
}

if user:
    user_metadata['username'] = user.username
    user_metadata['user_id'] = user.id

for datekey in (
        'schedule_start',
        'enrollment_time',
        'course_start',
        'course_end',
        'upgrade_deadline'
):
    user_metadata[datekey] = (
        context.get(datekey).isoformat() if context.get(datekey) else None
    )

course_key = context.get('course_key')
if course and not course_key:
    course_key = course.id

if course_key:
    if isinstance(course_key, CourseKey):
        user_metadata['course_key_fields'] = {
            'org': course_key.org,
            'course': course_key.course,
            'run': course_key.run,
        }

        if not course_id:
            user_metadata['course_id'] = unicode(course_key)
    elif isinstance(course_key, basestring):
        user_metadata['course_id'] = course_key

%>
<script type="application/json" id="user-metadata">
    ${user_metadata | n, dump_js_escaped_json}
</script>
