def categorize_data(self, csv_data): return_objs = [] Contribution = namedtuple('Contribution', self.csv_header_row.replace(' ', '_')) for line in csv_data.split('\n'): # explicity defining delimiter because otherwise fails in case of single line if not line: continue # cur_obj will be the person or organization that made the contribution cur_obj = None contribution = Contribution(*line.split(',')) if contribution.Contributor_Type in self.business_contribution_types: cur_obj = Organization(contribution.Contributor_Name) elif contribution.Contributor_Type in self.individual_contribution_types: cur_obj = Person(contribution.Contributor_Name) elif contribution.Contributor_Type == 'Unknown/Anonymous': if contribution.Contributor_Name: #ignoring un-named contributors #these look like catch-all business contributions cur_obj = Organization(contribution.Contributor_Name) if cur_obj: #we don't set cur_obj in the event that there was an #anonymous/unknown contribution without a Contribution_Name #so we need to check that it exists before adding to it cur_obj.add_source(url=self.search_url) cur_obj.source_identified = True if contribution.Contributor_Address: cur_obj.add_contact_detail(type='address', value=contribution.Contributor_Address) if contribution.Employer_Name: cur_obj.extras['Employer'] = contribution.Employer_Name if contribution.Employer_Occupation: cur_obj.extras['Occupation'] = contribution.Employer_Occupation #recipiant_obj is the organization that received the contribution recipiant_obj = Organization(contribution.Receiving_Committee) recipiant_obj.extras['Office'] = contribution.Office recipiant_obj.extras['Filing Period'] = contribution.Filing_Period recipiant_obj.extras['Fundtype'] = contribution.Fundtype #transaction is the event linking the donor and recipiant transaction = Event('Contribution', contribution.Contribution_Date, 'EST', 'Maryland') #EST and Maryland b/c MD transaction.extras['Contribution Amount'] = contribution.Contribution_Amount transaction.extras['Contribution Type'] = contribution.Contribution_Type transaction.add_source(url=self.search_url) #transaction.source_identified = True transaction.participants.append(cur_obj.as_dict()) transaction.participants.append(recipiant_obj.as_dict()) yield (cur_obj, recipiant_obj, transaction) else: yield []
def categorize_data(self, csv_data): #Is there a better place to define this? return_objs = [] Contribution = namedtuple('Contribution', self.csv_header_row.replace(' ', '_')) for line in csv_data.split('\n'): # explicity defining delimiter because otherwise fails in case of single line if not line: continue cur_obj = None try: contribution = Contribution(*line.split(',')) except Exception as e: import pdb; pdb.set_trace() if contribution.Contributor_Type in self.business_contribution_types: cur_obj = Organization(contribution.Contributor_Name) elif contribution.Contributor_Type in self.individual_contribution_types: cur_obj = Person(contribution.Contributor_Name) elif contribution.Contributor_Type == 'Unknown/Anonymous': if contribution.Contributor_Name: #ignoring un-named contributors #these look like catch-all business contributions cur_obj = Organization(contribution.Contributor_Name) if cur_obj: cur_obj.add_source(url=self.search_url) cur_obj.source_identified = True if contribution.Contributor_Address: cur_obj.add_contact_detail(type='address', value=contribution.Contributor_Address) if contribution.Employer_Name: cur_obj.extras['Employer'] = contribution.Employer_Name if contribution.Employer_Occupation: cur_obj.extras['Occupation'] = contribution.Employer_Occupation recipiant_obj = Organization(contribution.Receiving_Committee) recipiant_obj.extras['Office'] = contribution.Office recipiant_obj.extras['Filing Period'] = contribution.Filing_Period recipiant_obj.extras['Fundtype'] = contribution.Fundtype transaction = Event('Contribution', contribution.Contribution_Date, 'EST', 'Maryland') #EST and Maryland b/c MD transaction.extras['Contribution Amount'] = contribution.Contribution_Amount transaction.extras['Contribution Type'] = contribution.Contribution_Type transaction.add_source(url=self.search_url) #transaction.source_identified = True transaction.participants.append(cur_obj.as_dict()) transaction.participants.append(recipiant_obj.as_dict()) yield (cur_obj, recipiant_obj, transaction) else: yield []