From 65f7a25f4955c8feb02d226e58c4e6b57a091e4f Mon Sep 17 00:00:00 2001 From: naath Date: Fri, 11 Apr 2014 21:30:08 +0100 Subject: [PATCH] refactor to put findparents/spouses/children in one place --- cgiFiles/ancestorGraph.py | 49 +++--- cgiFiles/bigGraph.py | 2 +- cgiFiles/jointAncestorGraph.py | 53 +++---- cgiFiles/smallGraph.py | 37 ++++- familyTree/askQuestion.py | 274 ++++++++++++++++++--------------- 5 files changed, 225 insertions(+), 190 deletions(-) diff --git a/cgiFiles/ancestorGraph.py b/cgiFiles/ancestorGraph.py index 36a9bf3..4adb104 100755 --- a/cgiFiles/ancestorGraph.py +++ b/cgiFiles/ancestorGraph.py @@ -5,52 +5,47 @@ import cgitb cgitb.enable() import sys sys.path.append('/home/naath/familyTreeProject/familyTree') -import askQuestion +import askQuestion as aQ import make_dot as d -def parents(ID,name): - findParents = "SELECT Name,parentID FROM parents LEFT JOIN"\ - +" people ON people.id=parents.parentid"\ - +" WHERE parents.id=?;" +def add_parents(ID,name): - pair =[] - for row in askQuestion.run_query(findParents,(ID,)): - if row[0]!=None: - thisN = row[0] + ' '+str(row[1]) - newName = thisN - newID = row[1] - if not d.has_node(thisN): - d.add_person(thisN) - parents(newID,newName) - pair.append(thisN) + [parents, parentIDs,parentNames] = aQ.find_parents(ID) - else: - thisN = row[1] + ',p ' + str(ID) - pair.append(thisN) - d.add_person(thisN) + pair = [] + for i in range(len(parents)): + newName = parents[i] + if parentIDs[i]!=0: + newID = parentIDs[i] + if not d.has_node(newName): + d.add_person(newName) + add_parents(newID,newName) + else: + d.add_person(newName) + pair.append(newName) d.add_marriage(pair[0],pair[1],[name],1) -def make_graph(ID,conn): +def make_graph(ID): global allAncestors d.start_dot() [out, allAncestors,trackLevel,aDict] = \ - askQuestion.all_ancestors(ID,'\n') + aQ.all_ancestors(ID,'\n') Self = allAncestors[0] s = "SELECT name||', '|| id FROM people WHERE ID=?" t = (Self,) - for row in askQuestion.run_query(s,t): + for row in aQ.run_query(s,t): Self = row[0] d.add_highlight(Self) - parents(allAncestors[0],Self) + add_parents(allAncestors[0],Self) d.add_subgraphs() @@ -58,13 +53,11 @@ def make_graph(ID,conn): d.render_dot() - askQuestion.close(conn) - form = cgi.FieldStorage() ID = form.getvalue('id') -conn = askQuestion.connect() -make_graph(ID,conn) -askQuestion.close(conn) +conn = aQ.connect() +make_graph(ID) +aQ.close(conn) diff --git a/cgiFiles/bigGraph.py b/cgiFiles/bigGraph.py index 35d2b33..8834ba1 100755 --- a/cgiFiles/bigGraph.py +++ b/cgiFiles/bigGraph.py @@ -20,7 +20,7 @@ famTree = askQuestion.list_people_parents() d.start_dot() for i in range(len(famTree)): -#for i in range(200): +#for i in range(100): self = famTree[i][0] d.add_person(self) diff --git a/cgiFiles/jointAncestorGraph.py b/cgiFiles/jointAncestorGraph.py index 099f042..0279bc9 100755 --- a/cgiFiles/jointAncestorGraph.py +++ b/cgiFiles/jointAncestorGraph.py @@ -6,36 +6,29 @@ cgitb.enable() import sys sys.path.append('/home/naath/familyTreeProject/familyTree') import make_dot as d -import askQuestion +import askQuestion as aQ -def parents(ID,name,startLevel,stopLevel): - - findParents = "SELECT Name,parentID FROM parents LEFT JOIN"\ - +" people ON people.id=parents.parentid"\ - +" WHERE parents.id=?;" +def add_parents(ID,name,startLevel,stopLevel): startLevel = startLevel + 1 if int(startLevel) == int(stopLevel)+1: return pair=[] - for row in askQuestion.run_query(findParents,(ID,)): - if row[0]!=None: - thisN = row[0] + ' '+str(row[1]) - - pair.append(thisN) - newName = thisN - newID = row[1] - if not d.has_node(thisN): - d.add_person(thisN) - parents(newID,newName,startLevel,stopLevel) - - else: - thisN = row[1] + ',p ' + str(ID) - pair.append(thisN) - d.add_person(thisN) - - + [parents, parentIDs,parentNames] = aQ.find_parents(ID) + + pair = [] + for i in range(len(parents)): + newName = parents[i] + if parentIDs[i]!=0: + newID = parentIDs[i] + if not d.has_node(newName): + d.add_person(newName) + add_parents(newID,newName,\ + startLevel,stopLevel) + else: + d.add_person(newName) + pair.append(newName) d.add_marriage(pair[0], pair[1],[name],1) @@ -45,26 +38,26 @@ def make_graph(ID,ID2,LA,LB): if int(LA)!=0: s = "SELECT name, id FROM people WHERE ID = ?;" - for row in askQuestion.run_query(s,(ID,)): + for row in aQ.run_query(s,(ID,)): thisN = row[0]+' ' + str(row[1]) d.add_highlight(thisN) newName = (thisN) newID = row[1] - parents(newID,newName,0,LA) + add_parents(newID,newName,0,LA) if int(LB)!=0: s = "SELECT name, id FROM people WHERE ID = ?;" - for row in askQuestion.run_query(s,(ID2,)): + for row in aQ.run_query(s,(ID2,)): thisN = row[0] +' '+ str(row[1]) d.add_highlight(thisN) newName = (thisN) newID = row[1] - parents(newID,newName,0,LB) + add_parents(newID,newName,0,LB) d.add_subgraphs() d.end_dot() d.render_dot() - askQuestion.close(conn) + aQ.close(conn) form = cgi.FieldStorage() @@ -73,7 +66,7 @@ ID2 = form.getvalue('id2') LA = form.getvalue('LA') LB = form.getvalue('LB') -conn = askQuestion.connect() +conn = aQ.connect() make_graph(ID,ID2,LA,LB) -askQuestion.close(conn) +aQ.close(conn) diff --git a/cgiFiles/smallGraph.py b/cgiFiles/smallGraph.py index ee15269..add7211 100755 --- a/cgiFiles/smallGraph.py +++ b/cgiFiles/smallGraph.py @@ -3,14 +3,30 @@ import cgi import cgitb import make_dot as d - +import sys +sys.path.append('/home/naath/familyTreeProject/familyTree') +import askQuestion as aQ cgitb.enable() -def make_graph(Self,parents,children,otherparents,spouses): +def find_person(ID): + s = "SELECT name || ','||ID FROM people WHERE ID=?" + t = (ID,) + + for row in aQ.run_query(s,t): + Self = row[0] + return Self + +#def make_graph(Self,parents,children,otherparents,spouses): +def make_graph(ID): + d.start_dot() + Self = find_person(ID) + d.add_highlight(Self) + [parents, parentIDs,parentNames] = aQ.find_parents(ID) + countSame = 2; for i in range(1,len(parents)): if parents[i]==parents[i-1]: @@ -19,10 +35,17 @@ def make_graph(Self,parents,children,otherparents,spouses): d.add_marriage(parents[0],parents[1],[Self],1) + [children,childrenID,childrenNames\ + ,otherparents,otherparentsID,otherParentsNames\ + ,childrenBorn] = \ + aQ.find_children(ID) + for i in range(len(otherparents)): c = children[i] op = otherparents[i] d.add_marriage(Self,op,[c],1) + + [spouses,spousesID,spousesNames] = aQ.find_spouses(ID) for i in range(len(spouses)): s = spouses[i] @@ -40,10 +63,8 @@ def make_graph(Self,parents,children,otherparents,spouses): form = cgi.FieldStorage() -Self = form.getvalue('Self') -p = form.getlist('p') -c = form.getlist('c') -op = form.getlist('op') -s = form.getlist('s') +ID = form.getvalue('ID') -make_graph(Self,p,c,op,s) +conn = aQ.connect() +make_graph(ID) +aQ.close(conn) diff --git a/familyTree/askQuestion.py b/familyTree/askQuestion.py index 841df2f..9135bef 100755 --- a/familyTree/askQuestion.py +++ b/familyTree/askQuestion.py @@ -224,42 +224,15 @@ def list_people_parents(): output = [] for row in run_query(s,()): - t = "SELECT parentid"\ - +" FROM parents"\ - +" WHERE id = ?;" - - u = "SELECT name,id"\ - +" FROM people"\ - +" WHERE id = ?"; - - parents =[] - for r in run_query(t,(row[1],)): - parentID = r[0] - hasParent = 0 - for q in run_query(u,(r[0],)): - parents.append(q[0] + ' ' + str(q[1])) - hasParent=1 - if hasParent==0: - parents.append(r[0] + ' p' +\ - str(row[1])) - - spouses=[] - v = "SELECT name,idb"\ - +" FROM marriages LEFT JOIN people"\ - +" ON idb = id"\ - +" WHERE ida = ?" - for r in run_query(v,(row[1],)): - if r[0]!=None: - spouses.append(r[0]+ ' '+str(r[1])) - else: - if len(r[1])>0: - spouses.append(r[1] + ' s' +\ - str(row[1])) + ID = row[1] + [parents, parentIDs,parentNames] = find_parents(ID) + [spouses,spousesID,spousesNames] = find_spouses(ID) + myName = row[0] myID = str(row[1]) - output.append([myName+ ' '+ myID,parents,spouses]) + output.append([myName+ ','+ myID,parents,spouses]) return output @@ -761,6 +734,116 @@ def rulers_of(aTerritory,newLine): return out + +def find_parents(ID): + s = "SELECT name, parentID"\ + +" FROM parents LEFT JOIN people"\ + +" ON people.ID = parentID"\ + +" WHERE parents.ID = ?;" + t = (ID,) + + parents = [] + parentIDs =[] + parentNames=[] + + for row in run_query(s,t): + if row[0]!=None: + p = row[0] + ',' + str(row[1]) + pID = row[1] + pN = row[0] + else: + p = row[1] + ',p ' + str(ID) + pID = 0 + pN = row[1] + parents.append(p) + parentIDs.append(pID) + parentNames.append(pN) + + if parents[1]==parents[0]: + parents[1] = parents[1] + ' 2' + + return [parents,parentIDs,parentNames] + +def find_spouses(ID): + t = (ID,) + + order = [["IDb","IDa"],["IDa","IDb"]] + + spouses = [] + spousesID=[] + spousesNames=[] + for o in order: + s = "SELECT name, marriages." + o[0]\ + +" FROM marriages LEFT JOIN people"\ + +" ON marriages." +o[0]+" = people.ID"\ + +" WHERE marriages."+o[1]+" = ?;" + + + for row in run_query(s,t): + if row[0]!=None: + s = row[0] + "," +str(row[1]) + sID = row[1] + sN = row[0] + elif row[1] !='': + s=row[1] + ",s " +str(ID) + sID = 0 + sN = row[1] + if row[1] !='': + spouses.append(s) + spousesID.append(sID) + spousesNames.append(sN) + + return [spouses,spousesID,spousesNames] + + +def find_children(ID): + s = "SELECT p1.name, p1.ID,p3.parentID,p4.name,p1.bornYear"\ + +" FROM people p1"\ + +" INNER JOIN parents p2"\ + +" ON p1.ID = p2.ID"\ + +" INNER JOIN parents p3"\ + +" ON p1.ID = p3.ID"\ + +" LEFT JOIN people"\ + +" p4 ON p3.parentID = p4.ID"\ + +" WHERE p2.parentID = ?"\ + +" AND p3.parentID<>?"\ + +" ORDER BY p1.bornYear;" + + t = (ID,ID) + + children =[] + childrenID=[] + childrenNames=[] + childrenBorn=[] + otherparents=[] + otherparentsID=[] + otherparentsNames=[] + + for row in run_query(s,t): + c = row[0] + ', ' + str(row[1]) + cID = row[1] + cName = row[0] + born = row[4] + children.append(c) + childrenID.append(cID) + childrenNames.append(cName) + childrenBorn.append(born) + if row[3]!=None: + op = row[3] + ', ' + str(row[2]) + opID = row[2] + opN = row[3] + else: + op = row[2] + ',s ' + ID + opID = 0 + opN = row[2] + otherparents.append(op) + otherparentsID.append(opID) + otherparentsNames.append(opN) + + return [children,childrenID,childrenNames\ + ,otherparents,otherparentsID,otherparentsNames\ + ,childrenBorn] + def person_info(personID,newLine): t = (personID,) @@ -827,98 +910,56 @@ def person_info(personID,newLine): mainDiv = mainDiv + '

' #find parents + + [parents,parentIDs,parentNames] = find_parents(personID) mainDiv = mainDiv + '

' - s = "SELECT people.Name,parents.parentID FROM"\ - +" parents LEFT JOIN people"\ - +" ON parents.parentID = people.ID"\ - +" WHERE parents.ID = ?" + for i in range(len(parents)): + r = [parentNames[i],parentIDs[i]] + mainDiv = mainDiv + print_tagged_name('Parent',r,newLine) + mainDiv = mainDiv + "

" - parents =[] - for row in run_query(s,t): - mainDiv = mainDiv + print_tagged_name('Parent',row,newLine) - if row[0]!=None: - parents.append(row[0] + ', ' + row[1]) - else: - parents.append(row[1] + ', p' + personID) - mainDiv = mainDiv + '

' #find spouses + [spouses,spousesID,spousesNames] = find_spouses(personID) + mainDiv = mainDiv + '

' - s = "SELECT people.NAME, marriages.IDb from"\ - +" marriages LEFT JOIN people"\ - +" ON people.ID = marriages.IDb"\ - +" WHERE marriages.IDa = ?"\ - +" ORDER BY IDb;" - spouses = [] - for row in run_query(s,t): - if row[0]!=None: - spouses.append(row[0] + ', '+str(row[1])) - elif row[1]!='': - spouses.append(row[1] + ', s' + personID) - if row[1]!='': - mainDiv = mainDiv + print_tagged_name('Spouse',row,newLine) - mainDiv = mainDiv + relationship_html(personID,row[1],newLine) - - s = "SELECT people.NAME, marriages.IDa from"\ - +" marriages LEFT JOIN people"\ - +" ON people.ID = marriages.IDa"\ - +" WHERE marriages.IDb = ?"\ - +" ORDER BY IDa;" - for row in run_query(s,t): - if row[0]!=None: - spouses.append(row[0] + ', '+str(row[1])) - else: - spouses.append(row[1] + ', s' + personID) - mainDiv = mainDiv + print_tagged_name('Spouse',row,newLine) - mainDiv = mainDiv + relationship_html(personID,row[1],newLine) + + for i in range(len(spouses)): + r = [spousesNames[i],spousesID[i]] + mainDiv = mainDiv + print_tagged_name('Spouse',r,newLine) + mainDiv = mainDiv + \ + relationship_html(personID,r[1],newLine) + mainDiv = mainDiv + '

