<commit_before>"""cdbifunc.py

Developer: <NAME>
Last Updated: June 18, 2014

This module holds all functions that will be called directly by the user
interface. This module uses several functions in cdbfunctions.py; the two
modules have been split to make designing the user interface as simple as
simple as possible.

"""

from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from datetime import datetime
from cdbtabledef import Household, Person, Volunteer, Visit
from cdbfunctions import *
from sqlafuntest import *

engine = create_engine('sqlite:///test_db.sqlite')
session = sessionmaker()
session.configure(bind=engine)

base = declarative_base()

s = session()

#Functions to connect to buttons
def quit_session():
	"""This function will close the session.
	"""
	s.close()
	
	
def cancel_changes():
	"""This function will rollback transactions & close session.
	"""
	s.rollback()
	s.close()
	
	
def reset(I_ID):
	"""	This function sends the original data back.
	"""
	info = select_client(I_ID)
	return info


def new_volunteer(s, firstname, lastname, phonenum=None):
	"""This function creates a new record for a volunteer.
	"""
	insert_volunteer(s, firstname, lastname, phonenum)
	
	
def new_client(Vol_ID, street, client_data_tuple_list, 
		dateJoined=datetime.now(), dateVerified=None,
		visitDate=datetime.now(), Apt=None,
		City='Troy', State='NY', Zip='12180', notes=None
		):
	"""Input:
	new_client(int Vol_ID, string street, 
			list[(string fname, string lname, date dob, int phone)],
			date dateJoined, date dateVerified, date visitDate, string Apt,
			string City, string State, int Zip, string Notes)
				
	This function creates a new person, household, and first visit.
	If the page viewed is for a new_client, then this connects to the SAVE
	button in the interface.
	
	For input, the function takes lists of strings for firstnames and 
	lastnames, a list of date objects for dobs, a list of integers for
	phonenums, strings for Apt, City, and State, and an integer for Zip.
	There is no return.
	
	"""
	
	#create new household
	newhouse = insert_household(s, street, dateVerified,
								Apt, City, State, Zip)
	
	#create new person for every household member
	data = client_data_tuple_list #variable renamed for simplicity	
	
	for i in range(0, len(data)):			
		fname = data[i][0]
		lname = data[i][1]
		dob = data[i][2]
		
		#check for phone number
		if len(data[i]) > 3: phone = data[i][3]
		else: phone = None
		pers = insert_person(s, fname, lname, dob, dateJoined, newhouse, phone)
		
		#the first person is the actual visitor; save for insert_visit
		if i == 0:
			s.commit()
			newpers = pers
	s.commit()
	
	#create new visit for household
	insert_visit(s, Vol_ID, newpers, newhouse, visitDate, notes)	
	s.commit()
	
	
#Function to retrieve all data for a client
def select_client(I_ID):
	"""This function returns all client data for a selected client.
	"""
	
	#find person and associated household
	pers = s.query(Person).filter(Person.id == I_ID).one()	
	house = s.query(Household).filter(Household.id == pers.HH_ID).one()
	
	info = [pers.id, pers.first_name, pers.last_name, pers.DOB, pers.age,
			pers.phone, pers.date_joined, house.street_address, house.apt,
			house.city, house.state, house.zip, house.date_verified]
			
	for member in house.members:
		info.append( (member.first_name, member.last_name, member.DOB, member.age) )

	agegroups = get_age_breakdown(house.members)
	info.append(agegroups)
	return info
	<commit_msg>Update cdbifunc.py<commit_after>"""cdbifunc.py

Developer: <NAME>
Last Updated: June 18, 2014

This module holds all functions that will be called directly by the user
interface. This module uses several functions in cdbfunctions.py; the two
modules have been split to make designing the user interface as simple as
simple as possible.

"""

from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from datetime import datetime
from cdbtabledef import Household, Person, Volunteer, Visit
from cdbfunctions import *
from sqlafuntest import *

engine = create_engine('sqlite:///test_db.sqlite')
session = sessionmaker()
session.configure(bind=engine)

base = declarative_base()

s = session()

#Functions to connect to buttons
def quit_session():
	"""This function will close the session.
	"""
	s.close()
	
	
def cancel_changes():
	"""This function will rollback transactions & close session.
	"""
	s.rollback()
	s.close()
	
	
def reset(I_ID):
	"""	This function sends the original data back.
	"""
	info = select_client(I_ID)
	return info


def new_volunteer(s, firstname, lastname, phonenum=None):
	"""This function creates a new record for a volunteer.
	"""
	insert_volunteer(s, firstname, lastname, phonenum)
	
	
