chiark / gitweb /
Added list by number of children; refarctored other list-by-count
[familyTree.git] / familyTree / askQuestion.py
index 70185030007dc948a29a9ea04122e5eb9e8bb9dc..d0380d734cbf9bcc435130f860f536d2ba6b3deb 100755 (executable)
@@ -3,9 +3,16 @@
 import sqlite3
 import findYear
 from string import Template
+import cgi
+import re
 
 global link_Template 
-link_Template= Template("<a href = http://www.chiark.greenend.org.uk/ucgi/~naath/$script>$text</a>")
+link_Template= Template(\
+       "<a href = http://www.chiark.greenend.org.uk/ucgi/~naath/$script"\
+       +" title=$title>$text</a>")
+def add_quotes(s):
+       s = str(s)
+       return "'"+s+"'"
 
 def run_query(s,t):
        c = make_cursor()
@@ -40,15 +47,54 @@ def print_tagged_query(relationship,s,t,newLine):
 def relationship_html(ID,ID2,newLine):
        if newLine=='<br>':
                relationship = common_ancestors(ID,ID2,newLine)[2]
-               script = "ancestors.py?ID="+str(ID)+"&ID2="+str(ID2)
-               url = link_Template.substitute(script = script,text = "Common ancestors")
-               return relationship + ' '+url + newLine
+                       
+               if relationship[-11:] != 'not related':
+                       script = "ancestors.py?ID="+str(ID)+"&ID2="+str(ID2)
+                       url = link_Template.substitute\
+                               (script = script,title = "Common ancestors"\
+                                       ,text = "Common ancestors")
+                       return relationship + ' '+url + newLine
+               else:
+                       return relationship + newLine
        else:
                return ''
 
-def terr_html(terr,newLine):
+def terr_html(terr,newLine,start,stop):
        if newLine=='<br>':
-               return link_Template.substitute(script = "territory.py?terr="+terr, text=terr)
+               if start == 0 and stop ==0:
+                       myTitle = add_quotes(terr)
+
+               else:
+                       s = "SELECT name,people.id"\
+                       +" FROM people INNER JOIN territories"\
+                       +" ON people.id = territories.id"\
+                       +" WHERE territory = ? AND stopyear <= ?"\
+                       +" ORDER BY startyear DESC;"
+
+                       t = (terr,start)
+                       myTitle = ''
+                       for row in run_query(s,t):
+                               myTitle = myTitle +"previous - " + row[0] \
+                               + ',' + str(row[1])
+                               break
+
+                       u = "SELECT name,people.id"\
+                        +" FROM people INNER JOIN territories"\
+                       +" ON people.id = territories.id"\
+                        +" WHERE territory = ? AND startyear >= ?"\
+                        +" ORDER BY startyear;"
+               
+                       v = (terr,stop)
+                        for r in run_query(u,v):
+                               myTitle = myTitle + '&#xA' +"next - " + r[0] \
+                                + ',' + str(r[1])
+                                break
+
+                       myTitle = add_quotes(myTitle)
+
+               return link_Template.substitute(\
+                       script = "territory.py?terr="+terr, title=myTitle,\
+                       text = terr)
        else:
                return terr
 def name_html(row,html):
@@ -56,14 +102,85 @@ def name_html(row,html):
                html=1
        elif html=='\n':
                html=0
-       if html == 1:
-               script = "person.py?ID=" + str(row[1])
-               name = row[0]
-               return link_Template.substitute(script = script, text = name)
+
        if row[0] == None:
                return row[1]
+       elif row[1]==0:
+               return row[0]
+       else:
+               if html==1:
+                       script = "person.py?ID=" + str(row[1])
+                       name = row[0]
+                       return link_Template.substitute(script = script\
+                               ,title = add_quotes(name),text = name)
+               else:
+                       return row[0] + "," +str(row[1])
+
+def print_people(n):
+       if n>1:
+               return ' people '
+       else:
+               return ' person '
+
+def print_age_child_count(row,newLine):
+       if newLine == '<br>':
+               script = "listAge.py?age="+str(row[0])
+               link = link_Template.substitute(script = \
+                       script, title = add_quotes(row[0]), text = row[0])
+               out = str(row[1])+print_people(row[1])
+
+               out = out + 'had children at age '+ link + newLine
+               return out
+       else:
+               return print_row(row,newLine)
+
+def print_age_death_count(row,newLine):
+       if newLine =='<br>':
+               script = "listAgeDeath.py?age="+str(row[0])
+               link = link_Template.substitute(script = script,\
+                       title = add_quotes(row[0]),text = row[0])
+               out = str(row[1])+print_people(row[1])
+               out = out + "died at age " + link + newLine
+               return out
+       else:
+               return print_row(row,newLine)
+
+def print_name_count(row,newLine):
+       if newLine=='<br>':
+               script = "name.py?name=" + row[0]
+               link = link_Template.substitute(script =\
+                       script, title = add_quotes(row[0]),text = row[0])
+               return str(row[1]) + " people called "+link + newLine
+       else:
+               return print_row(row,newLine)   
+
+def print_children_count(row,newLine):
+       out = str(row[0])
+
+       if row[0]==1:
+               out = out + ' person '
        else:
-               return row[0] + "," +str(row[1])
+               out = out + ' people '
+       out = out +'had ' 
+
+       if newLine == '<br>':
+               script = "listChildCount.py?nc="+str(row[1])
+               link = link_Template.substitute(script =\
+                       script, title = add_quotes(row[1]),text = row[1])
+       else:
+               link = str(row[1])
+
+       out = out + link
+
+       if row[1] == 1:
+               out = out  + ' child '
+       else:
+               out = out + ' children '
+
+
+       out  = out  + newLine
+
+       return out
 
 def print_tagged_name(relationship,row,newLine):
        if row[0]==None:
@@ -79,6 +196,36 @@ def print_tagged_name(relationship,row,newLine):
                        out = relationship + ": " + name_html(row,html)
        return out + newLine
 
+def month_numbers(monthN):
+       if monthN == 0:
+               month ='unknown month'
+       elif monthN == 1:
+               month ='January'
+       elif monthN==2:
+               month ='February'
+       elif monthN==3:
+               month ='March'
+       elif monthN==4:
+               month ='April'
+       elif monthN==5:
+               month ='May'
+       elif monthN==6:
+               month ='June'
+       elif monthN==7:
+               month ='July'
+       elif monthN==8:
+               month ='August'
+       elif monthN==9:
+               month ='September'
+       elif monthN==10:
+               month ='October'
+       elif monthN==11:
+               month ='November'
+       elif monthN==12:
+               month ='December'
+       else:
+               month = 'Incorrectly entered month ' + str(monthN)
+       return month
 
 def ordinal_numbers(number):
        number = int(number)
@@ -99,9 +246,26 @@ def list_territories(newLine):
 
        out = ''
        for row in run_query(s,()):
-               out =out + terr_html(row[0],newLine) +newLine
+               out =out + terr_html(row[0],newLine,0,0) +newLine
        return out
 
+def list_people_parents():
+       s = "SELECT name,id"\
+               +" FROM people"\
+               +" ORDER BY id;"
+
+       output = []
+       for row in run_query(s,()):
+
+               ID = row[1]
+               [parents, parentIDs,parentNames] = find_parents(ID)
+               [spouses,spousesID,spousesNames] = find_spouses(ID)
+               
+               [self,myID,myName] = find_person(ID)
+               output.append([self,parents,spouses])
+       return output
+
+
 def list_people(newLine):
        s = "SELECT name,id,bornyear"\
        +" FROM people"\
@@ -118,9 +282,74 @@ def list_people(newLine):
                        century = row[2]/100 + 1
                        out = out +newLine+ 'born in ' 
 
-                       out = out +ordinal_numbers(century) + ' century:' + newLine
+                       out = out +ordinal_numbers(century) \
+                               + ' century:' + newLine
 
                out = out + name_html(row,newLine) +newLine
