From 20c7c867036a96adbfdf1910e1c1a0ac049fc89a Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 25 Oct 2020 21:00:30 +0100 Subject: [PATCH] restructure to move uncloudnetwork into core --- uncloud/admin.py | 4 +- .../management/commands/db-add-defaults.py | 3 +- uncloud/migrations/0006_auto_20201025_1931.py | 40 +++++++++++++++ uncloud/models.py | 45 ++++++++++++++++- uncloud_net/admin.py | 5 +- .../migrations/0011_auto_20201025_1931.py | 29 +++++++++++ uncloud_net/models.py | 49 +++++-------------- uncloud_pay/models.py | 2 +- 8 files changed, 130 insertions(+), 47 deletions(-) create mode 100644 uncloud/migrations/0006_auto_20201025_1931.py create mode 100644 uncloud_net/migrations/0011_auto_20201025_1931.py diff --git a/uncloud/admin.py b/uncloud/admin.py index 330ba79..4ecc53a 100644 --- a/uncloud/admin.py +++ b/uncloud/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import UncloudProvider +from .models import UncloudProvider, UncloudNetwork -for m in [ UncloudProvider ]: +for m in [ UncloudProvider, UncloudNetwork ]: admin.site.register(m) diff --git a/uncloud/management/commands/db-add-defaults.py b/uncloud/management/commands/db-add-defaults.py index 60bcdb2..605c8f5 100644 --- a/uncloud/management/commands/db-add-defaults.py +++ b/uncloud/management/commands/db-add-defaults.py @@ -7,8 +7,7 @@ from django.contrib.auth import get_user_model from django.conf import settings from uncloud_pay.models import BillingAddress, RecurringPeriod, Product -from uncloud_net.models import UncloudNetwork -from uncloud.models import UncloudProvider +from uncloud.models import UncloudProvider, UncloudNetwork class Command(BaseCommand): diff --git a/uncloud/migrations/0006_auto_20201025_1931.py b/uncloud/migrations/0006_auto_20201025_1931.py new file mode 100644 index 0000000..d1162ef --- /dev/null +++ b/uncloud/migrations/0006_auto_20201025_1931.py @@ -0,0 +1,40 @@ +# Generated by Django 3.1 on 2020-10-25 19:31 + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud', '0005_uncloudprovider_coupon_network'), + ] + + operations = [ + migrations.CreateModel( + name='UncloudNetwork', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('network_address', models.GenericIPAddressField(unique=True)), + ('network_mask', models.IntegerField(validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(128)])), + ('description', models.CharField(max_length=256)), + ], + ), + migrations.AlterField( + model_name='uncloudprovider', + name='billing_network', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='uncloudproviderbill', to='uncloud.uncloudnetwork'), + ), + migrations.AlterField( + model_name='uncloudprovider', + name='coupon_network', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='uncloudprovidercoupon', to='uncloud.uncloudnetwork'), + ), + migrations.AlterField( + model_name='uncloudprovider', + name='referral_network', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='uncloudproviderreferral', to='uncloud.uncloudnetwork'), + ), + ] +2 diff --git a/uncloud/models.py b/uncloud/models.py index 623d144..5a65f1c 100644 --- a/uncloud/models.py +++ b/uncloud/models.py @@ -2,10 +2,9 @@ from django.db import models from django.db.models import JSONField, Q from django.utils import timezone from django.utils.translation import gettext_lazy as _ +from django.core.validators import MinValueValidator, MaxValueValidator from uncloud import COUNTRIES -from uncloud_net.models import UncloudNetwork - class UncloudModel(models.Model): """ @@ -66,6 +65,48 @@ class UncloudAddress(models.Model): class Meta: abstract = True + +### +# UncloudNetworks are used as identifiers - such they are a base of uncloud + +class UncloudNetwork(models.Model): + """ + Storing IP networks + """ + + network_address = models.GenericIPAddressField(null=False, unique=True) + network_mask = models.IntegerField(null=False, + validators=[MinValueValidator(0), + MaxValueValidator(128)] + ) + + description = models.CharField(max_length=256) + + @classmethod + def populate_db_defaults(cls): + for net, desc in [ + ( "2a0a:e5c0:11::", "uncloud Billing" ), + ( "2a0a:e5c0:11:1::", "uncloud Referral" ), + ( "2a0a:e5c0:11:2::", "uncloud Coupon" ) + ]: + obj, created = cls.objects.get_or_create(network_address=net, + defaults= { + 'network_mask': 64, + 'description': desc + } + ) + + + def save(self, *args, **kwargs): + if not ':' in self.network_address and self.network_mask > 32: + raise FieldError("Mask cannot exceed 32 for IPv4") + + super().save(*args, **kwargs) + + + def __str__(self): + return f"{self.network_address}/{self.network_mask} {self.description}" + ### # Who is running / providing this instance of uncloud? diff --git a/uncloud_net/admin.py b/uncloud_net/admin.py index 29782ef..dec357a 100644 --- a/uncloud_net/admin.py +++ b/uncloud_net/admin.py @@ -1,6 +1,5 @@ from django.contrib import admin -from .models import UncloudNetwork -for m in [ UncloudNetwork ]: - admin.site.register(m) +# for m in [ UncloudNetwork ]: +# admin.site.register(m) diff --git a/uncloud_net/migrations/0011_auto_20201025_1931.py b/uncloud_net/migrations/0011_auto_20201025_1931.py new file mode 100644 index 0000000..c4135d9 --- /dev/null +++ b/uncloud_net/migrations/0011_auto_20201025_1931.py @@ -0,0 +1,29 @@ +# Generated by Django 3.1 on 2020-10-25 19:31 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('uncloud', '0006_auto_20201025_1931'), + ('uncloud_net', '0010_auto_20201011_2009'), + ] + + operations = [ + migrations.CreateModel( + name='ReverseDNSEntry', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('ip_address', models.GenericIPAddressField(unique=True)), + ('name', models.CharField(max_length=253)), + ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.DeleteModel( + name='UncloudNetwork', + ), + ] diff --git a/uncloud_net/models.py b/uncloud_net/models.py index e4e1bb1..afd5004 100644 --- a/uncloud_net/models.py +++ b/uncloud_net/models.py @@ -6,43 +6,6 @@ from django.contrib.auth import get_user_model from django.core.validators import MinValueValidator, MaxValueValidator from django.core.exceptions import FieldError -class UncloudNetwork(models.Model): - """ - Storing IP networks - """ - - network_address = models.GenericIPAddressField(null=False, unique=True) - network_mask = models.IntegerField(null=False, - validators=[MinValueValidator(0), - MaxValueValidator(128)] - ) - - description = models.CharField(max_length=256) - - @classmethod - def populate_db_defaults(cls): - for net, desc in [ - ( "2a0a:e5c0:11::", "uncloud Billing" ), - ( "2a0a:e5c0:11:1::", "uncloud Referral" ), - ( "2a0a:e5c0:11:2::", "uncloud Coupon" ) - ]: - obj, created = cls.objects.get_or_create(network_address=net, - defaults= { - 'network_mask': 64, - 'description': desc - } - ) - - - def save(self, *args, **kwargs): - if not ':' in self.network_address and self.network_mask > 32: - raise FieldError("Mask cannot exceed 32 for IPv4") - - super().save(*args, **kwargs) - - - def __str__(self): - return f"{self.network_address}/{self.network_mask} {self.description}" class MACAdress(models.Model): default_prefix = 0x420000000000 @@ -219,3 +182,15 @@ class VPNNetwork(models.Model): self.network.save() super().save(*args, **kwargs) print("deleted {}".format(self)) + + +class ReverseDNSEntry(models.Model): + """ + A reverse DNS entry + """ + owner = models.ForeignKey(get_user_model(), + on_delete=models.CASCADE) + + ip_address = models.GenericIPAddressField(null=False, unique=True) + + name = models.CharField(max_length=253, null=False) diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 5bff5a3..00163b6 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -1061,7 +1061,7 @@ class Bill(models.Model): provider = UncloudProvider.objects.get() # Assume always VAT inside the country - if provider.country = self.billing_address.country: + if provider.country == self.billing_address.country: vat_rate = VATRate.objects.get(country=provider.country, when=self.ending_date) elif self.billing_address.country in EU: