From 29cd7c84191aee76b5a104ce3d98124c1899691d Mon Sep 17 00:00:00 2001 From: muhammad-ammar Date: Fri, 15 Sep 2023 12:33:21 +0500 Subject: [PATCH] feat: associate an opportunity id with a subscription license --- .../migrations/0057_auto_20230915_0722.py | 44 ++++++++++++++++++ ...nsesource_subscriptionlicensesourcetype.py | 44 ++++++++++++++++++ license_manager/apps/subscriptions/models.py | 46 +++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 license_manager/apps/subscriptions/migrations/0057_auto_20230915_0722.py create mode 100644 license_manager/apps/subscriptions/migrations/0058_subscriptionlicensesource_subscriptionlicensesourcetype.py diff --git a/license_manager/apps/subscriptions/migrations/0057_auto_20230915_0722.py b/license_manager/apps/subscriptions/migrations/0057_auto_20230915_0722.py new file mode 100644 index 00000000..b938f09e --- /dev/null +++ b/license_manager/apps/subscriptions/migrations/0057_auto_20230915_0722.py @@ -0,0 +1,44 @@ +# Generated by Django 3.2.21 on 2023-09-15 07:22 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('subscriptions', '0056_auto_20230530_1901'), + ] + + operations = [ + migrations.AlterField( + model_name='historicalsubscriptionplan', + name='salesforce_opportunity_id', + field=models.CharField(blank=True, help_text='Deprecated -- 18 character value, derived from Salesforce Opportunity record.', max_length=18, null=True, validators=[django.core.validators.MinLengthValidator(18)]), + ), + migrations.AlterField( + model_name='historicalsubscriptionplan', + name='salesforce_opportunity_line_item', + field=models.CharField(blank=True, help_text='18 character value -- Locate the appropriate Salesforce Opportunity Line Item record and copy it here.', max_length=18, null=True, validators=[django.core.validators.MinLengthValidator(18)]), + ), + migrations.AlterField( + model_name='historicalsubscriptionplanrenewal', + name='salesforce_opportunity_id', + field=models.CharField(help_text='Locate the appropriate Salesforce Opportunity record and copy the Opportunity ID field (18 characters). Note that this is not the same Salesforce Opportunity ID associated with the linked subscription.', max_length=18, validators=[django.core.validators.MinLengthValidator(18)], verbose_name='Salesforce Opportunity Line Item'), + ), + migrations.AlterField( + model_name='subscriptionplan', + name='salesforce_opportunity_id', + field=models.CharField(blank=True, help_text='Deprecated -- 18 character value, derived from Salesforce Opportunity record.', max_length=18, null=True, validators=[django.core.validators.MinLengthValidator(18)]), + ), + migrations.AlterField( + model_name='subscriptionplan', + name='salesforce_opportunity_line_item', + field=models.CharField(blank=True, help_text='18 character value -- Locate the appropriate Salesforce Opportunity Line Item record and copy it here.', max_length=18, null=True, validators=[django.core.validators.MinLengthValidator(18)]), + ), + migrations.AlterField( + model_name='subscriptionplanrenewal', + name='salesforce_opportunity_id', + field=models.CharField(help_text='Locate the appropriate Salesforce Opportunity record and copy the Opportunity ID field (18 characters). Note that this is not the same Salesforce Opportunity ID associated with the linked subscription.', max_length=18, validators=[django.core.validators.MinLengthValidator(18)], verbose_name='Salesforce Opportunity Line Item'), + ), + ] diff --git a/license_manager/apps/subscriptions/migrations/0058_subscriptionlicensesource_subscriptionlicensesourcetype.py b/license_manager/apps/subscriptions/migrations/0058_subscriptionlicensesource_subscriptionlicensesourcetype.py new file mode 100644 index 00000000..14fa5127 --- /dev/null +++ b/license_manager/apps/subscriptions/migrations/0058_subscriptionlicensesource_subscriptionlicensesourcetype.py @@ -0,0 +1,44 @@ +# Generated by Django 3.2.21 on 2023-09-18 04:32 + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone +import model_utils.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('subscriptions', '0057_auto_20230915_0722'), + ] + + operations = [ + migrations.CreateModel( + name='SubscriptionLicenseSourceType', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), + ('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')), + ('name', models.CharField(max_length=64)), + ('slug', models.SlugField(max_length=30, unique=True)), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='SubscriptionLicenseSource', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), + ('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')), + ('source_id', models.CharField(help_text='18 character value -- Salesforce Opportunity ID', max_length=18, validators=[django.core.validators.MinLengthValidator(18)])), + ('license', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='source', to='subscriptions.license')), + ('source_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subscriptions.subscriptionlicensesourcetype')), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/license_manager/apps/subscriptions/models.py b/license_manager/apps/subscriptions/models.py index 8b4302f6..33165c06 100644 --- a/license_manager/apps/subscriptions/models.py +++ b/license_manager/apps/subscriptions/models.py @@ -1291,6 +1291,52 @@ def _clean_up_duplicate_licenses(cls, duplicate_licenses): return sorted_licenses[0] +class SubscriptionLicenseSourceType(TimeStampedModel): + """ + Subscription License Source Type + + .. no_pii: + """ + + AMT = 'AMT' + + name = models.CharField(max_length=64) + slug = models.SlugField(max_length=30, unique=True) + + @classmethod + def get_source_type(cls, source_slug): + """ + Retrieve the source type based on the slug. + """ + try: + return cls.objects.get(slug=source_slug) + except SubscriptionLicenseSourceType.DoesNotExist: + return None + + def __str__(self): + """ + String representation of the source. + """ + return "SubscriptionLicenseSourceType: {name}, Slug: {slug}".format(name=self.name, slug=self.slug) + + +class SubscriptionLicenseSource(TimeStampedModel): + + license = models.OneToOneField( + License, + related_name='source', + on_delete=models.CASCADE, + ) + source_id = models.CharField( + max_length=SALESFORCE_ID_LENGTH, + validators=[MinLengthValidator(SALESFORCE_ID_LENGTH)], + help_text=_( + "18 character value -- Salesforce Opportunity ID" + ) + ) + source_type = models.ForeignKey(SubscriptionLicenseSourceType, on_delete=models.CASCADE) + + class SubscriptionsFeatureRole(UserRole): """ User role definitions specific to subscriptions.