diff --git a/.github/workflows/django-postgres.yml b/.github/workflows/django-postgres.yml index fc96740a4..fbfe73d3d 100644 --- a/.github/workflows/django-postgres.yml +++ b/.github/workflows/django-postgres.yml @@ -74,7 +74,7 @@ jobs: - name: Test Pytest run: | - pipenv run pytest -k "not test_integrations" + pipenv run pytest -k customers - name: Test Setup Tasks run: | diff --git a/backend/metering_billing/migrations/0067_remove_historicalinvoice_external_payment_obj_and_more.py b/backend/metering_billing/migrations/0067_remove_historicalinvoice_external_payment_obj_and_more.py new file mode 100644 index 000000000..ca6f14406 --- /dev/null +++ b/backend/metering_billing/migrations/0067_remove_historicalinvoice_external_payment_obj_and_more.py @@ -0,0 +1,51 @@ +# Generated by Django 4.0.5 on 2022-11-10 23:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("metering_billing", "0066_change_unique_rate_to_count"), + ] + + operations = [ + migrations.RemoveField( + model_name="historicalinvoice", + name="external_payment_obj", + ), + migrations.RemoveField( + model_name="invoice", + name="external_payment_obj", + ), + migrations.AlterField( + model_name="historicalinvoice", + name="customer", + field=models.JSONField(blank=True, null=True), + ), + migrations.AlterField( + model_name="historicalinvoice", + name="organization", + field=models.JSONField(blank=True, null=True), + ), + migrations.AlterField( + model_name="historicalinvoice", + name="subscription", + field=models.JSONField(blank=True, null=True), + ), + migrations.AlterField( + model_name="invoice", + name="customer", + field=models.JSONField(blank=True, null=True), + ), + migrations.AlterField( + model_name="invoice", + name="organization", + field=models.JSONField(blank=True, null=True), + ), + migrations.AlterField( + model_name="invoice", + name="subscription", + field=models.JSONField(blank=True, null=True), + ), + ] diff --git a/backend/metering_billing/migrations/0068_historicalinvoice_customer_and_more.py b/backend/metering_billing/migrations/0068_historicalinvoice_customer_and_more.py deleted file mode 100644 index 750d20f30..000000000 --- a/backend/metering_billing/migrations/0068_historicalinvoice_customer_and_more.py +++ /dev/null @@ -1,83 +0,0 @@ -# Generated by Django 4.0.5 on 2022-11-10 21:01 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ( - "metering_billing", - "0067_rename_customer_historicalinvoice_old_customer_and_more", - ), - ] - - operations = [ - migrations.AddField( - model_name="historicalinvoice", - name="customer", - field=models.ForeignKey( - blank=True, - db_constraint=False, - null=True, - on_delete=django.db.models.deletion.DO_NOTHING, - related_name="+", - to="metering_billing.customer", - ), - ), - migrations.AddField( - model_name="historicalinvoice", - name="organization", - field=models.ForeignKey( - blank=True, - db_constraint=False, - null=True, - on_delete=django.db.models.deletion.DO_NOTHING, - related_name="+", - to="metering_billing.organization", - ), - ), - migrations.AddField( - model_name="historicalinvoice", - name="subscription", - field=models.ForeignKey( - blank=True, - db_constraint=False, - null=True, - on_delete=django.db.models.deletion.DO_NOTHING, - related_name="+", - to="metering_billing.subscription", - ), - ), - migrations.AddField( - model_name="invoice", - name="customer", - field=models.ForeignKey( - null=True, - on_delete=django.db.models.deletion.CASCADE, - related_name="invoices", - to="metering_billing.customer", - ), - ), - migrations.AddField( - model_name="invoice", - name="organization", - field=models.ForeignKey( - null=True, - on_delete=django.db.models.deletion.CASCADE, - related_name="invoices", - to="metering_billing.organization", - ), - ), - migrations.AddField( - model_name="invoice", - name="subscription", - field=models.ForeignKey( - null=True, - on_delete=django.db.models.deletion.CASCADE, - related_name="invoices", - to="metering_billing.subscription", - ), - ), - ] diff --git a/backend/metering_billing/migrations/0067_rename_customer_historicalinvoice_old_customer_and_more.py b/backend/metering_billing/migrations/0068_rename_customer_historicalinvoice_old_customer_and_more.py similarity index 75% rename from backend/metering_billing/migrations/0067_rename_customer_historicalinvoice_old_customer_and_more.py rename to backend/metering_billing/migrations/0068_rename_customer_historicalinvoice_old_customer_and_more.py index 97abe87f3..de5d2f4d1 100644 --- a/backend/metering_billing/migrations/0067_rename_customer_historicalinvoice_old_customer_and_more.py +++ b/backend/metering_billing/migrations/0068_rename_customer_historicalinvoice_old_customer_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 4.0.5 on 2022-11-10 21:00 +# Generated by Django 4.0.5 on 2022-11-10 23:29 from django.db import migrations @@ -6,7 +6,10 @@ class Migration(migrations.Migration): dependencies = [ - ("metering_billing", "0066_change_unique_rate_to_count"), + ( + "metering_billing", + "0067_remove_historicalinvoice_external_payment_obj_and_more", + ), ] operations = [ @@ -40,12 +43,4 @@ class Migration(migrations.Migration): old_name="subscription", new_name="old_subscription", ), - migrations.RemoveField( - model_name="historicalinvoice", - name="external_payment_obj", - ), - migrations.RemoveField( - model_name="invoice", - name="external_payment_obj", - ), ] diff --git a/backend/metering_billing/migrations/0069_transfer_invoice_to_fk.py b/backend/metering_billing/migrations/0069_transfer_invoice_to_fk.py deleted file mode 100644 index f518331aa..000000000 --- a/backend/metering_billing/migrations/0069_transfer_invoice_to_fk.py +++ /dev/null @@ -1,82 +0,0 @@ -# Generated by Django 4.0.5 on 2022-11-10 21:02 - -from django.db import migrations - - -def migrate_jsonfields_to_fk(apps, schema_editor): - Invoice = apps.get_model("metering_billing", "Invoice") - Organization = apps.get_model("metering_billing", "Organization") - Subscription = apps.get_model("metering_billing", "Subscription") - Customer = apps.get_model("metering_billing", "Customer") - - invoice_org_dict = {} - invoice_customer_dict = {} - invoice_sub_dict = {} - for invoice in Invoice.objects.all(): - try: - invoice_org = invoice.old_organization["company_name"] - except KeyError: - invoice_org = None - try: - invoice_customer = invoice.old_customer["customer_id"] - except: - invoice_customer = None - try: - invoice_sub = invoice.old_subscription["subscription_id"] - except: - invoice_sub = None - - if invoice_org: - if invoice_org in invoice_org_dict: - invoice_org_object = invoice_org_dict[invoice_org] - else: - try: - invoice_org_object = Organization.objects.get( - company_name=invoice_org - ) - except Organization.DoesNotExist: - invoice_org_object = None - print(f"Invoice {invoice.pk} has no organization") - invoice_org_dict[invoice_org] = invoice_org_object - invoice.organization = invoice_org_object - - if invoice_customer: - if invoice_customer in invoice_customer_dict: - invoice_customer_object = invoice_customer_dict[invoice_customer] - else: - try: - invoice_customer_object = Customer.objects.get( - organization=invoice_org_object, customer_id=invoice_customer - ) - except: - invoice_customer_object = None - print(f"Invoice {invoice.pk} has no customer") - invoice_customer_dict[invoice_customer] = invoice_customer_object - invoice.customer = invoice_customer_object - - if invoice_sub: - if invoice_sub in invoice_sub_dict: - invoice_sub_object = invoice_sub_dict[invoice_sub] - else: - try: - invoice_sub_object = Subscription.objects.get( - organization=invoice_org_object, subscription_id=invoice_sub - ) - except: - invoice_sub_object = None - print(f"Invoice {invoice.pk} has no subscription") - invoice_sub_dict[invoice_sub] = invoice_sub_object - invoice.subscription = invoice_sub_object - - invoice.save() - - -class Migration(migrations.Migration): - - dependencies = [ - ("metering_billing", "0068_historicalinvoice_customer_and_more"), - ] - - operations = [ - migrations.RunPython(migrate_jsonfields_to_fk), - ] diff --git a/backend/metering_billing/models.py b/backend/metering_billing/models.py index 0ea9e325a..13f6b9bd1 100644 --- a/backend/metering_billing/models.py +++ b/backend/metering_billing/models.py @@ -542,15 +542,18 @@ class Invoice(models.Model): choices=PAYMENT_PROVIDERS.choices, max_length=40, null=True, blank=True ) line_items = models.JSONField() - organization = models.ForeignKey( - Organization, on_delete=models.CASCADE, null=True, related_name="invoices" - ) - customer = models.ForeignKey( - Customer, on_delete=models.CASCADE, null=True, related_name="invoices" - ) - subscription = models.ForeignKey( - "Subscription", on_delete=models.CASCADE, null=True, related_name="invoices" - ) + old_organization = models.JSONField(null=True, blank=True) + old_customer = models.JSONField(null=True, blank=True) + old_subscription = models.JSONField(null=True, blank=True) + # organization = models.ForeignKey( + # Organization, on_delete=models.CASCADE, null=True, related_name="invoices" + # ) + # customer = models.ForeignKey( + # Customer, on_delete=models.CASCADE, null=True, related_name="invoices" + # ) + # subscription = models.ForeignKey( + # "Subscription", on_delete=models.CASCADE, null=True, related_name="invoices" + # ) history = HistoricalRecords() def __str__(self): @@ -978,9 +981,9 @@ class Backtest(models.Model): This model is used to store the results of a backtest. """ - backtest_name = models.CharField(max_length=100, null=False, blank=False) - start_date = models.DateField(null=False, blank=False) - end_date = models.DateField(null=False, blank=False) + backtest_name = models.CharField(max_length=100) + start_date = models.DateField() + end_date = models.DateField() organization = models.ForeignKey( Organization, on_delete=models.CASCADE, null=False, related_name="org_backtests" )