def new_client(Vol_ID, street, client_data_tuple_list, 
		dateJoined=datetime.now(), dateVerified=None,
		visitDate=datetime.now(), Apt=None,
		City='Troy', State='NY', Zip='12180', notes=None
		):
	"""Input:
	new_client(int Vol_ID, string street, 
		list[(string fname, string lname, date dob, int phone)],
		date dateJoined, date dateVerified, date visitDate, string Apt,
		string City, string State, int Zip, string Notes)
				
	This function creates a new person, household, and first visit.
	If the page viewed is for a new_client, then this connects to the SAVE
	button in the interface.
	
	For input, the function takes lists of strings for firstnames and 
	lastnames, a list of date objects for dobs, a list of integers for
	phonenums, strings for Apt, City, and State, and an integer for Zip.
	There is no return.
	
	"""
	
	#create new household
	newhouse = insert_household(s, street, dateVerified,
				Apt, City, State, Zip)
	
	#create new person for every household member
	data = client_data_tuple_list #variable renamed for simplicity	
	
	for i in range(0, len(data)):			
		fname = data[i][0]
		lname = data[i][1]
		dob = data[i][2]
		
		#check for phone number
		if len(data[i]) > 3: phone = data[i][3]
		else: phone = None
		pers = insert_person(s, fname, lname, dob, dateJoined, newhouse, phone)
		
		#the first person is the actual visitor; save for insert_visit
		if i == 0:
			#s.commit()
			newpers = pers
	#s.commit()
	
	#create new visit for household
	insert_visit(s, Vol_ID, newpers, newhouse, visitDate, notes)	
	#s.commit()
	
	
def update_all(Vol_ID, I_ID, street, client_data_tuple_list, 
		new_client_list=None, visitDate=datetime.now(),
		dateVerified = None, apt = None,	city = 'Troy',
		state = 'NY', zip = '12180', notes=None):
	"""Input:
	update_all(int Vol_ID, int I_ID, string street, 
		list[(int id, string fname, string lname, date dob, int phone)],
		list[string fname, string lname, data dob, int phone)],
		date visitDate, date dateVerified, string apt,
		string city, string state, int zip, string notes)
				
	This function will update a household and all members in the household,
	add any new members, and create a new visit record.
	"""
	pers = s.query(Person).filter(Person.id == I_ID).one()
	house = s.query(Household).filter(Household.id == pers.HH_ID).one()
	
	#update household
	update_household(s, house.id, street, city, state,
					zip, apt, dateVerified)
	
	#add new clients (if they exist)
	ncl = new_client_list #renamed for simplicity
	if ncl == None: pass
	else:
		for i in range(0, len(ncl)):
			fname = ncl[i][0]
			lname = ncl[i][1]
			dob = ncl[i][2]
			
			#check for phone number
			if len(ncl[i]) > 3: pho = ncl[i][3]
			else: pho = None
			newpers = insert_person(s, fname, lname, dob, house.id, phonenum=pho)
	
	#update old clients
	ctl = client_data_tuple_list #renamed for simplicity
	for i in range(0, len(ctl)):
		iid = ctl[i][0]
		fname = ctl[i][1]
		lname = ctl[i][2]
		dob = ctl[i][3]
		
		#check for phone number
		if len(ctl[i]) > 4: pho = ctl[i][4]
		else: pho = None
		update_person(s, iid, fname, lname, dob, phonenum=pho)
		
	
	#create a new visit	
	insert_visit(s, Vol_ID, pers.id, house.id, visitDate, notes)
	
	
#Function to retrieve all data for a client
def select_client(I_ID):
	"""This function returns all client data for a selected client.
	"""
	
	#find person and associated household
	pers = s.query(Person).filter(Person.id == I_ID).one()	
	house = s.query(Household).filter(Household.id == pers.HH_ID).one()
	
	info = [pers.id, pers.first_name, pers.last_name, pers.DOB, pers.age,
			pers.phone, pers.date_joined, house.street_address, house.apt,
			house.city, house.state, house.zip, house.date_verified]
			
	for member in house.members:
		info.append( (member.first_name, member.last_name, member.DOB, member.age) )

	agegroups = get_age_breakdown(house.members)
	info.append(agegroups)
	return info
	
{'fraction_non_alphanumeric': 0.10537442973183488, 'fraction_numerical': 0.006008679203293646, 'mean_word_length': 4.369175627240144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2516873', 'n_tokens_mistral': 3215, 'n_tokens_neox': 2930, 'n_words': 1005}