From 6995995155f67cf374d39ac195918e5772705499 Mon Sep 17 00:00:00 2001 From: Michael Volo Date: Tue, 2 Jan 2024 17:05:43 -0600 Subject: [PATCH] Remove book contact check (#1533) * Wagtail 4.1.9 upgrade (#1522) * Updated to wagtail 4.1.9 and replaced WagtailPageTests with WagtailPageTestCase * Fixes migration conflict * Added source field to Thank You Note (#1529) * Update admin.py (#1532) Co-authored-by: Staxly * updating book filter to exclude (#1530) Co-authored-by: Staxly Co-authored-by: Michael Volo * remove unused account id field from resource downloads * do not check for book and contact on resource creation * remove rejection test * Require UUID, do not require resource name * set default uuid to something random --------- Co-authored-by: Ed Woodward Co-authored-by: Colby Co-authored-by: Staxly --- ...salesforce__account_b11d37_idx_and_more.py | 20 ++++++++++++++ ..._resourcedownload_account_uuid_and_more.py | 23 ++++++++++++++++ salesforce/models.py | 8 +++--- salesforce/serializers.py | 21 +++++++-------- salesforce/tests.py | 26 +++++-------------- 5 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 salesforce/migrations/0106_remove_resourcedownload_salesforce__account_b11d37_idx_and_more.py create mode 100644 salesforce/migrations/0107_alter_resourcedownload_account_uuid_and_more.py diff --git a/salesforce/migrations/0106_remove_resourcedownload_salesforce__account_b11d37_idx_and_more.py b/salesforce/migrations/0106_remove_resourcedownload_salesforce__account_b11d37_idx_and_more.py new file mode 100644 index 000000000..410a219e9 --- /dev/null +++ b/salesforce/migrations/0106_remove_resourcedownload_salesforce__account_b11d37_idx_and_more.py @@ -0,0 +1,20 @@ +# Generated by Django 4.1.7 on 2023-12-13 19:28 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("salesforce", "0105_auto_20221212_1543"), + ] + + operations = [ + migrations.RemoveIndex( + model_name="resourcedownload", + name="salesforce__account_b11d37_idx", + ), + migrations.RemoveField( + model_name="resourcedownload", + name="account_id", + ), + ] diff --git a/salesforce/migrations/0107_alter_resourcedownload_account_uuid_and_more.py b/salesforce/migrations/0107_alter_resourcedownload_account_uuid_and_more.py new file mode 100644 index 000000000..f464ac21a --- /dev/null +++ b/salesforce/migrations/0107_alter_resourcedownload_account_uuid_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.7 on 2023-12-13 21:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("salesforce", "0106_remove_resourcedownload_salesforce__account_b11d37_idx_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="resourcedownload", + name="account_uuid", + field=models.UUIDField(default="19d58898-0b29-488f-a35a-6288e3c0b44c"), + preserve_default=False, + ), + migrations.AlterField( + model_name="resourcedownload", + name="resource_name", + field=models.CharField(blank=True, max_length=255, null=True), + ), + ] diff --git a/salesforce/models.py b/salesforce/models.py index 0d579e851..148941cf5 100644 --- a/salesforce/models.py +++ b/salesforce/models.py @@ -388,18 +388,16 @@ class ResourceDownload(models.Model): ('Chegg Reader', 'Chegg Reader'), ) book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True) - book_format = models.CharField(max_length=100, choices=BOOK_FORMATS, null=True , blank=True) - account_id = models.IntegerField(blank=True, null=True) # TODO: remove this field after migrating data to UUID (see management command) - account_uuid = models.UUIDField(null=True) + book_format = models.CharField(max_length=100, choices=BOOK_FORMATS, null=True, blank=True) + account_uuid = models.UUIDField(null=False) created = models.DateTimeField(auto_now_add=True) edited = models.DateTimeField(auto_now=True) last_access = models.DateTimeField() - resource_name = models.CharField(max_length=255, null=True, blank=False) + resource_name = models.CharField(max_length=255, null=True, blank=True) contact_id = models.CharField(max_length=100, null=True, blank=True) class Meta: indexes = [ - models.Index(fields=['account_id', ]), models.Index(fields=['account_uuid', ]), models.Index(fields=['book', ]), ] diff --git a/salesforce/serializers.py b/salesforce/serializers.py index a15d558d9..3a06751e7 100644 --- a/salesforce/serializers.py +++ b/salesforce/serializers.py @@ -120,17 +120,16 @@ def create(self, validated_data): resource_name = validated_data.get('resource_name', None) contact_id = validated_data.get('contact_id', None) rd = [] - if book and contact_id: - try: - rd = ResourceDownload.objects.filter(account_uuid=account_uuid, book=book) - if resource_name: - rd.filter(resource_name=resource_name) - - rd = rd[0] # we only need the first result - but there might already be duplicates so this should handle that - rd.contact_id = contact_id - rd.save() - except (ResourceDownload.DoesNotExist, IndexError): - rd = ResourceDownload.objects.create(**validated_data) + try: + rd = ResourceDownload.objects.filter(account_uuid=account_uuid, book=book) + if resource_name: + rd.filter(resource_name=resource_name) + + rd = rd[0] + rd.contact_id = contact_id + rd.save() + except (ResourceDownload.DoesNotExist, IndexError): + rd = ResourceDownload.objects.create(**validated_data) return rd diff --git a/salesforce/tests.py b/salesforce/tests.py index 8a1aa0df9..5972346f3 100644 --- a/salesforce/tests.py +++ b/salesforce/tests.py @@ -214,24 +214,12 @@ def test_resource_download_post(self): locale=root_page.locale ) book_index.add_child(instance=book) - data = {"book": book.pk,"book_format": "PDF","account_uuid": "310bb96b-0df8-4d10-a759-c7d366c1f524", "resource_name": "Book PDF", "contact_id": "0032f00003zYVdSAAZ"} + data = { + "book": book.pk, + "book_format": "PDF", + "account_uuid": "310bb96b-0df8-4d10-a759-c7d366c1f524", + "resource_name": "Book PDF", + "contact_id": "0032f00003zYVdSAAZ" + } response = self.client.post('/apps/cms/api/salesforce/download-tracking/', data, format='json') self.assertEqual("PDF", response.data['book_format']) - - def test_resource_download_post_rejection(self): - root_page = Page.objects.get(title="Root") - book_index = BookIndex.objects.all()[0] - book = Book(title="Biology 2e", - slug="biology-2e", - cnx_id='031da8d3-b525-429c-80cf-6c8ed997744b', - salesforce_book_id='', - description="Test Book", - cover=self.test_doc, - title_image=self.test_doc, - publish_date=datetime.date.today(), - locale=root_page.locale - ) - book_index.add_child(instance=book) - data = {"book": book.pk,"book_format": "PDF","account_uuid": "310bb96b-0df8-4d10-a759-c7d366c1f524", "resource_name": "Book PDF", "contact_id": ""} - response = self.client.post('/apps/cms/api/salesforce/download-tracking/', data, format='json') - self.assertEqual(None, response.data['book'])