From a912c867c8ba8991ee9d36d7cf762eb48f34ecbb Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sat, 6 Nov 2021 08:27:39 +0530 Subject: [PATCH] names: Create a generic TLS domain selection form Signed-off-by: Joseph Nuthalapati Reviewed-by: James Valleroy --- plinth/forms.py | 18 ++++++++++++++++++ plinth/modules/names/__init__.py | 11 +++++++++++ plinth/modules/quassel/__init__.py | 8 +------- plinth/modules/quassel/forms.py | 27 --------------------------- plinth/modules/quassel/views.py | 5 ++--- 5 files changed, 32 insertions(+), 37 deletions(-) delete mode 100644 plinth/modules/quassel/forms.py diff --git a/plinth/forms.py b/plinth/forms.py index 5bf5e739f..c783b99b7 100644 --- a/plinth/forms.py +++ b/plinth/forms.py @@ -39,6 +39,24 @@ class DomainSelectionForm(forms.Form): 'changed later.'), choices=[]) +class TLSDomainForm(forms.Form): + """Form to select a TLS domain for an app.""" + + def get_domain_choices(): + """Double domain entries for inclusion in the choice field.""" + from plinth.modules.names import get_available_tls_domains + return ((domain, domain) for domain in get_available_tls_domains()) + + domain = forms.ChoiceField( + choices=get_domain_choices(), + label=_('TLS domain'), + help_text=_( + 'Select a domain to use TLS with. If the list is empty, please ' + 'configure at least one domain with certificates.'), + required=False, + ) + + class LanguageSelectionFormMixin: """Form mixin for selecting the user's preferred language.""" diff --git a/plinth/modules/names/__init__.py b/plinth/modules/names/__init__.py index f6aa8e2ae..7ca071ceb 100644 --- a/plinth/modules/names/__init__.py +++ b/plinth/modules/names/__init__.py @@ -93,3 +93,14 @@ def on_domain_removed(sender, domain_type, name='', **kwargs): logger.info('Remove domain %s of type %s', domain_name.name, domain_type) + + +###################################################### +# Domain utilities meant to be used by other modules # +###################################################### + + +def get_available_tls_domains(): + """Return an iterator with all domains able to have a certificate.""" + return (domain.name for domain in components.DomainName.list() + if domain.domain_type.can_have_certificate) diff --git a/plinth/modules/quassel/__init__.py b/plinth/modules/quassel/__init__.py index f9a0333f6..1f7c03a93 100644 --- a/plinth/modules/quassel/__init__.py +++ b/plinth/modules/quassel/__init__.py @@ -114,12 +114,6 @@ def setup(helper, old_version=None): app.get_component('letsencrypt-quassel').setup_certificates() -def get_available_domains(): - """Return an iterator with all domains able to have a certificate.""" - return (domain.name for domain in names.components.DomainName.list() - if domain.domain_type.can_have_certificate) - - def set_domain(domain): """Set the TLS domain by writing a file to data directory.""" if domain: @@ -136,7 +130,7 @@ def get_domain(): pass if not domain: - domain = next(get_available_domains(), None) + domain = next(names.get_available_tls_domains(), None) set_domain(domain) return domain diff --git a/plinth/modules/quassel/forms.py b/plinth/modules/quassel/forms.py deleted file mode 100644 index 72eef7e3d..000000000 --- a/plinth/modules/quassel/forms.py +++ /dev/null @@ -1,27 +0,0 @@ -# SPDX-License-Identifier: AGPL-3.0-or-later -""" -Forms for Quassel app. -""" - -from django import forms -from django.utils.translation import gettext_lazy as _ - -from plinth.modules import quassel - - -def get_domain_choices(): - """Double domain entries for inclusion in the choice field.""" - return ((domain, domain) for domain in quassel.get_available_domains()) - - -class QuasselForm(forms.Form): - """Form to select a TLS domain for Quassel.""" - - domain = forms.ChoiceField( - choices=get_domain_choices, - label=_('TLS domain'), - help_text=_( - 'Select a domain to use TLS with. If the list is empty, please ' - 'configure at least one domain with certificates.'), - required=False, - ) diff --git a/plinth/modules/quassel/views.py b/plinth/modules/quassel/views.py index a5e671d0b..90d2ca56b 100644 --- a/plinth/modules/quassel/views.py +++ b/plinth/modules/quassel/views.py @@ -3,15 +3,14 @@ from django.contrib import messages from django.utils.translation import gettext_lazy as _ +from plinth.forms import TLSDomainForm from plinth.modules import quassel from plinth.views import AppView -from .forms import QuasselForm - class QuasselAppView(AppView): app_id = 'quassel' - form_class = QuasselForm + form_class = TLSDomainForm def get_initial(self): """Return the values to fill in the form."""