+
+               if row[2] == 0: #unknown year
+
+                       t = (row[1],) #person ID
+
+
+                       #died
+                       u = "SELECT diedyear FROM people WHERE ID = ?;"
+
+                       bornAfter = 0
+                       for r in run_query(u,t):
+                               if r[0] !=0:
+                                       out = out + "died: "\
+                                        + str(r[0]) + newLine
+                                       bornAfter = r[0] -100
+
+                       #find children
+                       u = "Select people.bornYear from"\
+                               +" people INNER JOIN parents"\
+                               +" ON people.ID = parents.ID"\
+                               +" WHERE parents.parentID = ?"\
+                               + " ORDER BY people.bornYear;"
+                       
+                       hadChild=[]
+                       
+                       for r in run_query(u,t):
+                               if r[0] != 0:
+                                       hadChild.append(r[0])
+                       
+                       bornBefore = 0
+                       if len(hadChild)!=0:
+                               out = out + "had children in: "
+                               for c in hadChild:
+                                       out = out + str(c) + ','
+                               out = out[:-1] + newLine
+
+                               bornBefore = hadChild[0]-12
+                               if bornAfter==0:
+                                       bornAfter = hadChild[0]-100
+                       
+                       u = "Select styles.startYear, styles.style from"\
+                                +" people INNER JOIN styles"\
+                                +" ON people.ID = styles.ID"\
+                                +" WHERE people.ID = ? and"\
+                                +" styles.startYear <>0"\
+                                +" ORDER BY styles.startYear;"
+
+                        for r in run_query(u,t):
+                                out = out + r[1] + " from " + str(r[0])\
+                                + newLine
+                               if bornAfter ==0:
+                                       bornAfter = r[0] -100
+                                break
+
+                       if bornAfter!=0:
+                               if bornBefore == 0: 
+                                       out = out + "probably born "\
+                                               +"after " + str(bornAfter)
+                               else:
+                                       out = out + "probably born "\
+                                               +"betwen " + str(bornAfter)\
+                                               +" and " + str(bornBefore)
+                               out = out + newLine
+
                year = row[2]
        return out
 
@@ -130,26 +359,225 @@ def count_names(newLine):
        +" GROUP BY firstName"\
        +" ORDER BY count(*) DESC;"
 
-       out = print_query(s,(),newLine)
+       out = ''
+       for row in run_query(s,()):
+               out = out + print_name_count(row,newLine)
+
+       return out
+
+
+
+def count_children(newLine):
+
+       s = "SELECT count(*),nc"\
+       +" FROM ("\
+       +" SELECT count(*) AS nc"\
+       +" FROM parents"\
+       +" GROUP BY parentID"\
+       +" HAVING parentID <>'?'"\
+       +" AND parentID <> '0')"\
+       +" GROUP BY nc;"
+
+       out = ''
+       for row in run_query(s,()):
+               out = out + print_children_count(row,newLine)
+       return out
+
+def parents_with_children(nChildren,newLine):
+       s = "SELECT name,parentID"\
+       + " FROM parents"\
+       + " LEFT JOIN people"\
+       + " ON parentID = people.ID"\
+       + " GROUP BY parentID"\
+       + " HAVING count(*) = ?"\
+       + " AND parentID <> 0"\
+       + " ORDER BY bornYear;"
+
+
+       u = "SELECT count(*)"\
+       +" FROM  parents INNER JOIN people"\
+       +" ON parents.ID = people.ID"\
+       +" WHERE parentID = ?"\
+       +" AND (diedyear-bornyear>? OR died='present');"
+
+       out = "People who had " + str(nChildren)
+       if nChildren==1:
+               out = out + " child"
+       else:
+               out = out  + " children"
+       out = out + ':' + newLine
+
+
+
+       for row in run_query(s,(nChildren,)):
+               out = out + name_html(row,newLine)
+               for r in run_query(u,(row[1],1)):       
+                       out += " %d  survived infancy" % r[0]
+               for r in run_query(u,(row[1],18)):
+                       out = out +" and " +str(r[0])+" survived to adulthood"
+               out = out +newLine
+       
+       return out
+
+def search_name(name,newLine):
+       s = "SELECT name, ID"\
+        +" FROM people"\
+        +" WHERE name LIKE ?;"
+
+        out = ''
+       IDs=[]
+       names=[]
+
+       out = out + 'Names starting with ' + name + ':' + newLine
+       t = (name + '%',)
+       fullIDs=[]
+       for row in run_query(s,t):
+                out = out + name_html(row,newLine) + newLine
+               fullIDs.append(row[1])
+               names.append(row[0])
+               IDs.append(row[1])
+
+        t = ('%' + name + '%',)
+       out = out+newLine + 'Names containing ' + name + ':' + newLine
+        for row in run_query(s,t):
+               if row[1] not in fullIDs:
+                       out = out + name_html(row,newLine) + newLine
+                       names.append(row[0])
+                       IDs.append(row[1])
        
