names: Create a generic TLS domain selection form

Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalapati 2021-11-06 08:27:39 +05:30 committed by James Valleroy
parent 5c62fc7599
commit a912c867c8
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
5 changed files with 32 additions and 37 deletions

View File

@ -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."""

View File

@ -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)

View File

@ -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

View File

@ -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,
)

View File

@ -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."""