mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
config: refactoring of config.py into views and form
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
37e431d1f1
commit
644b4ef4e5
11 files changed
+177
-143
No files matched your search
@@ -0,0 +1,114 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Forms for basic system configuration
|
||||
"""
|
||||
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.conf import settings
|
||||
from django.utils import translation
|
||||
|
||||
from plinth import cfg
|
||||
from plinth.utils import format_lazy
|
||||
|
||||
import plinth
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
HOSTNAME_REGEX = r'^[a-zA-Z0-9]([-a-zA-Z0-9]{,61}[a-zA-Z0-9])?$'
|
||||
|
||||
|
||||
class TrimmedCharField(forms.CharField):
|
||||
"""Trim the contents of a CharField"""
|
||||
def clean(self, value):
|
||||
"""Clean and validate the field value"""
|
||||
if value:
|
||||
value = value.strip()
|
||||
|
||||
return super(TrimmedCharField, self).clean(value)
|
||||
|
||||
|
||||
def domain_label_validator(domainname):
|
||||
"""Validate domain name labels."""
|
||||
for label in domainname.split('.'):
|
||||
if not re.match(HOSTNAME_REGEX, label):
|
||||
raise ValidationError(_('Invalid domain name'))
|
||||
|
||||
|
||||
class ConfigurationForm(forms.Form):
|
||||
"""Main system configuration form"""
|
||||
# See:
|
||||
# https://tools.ietf.org/html/rfc952
|
||||
# https://tools.ietf.org/html/rfc1035#section-2.3.1
|
||||
# https://tools.ietf.org/html/rfc1123#section-2
|
||||
# https://tools.ietf.org/html/rfc2181#section-11
|
||||
hostname = TrimmedCharField(
|
||||
label=ugettext_lazy('Hostname'),
|
||||
help_text=format_lazy(ugettext_lazy(
|
||||
'Hostname is the local name by which other devices on the local '
|
||||
'network can reach your {box_name}. It must start and end with '
|
||||
'an alphabet or a digit and have as interior characters only '
|
||||
'alphabets, digits and hyphens. Total length must be 63 '
|
||||
'characters or less.'), box_name=ugettext_lazy(cfg.box_name)),
|
||||
validators=[
|
||||
validators.RegexValidator(
|
||||
HOSTNAME_REGEX,
|
||||
ugettext_lazy('Invalid hostname'))])
|
||||
|
||||
domainname = TrimmedCharField(
|
||||
label=ugettext_lazy('Domain Name'),
|
||||
help_text=format_lazy(ugettext_lazy(
|
||||
'Domain name is the global name by which other devices on the '
|
||||
'Internet can reach your {box_name}. It must consist of labels '
|
||||
'separated by dots. Each label must start and end with an '
|
||||
'alphabet or a digit and have as interior characters only '
|
||||
'alphabets, digits and hyphens. Length of each label must be 63 '
|
||||
'characters or less. Total length of domain name must be 253 '
|
||||
'characters or less.'), box_name=ugettext_lazy(cfg.box_name)),
|
||||
required=False,
|
||||
validators=[
|
||||
validators.RegexValidator(
|
||||
r'^[a-zA-Z0-9]([-a-zA-Z0-9.]{,251}[a-zA-Z0-9])?$',
|
||||
ugettext_lazy('Invalid domain name')),
|
||||
domain_label_validator])
|
||||
|
||||
language = forms.ChoiceField(
|
||||
label=ugettext_lazy('Language'),
|
||||
help_text=ugettext_lazy(
|
||||
'Language for this web administration interface'),
|
||||
required=False)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Set limited language choices."""
|
||||
super().__init__(*args, **kwargs)
|
||||
languages = []
|
||||
for language_code, language_name in settings.LANGUAGES:
|
||||
locale_code = translation.to_locale(language_code)
|
||||
plinth_dir = os.path.dirname(plinth.__file__)
|
||||
if language_code == 'en' or os.path.exists(
|
||||
os.path.join(plinth_dir, 'locale', locale_code)):
|
||||
languages.append((language_code, language_name))
|
||||
|
||||
self.fields['language'].choices = languages
|
||||
Reference in new issue
Block a user