def print_donors(self): """ Prints and returns a list of all donor names. """ try: self.database.connect() self.database.execute_sql(self.fk) for donor in Donor.select(): print(donor.name) return [donor.name for donor in Donor.select()] except Exception as e: logger.error(e) finally: self.database.close()
def send_letters(self): """ Writes a letter to every donor thanking them for their total donations.. """ # Option flag to prevent cluttering the mailbox every time option_flag = False option = input("Do you want to actually create the letters? Y/N: ") if option.lower() == 'y' or 'yes': option_flag = True try: self.database.connect() self.database.execute_sql(self.fk) for donor in Donor.select(): donor_name = donor.name total_donations = donor.total_donation_amt # Send letter. Leaving this as print for now but built in an actual file writer letter = f'Dear {donor_name},\n' letter += f'Thank you for your generous donations of ${total_donations:.2f}.\n' letter += f' Sincerely, \n' letter += f' - Team' print(letter) if option_flag: with open(f"{donor_name}_thanks.txt", 'w+') as outfile: outfile.write(letter) except Exception as e: logger.error(e) finally: self.database.close()
def print_all_donor_info(self): """ Save as print donors but includes donation statistics. """ try: self.database.connect() self.database.execute_sql(self.fk) for donor in Donor.select(): print(f"{donor.name} has donated ${donor.total_donation_amt:.02f} over {donor.num_donations} donations, averaging ${donor.avg_donation:.02f}") except Exception as e: logger.error(e) finally: self.database.close()
def donor_report(self): """ Creates a formatted report of donors and their donations. """ # Table header formatting line_one = '{:20} | {:>15} | {:>5} | {:>15}'.format( 'Donor', 'Total', '#', 'Avg' ) print(line_one) print('-' * (len(line_one))) # Get actual data try: self.database.connect() self.database.execute_sql(self.fk) for donor in Donor.select(): print(f'{donor.name:<20} | {donor.total_donation_amt:>15,.2f} | {donor.num_donations:>5} | {donor.avg_donation:>15,.2f}') except Exception as e: logger.error(e) finally: self.database.close()