+       s = "SELECT name,people.ID,style"\
+       +" FROM people INNER JOIN styles"\
+       +" ON styles.id = people.id"\
+       +" WHERE style LIKE ?;"
+       out = out +newLine+ 'Styles containing ' + name + ':' + newLine
+       for row in run_query(s,t):
+               out = out + name_html(row,newLine)+' ' + row[2] + newLine
+
+        return [out,names,IDs]
+
+
+def people_with_name(name,newLine):
+       s = "SELECT name, ID"\
+       +" FROM people"\
+       +" WHERE firstname = ?;"
+
+       out = ''
+
+       t = (name,)
+
+       for row in run_query(s,t):
+               out = out + name_html(row,newLine) + newLine
+
+       return out
+
+def count_birth_month(newLine):
+       s = "SELECT bornMonth, count(*)"\
+               +" FROM people"\
+               +" GROUP BY bornMonth"\
+               +" ORDER BY bornMonth;"
+
+       t = "SELECT * FROM people WHERE bornMonth = ?;"
+
+       out = ''
+       for row in run_query(s,()):
+               month = month_numbers(row[0])
+               out = out + month + ': ' + str(row[1]) + newLine
 
+               if row[0]>12:
+                       u = (row[0],)
+                       out =  out +print_query(t,u,newLine)
+               
+       return out
+
+def count_death_month(newLine):
+        s = "SELECT diedMonth, count(*)"\
+                +" FROM people"\
+                +" GROUP BY diedMonth"\
+                +" ORDER BY diedMonth;"
+
+       t = "SELECT * FROM people WHERE diedMonth = ?;"
+
+        out = ''
+        for row in run_query(s,()):
+                month = month_numbers(row[0])
+                out = out + month + ': ' + str(row[1]) + newLine
+
+               if row[0]>12:
+                        u = (row[0],)
+                        out =  out +print_query(t,u,newLine)
+
+        return out
+
+def count_age_at_child(newLine):
+
+       s = "select p1.bornYear - p2.bornYear as age, count(*)"\
+               +" FROM"\
+               +" parents INNER JOIN people p1"\
+               +" ON parents.ID = p1.ID"\
+               +" INNER JOIN people p2"\
+               +" ON parents.parentID = p2.ID"\
+               +" WHERE p1.bornYear <> 0 and p2.bornYear<>0"\
+               +" GROUP BY age;"
+
+       out = ''
+       for row in run_query(s,()):
+               out = out + print_age_child_count(row,newLine)
+
+       return out
+
+def people_had_child_at_age(age,newLine):
+
+       s = "select p1.bornYear - p2.bornYear as age, p1.name, p1.ID"\
+                +",p2.name,p2.ID FROM"\
+                +" parents INNER JOIN people p1"\
+                +" ON parents.ID = p1.ID"\
+                +" INNER JOIN people p2"\
+                +" ON parents.parentID = p2.ID"\
+               +" WHERE age = ? AND p1.bornYear<>0 AND p2.bornYear<>0"
+
+       t = (int(age),)
+
+       out = ''
+       out = 'At age ' + str(age) + ' :'
+       for row in run_query(s,t):
+               out = out + newLine
+               out =out + name_html([row[3],row[4]],newLine) + ' had '\
+                       +name_html([row[1],row[2]],newLine)
+
+       return out
+
+def count_age_at_death(newLine):
+       s = "select diedYear-bornYear as age,count(*)"\
+               +" FROM people"\
+               +" WHERE diedYear<>0 AND bornYear<>0"\
+               +" GROUP BY age;"
+       out=''
+       for row in run_query(s,()):
+               out = out + print_age_death_count(row,newLine)
+
+       return out
+
+def people_died_at_age(age,newLine):
+       s = "SELECT diedYear-bornYear as age, name,ID"\
+               +" FROM people"\
+               +" WHERE age = ? AND bornYear<>0 AND diedYear<>0;"
+       t = (int(age),)
+       out =''
+       out = 'These people died at age ' +str(age) + ' :'
+       for row in run_query(s,t):
+               out = out +newLine
+               out = out + name_html([row[1],row[2]],newLine)
        return out
 
 def all_ancestors(personID,newLine):
        #find parents
