def test_update_and_retrieve_preference_info(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_preferences(self.USERNAME, preference_key='preference_value') preferences = profile_api.preference_info(self.USERNAME) self.assertEqual(preferences['preference_key'], 'preference_value')
def test_update_and_retrieve_preference_info_unicode(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_preferences(self.USERNAME, **{u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ': u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd'}) preferences = profile_api.preference_info(self.USERNAME) self.assertEqual(preferences[u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ'], u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd')
def test_update_and_retrieve_preference_info_unicode(self): account_api.create_account(self.USERNAME, self.PASSWORD, self.EMAIL) profile_api.update_preferences( self.USERNAME, **{u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ': u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd'}) preferences = profile_api.preference_info(self.USERNAME) self.assertEqual(preferences[u'ⓟⓡⓔⓕⓔⓡⓔⓝⓒⓔ_ⓚⓔⓨ'], u'ǝnןɐʌ_ǝɔuǝɹǝɟǝɹd')
def preference_handler(request): """Change the user's preferences. At the moment, the only supported preference is the user's language choice. Args: request (HttpRequest) Returns: HttpResponse: 204 if successful HttpResponse: 302 if not logged in (redirect to login page) HttpResponse: 400 if no language is provided, or an unreleased language is provided HttpResponse: 405 if using an unsupported HTTP method HttpResponse: 500 if an unexpected error occurs. Example: PUT /profile/preferences """ put = QueryDict(request.body) username = request.user.username new_language = put.get('language') if new_language is None: return HttpResponseBadRequest("Missing param 'language'") # Check that the provided language code corresponds to a released language released_languages = language_api.released_languages() if new_language in [language.code for language in released_languages]: try: profile_api.update_preferences(username, **{LANGUAGE_KEY: new_language}) request.session['django_language'] = new_language except profile_api.ProfileUserNotFound: return HttpResponseServerError() else: return HttpResponseBadRequest( "Provided language code corresponds to an unreleased language" ) # A 204 is intended to allow input for actions to take place # without causing a change to the user agent's active document view. return HttpResponse(status=204)
def test_retrieve_and_update_preference_info_no_user(self): preferences = profile_api.preference_info(self.USERNAME) self.assertEqual(preferences, {}) profile_api.update_preferences(self.USERNAME, preference_key='preference_value')