diff --git a/matrixhosting/forms.py b/matrixhosting/forms.py index 691da27..5521c3d 100644 --- a/matrixhosting/forms.py +++ b/matrixhosting/forms.py @@ -1,35 +1,8 @@ -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 from .models import VMInstance -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) - -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' +from uncloud.forms import MainForm, MainModelForm, DomainNameField class InitialRequestForm(MainForm): @@ -55,30 +28,4 @@ class RequestDomainsNamesForm(MainForm): raise ValidationError("webclient name already exists") return webclient_name -class RequestDomainsForm(MainForm): - 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 *'})) - 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 - - -class BillingAddressForm(MainModelForm): - class Meta: - model = BillingAddress - fields = ['full_name', 'street', - 'city', 'postal_code', 'country', 'vat_number', 'active', 'owner'] - diff --git a/matrixhosting/migrations/0014_alter_vminstance_order.py b/matrixhosting/migrations/0014_alter_vminstance_order.py new file mode 100644 index 0000000..bf4b6e4 --- /dev/null +++ b/matrixhosting/migrations/0014_alter_vminstance_order.py @@ -0,0 +1,20 @@ +# Generated by Django 3.2.4 on 2021-09-06 08:06 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0031_auto_20210819_1304'), + ('matrixhosting', '0013_auto_20210808_1652'), + ] + + operations = [ + migrations.AlterField( + model_name='vminstance', + name='order', + field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='matrix_instance_id', to='uncloud_pay.order'), + ), + ] diff --git a/matrixhosting/models.py b/matrixhosting/models.py index 1cabb0f..3c08ebc 100644 --- a/matrixhosting/models.py +++ b/matrixhosting/models.py @@ -1,6 +1,5 @@ import logging -import uuid -import os +import datetime import json import sys import gitlab @@ -17,6 +16,7 @@ from uncloud_pay.models import Order, BillRecord # Initialize logger. logger = logging.getLogger(__name__) + class VMInstance(models.Model): owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, @@ -30,7 +30,7 @@ class VMInstance(models.Model): config = models.JSONField(null=False, blank=False) - order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='instance_id') + order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='matrix_instance_id') creation_date = models.DateTimeField(auto_now_add=True) @@ -43,12 +43,12 @@ class VMInstance(models.Model): result = yaml.dump(self.config) gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_OAUTH_TOKEN) project = gl.projects.get(settings.GITLAB_PROJECT_ID) - project.files.create({'file_path': settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml', + project.files.create({'file_path': settings.GITLAB_YAML_DIR + f'matrix/{self.vm_name}.yaml', 'branch': 'master', 'content': result, 'author_email': settings.GITLAB_AUTHOR_EMAIL, 'author_name': settings.GITLAB_AUTHOR_NAME, - 'commit_message': f'Add New Deployment for matrix-{self.vm_name}'}) + 'commit_message': f'Add New Deployment for matrix/{self.vm_name}'}) super().save(*args, **kwargs) def delete(self, *args, **kwargs): @@ -58,11 +58,11 @@ class VMInstance(models.Model): return super().delete(*args, **kwargs) gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_OAUTH_TOKEN) project = gl.projects.get(settings.GITLAB_PROJECT_ID) - f_path = settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml' + f_path = settings.GITLAB_YAML_DIR + f'matrix/{self.vm_name}.yaml' file = project.files.get(file_path=f_path, ref='master') if file: project.files.delete(file_path=f_path, - commit_message=f'Delete matrix-{self.vm_name}', branch='master', + commit_message=f'Delete matrix/{self.vm_name}', branch='master', author_email=settings.GITLAB_AUTHOR_EMAIL, author_name=settings.GITLAB_AUTHOR_NAME) diff --git a/matrixhosting/templates/matrixhosting/includes/_navbar.html b/matrixhosting/templates/matrixhosting/includes/_navbar.html index 01aca4f..edddc61 100644 --- a/matrixhosting/templates/matrixhosting/includes/_navbar.html +++ b/matrixhosting/templates/matrixhosting/includes/_navbar.html @@ -19,7 +19,7 @@ {% url 'matrix:index' as index_url %} {% url 'matrix:orders' as orders_url %} {% url 'matrix:instances' as instances_url %} - {% url 'matrix:billing' as payments_url %} + {% url 'uncloud_pay:billing' as payments_url %} {% url 'matrix:pricing' as pricing_url %}