django-orchestra-test/orchestra/apps/saas/services/options.py

120 lines
4.6 KiB
Python
Raw Normal View History

2014-09-26 19:21:09 +00:00
from django import forms
2014-11-09 10:16:07 +00:00
from django.core.exceptions import ValidationError
2014-11-20 15:34:59 +00:00
from django.utils.safestring import mark_safe
2014-09-26 19:21:09 +00:00
from django.utils.translation import ugettext_lazy as _
from orchestra import plugins
2015-04-02 16:14:55 +00:00
from orchestra.apps.orchestration.models import BackendOperation as Operation
2014-11-20 15:34:59 +00:00
from orchestra.core import validators
from orchestra.forms import widgets
2015-04-02 16:14:55 +00:00
from orchestra.plugins.forms import PluginDataForm
2014-09-26 19:21:09 +00:00
from orchestra.utils.functional import cached
from orchestra.utils.python import import_class, random_ascii
2014-09-26 19:21:09 +00:00
from .. import settings
2014-11-20 15:34:59 +00:00
class SoftwareServiceForm(PluginDataForm):
site_url = forms.CharField(label=_("Site URL"), widget=widgets.ShowTextWidget, required=False)
2014-11-20 15:34:59 +00:00
password = forms.CharField(label=_("Password"), required=False,
widget=widgets.ReadOnlyWidget('<strong>Unknown password</strong>'),
2015-03-23 15:36:51 +00:00
help_text=_("Passwords are not stored, so there is no way to see this "
2014-11-20 15:34:59 +00:00
"service's password, but you can change the password using "
"<a href=\"password/\">this form</a>."))
password1 = forms.CharField(label=_("Password"), validators=[validators.validate_password],
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput,
help_text=_("Enter the same password as above, for verification."))
2015-03-27 19:50:54 +00:00
class Meta:
exclude = ('database',)
2014-11-20 15:34:59 +00:00
def __init__(self, *args, **kwargs):
super(SoftwareServiceForm, self).__init__(*args, **kwargs)
self.is_change = bool(self.instance and self.instance.pk)
if self.is_change:
site_domain = self.instance.get_site_domain()
2014-11-20 15:34:59 +00:00
self.fields['password1'].required = False
self.fields['password1'].widget = forms.HiddenInput()
self.fields['password2'].required = False
self.fields['password2'].widget = forms.HiddenInput()
else:
self.fields['password'].widget = forms.HiddenInput()
self.fields['password1'].help_text = _("Suggestion: %s") % random_ascii(10)
site_domain = self.plugin.site_domain
if site_domain:
site_link = '<a href="http://%s">%s</a>' % (site_domain, site_domain)
2014-11-20 15:34:59 +00:00
else:
site_link = '&lt;site_name&gt;.%s' % self.plugin.site_base_domain
self.fields['site_url'].initial = site_link
self.fields['name'].label = _("Username")
2014-11-20 15:34:59 +00:00
def clean_password2(self):
if not self.is_change:
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 forms.ValidationError(msg)
return password2
def save(self, commit=True):
obj = super(SoftwareServiceForm, self).save(commit=commit)
if not self.is_change:
obj.set_password(self.cleaned_data["password1"])
return obj
2014-09-26 19:21:09 +00:00
class SoftwareService(plugins.Plugin):
2014-11-20 15:34:59 +00:00
form = SoftwareServiceForm
site_domain = None
site_base_domain = None
2015-03-23 15:36:51 +00:00
has_custom_domain = False
2014-10-11 12:43:08 +00:00
icon = 'orchestra/icons/apps.png'
class_verbose_name = _("Software as a Service")
2015-03-23 15:36:51 +00:00
plugin_field = 'service'
2014-09-26 19:21:09 +00:00
@classmethod
@cached
def get_plugins(cls):
plugins = []
for cls in settings.SAAS_ENABLED_SERVICES:
plugins.append(import_class(cls))
return plugins
2015-03-04 21:06:16 +00:00
def get_change_readonly_fileds(cls):
2015-03-23 15:36:51 +00:00
fields = super(SoftwareService, cls).get_change_readonly_fileds()
return fields + ('name',)
2015-03-04 21:06:16 +00:00
def get_site_domain(self):
return self.site_domain or '.'.join(
(self.instance.name, self.site_base_domain)
)
2014-11-20 15:34:59 +00:00
2015-04-02 16:14:55 +00:00
def clean_data(self):
data = super(SoftwareService, self).clean_data()
if not self.instance.pk:
try:
log = Operation.execute_action(self.instance, 'validate_creation')[0]
except IndexError:
pass
else:
errors = {}
if 'user-exists' in log.stdout:
errors['name'] = _("User with this username already exists.")
elif 'email-exists' in log.stdout:
errors['email'] = _("User with this email address already exists.")
if errors:
raise ValidationError(errors)
return data
2015-03-23 15:36:51 +00:00
def save(self):
pass
2014-09-26 19:21:09 +00:00
2015-03-23 15:36:51 +00:00
def delete(self):
pass
def get_related(self):
return []