minor fixes of doi resolver

Closes #1047
This commit is contained in:
Noémi Ványi 2017-11-01 13:58:48 +01:00
parent 575159b194
commit 1b10abfc92
4 changed files with 19 additions and 32 deletions

View file

@ -1,7 +1,6 @@
from flask_babel import gettext from flask_babel import gettext
import re import re
from searx.url_utils import urlparse, parse_qsl from searx.url_utils import urlparse, parse_qsl
from flask import request
from searx import settings from searx import settings
@ -26,12 +25,12 @@ def extract_doi(url):
return None return None
def get_doi_resolver(): def get_doi_resolver(args, preference_doi_resolver):
doi_resolvers = settings['doi_resolvers'] doi_resolvers = settings['doi_resolvers']
doi_resolver = request.args.get('doi_resolver', request.preferences.get_value('doi_resolver'))[0] doi_resolver = args.get('doi_resolver', preference_doi_resolver)[0]
if doi_resolver not in doi_resolvers: if doi_resolver not in doi_resolvers:
doi_resolvers = settings['default_doi_resolver'] doi_resolvers = settings['default_doi_resolver']
return doi_resolvers[doi_resolver] return doi_resolver
def on_result(request, search, result): def on_result(request, search, result):
@ -40,6 +39,6 @@ def on_result(request, search, result):
for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'): for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
if doi.endswith(suffix): if doi.endswith(suffix):
doi = doi[:-len(suffix)] doi = doi[:-len(suffix)]
result['url'] = get_doi_resolver() + doi result['url'] = get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')) + doi
result['parsed_url'] = urlparse(result['url']) result['parsed_url'] = urlparse(result['url'])
return True return True

View file

@ -15,7 +15,7 @@ LANGUAGE_CODES = [l[0] for l in languages]
LANGUAGE_CODES.append('all') LANGUAGE_CODES.append('all')
DISABLED = 0 DISABLED = 0
ENABLED = 1 ENABLED = 1
DOI_RESOLVERS = [r for r in settings['doi_resolvers'].keys()] DOI_RESOLVERS = list(settings['doi_resolvers'])
class MissingArgumentException(Exception): class MissingArgumentException(Exception):

View file

@ -118,6 +118,18 @@
<option value="0" {% if not results_on_new_tab %}selected="selected"{% endif %}>{{ _('Off')}}</option> <option value="0" {% if not results_on_new_tab %}selected="selected"{% endif %}>{{ _('Off')}}</option>
</select> </select>
{{ preferences_item_footer(info, label, rtl) }} {{ preferences_item_footer(info, label, rtl) }}
{% set label = _('Open Access DOI resolver') %}
{% set info = _('Redirect to open-access versions of publications when available (plugin required)') %}
{{ preferences_item_header(info, label, rtl) }}
<select class="form-control" id='doi_resolver' name='doi_resolver'>
{% for doi_resolver_name,doi_resolver_url in doi_resolvers.items() %}
<option value="{{ doi_resolver_name }}" {% if doi_resolver_name == current_doi_resolver %}selected="selected"{% endif %}>
{{ doi_resolver_name }} - {{ doi_resolver_url }}
</option>
{% endfor %}
</select>
{{ preferences_item_footer(info, label, rtl) }}
</div> </div>
</fieldset> </fieldset>
</div> </div>
@ -223,23 +235,6 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ _('OA DOI rewrite') }}</h3>
</div>
<div class="panel-body">
<div class="col-xs-6 col-sm-4 col-md-6">{{ _('Avoid paywalls by redirecting to open-access versions of publications when available') }}</div>
<div class="col-xs-6 col-sm-4 col-md-6">
<select class="form-control" id='doi_resolver' name='doi_resolver'>
{% for doi_resolver_name,doi_resolver_url in doi_resolvers.items() %}
<option value="{{ doi_resolver_name }}" {% if doi_resolver_name == current_doi_resolver %}selected="selected"{% endif %}>
{{ doi_resolver_name }} - {{ doi_resolver_url }}
</option>
{% endfor %}
</select>
</div>
</div>
</div>
</div> </div>
</fieldset> </fieldset>
</div> </div>

View file

@ -66,6 +66,7 @@ from searx.search import SearchWithPlugins, get_search_query_from_webapp
from searx.query import RawTextQuery from searx.query import RawTextQuery
from searx.autocomplete import searx_bang, backends as autocomplete_backends from searx.autocomplete import searx_bang, backends as autocomplete_backends
from searx.plugins import plugins from searx.plugins import plugins
from searx.plugins.oa_doi_rewrite import get_doi_resolver
from searx.preferences import Preferences, ValidationException from searx.preferences import Preferences, ValidationException
from searx.answerers import answerers from searx.answerers import answerers
from searx.url_utils import urlencode, urlparse, urljoin from searx.url_utils import urlencode, urlparse, urljoin
@ -164,14 +165,6 @@ def get_locale():
return locale return locale
def get_doi_resolver():
doi_resolvers = settings['doi_resolvers']
doi_resolver = request.args.get('doi_resolver', request.preferences.get_value('doi_resolver'))[0]
if doi_resolver not in doi_resolvers:
doi_resolvers = settings['default_doi_resolver']
return doi_resolver
# code-highlighter # code-highlighter
@app.template_filter('code_highlighter') @app.template_filter('code_highlighter')
def code_highlighter(codelines, language=None): def code_highlighter(codelines, language=None):
@ -704,7 +697,7 @@ def preferences():
themes=themes, themes=themes,
plugins=plugins, plugins=plugins,
doi_resolvers=settings['doi_resolvers'], doi_resolvers=settings['doi_resolvers'],
current_doi_resolver=get_doi_resolver(), current_doi_resolver=get_doi_resolver(request.args, request.preferences.get_value('doi_resolver')),
allowed_plugins=allowed_plugins, allowed_plugins=allowed_plugins,
theme=get_current_theme_name(), theme=get_current_theme_name(),
preferences_url_params=request.preferences.get_as_url_params(), preferences_url_params=request.preferences.get_as_url_params(),