searx: Safe Search setting

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Joseph Nuthalapati 2018-02-23 12:36:16 +05:30 committed by Sunil Mohan Adapa
parent 26a53e6649
commit 619c3cf79c
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
5 changed files with 129 additions and 7 deletions

View File

@ -43,6 +43,16 @@ def parse_arguments():
subparsers.add_parser(
'setup', help="Perform post-installation operations for Searx")
safe_search = subparsers.add_parser(
'set-safe-search',
help='Set the default filter for safe search on Searx')
safe_search.add_argument(
'filter', type=int,
help='Filter results. 0: None, 1: Moderate, 2: Strict')
subparsers.add_parser('get-safe-search',
help='Print the value of the safe search setting.')
subparsers.required = True
return parser.parse_args()
@ -79,6 +89,23 @@ def _set_timeout(settings):
settings['outgoing']['request_timeout'] = 20.0
def subcommand_set_safe_search(arguments):
"""Set safe search filter for search results."""
value = arguments.filter
settings = read_settings()
settings['search']['safe_search'] = value
write_settings(settings)
def subcommand_get_safe_search(_):
"""Print the value of the safe search setting."""
if os.path.exists(SETTINGS_FILE):
settings = read_settings()
print(settings['search']['safe_search'])
else:
print(0)
def read_settings():
with open(SETTINGS_FILE, 'rb') as settings_file:
return yaml.load(settings_file)

View File

@ -94,6 +94,12 @@ def add_shortcut():
url='/searx', login_required=True)
def get_safe_search_setting():
"""Get the current value of the safe search setting for Seax."""
value = actions.superuser_run('searx', ['get-safe-search'])
return int(value.strip())
def is_enabled():
"""Return whether the module is enabled."""
return (action_utils.webserver_is_enabled('searx-freedombox')

View File

@ -0,0 +1,32 @@
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Plinth form for configuring Searx.
"""
from django import forms
from django.utils.translation import ugettext_lazy as _
from plinth.forms import ServiceForm
class SearxForm(ServiceForm):
"""Searx configuration form."""
safe_search = forms.ChoiceField(label="Safe Search", help_text=_(
'Select the default family filter to apply to your search results.'),
choices=((0, 'None'), (1, 'Moderate'),
(2, 'Strict')))

View File

@ -20,13 +20,8 @@ URLs for the Searx module.
from django.conf.urls import url
from plinth.modules import searx
from plinth.views import ServiceView
from .views import SearxServiceView
urlpatterns = [
url(r'^apps/searx/$',
ServiceView.as_view(
service_id='searx', diagnostics_module_name='searx',
description=searx.description, clients=searx.clients,
show_status_block=False), name='index'),
url(r'^apps/searx/$', SearxServiceView.as_view(), name='index'),
]

View File

@ -0,0 +1,62 @@
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Plinth views for Searx.
"""
from django.contrib import messages
from django.utils.translation import ugettext as _
from plinth import actions, views
from plinth.errors import ActionError
from plinth.modules.searx import clients, description, get_safe_search_setting
from .forms import SearxForm
class SearxServiceView(views.ServiceView):
"""Serve configuration page."""
clients = clients
description = description
diagnostics_module_name = 'searx'
service_id = 'searx'
form_class = SearxForm
show_status_block = False
def get_initial(self):
"""Return the status of the service to fill in the form."""
initial = super().get_initial()
initial['safe_search'] = get_safe_search_setting()
return initial
def form_valid(self, form):
"""Apply the changes submitted in the form."""
form_data = form.cleaned_data
if form_data['safe_search']:
try:
actions.superuser_run(
'searx',
['set-safe-search',
str(form_data['safe_search'])])
messages.success(self.request,
_('Safe search setting updated'))
except ActionError as e:
messages.error(self.request,
_('Failed to update safe search setting'))
return super().form_valid(form)