-        s = "SELECT people.Name,parents.parentID FROM"\
-                +" parents LEFT JOIN people"\
-                +" ON parents.parentID = people.ID"\
-                +" WHERE parents.ID = ?"\
-               +" AND parents.parentID <> '.';"
-
 
        ancestors = [personID]
        allAncestors = [personID]
        trackLevel = [0]
        level = 0
 
-       t = "SELECT name,id FROM people WHERE id==?"
+       t = "SELECT name,id FROM people WHERE id=?"
        id = (personID,)
 
        out = "Ancestors of "
@@ -159,18 +587,28 @@ def all_ancestors(personID,newLine):
        while len(ancestors)>0:
                level = level+1
                newA =[]
-               out = out+newLine + parent_level(level,'parent') +':' + newLine
+               thisout = newLine + parent_level(level,'parent') +\
+                       ':' + newLine
                for ancestor in ancestors:
-                       id = (ancestor,)
-                       for row in run_query(s,id):
-                               out = out + name_html(row,newLine)+newLine
-                               if row[1] not in allAncestors and is_number(row[1])!=0:
-                                       newA.append(row[1])
-                                       allAncestors.append(row[1])
+                       [parents, parentIDs,parentNames] \
+                               = find_parents(ancestor)
+                       for i in range(len(parents)):
+                               r = [parentNames[i],parentIDs[i]]
+                               thisout = thisout + \
+                               name_html(r,newLine)+newLine
+                               
+                               if r[1] not in allAncestors\
+                               and r[1]!=0:
+                                       newA.append(r[1])
+                                       allAncestors.append(r[1])
                                        trackLevel.append(level)
+                               
                ancestors = newA
-       
+               out  = out+thisout
+
 
+       image = "<img src = ancestorGraph.py?id="+str(personID)+">"
+       out = out+newLine + image+newLine
        return [out, allAncestors,trackLevel]
 
 
@@ -221,8 +659,6 @@ def common_ancestors(IDA, IDB,newLine):
                s = s+ "?,"
        if len(common)>0:
                s = s[:-1]
-
-
        s = s+") ORDER BY bornyear;"
 
 
@@ -250,8 +686,10 @@ def common_ancestors(IDA, IDB,newLine):
        +" WHERE id=?"
 
        out  = out + newLine + 'Most Recent Common Ancestors:' + newLine
+       mrca = []
        for a in indexA:
                t = (common[a],)
+               mrca.append(common[a])
                out = out + print_tagged_query('',s,t,newLine)
                if a!=indexA[-1]:
                        out = out + 'and' + newLine
@@ -262,13 +700,6 @@ def common_ancestors(IDA, IDB,newLine):
 
        out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
 
-       out = out + newLine
-       for b in indexB:
-               t = (common[b],)
-               out = out + print_tagged_query('',s,t,newLine)
-               if b!=indexB[-1]:
-                       out = out + 'and' + newLine
-
        out = out + parent_level(bLevels[indexB[0]],'parent')
        if len(indexB)>1:
                out = out + 's'
@@ -281,6 +712,15 @@ def common_ancestors(IDA, IDB,newLine):
        related = relationship(al,bl,names)
        out = out+newLine + related
 
