Expand conversion of config<->url params

Config settings can now be translated to and from URL params using a
predetermined set of "safe" keys (i.e. config settings that easily
translate to URL params).
This commit is contained in:
Ben Busby 2021-11-23 17:52:19 -07:00
parent 3c06519130
commit b7798c9382
No known key found for this signature in database
GPG Key ID: 339B7B7EB5333D14
5 changed files with 36 additions and 13 deletions

View File

@ -17,7 +17,7 @@ class Config:
self.block = os.getenv('WHOOGLE_CONFIG_BLOCK', '')
self.block_title = os.getenv('WHOOGLE_CONFIG_BLOCK_TITLE', '')
self.block_url = os.getenv('WHOOGLE_CONFIG_BLOCK_URL', '')
self.ctry = os.getenv('WHOOGLE_CONFIG_COUNTRY', 'US')
self.country = os.getenv('WHOOGLE_CONFIG_COUNTRY', 'US')
self.theme = os.getenv('WHOOGLE_CONFIG_THEME', 'system')
self.safe = read_config_bool('WHOOGLE_CONFIG_SAFE')
self.dark = read_config_bool('WHOOGLE_CONFIG_DARK') # deprecated
@ -33,9 +33,11 @@ class Config:
self.safe_keys = [
'lang_search',
'lang_interface',
'ctry',
'dark',
'theme'
'country',
'theme',
'alts',
'new_tab',
'safe'
]
# Skip setting custom config if there isn't one
@ -107,3 +109,17 @@ class Config:
continue
self[param_key] = params.get(param_key)
return self
def to_params(self) -> str:
"""Generates a set of safe params for using in Whoogle URLs
Returns:
str -- a set of URL parameters
"""
param_str = ''
for safe_key in self.safe_keys:
if not self[safe_key]:
continue
param_str = param_str + f'&{safe_key}={self[safe_key]}'
return param_str

View File

@ -120,7 +120,7 @@ def gen_query(query, args, config, near_city=None) -> str:
if 'chips' in args:
param_dict['chips'] = '&chips=' + args.get('chips')
param_dict['gl'] = ('&gl=' + config.ctry) if config.ctry else ''
param_dict['gl'] = ('&gl=' + config.country) if config.country else ''
param_dict['hl'] = '&hl=' + (
config.lang_interface.replace('lang_', '')
if config.lang_interface else ''

View File

@ -176,3 +176,10 @@ details summary {
padding: 10px;
font-weight: bold;
}
/* Mobile styles */
@media (max-width: 1000px) {
select {
width: 100%;
}
}

View File

@ -85,15 +85,15 @@
<div class="config-fields">
<form id="config-form" action="config" method="post">
<div class="config-options">
<div class="config-div config-div-ctry">
<label for="config-ctry">{{ translation['config-country'] }}: </label>
<select name="ctry" id="config-ctry">
{% for ctry in countries %}
<option value="{{ ctry.value }}"
{% if ctry.value in config.ctry %}
<div class="config-div config-div-country">
<label for="config-country">{{ translation['config-country'] }}: </label>
<select name="country" id="config-country">
{% for country in countries %}
<option value="{{ country.value }}"
{% if country.value in config.country %}
selected
{% endif %}>
{{ ctry.name }}
{{ country.name }}
</option>
{% endfor %}
</select>

View File

@ -9,7 +9,7 @@ demo_config = {
'nojs': str(random.getrandbits(1)),
'lang_interface': random.choice(app.config['LANGUAGES'])['value'],
'lang_search': random.choice(app.config['LANGUAGES'])['value'],
'ctry': random.choice(app.config['COUNTRIES'])['value']
'country': random.choice(app.config['COUNTRIES'])['value']
}