def delete_donor(name): """ Delete donor """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') logger.info('Working with Donor class') logger.info('Deleting Donor record...') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') with database.transaction(): donor = Donor.get(donor_name=name) delete_donor_donations = Donation.delete()\ .where(Donation.donation_donor == name) donor.delete_instance() delete_donor_donations.execute() logger.info('Database delete successful') except Exception as e: logger.info(f'Error deleting {name}') logger.info(e) finally: logger.info('database closes') database.close()
def delete_donation(donor, donation): """ Delete donation """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') logger.info('Working with Donation class') logger.info('Deleting Donation record...') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') with database.transaction(): donation = Donation.get(Donation.donation_donor == donor and Donation.donation_amount == donation) donation.delete_instance() logger.info('Database delete successful') except Exception as e: logger.info(f'Error updating {donation} for {donor}') logger.info(e) finally: logger.info('database closes') database.close()
def create_donor(name): """ Add new donor to database """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') logger.info('Working with Donor class') logger.info('Creating Donor record...') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') with database.transaction(): new_donor = Donor.create(donor_name=name) new_donor.save() logger.info('Database add successful') except Exception as e: logger.info(f'Error creating = {name}') logger.info(e) finally: logger.info('database closes') database.close()
def update_donation(donor, old_donation, new_donation): """ Update donation amount """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') logger.info('Working with Donation class') logger.info('Updating Donation record...') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') with database.transaction(): update_donation = Donation.update(donation_amount=new_donation)\ .where(Donation.donation_donor == donor and Donation.donation_amount == old_donation) update_donation.execute() logger.info('Database update successful') except Exception as e: logger.info(f'Error updating {donor} donation to {new_donation}') logger.info(e) finally: logger.info('database closes') database.close()
def get_donor_donations(): """ Get donor donations from database """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') query = (Donation.select(Donation, Donor).join(Donor, JOIN.INNER)) donor_donations = {} for donation in query: try: donor_donations\ .setdefault(donation.donation_donor.donor_name, []).append(float(donation.donation_amount)) except Exception as e: logger.info(e) except Exception as e: logger.info(e) finally: database.close() return donor_donations
def get_donor_names(): """ Get donor names from database """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') query = (Donor.select()) donor_names = [] for donor in query: try: donor_names.append(donor.donor_name) except Exception as e: logger.info(e) except Exception as e: logger.info(e) finally: database.close() return donor_names
def populate_donations(): """ Add donation data to database """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') logger.info('Working with Donation class') DONATION_AMOUNT = 0 DONATION_DONOR = 1 donations = [ (100, 'Stuart'), (250, 'Cayce') ] logger.info('Creating Donation records...') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') for donation in donations: with database.transaction(): new_donation = Donation.create( donation_amount=donation[DONATION_AMOUNT], donation_donor=donation[DONATION_DONOR]) new_donation.save() logger.info('Database add successful') logger.info('Print the Donation records we saved...') for donation in Donation: logger.info('Donation amount: {}, donor name: {}' .format(donation.donation_amount, donation.donation_donor)) except Exception as e: logger.info(f'Error creating = {donation[DONATION_AMOUNT]}') logger.info(e) finally: logger.info('database closes') database.close()
def populate_donors(): """ Add donor data to database """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') logger.info('Working with Donor class') donors = [ 'Stuart', 'Cayce' ] logger.info('Creating Donor records...') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') for donor in donors: with database.transaction(): new_donor = Donor.create( donor_name=donor) new_donor.save() logger.info('Database add successful') logger.info('Print the Donor records we saved...') for saved_donor in Donor: logger.info(f'{saved_donor.donor_name}') except Exception as e: logger.info(f'Error creating = {donor}') logger.info(e) finally: logger.info('database closes') database.close()
def update_donor(old_name, new_name): """ Update donor name """ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') logger.info('Working with Donor class') logger.info('Updating Donor record...') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') with database.transaction(): update_donor_name = Donor.update(donor_name=new_name)\ .where(Donor.donor_name == old_name) update_donor_donations = Donation.update(donation_donor=new_name)\ .where(Donation.donation_donor == old_name) update_donor_name.execute() update_donor_donations.execute() logger.info('Database update successful') except Exception as e: logger.info(f'Error updating {old_name} to {new_name}') logger.info(e) finally: logger.info('database closes') database.close()
def join_classes(): logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) database = SqliteDatabase('mailroom.db') try: database.connect() database.execute_sql('PRAGMA foreign_keys = ON;') query = (Donation .select(Donation, Donor) .join(Donor, JOIN.INNER)) donations = [] for donation in query: try: logger.info(f'{donation.donation_donor.donor_name} donated: ' f'{donation.donation_amount}') donation_info = { 'donor': donation.donation_donor.donor_name, 'amount': donation.donation_amount } donations.append(donation_info) except Exception as e: logger.info(e) except Exception as e: logger.info(e) finally: print_donations(donations) database.close()