From aaea6bf00ef7092ce5caf77b880e474df5240367 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sat, 6 Nov 2021 09:50:32 +0530 Subject: [PATCH] tt-rss: Allow selection of a domain name Fixes: #2136 Signed-off-by: Joseph Nuthalapati [james: Fix comment] Signed-off-by: James Valleroy Reviewed-by: James Valleroy --- actions/ttrss | 34 +++++++++++++++++++++++++++++--- plinth/modules/ttrss/__init__.py | 18 +++++++++++++++++ plinth/modules/ttrss/urls.py | 6 ++---- plinth/modules/ttrss/views.py | 28 ++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 plinth/modules/ttrss/views.py diff --git a/actions/ttrss b/actions/ttrss index 5867b7543..e54fa6ef3 100755 --- a/actions/ttrss +++ b/actions/ttrss @@ -29,6 +29,13 @@ def parse_arguments(): subparsers.add_parser('dump-database', help='Dump database to file') subparsers.add_parser('restore-database', help='Restore database from file') + subparsers.add_parser('get-domain', + help='Get the domain set for Tiny Tiny RSS.') + set_domain = subparsers.add_parser( + 'set-domain', help='Set the domain to be used by Tiny Tiny RSS.') + set_domain.add_argument( + 'domain_name', + help='The domain name that will be used by Tiny Tiny RSS.') subparsers.required = True return parser.parse_args() @@ -40,6 +47,29 @@ def subcommand_pre_setup(_): ['tt-rss tt-rss/database-type string pgsql']) +def subcommand_get_domain(_): + """Get the domain set for Tiny Tiny RSS.""" + aug = load_augeas() + + from urllib.parse import urlparse + for match in aug.match('/files' + CONFIG_FILE + '/define'): + if aug.get(match) == 'SELF_URL_PATH': + url = aug.get(match + '/value').strip("'") + print(urlparse(url).netloc) + + +def subcommand_set_domain(args): + """Set the domain to be used by Tiny Tiny RSS.""" + url = f"'https://{args.domain_name}/tt-rss/'" + aug = load_augeas() + + for match in aug.match('/files' + CONFIG_FILE + '/define'): + if aug.get(match) == 'SELF_URL_PATH': + aug.set(match + '/value', url) + + aug.save() + + def subcommand_setup(_): """Setup Tiny Tiny RSS configuration.""" aug = load_augeas() @@ -49,9 +79,7 @@ def subcommand_setup(_): skip_self_url_path_exists = False for match in aug.match('/files' + CONFIG_FILE + '/define'): - if aug.get(match) == 'SELF_URL_PATH': - aug.set(match + '/value', "'http://localhost/tt-rss/'") - elif aug.get(match) == 'PLUGINS': + if aug.get(match) == 'PLUGINS': aug.set(match + '/value', "'auth_remote, note'") elif aug.get(match) == '_SKIP_SELF_URL_PATH_CHECKS': skip_self_url_path_exists = True diff --git a/plinth/modules/ttrss/__init__.py b/plinth/modules/ttrss/__init__.py index 40d7ea7cb..1ae8a73c0 100644 --- a/plinth/modules/ttrss/__init__.py +++ b/plinth/modules/ttrss/__init__.py @@ -102,6 +102,13 @@ class TTRSSApp(app_module.App): super().enable() actions.superuser_run('ttrss', ['enable-api-access']) + # Try to set the domain to one of the available TLS domains + domain = get_domain() + if not domain or domain == 'localhost': + from plinth.modules import names + domain = next(names.get_available_tls_domains(), None) + set_domain(domain) + class TTRSSBackupRestore(BackupRestore): """Component to backup/restore TT-RSS""" @@ -136,3 +143,14 @@ def force_upgrade(helper, packages): helper.install(['tt-rss'], force_configuration='new') actions.superuser_run('ttrss', ['setup']) return True + + +def get_domain(): + """Read TLS domain from tt-rss config file.""" + return actions.superuser_run('ttrss', ['get-domain']).strip() + + +def set_domain(domain): + """Set the TLS domain in tt-rss configuration file.""" + if domain: + actions.superuser_run('ttrss', ['set-domain', domain]) diff --git a/plinth/modules/ttrss/urls.py b/plinth/modules/ttrss/urls.py index 310722a56..737e9dea1 100644 --- a/plinth/modules/ttrss/urls.py +++ b/plinth/modules/ttrss/urls.py @@ -5,8 +5,6 @@ URLs for the Tiny Tiny RSS module. from django.urls import re_path -from plinth.views import AppView +from .views import TTRSSAppView -urlpatterns = [ - re_path(r'^apps/ttrss/$', AppView.as_view(app_id='ttrss'), name='index') -] +urlpatterns = [re_path(r'^apps/ttrss/$', TTRSSAppView.as_view(), name='index')] diff --git a/plinth/modules/ttrss/views.py b/plinth/modules/ttrss/views.py new file mode 100644 index 000000000..c71be5ae0 --- /dev/null +++ b/plinth/modules/ttrss/views.py @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later + +from django.contrib import messages +from django.utils.translation import gettext_lazy as _ + +from plinth.forms import TLSDomainForm +from plinth.modules import ttrss +from plinth.views import AppView + + +class TTRSSAppView(AppView): + app_id = 'ttrss' + form_class = TLSDomainForm + + def get_initial(self): + """Return the values to fill in the form.""" + initial = super().get_initial() + initial['domain'] = ttrss.get_domain() + return initial + + def form_valid(self, form): + """Change the domain of TT-RSS app.""" + data = form.cleaned_data + if ttrss.get_domain() != data['domain']: + ttrss.set_domain(data['domain']) + messages.success(self.request, _('Configuration updated')) + + return super().form_valid(form)