fix: Use UserProfile.meta for city field if set in the profile information report

There are two distinct ways to store a city for users in edx-platform:
one directly in UserProfile.city, and another in UserProfile.meta.

Depending on configuration, both fields can be used. Though the
UserProfile.meta['city'] field is not set unless specifically configured,
so we choose this over the UserProfile.city field when generating the student
features report.

Additional details and discussion available on edx/edx-platform#23171
This commit is contained in:
Josh McLaughlin
2021-04-19 07:48:49 -07:00
committed by Braden MacDonald
parent 74340c368d
commit ddffeaac4a

View File

@@ -97,6 +97,7 @@ def enrolled_students_features(course_key, features):
"""
include_cohort_column = 'cohort' in features
include_team_column = 'team' in features
include_city_column = 'city' in features
include_enrollment_mode = 'enrollment_mode' in features
include_verification_status = 'verification_status' in features
include_program_enrollments = 'external_user_key' in features
@@ -152,6 +153,13 @@ def enrolled_students_features(course_key, features):
for meta_feature, meta_key in meta_features:
student_dict[meta_feature] = meta_dict.get(meta_key)
# There are two separate places where the city value can be stored,
# one used by account settings and the other used by the registration form.
# If the account settings value (meta.city) is set, it takes precedence.
meta_city = meta_dict.get('city')
if include_city_column and meta_city:
student_dict['city'] = meta_city
if include_cohort_column:
# Note that we use student.course_groups.all() here instead of
# student.course_groups.filter(). The latter creates a fresh query,