Skip to content

Commit 6516d9b

Browse files
committed
code for downloading salesforce data with test endpoint in the admin api
1 parent c745a9b commit 6516d9b

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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()

src/server/api/admin_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from api import jwt_ops
1717
from config import RAW_DATA_PATH
18+
from api.API_ingest.salesforce_api_handler import ingest_data
1819

1920
ALLOWED_EXTENSIONS = {"csv", "xlsx"}
2021

@@ -397,6 +398,12 @@ def hit_gdrs():
397398
num_scores = generate_dummy_rfm_scores()
398399
return jsonify({"scores added" : num_scores})
399400

401+
@admin_api.route("/api/admin/test-salesforce-ingest")
402+
def ingest_salesforce():
403+
current_app.logger.info("Getting latest Salesforce data")
404+
ingest_data()
405+
return jsonify(200)
406+
400407

401408
# def pdfr():
402409
# dlist = pull_donations_for_rfm()

src/server/bin/export_secrets.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
set -o allexport
2-
source secrets_dict.py
2+
source bin/secrets_dict.py
33
set +o allexport

src/server/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,17 @@ def insert_from_df(cls, df, conn):
379379
)
380380

381381
conn.execute(insert(cls).values(matched_pairs).on_conflict_do_nothing())
382+
383+
class SalesforceDonations(Base):
384+
__tablename__ = "salesforcedonations"
385+
386+
_id = sa.Column(sa.Integer, primary_key=True)
387+
opp_id = sa.Column(sa.String)
388+
recurring_donor = sa.Column(sa.Boolean)
389+
primary_contact = sa.Column(sa.String)
390+
contact_id = sa.Column(sa.String)
391+
amount = sa.Column(sa.Numeric)
392+
close_date = sa.Column(sa.Date)
393+
donation_type = sa.Column(sa.String)
394+
primary_campaign_source = sa.Column(sa.String)
395+

src/server/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ jellyfish
1515
networkx
1616
jinja2<3.1.0
1717
itsdangerous==2.0.1 # TODO: upgrade flask so we don't have to do this
18+
simple-salesforce
1819
werkzeug==2.0.3

0 commit comments

Comments
 (0)