1
+ import os
2
+
3
+ from sqlalchemy .orm import sessionmaker
4
+ from simple_salesforce import Salesforce
5
+ from config import engine
6
+ from models import SalesForceContacts , SalesforceDonations
7
+
8
+ def ingest_data ():
9
+
10
+ Session = sessionmaker (engine )
11
+
12
+ session = Session ()
13
+ session .execute ("TRUNCATE TABLE salesforcecontacts" )
14
+
15
+ sf = Salesforce (domain = os .getenv ('SALESFORCE_DOMAIN' ), password = os .getenv ('SALESFORCE_PW' ), username = os .getenv ('SALESFORCE_USERNAME' ), organizationId = os .getenv ('SALESFORCE_ORGANIZATION_ID' ), security_token = os .getenv ('SALESFORCE_SECURITY_TOKEN' ))
16
+ results = sf .query ("SELECT Contact_ID_18__c, FirstName, LastName, Contact.Account.Name, MailingCountry, MailingStreet, MailingCity, MailingState, MailingPostalCode, Phone, MobilePhone, Email FROM Contact" )
17
+ done = False
18
+ while not done :
19
+ for row in results ['records' ]:
20
+ account_name = row ['Account' ]['Name' ] if row ['Account' ] is not None else None
21
+ contact = SalesForceContacts (contact_id = row ['Contact_ID_18__c' ],
22
+ first_name = row ['FirstName' ],
23
+ last_name = row ['LastName' ],
24
+ account_name = account_name ,
25
+ mailing_country = row ['MailingCountry' ],
26
+ mailing_street = row ['MailingStreet' ],
27
+ mailing_city = row ['MailingCity' ],
28
+ mailing_state_province = row ['MailingState' ],
29
+ mailing_zip_postal_code = row ['MailingPostalCode' ],
30
+ phone = row ['Phone' ],
31
+ mobile = row ['MobilePhone' ],
32
+ email = ['Email' ])
33
+ session .add (contact )
34
+ done = results ['done' ]
35
+ if not done :
36
+ results = sf .query_more (results ['nextRecordsUrl' ])
37
+
38
+
39
+
40
+ session .execute ("TRUNCATE TABLE salesforcedonations" )
41
+ results = sf .query ("SELECT Opportunity_ID_18__c, npe03__Recurring_Donation__c, Opportunity.Account.Name, Contact_ID_18__c, Amount, CloseDate, Type, Campaign.Name FROM Opportunity" )
42
+ done = False
43
+ while not done :
44
+ for row in results ['records' ]:
45
+ account_name = row ['Account' ]['Name' ] if row ['Account' ] is not None else None
46
+ donation = SalesforceDonations (opp_id = row ['Opportunity_ID_18__c' ],
47
+ recurring_donor = False if row ['npe03__Recurring_Donation__c' ] is None else True ,
48
+ primary_contact = account_name ,
49
+ contact_id = row ['Contact_ID_18__c' ],
50
+ amount = row ['Amount' ],
51
+ close_date = row ['CloseDate' ],
52
+ donation_type = row ['Type' ])
53
+ session .add (donation )
54
+ done = results ['done' ]
55
+ if not done :
56
+ results = sf .query_more (results ['nextRecordsUrl' ])
57
+
58
+ session .commit ()
0 commit comments