' #find children - s = "Select people.NAME, people.ID ,people.bornYear"\ - +" FROM people INNER JOIN parents"\ - +" ON people.ID = parents.ID"\ - +" WHERE parents.parentID = ?"\ - +" ORDER BY people.bornYear;" - - children = [] - ops =[] + [children,childrenID,childrenNames\ + ,otherparents,otherparentsID,otherparentsNames\ + ,childrenBorn] = \ + find_children(personID) + top = '' - for row in run_query(s,t): - thisChild = print_tagged_name('Child',row,newLine) - children.append(row[0] + ', ' + str(row[1])) - - #find children's other parent - u = "Select people.NAME, parents.parentID FROM"\ - +" parents LEFT JOIN people"\ - +" ON people.ID = parents.parentID"\ - +" WHERE parents.ID = ? AND parents.parentID <>?;" - - ids = (row[1],t[0]) - - op = 0 - for r in run_query(u,ids): - op = 1 - if r[0]!=None: - ops.append(r[0] + ', ' + str(r[1])) - else: - ops.append(r[1] + ', s' + personID) - if op==0: - ops.append('?' + ', s' + personID) - - if top!=ops[-1]: - mainDiv = mainDiv + '

' + for i in range(len(children)): + cr = [childrenNames[i],childrenID[i]] + thisChild = print_tagged_name('Child',cr,newLine) + + opr=[otherparentsNames[i],otherparentsID[i]] + top = otherparentsNames[i] + if i==0 or top != otherparentsNames[i-1]: + mainDiv = mainDiv +'

' mainDiv = mainDiv + '

' - mainDiv = mainDiv + print_tagged_name('With',r, newLine) + mainDiv = mainDiv + print_tagged_name\ + ('With',opr, newLine) - top = ops[-1] - #age when child born - if row[2] !=0 and bornYear != 0: - age = row[2]-bornYear + cb = childrenBorn[i] + if cb!=0 and bornYear != 0: + age = cb-bornYear thisChild = thisChild[:-4] + \ " at the age of "+str(age) + newLine mainDiv = mainDiv + thisChild + mainDiv = mainDiv + '

' output = '
'; @@ -940,21 +981,8 @@ def person_info(personID,newLine): output = output + imageDiv + "
" - Self = name +', ' + str(personID) - - graph = "smallGraph.py?Self="+Self - for p in parents: - graph = graph + '&p='+p - for c in children: - graph = graph + '&c='+c - for op in ops: - if op !=None: - graph = graph + '&op='+op - for s in spouses: - if s !=None: - graph = graph + '&s='+str(s) - - graph = graph.replace(' ','%20') + graph = "smallGraph.py?ID="+str(personID) + graph ="" -- 2.30.2