Skip to content

Commit

Permalink
fix adoption query for renewal form
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo committed Oct 18, 2024
1 parent 9809e9a commit 2ef4ddc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
21 changes: 18 additions & 3 deletions salesforce/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@ class SchoolAdmin(admin.ModelAdmin):
def has_add_permission(self, request):
return False


class AdoptionOpportunityRecordAdmin(admin.ModelAdmin):
list_display = ['account_uuid', 'book_name', 'fall_student_number', 'spring_student_number', 'summer_student_number']
list_filter = ('book_name', 'created')
search_fields = ['account_uuid', ]
list_display = ['account_uuid', 'book_name', 'students', 'savings']
list_filter = ('book_name', 'created', 'opportunity_stage')
search_fields = ['account_uuid', 'opportunity_id']
readonly_fields = [
'opportunity_id',
'opportunity_stage',
'account_uuid',
'adoption_type',
'base_year',
'confirmation_date',
'confirmation_type',
'how_using',
'savings',
'students',
'book_name',
'created'
]

def has_add_permission(self, request):
return False
Expand Down
43 changes: 30 additions & 13 deletions salesforce/management/commands/update_opportunities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from salesforce.models import AdoptionOpportunityRecord
from salesforce.salesforce import Salesforce


class Command(BaseCommand):
help = "update book adoptions from salesforce.com for getting adoptions by account uuid"

Expand All @@ -11,30 +12,46 @@ def handle(self, *args, **options):
now = datetime.datetime.now()

year = now.year
if now.month < 7: # Salesforce needs the school base year, this is how they calculate it
if now.month > 7: # Salesforce needs the school base year, this is how they calculate it
year = year - 1


# truncate the table
AdoptionOpportunityRecord.objects.all().delete()

# then we will get any new records
command = "SELECT Id, Contact__r.Accounts_UUID__c, Book__r.Name, Base_Year__c, IsWon from Opportunity WHERE Contact__r.Accounts_UUID__c != null AND Base_Year__c = {} AND IsWon = True".format(year)

response = sf.query_all(command)
query = ("SELECT Id, "
"Adoption_Type__c, "
"Base_Year__c, "
"Confirmation_Date__c, "
"Confirmation_Type__c, "
"How_Using__c, "
"Savings__c, "
"Students__c, "
"Opportunity__r.Book__r.Name, "
"Opportunity__r.StageName, "
"Opportunity__r.Contact__r.Accounts_UUID__c "
"FROM Adoption__c WHERE "
"Base_Year__c = {} AND Opportunity__r.Contact__r.Accounts_UUID__c != null "
"AND Confirmation_Type__c = 'OpenStax Confirmed Adoption' LIMIT 100").format(year)

response = sf.query(query)
records = response['records']

num_created = 0
for record in records:
opportunity, created = AdoptionOpportunityRecord.objects.update_or_create(
opportunity_id=record['Id'],
defaults = {'account_uuid': record['Contact__r']['Accounts_UUID__c'],
'book_name': record['Book__r']['Name'],
}
)
defaults={'account_uuid': record['Opportunity__r']['Contact__r']['Accounts_UUID__c'],
'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'],
'book_name': record['Opportunity__r']['Book__r']['Name'],
}
)
opportunity.save()
if created:
num_created = num_created + 1

response = self.style.SUCCESS("Successfully updated opportunity records. {} were newly created.".format(num_created))
self.stdout.write(response)
3 changes: 1 addition & 2 deletions salesforce/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def list(self, request):
queryset = AdoptionOpportunityRecord.objects.filter(account_uuid=account_uuid)
book_list = []
for record in queryset:
student_nums = [record.fall_student_number or 0, record.spring_student_number or 0, record.summer_student_number or 0]
book_list.append({"name": record.book_name , "students": str(max(student_nums))})
book_list.append({"name": record.book_name , "students": str(max(record.students))})
data = {"Books": book_list}

return JsonResponse(data)
Expand Down

0 comments on commit 2ef4ddc

Please sign in to comment.