+
+       image = "<img src = jointAncestorGraph.py?id="+str(IDA)\
+               +"&id2="+str(IDB) + "&LA=" + str(min(aLevels)) \
+               +"&LB=" + str(min(bLevels))+">"
+
+
+
+        out = out+newLine + image+newLine
+
        return [out,common,related]
 
 def relationship(level1, level2,names):
@@ -356,35 +796,184 @@ def rulers_of(aTerritory,newLine):
        out = ''
        for row in run_query(tq,(aTerritory+'%',)):
                if row[4]!=last and last!='':
-                        out  = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
+                        out  = out + 'Rulers of '+terr_html(last,newLine,0,0) \
+                       +':'+ newLine +thisT +newLine
                         thisT = ''
 
                thisT = thisT +name_html(row,newLine)
                thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
                last = row[4]
 
-       out  = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
+       out  = out + 'Rulers of '+terr_html(row[4],newLine,0,0) +':'+ \
+               newLine +thisT
 
        return out      
 
+def find_person(ID):
+        s = "SELECT name || ','||ID, name, ID FROM people WHERE ID=?"
+        t = (ID,)
+
+        for row in run_query(s,t):
+                Self = row[0]
+               selfName = row[1]
+               selfID = row[2]
+                return [Self, selfID,selfName]
+def isKing(ID):
+       ID = int(ID.split(',')[-1])
+       s=  "SELECT style FROM styles WHERE ID=?"
+       t = (ID,)       
+
+       k = 0
+       spellingsOfKing = ['King','Queen','king','queen']
+       for row in run_query(s,t):
+               for s in spellingsOfKing:
+                       if re.match('.*'+s,row[0]) != None:
+                               k = 1
+
+       return k
+
+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)
+
+       childrenBorn=[]
+       nodes=[]
+       IDs=[]  
+       names=[]
+
+        for row in run_query(s,t):
+                c = row[0] + ',' + str(row[1])
+                cID = row[1]
+               cName = row[0]
+               born = row[4]
+                childrenBorn.append(born)
+               if row[3]!=None:
+                        op = row[3] + ',' + str(row[2])
+                        opID = row[2]
+                       opN = row[3]
+                else:
+                        op = row[2] + ',s' + str(ID)
+                        opID = 0
+                       opN = row[2]
+
+               nodes.append([c,op])
+               IDs.append([cID,opID])
+               names.append([cName,opN])
+
+        return [nodes,IDs,names,childrenBorn]
+
 def person_info(personID,newLine):
        t = (personID,)
 
-       output = '';
-       
+       if newLine=='<br>':
+               startP = '<p>'
+               endP = '</p>'
+       else:
+               startP = ''
+               endP = newLine
+
+       mainDiv = ''    
        #Id, Name, Dates, Style, Style-Dates
        s = "SELECT * FROM people WHERE ID = ?"
        for row in run_query(s,t):
-               output = output + 'ID: '+str(row[0]) +newLine
-               output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
-               output = output + 'Born: '+row[3] + newLine
-               output = output + 'Died: '+row[5] + newLine
+               mainDiv = mainDiv + startP
+               mainDiv = mainDiv  + 'ID: '+str(row[0]) +newLine
+               mainDiv = mainDiv + print_tagged_name('Name',[row[1], row[0]]\
+                       ,newLine)
+               mainDiv = mainDiv + endP
+               name = row[1]
+               url = row[9]
+               picture = row[10]
+
+               mainDiv = mainDiv + startP
+               mainDiv = mainDiv + newLine + 'Born: '+row[3] + newLine
+               bornYear = row[4]
+               mainDiv = mainDiv + 'Died: '+row[5]
+
+               if row[6] != 0 and row[4] !=0:
+                       mainDiv = mainDiv + ", aged " \
+                               +str(row[6]-row[4])
+               mainDiv = mainDiv + endP
+
 
        s = "SELECT * FROM styles WHERE ID = ?"
        for row in run_query(s,t):
-               output = output +newLine+ 'Style: '+row[1] + newLine
+               mainDiv = mainDiv + startP
+               mainDiv = mainDiv +newLine+ 'Style: '+row[1] + newLine
 
