2021-07-19 14:36:10 +00:00
|
|
|
import tldextract
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.forms import ModelForm
|
|
|
|
from django.utils.translation import get_language, ugettext_lazy as _
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from .validators import domain_name_validator
|
2021-08-09 07:43:11 +00:00
|
|
|
from .models import VMInstance
|
2021-07-19 14:36:10 +00:00
|
|
|
from uncloud_pay.models import BillingAddress
|
|
|
|
|
|
|
|
|
|
|
|
class DomainNameField(forms.CharField):
|
|
|
|
description = 'Domain name form field'
|
|
|
|
default_validators = [domain_name_validator, ]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DomainNameField, self).__init__(*args, **kwargs)
|
|
|
|
|
2021-07-30 06:51:35 +00:00
|
|
|
class MainModelForm(ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(MainModelForm, self).__init__(*args, **kwargs)
|
|
|
|
for visible in self.visible_fields():
|
|
|
|
visible.field.widget.attrs['class'] = 'form-control'
|
|
|
|
|
|
|
|
class MainForm(forms.Form):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(MainForm, self).__init__(*args, **kwargs)
|
|
|
|
for visible in self.visible_fields():
|
|
|
|
if (isinstance(visible.field.widget, forms.TextInput)):
|
|
|
|
visible.field.widget.attrs['class'] = 'form-control'
|
|
|
|
elif (isinstance(visible.field.widget, forms.CheckboxInput)):
|
|
|
|
visible.field.widget.attrs['class'] = 'custom-control-input'
|
|
|
|
|
|
|
|
|
|
|
|
class InitialRequestForm(MainForm):
|
2021-07-19 14:36:10 +00:00
|
|
|
cores = forms.IntegerField(label='CPU', min_value=1, max_value=48, initial=1)
|
|
|
|
memory = forms.IntegerField(label='RAM', min_value=2, max_value=200, initial=2)
|
|
|
|
storage = forms.IntegerField(label='Storage', min_value=100, max_value=10000, initial=100)
|
2021-07-26 14:11:19 +00:00
|
|
|
pricing_name = forms.CharField(required=True)
|
|
|
|
|
2021-08-09 07:43:11 +00:00
|
|
|
class RequestDomainsNamesForm(MainForm):
|
|
|
|
homeserver_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Homeserver Name *'}))
|
|
|
|
webclient_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Webclient Name *'}))
|
|
|
|
is_open_registration = forms.BooleanField(required=False, initial=False)
|
|
|
|
|
|
|
|
def clean_homeserver_name(self):
|
|
|
|
homeserver_name = self.cleaned_data['homeserver_name']
|
|
|
|
if VMInstance.objects.filter(homeserver_domain=f"{homeserver_name}.matrix.ungleich.cloud").exists():
|
|
|
|
raise ValidationError("homeserver name already exists")
|
|
|
|
return homeserver_name
|
|
|
|
|
|
|
|
def clean_webclient_name(self):
|
|
|
|
webclient_name = self.cleaned_data['webclient_name']
|
|
|
|
if VMInstance.objects.filter(webclient_domain=f"{webclient_name}.matrix.0co2.cloud").exists():
|
|
|
|
raise ValidationError("webclient name already exists")
|
|
|
|
return webclient_name
|
|
|
|
|
|
|
|
class RequestDomainsForm(MainForm):
|
2021-07-30 06:51:35 +00:00
|
|
|
matrix_domain = DomainNameField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Matrix Domain *'}))
|
|
|
|
homeserver_domain = DomainNameField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Homeserver Domain *'}))
|
|
|
|
webclient_domain = DomainNameField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Webclient Domain *'}))
|
2021-07-19 14:36:10 +00:00
|
|
|
is_open_registration = forms.BooleanField(required=False, initial=False)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
homeserver_domain = self.cleaned_data.get('homeserver_domain', False)
|
|
|
|
webclient_domain = self.cleaned_data.get('webclient_domain', False)
|
|
|
|
if homeserver_domain and webclient_domain:
|
|
|
|
# Homserver-Domain and Webclient-Domain cannot be below the same second level domain (i.e. homeserver.abc.ch and webclient.def.cloud are ok,
|
|
|
|
# homeserver.abc.ch and webclient.abc.ch are not ok
|
|
|
|
homeserver_base = tldextract.extract(homeserver_domain).domain
|
|
|
|
webclient_base = tldextract.extract(webclient_domain).domain
|
|
|
|
if homeserver_base == webclient_base:
|
|
|
|
self._errors['webclient_domain'] = self.error_class([
|
|
|
|
'Homserver-Domain and Webclient-Domain cannot be below the same second level domain'])
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
|
2021-07-30 06:51:35 +00:00
|
|
|
class BillingAddressForm(MainModelForm):
|
2021-07-19 14:36:10 +00:00
|
|
|
class Meta:
|
|
|
|
model = BillingAddress
|
|
|
|
fields = ['full_name', 'street',
|
|
|
|
'city', 'postal_code', 'country', 'vat_number', 'active', 'owner']
|
|
|
|
|
|
|
|
|