2014-11-12 16:33:40 +00:00
|
|
|
from django import forms
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.encoding import force_str
|
2014-11-12 16:33:40 +00:00
|
|
|
|
2016-05-07 10:32:51 +00:00
|
|
|
from orchestra.admin.utils import admin_link
|
2015-04-27 12:24:17 +00:00
|
|
|
from orchestra.forms.widgets import SpanWidget
|
2015-03-04 21:06:16 +00:00
|
|
|
|
2023-08-01 14:41:03 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from orchestra.core import validators
|
|
|
|
from orchestra.utils.python import random_ascii
|
2023-08-24 10:23:08 +00:00
|
|
|
from orchestra.settings import NEW_SERVERS, WEB_SERVERS
|
2023-08-01 14:41:03 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2023-08-24 10:23:08 +00:00
|
|
|
import textwrap
|
|
|
|
from orchestra.contrib.orchestration.models import Server
|
2014-11-12 16:33:40 +00:00
|
|
|
|
2016-05-07 10:32:51 +00:00
|
|
|
class PluginForm(forms.ModelForm):
|
2014-11-12 16:33:40 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2016-05-07 10:32:51 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2015-03-04 21:06:16 +00:00
|
|
|
if self.plugin_field in self.fields:
|
|
|
|
value = self.plugin.get_name()
|
2023-10-24 16:59:02 +00:00
|
|
|
display = '%s <a href=".">change</a>' % force_str(self.plugin.verbose_name)
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields[self.plugin_field].widget = SpanWidget(original=value, display=display)
|
2015-04-16 13:15:21 +00:00
|
|
|
help_text = self.fields[self.plugin_field].help_text
|
|
|
|
self.fields[self.plugin_field].help_text = getattr(self.plugin, 'help_text', help_text)
|
2016-05-07 10:32:51 +00:00
|
|
|
|
|
|
|
class PluginDataForm(PluginForm):
|
|
|
|
data = forms.CharField(widget=forms.HiddenInput, required=False)
|
2023-08-24 10:39:59 +00:00
|
|
|
target_server = forms.ModelChoiceField(queryset=Server.objects.filter(name__in=WEB_SERVERS),)
|
2016-05-07 10:32:51 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-08-24 10:39:59 +00:00
|
|
|
self.fields['sftpuser'].widget = forms.HiddenInput()
|
2015-03-27 19:50:54 +00:00
|
|
|
if self.instance:
|
2014-11-12 16:33:40 +00:00
|
|
|
for field in self.declared_fields:
|
|
|
|
initial = self.fields[field].initial
|
2015-03-27 19:50:54 +00:00
|
|
|
self.fields[field].initial = self.instance.data.get(field, initial)
|
2015-03-04 21:06:16 +00:00
|
|
|
if self.instance.pk:
|
2015-04-16 13:15:21 +00:00
|
|
|
# Admin Readonly fields are not availeble in self.fields, so we use Meta
|
|
|
|
plugin = getattr(self.instance, '%s_class' % self.plugin_field)
|
|
|
|
plugin_help_text = getattr(plugin, 'help_text', '')
|
2016-04-30 11:23:13 +00:00
|
|
|
model_help_text = self.instance._meta.get_field(self.plugin_field).help_text
|
2015-04-16 13:15:21 +00:00
|
|
|
self._meta.help_texts = {
|
|
|
|
self.plugin_field: plugin_help_text or model_help_text
|
|
|
|
}
|
2016-02-11 14:24:09 +00:00
|
|
|
for field in self.plugin.get_change_readonly_fields():
|
2015-10-09 12:54:30 +00:00
|
|
|
value = getattr(self.instance, field, None) or self.instance.data.get(field)
|
2015-03-23 15:36:51 +00:00
|
|
|
display = value
|
|
|
|
foo_display = getattr(self.instance, 'get_%s_display' % field, None)
|
|
|
|
if foo_display:
|
|
|
|
display = foo_display()
|
2015-03-04 21:06:16 +00:00
|
|
|
self.fields[field].required = False
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields[field].widget = SpanWidget(original=value, display=display)
|
2014-11-12 16:33:40 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2016-05-07 10:32:51 +00:00
|
|
|
super().clean()
|
2014-11-12 16:33:40 +00:00
|
|
|
data = {}
|
2015-04-02 16:14:55 +00:00
|
|
|
# Update data fields
|
|
|
|
for field in self.declared_fields:
|
2014-11-12 16:33:40 +00:00
|
|
|
try:
|
|
|
|
data[field] = self.cleaned_data[field]
|
|
|
|
except KeyError:
|
2015-04-02 16:14:55 +00:00
|
|
|
data[field] = self.data[field]
|
|
|
|
# Keep old data fields
|
|
|
|
for field, value in self.instance.data.items():
|
|
|
|
if field not in data:
|
|
|
|
try:
|
|
|
|
data[field] = self.cleaned_data[field]
|
|
|
|
except KeyError:
|
|
|
|
data[field] = value
|
2014-11-12 16:33:40 +00:00
|
|
|
self.cleaned_data['data'] = data
|
2016-05-07 10:32:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PluginModelAdapterForm(PluginForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(PluginForm, self).__init__(*args, **kwargs)
|
|
|
|
if self.plugin_field in self.fields:
|
|
|
|
# Provide a link to the related DB object change view
|
|
|
|
value = self.plugin.related_instance.pk
|
|
|
|
link = admin_link()(self.plugin.related_instance)
|
|
|
|
display = '%s <a href=".">change</a>' % link
|
|
|
|
self.fields[self.plugin_field].widget = SpanWidget(original=value, display=display)
|
|
|
|
help_text = self.fields[self.plugin_field].help_text
|
2023-08-01 14:41:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------
|
|
|
|
|
|
|
|
class ExtendedPluginDataForm(PluginDataForm):
|
|
|
|
# añade campos de username para creacion de sftpuser en servidores nuevos
|
|
|
|
username = forms.CharField(label=_("Username"), max_length=16,
|
|
|
|
required=False, validators=[validators.validate_name],
|
|
|
|
help_text=_("Required. 16 characters or fewer. Letters, digits and "
|
|
|
|
"@/./+/-/_ only."),
|
|
|
|
error_messages={
|
|
|
|
'invalid': _("This value may contain 16 characters or fewer, only letters, numbers and "
|
|
|
|
"@/./+/-/_ characters.")})
|
|
|
|
password1 = forms.CharField(label=_("Password"), required=False,
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
|
|
|
|
validators=[validators.validate_password],
|
|
|
|
help_text=_("Suggestion: %s") % random_ascii(15))
|
|
|
|
password2 = forms.CharField(label=_("Password confirmation"), required=False,
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
help_text=_("Enter the same password as above, for verification."))
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ExtendedPluginDataForm, self).__init__(*args, **kwargs)
|
|
|
|
if self.instance.id is not None:
|
|
|
|
self.fields['username'].widget = forms.HiddenInput()
|
|
|
|
self.fields['password1'].widget = forms.HiddenInput()
|
|
|
|
self.fields['password2'].widget = forms.HiddenInput()
|
|
|
|
|
2023-08-24 10:23:08 +00:00
|
|
|
if not self.instance.pk:
|
|
|
|
self.fields['target_server'].widget.attrs['onChange'] = textwrap.dedent("""\
|
|
|
|
field = $(".field-username, .field-password1, .field-password2");
|
|
|
|
input = $("#id_username, #id_password1, #id_password2");
|
|
|
|
if (%s.includes(this.options[this.selectedIndex].text)) {
|
|
|
|
field.removeClass("hidden");
|
|
|
|
} else {
|
|
|
|
field.addClass("hidden");
|
|
|
|
input.val("");
|
|
|
|
};""" % list(NEW_SERVERS)
|
|
|
|
)
|
|
|
|
|
2023-08-01 14:41:03 +00:00
|
|
|
def clean_username(self):
|
|
|
|
if not self.instance.id:
|
|
|
|
webapp_server = self.cleaned_data.get("target_server")
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
if webapp_server is None:
|
|
|
|
self.add_error("target_server", _("choice some server"))
|
|
|
|
else:
|
|
|
|
if webapp_server.name in NEW_SERVERS and not username:
|
|
|
|
self.add_error("username", _("SFTP user is required by new webservers"))
|
|
|
|
return username
|
|
|
|
|
|
|
|
def clean_password2(self):
|
|
|
|
password1 = self.cleaned_data.get("password1")
|
|
|
|
password2 = self.cleaned_data.get("password2")
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
msg = _("The two password fields didn't match.")
|
|
|
|
raise ValidationError(msg)
|
|
|
|
return password2
|