From 619c3cf79ce118e1f5460e94ece37349d6f45e42 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Fri, 23 Feb 2018 12:36:16 +0530 Subject: [PATCH] searx: Safe Search setting Signed-off-by: Joseph Nuthalapati Reviewed-by: Sunil Mohan Adapa --- actions/searx | 27 ++++++++++++++ plinth/modules/searx/__init__.py | 6 ++++ plinth/modules/searx/forms.py | 32 +++++++++++++++++ plinth/modules/searx/urls.py | 9 ++--- plinth/modules/searx/views.py | 62 ++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 plinth/modules/searx/forms.py create mode 100644 plinth/modules/searx/views.py diff --git a/actions/searx b/actions/searx index b6e41796f..eae9806aa 100755 --- a/actions/searx +++ b/actions/searx @@ -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) diff --git a/plinth/modules/searx/__init__.py b/plinth/modules/searx/__init__.py index ee51d7058..dbbf67e22 100644 --- a/plinth/modules/searx/__init__.py +++ b/plinth/modules/searx/__init__.py @@ -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') diff --git a/plinth/modules/searx/forms.py b/plinth/modules/searx/forms.py new file mode 100644 index 000000000..aea928183 --- /dev/null +++ b/plinth/modules/searx/forms.py @@ -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 . +# +""" +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'))) diff --git a/plinth/modules/searx/urls.py b/plinth/modules/searx/urls.py index 42ce837e8..66307c8e9 100644 --- a/plinth/modules/searx/urls.py +++ b/plinth/modules/searx/urls.py @@ -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'), ] diff --git a/plinth/modules/searx/views.py b/plinth/modules/searx/views.py new file mode 100644 index 000000000..2684a47ab --- /dev/null +++ b/plinth/modules/searx/views.py @@ -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 . +# +""" +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)