2015-04-26 13:53:00 +00:00
|
|
|
from django.contrib import admin, messages
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.shortcuts import render
|
2015-04-26 15:52:40 +00:00
|
|
|
from django.views import generic
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import ngettext, gettext_lazy as _
|
2015-04-26 13:53:00 +00:00
|
|
|
|
2015-05-04 19:52:53 +00:00
|
|
|
from orchestra.contrib.settings import Setting
|
2015-05-19 13:27:04 +00:00
|
|
|
from orchestra.utils import sys
|
2015-04-26 13:53:00 +00:00
|
|
|
|
|
|
|
from . import parser
|
|
|
|
from .forms import SettingFormSet
|
|
|
|
|
|
|
|
|
2015-04-26 15:52:40 +00:00
|
|
|
class SettingView(generic.edit.FormView):
|
2015-04-26 13:53:00 +00:00
|
|
|
template_name = 'admin/settings/change_form.html'
|
2015-04-27 14:54:17 +00:00
|
|
|
reload_template_name = 'admin/settings/reload.html'
|
2015-04-26 13:53:00 +00:00
|
|
|
form_class = SettingFormSet
|
|
|
|
success_url = '.'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(SettingView, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'title': _("Change settings"),
|
|
|
|
'settings_file': parser.get_settings_file(),
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
initial_data = []
|
|
|
|
prev_app = None
|
|
|
|
account = 0
|
|
|
|
for name, setting in Setting.settings.items():
|
|
|
|
app = name.split('_')[0]
|
|
|
|
initial = {
|
|
|
|
'name': setting.name,
|
|
|
|
'help_text': setting.help_text,
|
|
|
|
'default': setting.default,
|
|
|
|
'type': type(setting.default),
|
|
|
|
'value': setting.value,
|
2015-04-26 15:52:40 +00:00
|
|
|
'setting': setting,
|
2015-04-26 13:53:00 +00:00
|
|
|
'app': app,
|
|
|
|
}
|
|
|
|
if app == 'ORCHESTRA':
|
|
|
|
initial_data.insert(account, initial)
|
|
|
|
account += 1
|
|
|
|
else:
|
|
|
|
initial_data.append(initial)
|
|
|
|
return initial_data
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
settings = Setting.settings
|
|
|
|
changes = {}
|
|
|
|
for data in form.cleaned_data:
|
|
|
|
setting = settings[data['name']]
|
|
|
|
if not isinstance(data['value'], parser.NotSupported) and setting.editable:
|
|
|
|
if setting.value != data['value']:
|
2015-10-04 19:57:00 +00:00
|
|
|
# Ignore differences between lists and tuples
|
|
|
|
if (type(setting.value) != type(data['value']) and
|
|
|
|
isinstance(data['value'], list) and
|
|
|
|
tuple(data['value']) == setting.value):
|
|
|
|
continue
|
2015-04-26 13:53:00 +00:00
|
|
|
if setting.default == data['value']:
|
|
|
|
changes[setting.name] = parser.Remove()
|
|
|
|
else:
|
2015-10-04 19:57:00 +00:00
|
|
|
changes[setting.name] = data['value']
|
2015-04-26 13:53:00 +00:00
|
|
|
if changes:
|
|
|
|
# Display confirmation
|
|
|
|
if not self.request.POST.get('confirmation'):
|
|
|
|
settings_file = parser.get_settings_file()
|
|
|
|
new_content = parser.apply(changes)
|
2015-05-19 13:27:04 +00:00
|
|
|
cmd = "cat <<EOF | diff %s -\n%s\nEOF" % (settings_file, new_content)
|
|
|
|
diff = sys.run(cmd, valid_codes=(1, 0)).stdout
|
2015-04-26 13:53:00 +00:00
|
|
|
context = self.get_context_data(form=form)
|
|
|
|
context['diff'] = diff
|
2015-10-04 19:57:00 +00:00
|
|
|
if not diff:
|
|
|
|
messages.warning(self.request, _("Changes detected but no diff %s.") % changes)
|
2015-04-26 13:53:00 +00:00
|
|
|
return self.render_to_response(context)
|
2015-05-25 19:16:07 +00:00
|
|
|
n = len(changes)
|
2015-04-26 13:53:00 +00:00
|
|
|
# Save changes
|
|
|
|
parser.save(changes)
|
2015-05-07 19:00:02 +00:00
|
|
|
sys.touch_wsgi()
|
2015-04-27 14:54:17 +00:00
|
|
|
context = {
|
|
|
|
'message': ngettext(
|
|
|
|
_("One change successfully applied, orchestra is being restarted."),
|
|
|
|
_("%s changes successfully applied, orchestra is being restarted.") % n,
|
|
|
|
n),
|
|
|
|
}
|
2023-11-17 12:25:13 +00:00
|
|
|
return render(None, self.reload_template_name, context)
|
2015-04-26 13:53:00 +00:00
|
|
|
else:
|
|
|
|
messages.success(self.request, _("No changes have been detected."))
|
|
|
|
return super(SettingView, self).form_valid(form)
|
|
|
|
|
|
|
|
|
2015-04-26 15:52:40 +00:00
|
|
|
class SettingFileView(generic.TemplateView):
|
|
|
|
template_name = 'admin/settings/view.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(SettingFileView, self).get_context_data(**kwargs)
|
|
|
|
settings_file = parser.get_settings_file()
|
|
|
|
with open(settings_file, 'r') as handler:
|
|
|
|
content = handler.read()
|
|
|
|
context.update({
|
|
|
|
'title': _("Settings file content"),
|
|
|
|
'settings_file': settings_file,
|
|
|
|
'content': content,
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register_url(r'^settings/setting/view/$', SettingFileView.as_view(), 'settings_setting_view')
|
|
|
|
admin.site.register_url(r'^settings/setting/$', SettingView.as_view(), 'settings_setting_change')
|