-
Notifications
You must be signed in to change notification settings - Fork 45
Refactors letter examples to be just a create letters from csv example #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,7 @@ nosetests.xml | |
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
#Generated Files | ||
examples/*/success.csv | ||
examples/*/errors.csv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Usage | ||
# python letter.py input.csv | ||
import sys,os | ||
sys.path.insert(0, os.path.abspath(__file__+'../../../..')) | ||
|
||
import lob | ||
import csv | ||
import datetime | ||
|
||
lob.api_key = 'test_fc26575412e92e22a926bc96c857f375f8b' | ||
|
||
skipFirstLine = True | ||
|
||
# Column indices in CSV | ||
name = 0 | ||
amount = 1 | ||
address_line1 = 2 | ||
address_line2 = 3 | ||
address_city = 4 | ||
address_state = 5 | ||
address_zip = 6 | ||
country = 'US' | ||
|
||
try: | ||
sys.argv[1] | ||
except IndexError: | ||
print "Please provide an input CSV file as an argument." | ||
sys.exit() | ||
|
||
# Open input files | ||
inputFile = open(sys.argv[1], 'rU') | ||
csvInput = csv.reader(inputFile) | ||
|
||
# Create output files | ||
errors = open(os.path.dirname(os.path.abspath(__file__)) + '/errors.csv', 'w') | ||
success = open(os.path.dirname(os.path.abspath(__file__)) + '/success.csv', 'w') | ||
|
||
# Loop through input CSV rows | ||
for idx, row in enumerate(csvInput): | ||
if skipFirstLine and idx == 0: | ||
continue | ||
|
||
# Create letter for verified addresses | ||
try: | ||
createdLetter = lob.Letter.create( | ||
description = 'Bill for ' + row[name], | ||
metadata = { | ||
'campaign': 'billing_statements', | ||
'csv': inputFile.name | ||
}, | ||
to_address = { | ||
'name': row[name], | ||
'address_line1': row[address_line1], | ||
'address_line2': row[address_line2], | ||
'address_city': row[address_city], | ||
'address_zip': row[address_zip], | ||
'address_state': row[address_state], | ||
}, | ||
from_address = { | ||
'name': 'Your Name/Company', | ||
'address_line1': '123 Test Avenue', | ||
'address_city': 'San Francisco', | ||
'address_state': 'CA', | ||
'address_zip': '94107', | ||
}, | ||
file = open(os.path.dirname(os.path.abspath(__file__)) + '/letter.html', 'r').read(), | ||
data = { | ||
'date': datetime.datetime.now().strftime("%m/%d/%Y"), | ||
'name': row[name], | ||
'amountDue': row[amount] | ||
}, | ||
color = True | ||
) | ||
except Exception, e: | ||
print "Error: " + str(e) + " in " + str(row) | ||
else: | ||
outputRow = createdLetter.id + "," | ||
outputRow += createdLetter.url + "," | ||
outputRow += row[name] + "," | ||
outputRow += row[amount] + "," | ||
outputRow += (createdLetter.to_address.address_line1 if createdLetter.to_address.address_line1 != None else " ") + "," | ||
outputRow += (createdLetter.to_address.address_line2 if createdLetter.to_address.address_line2 != None else " ") + "," | ||
outputRow += (createdLetter.to_address.address_city if createdLetter.to_address.address_city != None else " ") + "," | ||
outputRow += (createdLetter.to_address.address_state if createdLetter.to_address.address_state != None else " ") + "," | ||
outputRow += (createdLetter.to_address.address_zip if createdLetter.to_address.address_zip != None else " ") + "\n" | ||
success.write(outputRow) | ||
|
||
|
||
errors.close() | ||
success.close() | ||
print "\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The errors should also be being logged to
errors.csv
. Right now we just open the file descriptor and close it. We used to be writing to it when the address verification failed. We want to make sure we log them since they could fail due to rate limits, file dimensions, etc.