2020-10-14 08:37:46 +00:00
|
|
|
from appsettings.settings import app_settings
|
|
|
|
from django.contrib.auth import get_user_model
|
2020-06-16 12:35:08 +00:00
|
|
|
from django.forms import ModelForm, ValidationError
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-06-16 12:35:08 +00:00
|
|
|
|
2020-10-14 08:37:46 +00:00
|
|
|
from .models import UserInstance, UserSSHKey
|
|
|
|
from .utils import validate_ssh_key
|
2020-06-16 12:35:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserInstanceForm(ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(UserInstanceForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
# Make user and instance fields not editable after creation
|
|
|
|
instance = getattr(self, 'instance', None)
|
|
|
|
if instance and instance.id is not None:
|
|
|
|
self.fields['user'].disabled = True
|
|
|
|
self.fields['instance'].disabled = True
|
|
|
|
|
|
|
|
def clean_instance(self):
|
|
|
|
instance = self.cleaned_data['instance']
|
2020-10-14 08:37:46 +00:00
|
|
|
if app_settings.ALLOW_INSTANCE_MULTIPLE_OWNER == 'False':
|
2020-06-16 12:35:08 +00:00
|
|
|
exists = UserInstance.objects.filter(instance=instance)
|
|
|
|
if exists:
|
|
|
|
raise ValidationError(_('Instance owned by another user'))
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserInstance
|
|
|
|
fields = '__all__'
|
2020-10-14 08:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProfileForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = get_user_model()
|
|
|
|
fields = ('first_name', 'last_name', 'email')
|
|
|
|
|
|
|
|
|
|
|
|
class UserSSHKeyForm(ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user', None)
|
|
|
|
self.publickeys = UserSSHKey.objects.filter(user=self.user)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean_keyname(self):
|
|
|
|
for key in self.publickeys:
|
|
|
|
if self.cleaned_data['keyname'] == key.keyname:
|
|
|
|
raise ValidationError(_("Key name already exist"))
|
|
|
|
|
|
|
|
return self.cleaned_data['keyname']
|
|
|
|
|
|
|
|
def clean_keypublic(self):
|
|
|
|
for key in self.publickeys:
|
|
|
|
if self.cleaned_data['keypublic'] == key.keypublic:
|
|
|
|
raise ValidationError(_("Public key already exist"))
|
|
|
|
|
|
|
|
if not validate_ssh_key(self.cleaned_data['keypublic']):
|
|
|
|
raise ValidationError(_('Invalid key'))
|
|
|
|
return self.cleaned_data['keypublic']
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
ssh_key = super().save(commit=False)
|
|
|
|
ssh_key.user = self.user
|
|
|
|
if commit:
|
|
|
|
ssh_key.save()
|
|
|
|
return ssh_key
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserSSHKey
|
|
|
|
fields = ('keyname', 'keypublic')
|