1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-11-01 12:04:15 +00:00
webvirtcloud/instances/forms.py

81 lines
3.4 KiB
Python
Raw Normal View History

2015-02-27 12:25:41 +00:00
import re
2020-07-13 09:33:09 +00:00
2015-02-27 12:25:41 +00:00
from django import forms
from django.utils.translation import gettext_lazy as _
2020-07-13 09:33:09 +00:00
from appsettings.models import AppSettings
from webvirtcloud.settings import QEMU_CONSOLE_LISTENER_ADDRESSES, QEMU_KEYMAPS
2020-07-13 09:33:09 +00:00
from .models import CreateInstance, Flavor
2020-07-13 09:33:09 +00:00
class FlavorForm(forms.ModelForm):
class Meta:
model = Flavor
2022-11-02 05:54:35 +00:00
fields = "__all__"
2020-07-13 09:33:09 +00:00
class ConsoleForm(forms.Form):
type = forms.ChoiceField()
listen_on = forms.ChoiceField()
generate_password = forms.BooleanField(required=False)
clear_password = forms.BooleanField(required=False)
2022-11-02 05:54:35 +00:00
password = forms.CharField(
widget=forms.PasswordInput(render_value=True),
required=False
)
2020-07-13 09:33:09 +00:00
clear_keymap = forms.BooleanField(required=False)
keymap = forms.ChoiceField(required=False)
def __init__(self, *args, **kwargs):
super(ConsoleForm, self).__init__(*args, **kwargs)
2022-11-02 05:54:35 +00:00
type_choices = (
(c, c)
for c in AppSettings.objects.get(key="QEMU_CONSOLE_DEFAULT_TYPE").choices_as_list()
)
keymap_choices = [("auto", "Auto")] + list((c, c) for c in QEMU_KEYMAPS)
self.fields["type"] = forms.ChoiceField(choices=type_choices)
self.fields["listen_on"] = forms.ChoiceField(
choices=QEMU_CONSOLE_LISTENER_ADDRESSES
)
self.fields["keymap"] = forms.ChoiceField(choices=keymap_choices)
2015-02-27 12:25:41 +00:00
class NewVMForm(forms.ModelForm):
# name = forms.CharField(error_messages={'required': _('No Virtual Machine name has been entered')}, max_length=64)
# firmware = forms.CharField(max_length=50, required=False)
# vcpu = forms.IntegerField(error_messages={'required': _('No VCPU has been entered')})
# vcpu_mode = forms.CharField(max_length=20, required=False)
# disk = forms.IntegerField(required=False)
# memory = forms.IntegerField(error_messages={'required': _('No RAM size has been entered')})
# networks = forms.CharField(error_messages={'required': _('No Network pool has been choosen')})
# nwfilter = forms.CharField(required=False)
# storage = forms.CharField(max_length=20, required=False)
# template = forms.CharField(required=False)
# images = forms.CharField(required=False)
# cache_mode = forms.CharField(error_messages={'required': _('Please select HDD cache mode')})
# hdd_size = forms.IntegerField(required=False)
# meta_prealloc = forms.BooleanField(required=False)
# virtio = forms.BooleanField(required=False)
# qemu_ga = forms.BooleanField(required=False)
# mac = forms.CharField(required=False)
# console_pass = forms.CharField(required=False, empty_value="", widget=forms.PasswordInput())
# graphics = forms.CharField(error_messages={'required': _('Please select a graphics type')})
# video = forms.CharField(error_messages={'required': _('Please select a video driver')})
# listener_addr = forms.ChoiceField(required=True, widget=forms.RadioSelect, choices=QEMU_CONSOLE_LISTENER_ADDRESSES)
class Meta:
model = CreateInstance
2022-11-02 05:54:35 +00:00
fields = "__all__"
exclude = ["compute"]
2015-02-27 12:25:41 +00:00
def clean_name(self):
2022-11-02 05:54:35 +00:00
name = self.cleaned_data["name"]
have_symbol = re.match("^[a-zA-Z0-9._-]+$", name)
2015-02-27 12:25:41 +00:00
if not have_symbol:
2022-11-02 05:54:35 +00:00
raise forms.ValidationError(
_(
"The name of the virtual machine must not contain any special characters"
)
)
2015-02-27 12:25:41 +00:00
return name