cleanup migrations

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-08-01 18:38:38 +02:00
commit f7b14bf507
22 changed files with 148 additions and 198 deletions

View file

@ -0,0 +1,77 @@
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.utils import timezone
from datetime import datetime, timedelta
from uncloud_pay.models import *
#import opennebula.models as one
from uncloud_vm.models import *
def vm_price_2020(cpu=1, ram=2, v6only=False):
if v6only:
discount = 9
else:
discount = 0
return cpu*3 + ram*4 - discount
def disk_price_2020(size_in_gb, disk_type):
if disk_type == VMDiskType.CEPH_SSD:
price = 3.5/10
elif disk_type == VMDiskType.CEPH_HDD:
price = 1.5/100
else:
raise Exception("not yet defined price")
return size_in_gb * price
class Command(BaseCommand):
help = 'Adding VMs / creating orders for user'
def add_arguments(self, parser):
parser.add_argument('--username', type=str, required=True)
def handle(self, *args, **options):
user = get_user_model().objects.get(username=options['username'])
addr, created = BillingAddress.objects.get_or_create(
owner=user,
active=True,
defaults={'organization': 'Undefined organisation',
'name': 'Undefined name',
'street': 'Undefined Street',
'city': 'Undefined city',
'postal_code': '8750',
'country': 'CH',
'active': True
}
)
orders = []
# 25206
vm25206 = VMProduct.objects.create(name="OpenNebula 25206",
cores=1,
ram_in_gb=4,
owner=user)
vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206,
owner=user,
size_in_gb=30)
order_vm_25206 = Order.objects.create(owner=user,
billing_address=addr,
starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
recurring_period=RecurringPeriod.PER_30D,
recurring_price = vm_price_2020(cpu=1, ram=4) / 2,
description = "VM %s" % vm25206
)
order_vm_25206_ssd = Order.objects.create(owner=user,
billing_address=addr,
starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
recurring_period=RecurringPeriod.PER_30D,
recurring_price = disk_price_2020(30, VMDiskType.CEPH_SSD) / 2,
description = vm25206_ssd
)

View file

@ -1,4 +1,4 @@
# Generated by Django 3.0.6 on 2020-06-21 12:34
# Generated by Django 3.0.6 on 2020-08-01 16:38
from django.conf import settings
import django.core.validators
@ -23,9 +23,9 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('creation_date', models.DateTimeField(auto_now_add=True)),
('starting_date', models.DateTimeField()),
('ending_date', models.DateTimeField()),
('due_date', models.DateField()),
('starting_date', models.DateTimeField(default=uncloud_pay.models.start_of_this_month)),
('ending_date', models.DateTimeField(default=uncloud_pay.models.end_of_this_month)),
('due_date', models.DateField(default=uncloud_pay.models.default_payment_delay)),
('valid', models.BooleanField(default=True)),
],
),
@ -53,10 +53,12 @@ class Migration(migrations.Migration):
('starting_date', models.DateTimeField(default=django.utils.timezone.now)),
('ending_date', models.DateTimeField(blank=True, null=True)),
('recurring_period', models.IntegerField(choices=[(31536000, 'Per 365 days'), (2592000, 'Per 30 days'), (604800, 'Per Week'), (86400, 'Per Day'), (3600, 'Per Hour'), (60, 'Per Minute'), (1, 'Per Second'), (0, 'Onetime')], default=2592000)),
('one_time_price', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
('recurring_price', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
('billing_address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.BillingAddress')),
('depends_on', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='parent_of', to='uncloud_pay.Order')),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('replaced_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='supersede', to='uncloud_pay.Order')),
('replaces', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='replaced_by', to='uncloud_pay.Order')),
],
),
migrations.CreateModel(
@ -115,7 +117,7 @@ class Migration(migrations.Migration):
name='BillRecord',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('usage_count', models.IntegerField(default=1)),
('quantity', models.IntegerField(default=1)),
('creation_date', models.DateTimeField(auto_now_add=True)),
('starting_date', models.DateTimeField()),
('ending_date', models.DateTimeField()),
@ -137,4 +139,8 @@ class Migration(migrations.Migration):
model_name='billingaddress',
constraint=models.UniqueConstraint(condition=models.Q(active=True), fields=('owner',), name='one_active_billing_address_per_user'),
),
migrations.AddConstraint(
model_name='bill',
constraint=models.UniqueConstraint(fields=('owner', 'starting_date', 'ending_date'), name='one_bill_per_month_per_user'),
),
]

View file

@ -1,33 +0,0 @@
# Generated by Django 3.0.6 on 2020-06-21 13:35
from django.db import migrations, models
import uncloud_pay.models
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='bill',
name='due_date',
field=models.DateField(default=uncloud_pay.models.default_payment_delay),
),
migrations.AlterField(
model_name='bill',
name='ending_date',
field=models.DateTimeField(default=uncloud_pay.models.end_of_this_month),
),
migrations.AlterField(
model_name='bill',
name='starting_date',
field=models.DateTimeField(default=uncloud_pay.models.start_of_this_month),
),
migrations.AddConstraint(
model_name='bill',
constraint=models.UniqueConstraint(fields=('owner', 'starting_date', 'ending_date'), name='one_bill_per_month_per_user'),
),
]

View file

@ -1,18 +0,0 @@
# Generated by Django 3.0.6 on 2020-06-21 14:42
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0002_auto_20200621_1335'),
]
operations = [
migrations.RenameField(
model_name='billrecord',
old_name='usage_count',
new_name='quantity',
),
]

View file

@ -1,23 +0,0 @@
# Generated by Django 3.0.6 on 2020-08-01 16:04
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0003_auto_20200621_1442'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='replaced_by',
),
migrations.AddField(
model_name='order',
name='replaces',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='replaced_by', to='uncloud_pay.Order'),
),
]

View file

@ -1,24 +0,0 @@
# Generated by Django 3.0.6 on 2020-08-01 16:26
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0004_auto_20200801_1604'),
]
operations = [
migrations.AddField(
model_name='order',
name='one_time_price',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AddField(
model_name='order',
name='recurring_price',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
]

View file

@ -398,7 +398,7 @@ class Order(models.Model):
def records(self):
return OrderRecord.objects.filter(order=self)
# these are reald fields!!!
# these are real fields!!!
# @property
# def one_time_price(self):
# return reduce(lambda acc, record: acc + record.one_time_price, self.records, 0)