hobo/hobo/maintenance/views.py

78 lines
3.1 KiB
Python

# hobo - portal to configure and deploy applications
# Copyright (C) 2015-2022 Entr'ouvert
#
# 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/>.
from django.conf import settings
from django.urls import reverse_lazy
from django.utils.functional import cached_property
from django.views.generic import FormView
from hobo.environment.utils import get_setting_variable
from .forms import MaintenanceForm
class HomeView(FormView):
template_name = 'hobo/maintenance/home.html'
form_class = MaintenanceForm
success_url = reverse_lazy('maintenance-home')
@cached_property
def maintenance_page_variable(self):
return get_setting_variable('MAINTENANCE_PAGE')
@cached_property
def maintenance_page_message_variable(self):
return get_setting_variable('MAINTENANCE_PAGE_MESSAGE')
@cached_property
def maintenance_pass_trough_header_variable(self):
return get_setting_variable('MAINTENANCE_PASS_THROUGH_HEADER')
@cached_property
def tenant_disable_cron_jobs_variable(self):
return get_setting_variable('TENANT_DISABLE_CRON_JOBS')
def get_initial(self):
initial = super().get_initial()
initial['maintenance_page'] = bool(self.maintenance_page_variable.json)
initial['maintenance_page_message'] = self.maintenance_page_message_variable.value
initial['maintenance_pass_trough_header'] = self.maintenance_pass_trough_header_variable.value
initial['disable_cron'] = bool(self.tenant_disable_cron_jobs_variable.json)
return initial
def form_valid(self, form):
self.maintenance_page_variable.json = form.cleaned_data['maintenance_page']
self.maintenance_page_variable.save()
self.maintenance_page_message_variable.value = form.cleaned_data['maintenance_page_message']
self.maintenance_page_message_variable.save()
self.maintenance_pass_trough_header_variable.value = form.cleaned_data[
'maintenance_pass_trough_header'
]
self.maintenance_pass_trough_header_variable.save()
self.tenant_disable_cron_jobs_variable.json = form.cleaned_data['disable_cron']
self.tenant_disable_cron_jobs_variable.save()
return super().form_valid(form)
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
pass_through_ips_setting = getattr(settings, 'MAINTENANCE_PASS_THROUGH_IPS', [])
ctx['pass_through_ips'] = ', '.join(pass_through_ips_setting)
return ctx
home = HomeView.as_view()