-               output = output + 'Territories:' + newLine
+               mainDiv = mainDiv + 'Territories:' + newLine
 
                u = "SELECT * FROM territories"\
                +"  WHERE ID =? AND startYear =? AND stopYear=?"
@@ -392,71 +981,187 @@ def person_info(personID,newLine):
 
                any = 0
                for r in run_query(u,v):
-                       output = output + terr_html(r[1],newLine) +','
+                       mainDiv = mainDiv \
+                       + terr_html(r[1],newLine,r[3],r[5])\
+                       +','
                        any = 1
                if any ==1:
-                       output = output[:-1] + newLine
+                       mainDiv = mainDiv[:-1] + newLine
 
-               output = output +  'From: '+row[2] + newLine
-                output = output +  'To: '+row[4] + newLine
+               mainDiv = mainDiv +  'From: '+row[2] + newLine
+                mainDiv = mainDiv +  'To: '+row[4]     
 
-       output = output + newLine
-       #find parents
-       s = "SELECT people.Name,parents.parentID FROM"\
-               +" parents LEFT JOIN people"\
-               +" ON parents.parentID = people.ID"\
-               +" WHERE parents.ID = ?"
-       for row in run_query(s,t):
-               output = output + print_tagged_name('Parent',row,newLine)
+               mainDiv = mainDiv + endP
 
-       #find spouses
-       s = "SELECT people.NAME, marriages.IDb from"\
-               +" marriages LEFT JOIN people"\
-               +" ON people.ID = marriages.IDb"\
-               +" WHERE marriages.IDa = ?"
+
+
+
+       mainDiv = mainDiv + startP
+       s = "SELECT people.Name,consort "\
+               +"FROM consorts LEFT JOIN people"\
+               +" ON people.ID = consorts.consort"\
+               +" WHERE consorts.ID = ?"
        for row in run_query(s,t):
-               output = output + newLine
-                output = output + print_tagged_name('Spouse',row,newLine)
-               output = output + relationship_html(personID,row[1],newLine)
+               mainDiv = mainDiv + print_tagged_name\
+               ('Consort of',row,newLine)
+       mainDiv = mainDiv + endP
 
-       s = "SELECT people.NAME, marriages.IDa from"\
-                +" marriages LEFT JOIN people"\
-                +" ON people.ID = marriages.IDa"\
-                +" WHERE marriages.IDb = ?"
-       for row in run_query(s,t):    
-               output = output + newLine
-                output = output + print_tagged_name('Spouse',row,newLine)
-               output = output + relationship_html(personID,row[1],newLine)
+       #find parents
 
-       output = output + newLine
+       [parents,parentIDs,parentNames] = find_parents(personID)
+       mainDiv = mainDiv + startP
+       for i in range(len(parents)):
+               r = [parentNames[i],parentIDs[i]]
+               mainDiv = mainDiv + print_tagged_name('Parent',r,newLine)
+       mainDiv = mainDiv + endP
 
-       #find children
-       s = "Select people.NAME, people.ID from"\
-               +" people INNER JOIN parents"\
-               +" ON people.ID = parents.ID"\
-               +" WHERE parents.parentID = ?"
+       #find spouses
 
-       for row in run_query(s,t):
-               output = output  + print_tagged_name('Child',row,newLine)
+       [spouses,spousesID,spousesNames] = find_spouses(personID)
 
-                #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 <> ?"
+       mainDiv = mainDiv + startP
 
-               ids = (row[1],t[0])
+       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)
 
-               for row in run_query(u,ids):
-                       output = output + print_tagged_name('With',row,newLine)
+       mainDiv  = mainDiv + endP
 
