def _verify_full_account_response(self, response, requires_parental_consent=False): """ Verify that all account fields are returned (even those that are not shareable). """ data = response.data self.assertEqual(17, len(data)) self.assertEqual(self.user.username, data["username"]) self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"]) self.assertEqual("US", data["country"]) self.assertEqual("f", data["gender"]) self.assertEqual(2000, data["year_of_birth"]) self.assertEqual("m", data["level_of_education"]) self.assertEqual("world peace", data["goals"]) self.assertEqual("Park Ave", data['mailing_address']) self.assertEqual(self.user.email, data["email"]) self.assertTrue(data["is_active"]) self.assertIsNotNone(data["date_joined"]) self.assertEqual("Tired mother of twins", data["bio"]) self._verify_profile_image_data(data, not requires_parental_consent) self.assertEquals(requires_parental_consent, data["requires_parental_consent"]) self.assertEqual([{"code": "en"}], data["language_proficiencies"]) self.assertEqual( UserPreference.get_value(self.user, 'account_privacy'), data["account_privacy"])
def render_to_fragment(self, request, course_id=None, **kwargs): # lint-amnesty, pylint: disable=arguments-differ """ Renders the user's course bookmarks as a fragment. """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True) language = UserPreference.get_value(request.user, 'pref-lang', default='en') context = { 'csrf': csrf(request)['csrf_token'], 'course': course, 'bookmarks_api_url': reverse('bookmarks'), 'language_preference': language, } html = render_to_string( 'course_bookmarks/course-bookmarks-fragment.html', context) inline_js = render_to_string( 'course_bookmarks/course_bookmarks_js.template', context) fragment = Fragment(html) self.add_fragment_resource_urls(fragment) fragment.add_javascript(inline_js) return fragment
def test_custom_visibility_over_age(self, api_client, requesting_username): self.create_mock_profile(self.user) # set user's custom visibility preferences set_user_preference(self.user, ACCOUNT_VISIBILITY_PREF_KEY, CUSTOM_VISIBILITY) shared_fields = ("bio", "language_proficiencies", "name") for field_name in shared_fields: set_user_preference(self.user, "visibility.{}".format(field_name), ALL_USERS_VISIBILITY) # make API request client = self.login_client(api_client, requesting_username) response = self.send_get(client) # verify response if requesting_username == "different_user": data = response.data self.assertEqual(6, len(data)) # public fields self.assertEqual(self.user.username, data["username"]) self.assertEqual(UserPreference.get_value(self.user, 'account_privacy'), data["account_privacy"]) self._verify_profile_image_data(data, has_profile_image=True) # custom shared fields self.assertEqual(TEST_BIO_VALUE, data["bio"]) self.assertEqual([{"code": TEST_LANGUAGE_PROFICIENCY_CODE}], data["language_proficiencies"]) self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"]) else: self._verify_full_account_response(response)
def test_custom_visibility_over_age(self, api_client, requesting_username): self.create_mock_profile(self.user) # set user's custom visibility preferences set_user_preference(self.user, ACCOUNT_VISIBILITY_PREF_KEY, CUSTOM_VISIBILITY) shared_fields = ("bio", "language_proficiencies", "name") for field_name in shared_fields: set_user_preference(self.user, "visibility.{}".format(field_name), ALL_USERS_VISIBILITY) # make API request client = self.login_client(api_client, requesting_username) response = self.send_get(client) # verify response if requesting_username == "different_user": data = response.data self.assertEqual(6, len(data)) # public fields self.assertEqual(self.user.username, data["username"]) self.assertEqual( UserPreference.get_value(self.user, 'account_privacy'), data["account_privacy"]) self._verify_profile_image_data(data, has_profile_image=True) # custom shared fields self.assertEqual(TEST_BIO_VALUE, data["bio"]) self.assertEqual([{ "code": TEST_LANGUAGE_PROFICIENCY_CODE }], data["language_proficiencies"]) self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"]) else: self._verify_full_account_response(response)
def render_to_fragment(self, request, course_id=None, **kwargs): """ Renders the user's course badges as a fragment. """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True) language = UserPreference.get_value(request.user, 'pref-lang', default='en') context = { 'csrf': csrf(request)['csrf_token'], 'course': course, 'badges_api_url': reverse("badges_api:user_assertions", kwargs={'username': request.user}), 'language_preference': language, 'user': User.objects.get(id=request.user.id) } html = render_to_string( 'course_badges/course-badges-progress-fragment.html', context) fragment = Fragment(html) self.add_fragment_resource_urls(fragment) return fragment
def test_custom_visibility_over_age(self, api_client, requesting_username): self.create_mock_profile(self.user) # set user's custom visibility preferences set_user_preference(self.user, ACCOUNT_VISIBILITY_PREF_KEY, CUSTOM_VISIBILITY) shared_fields = ("bio", "language_proficiencies", "name") for field_name in shared_fields: set_user_preference(self.user, "visibility.{}".format(field_name), ALL_USERS_VISIBILITY) # make API request client = self.login_client(api_client, requesting_username) response = self.send_get(client) # verify response if requesting_username == "different_user": data = response.data assert 6 == len(data) # public fields assert self.user.username == data['username'] assert UserPreference.get_value(self.user, 'account_privacy') == data['account_privacy'] self._verify_profile_image_data(data, has_profile_image=True) # custom shared fields assert TEST_BIO_VALUE == data['bio'] assert [{'code': TEST_LANGUAGE_PROFICIENCY_CODE}] == data['language_proficiencies'] assert ((self.user.first_name + ' ') + self.user.last_name) == data['name'] else: self._verify_full_account_response(response)
def _verify_full_account_response(self, response, requires_parental_consent=False, year_of_birth=2000): """ Verify that all account fields are returned (even those that are not shareable). """ data = response.data assert 27 == len(data) # public fields (3) expected_account_privacy = ( PRIVATE_VISIBILITY if requires_parental_consent else UserPreference.get_value(self.user, 'account_privacy')) assert expected_account_privacy == data['account_privacy'] self._verify_profile_image_data(data, not requires_parental_consent) assert self.user.username == data['username'] # additional shareable fields (8) assert TEST_BIO_VALUE == data['bio'] assert 'US' == data['country'] assert data['date_joined'] is not None assert data['last_login'] is not None assert [{ 'code': TEST_LANGUAGE_PROFICIENCY_CODE }] == data['language_proficiencies'] assert 'm' == data['level_of_education'] assert data['social_links'] is not None assert UserPreference.get_value(self.user, 'time_zone') == data['time_zone'] assert data['accomplishments_shared'] is not None assert ((self.user.first_name + ' ') + self.user.last_name) == data['name'] # additional admin fields (11) assert self.user.email == data['email'] assert self.user.id == data['id'] assert data['extended_profile'] is not None assert 'MA' == data['state'] assert 'f' == data['gender'] assert 'world peace' == data['goals'] assert data['is_active'] assert 'Park Ave' == data['mailing_address'] assert requires_parental_consent == data['requires_parental_consent'] assert data['secondary_email'] is None assert data['secondary_email_enabled'] is None assert year_of_birth == data['year_of_birth']
def _verify_full_account_response(self, response, requires_parental_consent=False, year_of_birth=2000): """ Verify that all account fields are returned (even those that are not shareable). """ data = response.data self.assertEqual(26, len(data)) # public fields (3) expected_account_privacy = ( PRIVATE_VISIBILITY if requires_parental_consent else UserPreference.get_value(self.user, 'account_privacy')) self.assertEqual(expected_account_privacy, data["account_privacy"]) self._verify_profile_image_data(data, not requires_parental_consent) self.assertEqual(self.user.username, data["username"]) # additional shareable fields (8) self.assertEqual(TEST_BIO_VALUE, data["bio"]) self.assertEqual("US", data["country"]) self.assertIsNotNone(data["date_joined"]) self.assertIsNotNone(data["last_login"]) self.assertEqual([{ "code": TEST_LANGUAGE_PROFICIENCY_CODE }], data["language_proficiencies"]) self.assertEqual("m", data["level_of_education"]) self.assertIsNotNone(data["social_links"]) self.assertEqual(UserPreference.get_value(self.user, 'time_zone'), data["time_zone"]) self.assertIsNotNone(data["accomplishments_shared"]) self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"]) # additional admin fields (10) self.assertEqual(self.user.email, data["email"]) self.assertIsNotNone(data["extended_profile"]) self.assertEqual("MA", data["state"]) self.assertEqual("f", data["gender"]) self.assertEqual("world peace", data["goals"]) self.assertTrue(data["is_active"]) self.assertEqual("Park Ave", data['mailing_address']) self.assertEqual(requires_parental_consent, data["requires_parental_consent"]) self.assertIsNone(data["secondary_email"]) self.assertIsNone(data["secondary_email_enabled"]) self.assertEqual(year_of_birth, data["year_of_birth"])
def get_user_email_language(user): """ Return the language most appropriate for writing emails to user. Returns None if the preference has not been set, or if the user does not exist. """ # Calling UserPreference directly instead of get_user_preference because the user requesting the # information is not "user" and also may not have is_staff access. return UserPreference.get_value(user, LANGUAGE_KEY)
def get_user_email_language(user): """ Return the language most appropriate for writing emails to user. Returns None if the preference has not been set, or if the user does not exist. """ # Calling UserPreference directly instead of get_user_preference because the user requesting the # information is not "user" and also may not have is_staff access. return UserPreference.get_value(user, LANGUAGE_KEY)
def has_permission(self, request, view): requested_profile_username = view.kwargs.get('username') # check whether requesting user is the owner of certs or not if request.user.username == requested_profile_username: return True user = User.objects.get(username=requested_profile_username) cert_privacy = UserPreference.get_value(user, 'visibility.course_certificates') return cert_privacy == 'all_users'
def get_profile_visibility(user_profile, user, configuration=None): """Returns the visibility level for the specified user profile.""" if user_profile.requires_parental_consent(): return PRIVATE_VISIBILITY if not configuration: configuration = settings.ACCOUNT_VISIBILITY_CONFIGURATION # Calling UserPreference directly because the requesting user may be different from existing_user # (and does not have to be is_staff). profile_privacy = UserPreference.get_value(user, ACCOUNT_VISIBILITY_PREF_KEY) return profile_privacy if profile_privacy else configuration.get('default_visibility')
def get_profile_visibility(user_profile, user, configuration=None): """Returns the visibility level for the specified user profile.""" if user_profile.requires_parental_consent(): return PRIVATE_VISIBILITY if not configuration: configuration = settings.ACCOUNT_VISIBILITY_CONFIGURATION # Calling UserPreference directly because the requesting user may be different from existing_user # (and does not have to be is_staff). profile_privacy = UserPreference.get_value(user, ACCOUNT_VISIBILITY_PREF_KEY) return profile_privacy if profile_privacy else configuration.get('default_visibility')
def share_with_facebook_friends(friend): """ Return true if the user's share_with_facebook_friends preference is set to true. """ # Calling UserPreference directly because the requesting user may be different (and not is_staff). try: existing_user = User.objects.get(username=friend['edX_username']) except ObjectDoesNotExist: return False return UserPreference.get_value(existing_user, 'share_with_facebook_friends') == 'True'
def share_with_facebook_friends(friend): """ Return true if the user's share_with_facebook_friends preference is set to true. """ # Calling UserPreference directly because the requesting user may be different (and not is_staff). try: existing_user = User.objects.get(username=friend['edX_username']) except ObjectDoesNotExist: return False return UserPreference.get_value(existing_user, 'share_with_facebook_friends') == 'True'
def claim_locale(self, data): """ Return the locale for the users based on their preferences. Does not return a value if the users have not set their locale preferences. """ # Calling UserPreference directly because it is not clear which user made the request. language = UserPreference.get_value(data['user'], LANGUAGE_KEY) # If the user has no language specified, return the default one. if not language: language = settings.LANGUAGE_CODE return language
def claim_locale(self, data): """ Return the locale for the users based on their preferences. Does not return a value if the users have not set their locale preferences. """ # Calling UserPreference directly because it is not clear which user made the request. language = UserPreference.get_value(data['user'], LANGUAGE_KEY) # If the user has no language specified, return the default one. if not language: language = settings.LANGUAGE_CODE return language
def _verify_full_account_response(self, response, requires_parental_consent=False, year_of_birth=2000): """ Verify that all account fields are returned (even those that are not shareable). """ data = response.data self.assertEqual(22, len(data)) # public fields (3) expected_account_privacy = ( PRIVATE_VISIBILITY if requires_parental_consent else UserPreference.get_value(self.user, 'account_privacy') ) self.assertEqual(expected_account_privacy, data["account_privacy"]) self._verify_profile_image_data(data, not requires_parental_consent) self.assertEqual(self.user.username, data["username"]) # additional shareable fields (8) self.assertEqual(TEST_BIO_VALUE, data["bio"]) self.assertEqual("US", data["country"]) self.assertIsNotNone(data["date_joined"]) self.assertEqual([{"code": TEST_LANGUAGE_PROFICIENCY_CODE}], data["language_proficiencies"]) self.assertEqual("m", data["level_of_education"]) self.assertIsNotNone(data["social_links"]) self.assertEqual(UserPreference.get_value(self.user, 'time_zone'), data["time_zone"]) self.assertIsNotNone(data["accomplishments_shared"]) self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"]) # additional admin fields (10) self.assertEqual(self.user.email, data["email"]) self.assertIsNotNone(data["extended_profile"]) self.assertEqual("f", data["gender"]) self.assertEqual("world peace", data["goals"]) self.assertTrue(data["is_active"]) self.assertEqual("Park Ave", data['mailing_address']) self.assertEquals(requires_parental_consent, data["requires_parental_consent"]) self.assertIsNone(data["secondary_email"]) self.assertEqual(year_of_birth, data["year_of_birth"])
def render_to_fragment(self, request, course_id=None, **kwargs): """ Renders the user's course bookmarks as a fragment. """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True) language = UserPreference.get_value(request.user, 'pref-lang', default='en') context = { 'csrf': csrf(request)['csrf_token'], 'course': course, 'bookmarks_api_url': reverse('bookmarks'), 'language_preference': language, } html = render_to_string('course_bookmarks/course-bookmarks-fragment.html', context) inline_js = render_to_string('course_bookmarks/course_bookmarks_js.template', context) fragment = Fragment(html) self.add_fragment_resource_urls(fragment) fragment.add_javascript(inline_js) return fragment
def _verify_full_account_response(self, response, requires_parental_consent=False): """ Verify that all account fields are returned (even those that are not shareable). """ data = response.data self.assertEqual(17, len(data)) self.assertEqual(self.user.username, data["username"]) self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"]) self.assertEqual("US", data["country"]) self.assertEqual("f", data["gender"]) self.assertEqual(2000, data["year_of_birth"]) self.assertEqual("m", data["level_of_education"]) self.assertEqual("world peace", data["goals"]) self.assertEqual("Park Ave", data['mailing_address']) self.assertEqual(self.user.email, data["email"]) self.assertTrue(data["is_active"]) self.assertIsNotNone(data["date_joined"]) self.assertEqual("Tired mother of twins", data["bio"]) self._verify_profile_image_data(data, not requires_parental_consent) self.assertEquals(requires_parental_consent, data["requires_parental_consent"]) self.assertEqual([{"code": "en"}], data["language_proficiencies"]) self.assertEqual(UserPreference.get_value(self.user, 'account_privacy'), data["account_privacy"])