diff --git a/computes/forms.py b/computes/forms.py index bbde4d4..3a27a39 100644 --- a/computes/forms.py +++ b/computes/forms.py @@ -2,131 +2,51 @@ import re from django import forms from django.utils.translation import ugettext_lazy as _ from computes.models import Compute +from vrtManager.connection import CONN_TCP, CONN_SSH, CONN_TLS, CONN_SOCKET +from .validators import validate_hostname -class ComputeAddTcpForm(forms.Form): - 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) - password = forms.CharField(error_messages={'required': _('No password has been entered')}, - max_length=100) - details = forms.CharField(max_length=50, required=False) +class TcpComputeForm(forms.ModelForm): + hostname = forms.CharField(validators=[validate_hostname]) + type = forms.IntegerField(widget=forms.HiddenInput, initial=CONN_TCP) - 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 host name must not contain any special characters')) - elif len(name) > 20: - raise forms.ValidationError(_('The host name must not exceed 20 characters')) - try: - Compute.objects.get(name=name) - except Compute.DoesNotExist: - return name - raise forms.ValidationError(_('This host is already connected')) - - def clean_hostname(self): - hostname = self.cleaned_data['hostname'] - have_symbol = re.match('[^a-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')) - try: - Compute.objects.get(hostname=hostname) - except Compute.DoesNotExist: - return hostname - raise forms.ValidationError(_('This host is already connected')) + class Meta: + model = Compute + fields = '__all__' -class ComputeAddSshForm(forms.Form): - 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=20) - details = forms.CharField(max_length=50, required=False) +class SshComputeForm(forms.ModelForm): + hostname = forms.CharField(validators=[validate_hostname], label=_("FQDN/IP")) + type = forms.IntegerField(widget=forms.HiddenInput, initial=CONN_SSH) - 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')) - try: - Compute.objects.get(name=name) - except Compute.DoesNotExist: - return name - raise forms.ValidationError(_('This host is already connected')) - - 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')) - try: - Compute.objects.get(hostname=hostname) - except Compute.DoesNotExist: - return hostname - raise forms.ValidationError(_('This host is already connected')) + class Meta: + model = Compute + exclude = ['password'] -class ComputeAddTlsForm(forms.Form): - 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) - password = forms.CharField(error_messages={'required': _('No password has been entered')}, - max_length=100) - details = forms.CharField(max_length=50, required=False) +class TlsComputeForm(forms.ModelForm): + hostname = forms.CharField(validators=[validate_hostname]) + type = forms.IntegerField(widget=forms.HiddenInput, initial=CONN_TLS) - 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 host name must not contain any special characters')) - elif len(name) > 20: - raise forms.ValidationError(_('The host name must not exceed 20 characters')) - try: - Compute.objects.get(name=name) - except Compute.DoesNotExist: - return name - raise forms.ValidationError(_('This host is already connected')) + class Meta: + model = Compute + fields = '__all__' - def clean_hostname(self): - hostname = self.cleaned_data['hostname'] - have_symbol = re.match('[^a-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')) - try: - Compute.objects.get(hostname=hostname) - except Compute.DoesNotExist: - return hostname - raise forms.ValidationError(_('This host is already connected')) + +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'] class ComputeEditHostForm(forms.Form): host_id = forms.CharField() - 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) + 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) password = forms.CharField(max_length=100) details = forms.CharField(max_length=50, required=False) @@ -148,21 +68,3 @@ class ComputeEditHostForm(forms.Form): elif wrong_ip: raise forms.ValidationError(_('Wrong IP address')) return hostname - - -class ComputeAddSocketForm(forms.Form): - name = forms.CharField(error_messages={'required': _('No hostname has been entered')}, max_length=64) - details = forms.CharField(error_messages={'required': _('No details has been entred')}, max_length=50) - - 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 host name must not contain any special characters')) - elif len(name) > 20: - raise forms.ValidationError(_('The host name must not exceed 20 characters')) - try: - Compute.objects.get(name=name) - except Compute.DoesNotExist: - return name - raise forms.ValidationError(_('This host is already connected')) diff --git a/computes/migrations/0002_auto_20200529_1320.py b/computes/migrations/0002_auto_20200529_1320.py new file mode 100644 index 0000000..194d885 --- /dev/null +++ b/computes/migrations/0002_auto_20200529_1320.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.12 on 2020-05-29 13:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('computes', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='compute', + name='name', + field=models.CharField(max_length=64, unique=True), + ), + ] diff --git a/computes/models.py b/computes/models.py index db224a3..bda751b 100644 --- a/computes/models.py +++ b/computes/models.py @@ -2,7 +2,7 @@ from django.db.models import Model, CharField, IntegerField class Compute(Model): - name = CharField(max_length=64) + name = CharField(max_length=64, unique=True) hostname = CharField(max_length=64) login = CharField(max_length=20) password = CharField(max_length=14, blank=True, null=True) diff --git a/computes/templates/computes/form.html b/computes/templates/computes/form.html new file mode 100644 index 0000000..7b3489e --- /dev/null +++ b/computes/templates/computes/form.html @@ -0,0 +1,29 @@ +{% extends "base.html" %} +{% load bootstrap3 %} +{% load font_awesome %} +{% load i18n %} + +{% block title %}{% trans "Add Compute" %}{% endblock %} + +{% block content %} +