def process_darklang_request(self, request): """ Proccess the request to Set or clear the DarkLang depending on the incoming request. Arguments: request (Request): The Django Request Object Returns: HttpResponse: View containing the form for setting the preview lang with the status included in the context """ context = { 'disable_courseware_js': True, 'uses_pattern_library': True } response = None if not DarkLangConfig.current().enabled: message = _('Preview Language is currently disabled') context.update({'form_submit_message': message}) context.update({'success': False}) response = render_to_response(self.template_name, context, request=request) elif 'set_language' in request.POST: # Set the Preview Language response = self._set_preview_language(request, context) elif 'reset' in request.POST: # Reset and clear the language preference response = self._clear_preview_language(request, context) return response
def test_disabled(self): DarkLangConfig(enabled=False, changed_by=self.user).save() self.assertAcceptEquals( 'notrel;q=0.3, rel;q=1.0, unrel;q=0.5', self.process_middleware_request(accept='notrel;q=0.3, rel;q=1.0, unrel;q=0.5') ) # With DarkLang disabled the clear should not change the session language self._set_client_session_language('rel') self._post_clear_preview_lang() self.client.get('/home') self.assert_session_lang_equals( 'rel', self.client.session ) # Test that setting the preview language with DarkLang disabled does nothing self._set_client_session_language('unrel') self._post_set_preview_lang('rel') self.client.get('/home') self.assert_session_lang_equals( 'unrel', self.client.session )
def released_languages(): """Retrieve the list of released languages. Constructs a list of Language tuples by intersecting the list of valid language tuples with the list of released language codes. Returns: list of Language: Languages in which full translations are available. Example: >>> print released_languages() [Language(code='en', name=u'English'), Language(code='fr', name=u'Français')] """ released_language_codes = DarkLangConfig.current().released_languages_list default_language_code = settings.LANGUAGE_CODE if default_language_code not in released_language_codes: released_language_codes.append(default_language_code) released_language_codes.sort() # Intersect the list of valid language tuples with the list # of release language codes released_languages = [ Language(tuple[0], tuple[1]) for tuple in settings.LANGUAGES if tuple[0] in released_language_codes ] return released_languages
def setUp(self): super(DarkLangMiddlewareTests, self).setUp() self.user = User() self.user.save() DarkLangConfig(released_languages='rel', changed_by=self.user, enabled=True).save()
def test_exact_match_gets_priority(self, accept_header, expected): # Release 'es-419, es, es-es' DarkLangConfig(released_languages=('es-419, es, es-es'), changed_by=self.user, enabled=True).save() self.assertAcceptEquals(expected, self.process_request(accept=accept_header))
def released_langs(self): """ Current list of released languages """ language_options = DarkLangConfig.current().released_languages_list if settings.LANGUAGE_CODE not in language_options: language_options.append(settings.LANGUAGE_CODE) return language_options
def test_partial_match_esar_es(self): # If I release 'es', 'es-AR' should get 'es', not English DarkLangConfig(released_languages=('es, en'), changed_by=self.user, enabled=True).save() self.assertAcceptEquals( 'es;q=1.0', self.process_request(accept='es-AR;q=1.0, pt;q=0.5'))
def test_partial_match_es419(self, accept_header, expected): # Release es-419 DarkLangConfig(released_languages=('es-419, en'), changed_by=self.user, enabled=True).save() self.assertAcceptEquals(expected, self.process_request(accept=accept_header))
def setUp(self): self.user = User() self.user.save() DarkLangConfig( released_languages='rel', changed_by=self.user, enabled=True ).save()
def test_accept_chinese_language_codes(self): DarkLangConfig(released_languages=('zh-cn, zh-tw'), changed_by=self.user, enabled=True).save() self.assertAcceptEquals( 'zh-CN;q=1.0, zh-TW;q=0.5, zh-TW;q=0.3', self.process_request( accept='zh-Hans;q=1.0, zh-Hant-TW;q=0.5, zh-hk;q=0.3'))
def process_request(self, request): """ Prevent user from requesting un-released languages except by using the preview-lang query string. """ if not DarkLangConfig.current().enabled: return self._clean_accept_headers(request) self._activate_preview_language(request)
def setUp(self): self.rf = RequestFactory() # Added to test middleware around dark lang user = User() user.save() DarkLangConfig( released_languages='pt, pt-br, es, de, es-ar, zh-cn, ar-sa', changed_by=user, enabled=True ).save()
def release_languages(self, languages): """ Release a set of languages using the dark lang interface. languages is a list of comma-separated lang codes, eg, 'ar, es-419' """ user = User() user.save() DarkLangConfig(released_languages=languages, changed_by=user, enabled=True).save()
def setUp(self): super(DarkLangMiddlewareTests, self).setUp() self.user = UserFactory.build(username='******', email='*****@*****.**', password='******') self.user.save() self.client = Client() self.client.login(username=self.user.username, password='******') DarkLangConfig( released_languages='rel', changed_by=self.user, enabled=True ).save()
def test_accept_mixed_case(self): self.assertAcceptEquals( 'rel-TER;q=1.0, REL;q=0.5', self.process_request(accept='rel-TER;q=1.0, REL;q=0.5')) DarkLangConfig(released_languages=('REL-TER'), changed_by=self.user, enabled=True).save() self.assertAcceptEquals( 'rel-ter;q=1.0', self.process_request(accept='rel-ter;q=1.0, rel;q=0.5'))
def test_accept_mixed_case(self): self.assertAcceptEquals( 'rel;q=1.0, rel;q=0.5', self.process_request(accept='rel-TER;q=1.0, REL;q=0.5')) DarkLangConfig(released_languages=('REL-TER'), changed_by=self.user, enabled=True).save() # Since we have only released "rel-ter", the requested code "rel" will # fuzzy match to "rel-ter", in addition to "rel-ter" exact matching "rel-ter" self.assertAcceptEquals( 'rel-ter;q=1.0, rel-ter;q=0.5', self.process_request(accept='rel-ter;q=1.0, rel;q=0.5'))
def test_partial_match_es_la(self, latin_america_code): # We need to figure out the best way to implement this. There are a ton of LA country # codes that ought to fall back to 'es-419' rather than 'es-es'. # http://unstats.un.org/unsd/methods/m49/m49regin.htm#americas # If I release 'es, es-419' # Latin American codes should get es-419 DarkLangConfig(released_languages=('es, es-419'), changed_by=self.user, enabled=True).save() self.assertAcceptEquals( 'es-419;q=1.0', self.process_request( accept='{};q=1.0, pt;q=0.5'.format(latin_america_code)))
def test_accept_multiple_released_langs(self): DarkLangConfig(released_languages=('rel, unrel'), changed_by=self.user, enabled=True).save() self.assertAcceptEquals( 'rel;q=1.0, unrel;q=0.5', self.process_request(accept='rel;q=1.0, unrel;q=0.5')) self.assertAcceptEquals( 'rel;q=1.0, unrel;q=0.5', self.process_request( accept='rel;q=1.0, notrel;q=0.3, unrel;q=0.5')) self.assertAcceptEquals( 'rel;q=1.0, unrel;q=0.5', self.process_request( accept='notrel;q=0.3, rel;q=1.0, unrel;q=0.5'))
def test_disabled(self): DarkLangConfig(enabled=False, changed_by=self.user).save() self.assertAcceptEquals( 'notrel;q=0.3, rel;q=1.0, unrel;q=0.5', self.process_request( accept='notrel;q=0.3, rel;q=1.0, unrel;q=0.5')) self.assertSessionLangEquals( 'rel', self.process_request(clear_lang=True, django_language='rel')) self.assertSessionLangEquals( 'unrel', self.process_request(clear_lang=True, django_language='unrel')) self.assertSessionLangEquals( 'rel', self.process_request(preview_lang='unrel', django_language='rel'))
def released_langs(self): """ Current list of released languages """ return DarkLangConfig.current().released_languages_list