-       output = output + newLine
+       #find children
+       [nodes,IDs,names,childrenBorn] = \
+               find_children(personID)
+
+       top = ''
+       for i in range(len(nodes)):
+               cr = [names[i][0],IDs[i][0]]
+               thisChild = print_tagged_name('Child',cr,newLine)
+
+               opr=[names[i][1],IDs[i][1]]
+               top = names[i][1]
+               if i==0 or  top != names[i-1][1]:
+                       mainDiv = mainDiv +endP
+                       mainDiv = mainDiv + startP
+                       mainDiv = mainDiv + print_tagged_name\
+                        ('With',opr, newLine)
+
+
+               #age when child born
+               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 + endP
+
+
+       if newLine == '<br>':
+               output = '<div id = "main" style = " float:left">';
+               output = output + mainDiv +  "</div>"
+
+               output = output + "<div id = 'image' "\
+                       +"style = 'float:left; margin-left:20px'>"
+
+               imageDiv = ''
+               if picture!='.':
+                       imageDiv = imageDiv + "<a href=" + url+">"\
+                       +"<img src=" + picture +" alt = 'wiki link'"\
+                       +" title = 'wiki link'></a>"\
+                       + newLine
+
+               elif url!='.' and url!='. ':
+                       imageDiv = imageDiv + "<a href=" + url +">"\
+                       +name + " (wiki link)</a>"+newLine
+
+               output = output + imageDiv + "</div>"
+
+
+               url = 'http://www.chiark.greenend.org.uk/ucgi/~naath/'\
+                       +'smallGraph.py'
+
+               form = ''
+               form = form + "<form id ='controlForm'"\
+               +" action ="+ url +" method = 'get'>"
+
+               form = form +\
+                       "<input type = 'hidden' name = 'ID' value = "\
+                       +personID + "><br>"
+
+                form = form +\
+               "Generations of Parents: "\
+               +"<input type = 'text' name = 'pl' value='1'>"
+               form = form + newLine
+               form = form + \
+               "Generations of Children: "\
+               +" <input type = 'text' name = 'cl' value = '1'>"
+               form = form + newLine
+                form = form + \
+                "Show siblings: <select name = 's'>"+\
+                "<option value = '0'>No</option>"+\
+                "<option value = '1'>Yes</option>"+\
+               "</select>"
+               form = form + newLine
+                form = form + \
+                "Show spouse's other spouses: <select name = 'os'>"+\
+                "<option value = '0'>No</option>"+\
+                "<option value = '1'>Yes</option>"+\
+                "</select>"
+               form = form + newLine
+                form = form + \
+                "Show parents' other spouses: <select name = 'pos'>"+\
+                "<option value = '0'>No</option>"+\
+                "<option value = '1'>Yes</option>"+\
+                "</select>"            
+               form = form + newLine
+                form = form + \
+               "Fount size: "+\
+                "<input type = 'text' name = 'fs' value='8'>"
+                form = form + newLine
+               form = form + "</form>"
+
+               graph =  "smallGraph.py?ID="+str(personID)+"&fs=8"
+
+               graph = "<img src ="+ graph + '>'
+
+               output = output + "<div id = 'graph' style = 'clear:both'>"
+               output = output + "<p id = 'agraph'>"+graph+"</p>"
+               output = output + "Draw this graph with more relatives:"
+               output = output + newLine + form
+               
+               output = output + "<button onclick='myFunction()'>"+\
+                       "Go</button>"
+
+               output = output + "</div>"
+
+               output = output +\
+               '<script>'+\
+               'function myFunction()'+\
+               '{'+\
+               'var x = document.getElementById("controlForm");'+\
+               'var txt = "<img src = " + x.action + "?";'+\
+               'for (var i=0;i<x.length;i++)'+\
+               '{'+\
+               'var n=x.elements[i].name;'+\
+               'var v=x.elements[i].value;'+\
+               'txt = txt + "&"+n+"="+v;'+\
+               '}'+\
+               'txt = txt + ">";'+\
+               'document.getElementById("agraph").innerHTML=txt;'+\
+               '}'+\
+               '</script>'
+
+       else:
+               output = mainDiv
 
        return output
 
 def connect():
        global conn
-       conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
+       conn = sqlite3.connect\
+               ('/home/naath/familyTreeProject/familyTree/tree.db')
        return conn
 
 def make_cursor():
@@ -465,13 +1170,3 @@ def make_cursor():
 def close(conn):
        conn.close
 
-#def main():
-
-#      [c, conn] = connect()   
-#
-#      person_info(1,c)
-#      person_info(17,c)
-#      person_info(38,c)
-#      person_info(90,c)
-#
-#      close(conn)