Merge pull request #764 from kvch/set-search-language-from-settings-yml

set search language from settings.yml
This commit is contained in:
Adam Tauber 2016-11-14 23:42:39 +01:00 committed by GitHub
commit 028e5b43d4
3 changed files with 18 additions and 13 deletions

View File

@ -49,28 +49,32 @@ class StringSetting(Setting):
class EnumStringSetting(Setting): class EnumStringSetting(Setting):
"""Setting of a value which can only come from the given choices""" """Setting of a value which can only come from the given choices"""
def _validate_selection(self, selection):
if selection not in self.choices:
raise ValidationException('Invalid value: "{0}"'.format(selection))
def _post_init(self): def _post_init(self):
if not hasattr(self, 'choices'): if not hasattr(self, 'choices'):
raise MissingArgumentException('Missing argument: choices') raise MissingArgumentException('Missing argument: choices')
self._validate_selection(self.value)
if self.value != '' and self.value not in self.choices:
raise ValidationException('Invalid default value: {0}'.format(self.value))
def parse(self, data): def parse(self, data):
if data not in self.choices and data != self.value: self._validate_selection(data)
raise ValidationException('Invalid choice: {0}'.format(data))
self.value = data self.value = data
class MultipleChoiceSetting(EnumStringSetting): class MultipleChoiceSetting(EnumStringSetting):
"""Setting of values which can only come from the given choices""" """Setting of values which can only come from the given choices"""
def _validate_selections(self, selections):
for item in selections:
if item not in self.choices:
raise ValidationException('Invalid value: "{0}"'.format(selections))
def _post_init(self): def _post_init(self):
if not hasattr(self, 'choices'): if not hasattr(self, 'choices'):
raise MissingArgumentException('Missing argument: choices') raise MissingArgumentException('Missing argument: choices')
for item in self.value: self._validate_selections(self.value)
if item not in self.choices:
raise ValidationException('Invalid default value: {0}'.format(self.value))
def parse(self, data): def parse(self, data):
if data == '': if data == '':
@ -78,9 +82,7 @@ class MultipleChoiceSetting(EnumStringSetting):
return return
elements = data.split(',') elements = data.split(',')
for item in elements: self._validate_selections(elements)
if item not in self.choices:
raise ValidationException('Invalid choice: {0}'.format(item))
self.value = elements self.value = elements
def parse_form(self, data): def parse_form(self, data):
@ -214,9 +216,10 @@ class Preferences(object):
super(Preferences, self).__init__() super(Preferences, self).__init__()
self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories), self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories),
'language': EnumStringSetting('all', choices=LANGUAGE_CODES), 'language': EnumStringSetting(settings['search']['language'],
choices=LANGUAGE_CODES),
'locale': EnumStringSetting(settings['ui']['default_locale'], 'locale': EnumStringSetting(settings['ui']['default_locale'],
choices=settings['locales'].keys()), choices=settings['locales'].keys() + ['']),
'autocomplete': EnumStringSetting(settings['search']['autocomplete'], 'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
choices=autocomplete.backends.keys() + ['']), choices=autocomplete.backends.keys() + ['']),
'image_proxy': MapSetting(settings['server']['image_proxy'], 'image_proxy': MapSetting(settings['server']['image_proxy'],

View File

@ -5,6 +5,7 @@ general:
search: search:
safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict
autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "startpage", "wikipedia" - leave blank to turn it off by default
language : "all"
server: server:
port : 8888 port : 8888

View File

@ -5,6 +5,7 @@ general:
search: search:
safe_search : 0 safe_search : 0
autocomplete : "" autocomplete : ""
language: "all"
server: server:
port : 11111 port : 11111