1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-11-01 20:14:15 +00:00
webvirtcloud/computes/forms.py

71 lines
2.6 KiB
Python
Raw Normal View History

2015-03-06 11:59:27 +00:00
import re
from django import forms
from django.utils.translation import ugettext_lazy as _
from computes.models import Compute
2020-05-29 13:25:36 +00:00
from vrtManager.connection import CONN_TCP, CONN_SSH, CONN_TLS, CONN_SOCKET
from .validators import validate_hostname
2015-03-06 11:59:27 +00:00
2020-05-29 13:25:36 +00:00
class TcpComputeForm(forms.ModelForm):
hostname = forms.CharField(validators=[validate_hostname])
type = forms.IntegerField(widget=forms.HiddenInput, initial=CONN_TCP)
2015-03-06 11:59:27 +00:00
2020-05-29 13:25:36 +00:00
class Meta:
model = Compute
fields = '__all__'
2015-03-06 11:59:27 +00:00
2020-05-29 13:25:36 +00:00
class SshComputeForm(forms.ModelForm):
hostname = forms.CharField(validators=[validate_hostname], label=_("FQDN/IP"))
type = forms.IntegerField(widget=forms.HiddenInput, initial=CONN_SSH)
2015-03-06 11:59:27 +00:00
2020-05-29 13:25:36 +00:00
class Meta:
model = Compute
exclude = ['password']
2015-03-06 11:59:27 +00:00
2020-05-29 13:25:36 +00:00
class TlsComputeForm(forms.ModelForm):
hostname = forms.CharField(validators=[validate_hostname])
type = forms.IntegerField(widget=forms.HiddenInput, initial=CONN_TLS)
class Meta:
model = Compute
fields = '__all__'
class SocketComputeForm(forms.ModelForm):
hostname = forms.CharField(widget=forms.HiddenInput, initial='localhost')
type = forms.IntegerField(widget=forms.HiddenInput, initial=CONN_SOCKET)
class Meta:
model = Compute
fields = ['name', 'details', 'hostname', 'type']
2015-03-06 11:59:27 +00:00
class ComputeEditHostForm(forms.Form):
host_id = forms.CharField()
2020-05-29 13:25:36 +00:00
name = forms.CharField(error_messages={'required': _('No hostname has been entered')}, max_length=64)
hostname = forms.CharField(error_messages={'required': _('No IP / Domain name has been entered')}, max_length=100)
login = forms.CharField(error_messages={'required': _('No login has been entered')}, max_length=100)
2015-03-06 11:59:27 +00:00
password = forms.CharField(max_length=100)
2018-07-31 07:15:59 +00:00
details = forms.CharField(max_length=50, required=False)
2015-03-06 11:59:27 +00:00
def clean_name(self):
name = self.cleaned_data['name']
have_symbol = re.match('[^a-zA-Z0-9._-]+', name)
if have_symbol:
raise forms.ValidationError(_('The name of the host must not contain any special characters'))
elif len(name) > 20:
raise forms.ValidationError(_('The name of the host must not exceed 20 characters'))
return name
def clean_hostname(self):
hostname = self.cleaned_data['hostname']
have_symbol = re.match('[^a-zA-Z0-9._-]+', hostname)
wrong_ip = re.match('^0.|^255.', hostname)
if have_symbol:
raise forms.ValidationError(_('Hostname must contain only numbers, or the domain name separated by "."'))
elif wrong_ip:
raise forms.ValidationError(_('Wrong IP address'))
return hostname