Skip to content

Commit

Permalink
only fetch active book adoptions (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo authored Dec 2, 2024
1 parent f00fe60 commit 55a243c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
7 changes: 5 additions & 2 deletions books/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

@csrf_exempt
def book_index(request):
page = BookIndex.objects.all()[0]
return redirect('/apps/cms/api/v2/pages/{}/'.format(page.pk))
try:
page = BookIndex.objects.all()[0]
return redirect('/apps/cms/api/v2/pages/{}/'.format(page.pk))
except IndexError:
raise Http404("Subject page not found")


@csrf_exempt
Expand Down
1 change: 0 additions & 1 deletion news/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ def news_article_subject_search(subject):
return articles_to_return



class NewsArticle(Page):
date = models.DateField("Post date")
heading = models.CharField(max_length=250, help_text="Heading displayed on website")
Expand Down
44 changes: 27 additions & 17 deletions salesforce/management/commands/update_opportunities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def query_base_year(self, base_year, adoption_status):
"Savings__c, "
"Students__c, "
"Opportunity__r.Book__r.Name, "
"Opportunity__r.Book__r.Active__c, "
"Opportunity__r.StageName, "
"Opportunity__r.Contact__r.Accounts_UUID__c "
"FROM Adoption__c WHERE "
Expand All @@ -30,24 +31,33 @@ def query_base_year(self, base_year, adoption_status):

return query

def process_results(self, results):
def process_results(self, results, delete_stale=False):
for i, record in enumerate(results):
# they have newer records than last year, delete all previous records for this user
if delete_stale:
adoptions = AdoptionOpportunityRecord.objects.filter(account_uuid=record['Opportunity__r']['Contact__r']['Accounts_UUID__c']).last()
if adoptions:
if adoptions.created.date() < datetime.date.today():
AdoptionOpportunityRecord.objects.filter(account_uuid=record['Opportunity__r']['Contact__r']['Accounts_UUID__c']).delete()

try:
opportunity, created = AdoptionOpportunityRecord.objects.update_or_create(
account_uuid=uuid.UUID(record['Opportunity__r']['Contact__r']['Accounts_UUID__c']),
book_name=record['Opportunity__r']['Book__r']['Name'],
defaults={'opportunity_id': record['Id'],
'opportunity_stage': record['Opportunity__r']['StageName'],
'adoption_type': record['Adoption_Type__c'],
'base_year': record['Base_Year__c'],
'confirmation_date': record['Confirmation_Date__c'],
'confirmation_type': record['Confirmation_Type__c'],
'how_using': record['How_Using__c'],
'savings': record['Savings__c'],
'students': record['Students__c']
}
)
opportunity.save()
# don't build records for non-active books
if record['Opportunity__r']['Book__r']['Active__c']:
opportunity, created = AdoptionOpportunityRecord.objects.update_or_create(
account_uuid=uuid.UUID(record['Opportunity__r']['Contact__r']['Accounts_UUID__c']),
book_name=record['Opportunity__r']['Book__r']['Name'],
defaults={'opportunity_id': record['Id'],
'opportunity_stage': record['Opportunity__r']['StageName'],
'adoption_type': record['Adoption_Type__c'],
'base_year': record['Base_Year__c'],
'confirmation_date': record['Confirmation_Date__c'],
'confirmation_type': record['Confirmation_Type__c'],
'how_using': record['How_Using__c'],
'savings': record['Savings__c'],
'students': record['Students__c']
}
)
opportunity.save()
except ValueError:
sentry_sdk.capture_message("Adoption {} has a badly formatted Account UUID: {}".format(record['Id'], record['Opportunity__r']['Contact__r']['Accounts_UUID__c']))
except TypeError:
Expand Down Expand Up @@ -76,7 +86,7 @@ def handle(self, *args, **options):
updated_records = self.query_base_year(current_base_year, "Current Adopter")

results = sf.bulk.Adoption__c.query(updated_records)
self.process_results(results)
self.process_results(results, delete_stale=True)

invalidate_cloudfront_caches('salesforce/renewal')

0 comments on commit 55a243c

Please sign in to comment.