Last active
August 29, 2015 14:00
-
-
Save rickcnagy/11193940 to your computer and use it in GitHub Desktop.
Format and update PLD companies from external dictionary. Can be CSV data or PLD API data. Formats fields like phone, address, company name, etc.
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
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python | |
import us | |
import pld | |
import titlecase | |
from pld import custom_field_key as custom_field | |
import api_logging | |
nces_serial_label = '945949' | |
randomize_label = '948322' | |
source_label = '946242' | |
type_label = '945947' | |
levels_label = '950809' | |
associations_label = '612564' | |
student_number_label = '611895' | |
teacher_number_label = '612563' | |
nces_candidate_label = '949279' | |
old_name_label = '949479' | |
k12_levels = {'PreK': '563829', 'K': '563830', | |
'Elem': '563831', 'MS': '563832', 'HS': '563833', | |
} | |
def update_company(company, csv_data, overwrite=False): | |
old_name = company['name'] | |
if overwrite: | |
company['city'] = csv_data['City'] | |
company['state'] = csv_data['State'] | |
company['name'] = csv_data['Name'] | |
company['address_1'] = csv_data['Address'] | |
company['phone1'] = csv_data['Phone'] | |
if not company['phone1']: | |
company['phone1'] == csv_data['Phone'] | |
if not company['city']: | |
company['city'] == csv_data['City'] | |
company['city'] = titlecase.tc(company['city']) | |
if (' - ' not in company['name']) and (', ' not in company['name']): | |
company['name'] = '{} - {}, {}'.format( | |
titlecase.tc(company['name']), | |
company['city'], | |
csv_data['State'], | |
) | |
try: | |
company['state'] = us.states.lookup(csv_data['State']).name | |
except AttributeError: | |
api_logging.error("Couldn't match state", company['state']) | |
company['phone1'] = titlecase.phone(company['phone1']) | |
if not company['phone1']: | |
api_logging.warning("Uploading empty phone", company) | |
data = { | |
custom_field(nces_serial_label): csv_data['NCES Serial #'], | |
custom_field(randomize_label): csv_data['Randomize #'], | |
custom_field(source_label): csv_data['Source'], | |
custom_field(type_label): csv_data['Type - for API'], | |
custom_field(associations_label): csv_data['Association(s)'], | |
custom_field(student_number_label): csv_data['# of students'], | |
custom_field(teacher_number_label): csv_data['# of teachers'], | |
custom_field(levels_label) + '[]': k12_level_picklist(csv_data['K12 Levels']), | |
custom_field(nces_candidate_label): '', | |
'company[city]': company['city'], | |
'company[state]': company['state'], | |
'company[name]': company['name'], | |
'company[address_1]': titlecase.tc(company['address_1']), | |
'company[phone1]': company['phone1'], | |
'company[phone4_desc]': '', | |
} | |
if overwrite: | |
data[custom_field(old_name_label)] = old_name | |
# api_logging.info("Would PUT", data) | |
pld.update_company(company, data) | |
def k12_level_picklist(k12_string): | |
levels = k12_string.split(',') | |
if len(levels) == 1 and levels[0] == '': | |
return | |
else: | |
return map(lambda x: k12_levels[x.strip()], levels) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment