chiark / gitweb /
changed graphs to manual dot file; added pictures; faffed about with text
[familyTree.git] / familyTree / askQuestion.py
index d8414412ff2ce187223096dc5bb7470ba7a615b8..841df2f2fc0d69f368e4b43b42bcf1744d089ae6 100755 (executable)
@@ -5,7 +5,13 @@ import findYear
 from string import Template
 
 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()
        return c.execute(s,t)
@@ -39,15 +45,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):
@@ -63,7 +108,7 @@ def name_html(row,html):
                        script = "person.py?ID=" + str(row[1])
                        name = row[0]
                        return link_Template.substitute(script = script\
-                               , text = name)
+                               ,title = add_quotes(name),text = name)
                else:
                        return row[0] + "," +str(row[1])
 
@@ -77,7 +122,7 @@ def print_age_child_count(row,newLine):
        if newLine == '<br>':
                script = "age.py?age="+str(row[0])
                link = link_Template.substitute(script = \
-                       script, text = row[0])
+                       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
@@ -88,7 +133,8 @@ def print_age_child_count(row,newLine):
 def print_age_death_count(row,newLine):
        if newLine =='<br>':
                script = "ageDeath.py?age="+str(row[0])
-               link = link_Template.substitute(script = script,text = 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
@@ -99,7 +145,7 @@ def print_name_count(row,newLine):
        if newLine=='<br>':
                script = "name.py?name=" + row[0]
                link = link_Template.substitute(script =\
-                       script, text = row[0])
+                       script, title = add_quotes(row[0]),text = row[0])
                return str(row[1]) + " people called "+link + newLine
        else:
                return print_row(row,newLine)   
@@ -168,7 +214,7 @@ 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():
@@ -206,8 +252,9 @@ def list_people_parents():
                        if r[0]!=None:
                                spouses.append(r[0]+ ' '+str(r[1]))
                        else:
-                               spouses.append(r[1] + ' s' +\
-                               str(row[1]))
+                               if len(r[1])>0:
+                                       spouses.append(r[1]  + ' s' +\
+                                       str(row[1]))
 
 
                myName = row[0]
@@ -315,14 +362,45 @@ def count_names(newLine):
 
        return out
 
+
+def search_name(name,newLine):
+       s = "SELECT name, ID"\
+        +" FROM people"\
+        +" WHERE name LIKE ?;"
+
+        out = ''
+
+       out = out + 'Names start with ' + name + ':' + newLine
+       t = (name + '%',)
+       fullIDs=[]
+       for row in run_query(s,t):
+                out = out + name_html(row,newLine) + newLine
+               fullIDs.append(row[1])
+        t = ('%' + name + '%',)
+       out = out+newLine + 'Names contain ' + name + ':' + newLine
+        for row in run_query(s,t):
+               if row[1] not in fullIDs:
+                       out = out + name_html(row,newLine) + newLine
+       
+       s = "SELECT name,people.ID,style"\
+       +" FROM people INNER JOIN styles"\
+       +" ON styles.id = people.id"\
+       +" WHERE style LIKE ?;"
+       out = out +newLine+ 'Styles contain ' + name + ':' + newLine
+       for row in run_query(s,t):
+               out = out + name_html(row,newLine)+' ' + row[2] + newLine
+
+        return out
+
+
 def people_with_name(name,newLine):
-       s = "SELECT Name, ID"\
+       s = "SELECT name, ID"\
        +" FROM people"\
-       +" WHERE Name LIKE ?;"
+       +" WHERE firstname LIKE ?;"
 
        out = ''
 
-       t = (name + '%',)
+       t = (name,)
 
        for row in run_query(s,t):
                out = out + name_html(row,newLine) + newLine
@@ -448,27 +526,33 @@ def all_ancestors(personID,newLine):
        for row in run_query(t,id):
                out = out + name_html(row,newLine)+newLine
 
+       aDict={}
+       aDict[level] = ancestors
        while len(ancestors)>0:
                level = level+1
                newA =[]
-               thisout = 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):
                                thisout = thisout + \
                                name_html(row,newLine)+newLine
-                               if row[1] not in allAncestors and \
-                               is_number(row[1])!=0:
+                               if row[1] not in allAncestors\
+                               and is_number(row[1]):
                                        newA.append(row[1])
                                        allAncestors.append(row[1])
                                        trackLevel.append(level)
+                               
                ancestors = newA
+               if len(ancestors)>0:
+                       aDict[level]=ancestors
                out  = out+thisout
 
 
        image = "<img src = ancestorGraph.py?id="+str(personID)+">"
        out = out+newLine + image+newLine
-       return [out, allAncestors,trackLevel]
+       return [out, allAncestors,trackLevel,aDict]
 
 
 def common_ancestors(IDA, IDB,newLine):
@@ -664,38 +748,52 @@ 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 person_info(personID,newLine):
        t = (personID,)
 
-       output = '';
-       
+       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
+               mainDiv = mainDiv + '<p>'
+               mainDiv = mainDiv  + 'ID: '+str(row[0]) +newLine
+               mainDiv = mainDiv + print_tagged_name('Name',[row[1], row[0]]\
+                       ,newLine)
+               mainDiv = mainDiv + '</p>'
                name = row[1]
-               output = output + 'Born: '+row[3] + newLine
+               url = row[9]
+               picture = row[10]
+
+               mainDiv = mainDiv + '<p>'
+               mainDiv = mainDiv + newLine + 'Born: '+row[3] + newLine
                bornYear = row[4]
-               output = output + 'Died: '+row[5] + ", aged " \
-                       +str(row[6]-row[4]) +newLine
+               mainDiv = mainDiv + 'Died: '+row[5]
+
+               if row[6] != 0 and row[4] !=0:
+                       mainDiv = mainDiv + ", aged " \
+                               +str(row[6]-row[4])
+               mainDiv = mainDiv + '</p>'
+
 
        s = "SELECT * FROM styles WHERE ID = ?"
        for row in run_query(s,t):
-               output = output +newLine+ 'Style: '+row[1] + newLine
+               mainDiv = mainDiv + '<p>'
+               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=?"
@@ -703,23 +801,33 @@ 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
+
+               mainDiv = mainDiv +  'From: '+row[2] + newLine
+                mainDiv = mainDiv +  'To: '+row[4]     
+
+               mainDiv = mainDiv + '</p>'
+
 
-               output = output +  'From: '+row[2] + newLine
-                output = output +  'To: '+row[4] + newLine
 
+
+       mainDiv = mainDiv + '<p>'
        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 + print_tagged_name('Consort',row,newLine)
+               mainDiv = mainDiv + print_tagged_name\
+               ('Consort of',row,newLine)
+       mainDiv = mainDiv + '</p>'
 
-       output = output + newLine
        #find parents
+       mainDiv = mainDiv + '<p>'
        s = "SELECT people.Name,parents.parentID FROM"\
                +" parents LEFT JOIN people"\
                +" ON parents.parentID = people.ID"\
@@ -727,13 +835,15 @@ def person_info(personID,newLine):
 
        parents =[]
        for row in run_query(s,t):
-               output = output + print_tagged_name('Parent',row,newLine)
+               mainDiv = mainDiv + print_tagged_name('Parent',row,newLine)
                if row[0]!=None:
-                       parents.append(row[0] + ' ' + row[1])
+                       parents.append(row[0] + ', ' + row[1])
                else:
-                       parents.append(row[1] + ' p' + personID)
-
+                       parents.append(row[1] + ', p' + personID)
+       mainDiv = mainDiv + '</p>'
        #find spouses
+
+       mainDiv = mainDiv + '<p>'
        s = "SELECT people.NAME, marriages.IDb from"\
                +" marriages LEFT JOIN people"\
                +" ON people.ID = marriages.IDb"\
@@ -742,12 +852,12 @@ def person_info(personID,newLine):
        spouses = []
        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) 
-               output = output + newLine
-                output = output + print_tagged_name('Spouse',row,newLine)
-               output = output + relationship_html(personID,row[1],newLine)
+                       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"\
@@ -756,14 +866,12 @@ def person_info(personID,newLine):
                +" ORDER BY IDa;"
        for row in run_query(s,t):    
                if row[0]!=None:
-                       spouses.append(row[0] + ' '+str(row[1]))
+                       spouses.append(row[0] + ', '+str(row[1]))
                else:
-                       spouses.append(row[1] + ' s' + personID)
-               output = output + newLine
-                output = output + print_tagged_name('Spouse',row,newLine)
-               output = output + relationship_html(personID,row[1],newLine)
-
-       output = output + newLine
+                       spouses.append(row[1] + ', s' + personID)
+                mainDiv = mainDiv + print_tagged_name('Spouse',row,newLine)
+               mainDiv = mainDiv + relationship_html(personID,row[1],newLine)
+       mainDiv  = mainDiv + '</p>'
 
        #find children
        s = "Select people.NAME, people.ID ,people.bornYear"\
@@ -774,13 +882,14 @@ def person_info(personID,newLine):
 
        children = []
        ops =[]
+       top = ''
        for row in run_query(s,t):
-               output = output  + print_tagged_name('Child',row,newLine)
-               children.append(row[0] + ' ' + str(row[1]))
+               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 INNER JOIN people"\
+                +" parents LEFT JOIN people"\
                 +" ON people.ID = parents.parentID"\
                 +" WHERE parents.ID = ? AND parents.parentID <>?;"
 
@@ -788,37 +897,69 @@ def person_info(personID,newLine):
 
                op = 0
                for r in run_query(u,ids):
-                       output = output + print_tagged_name('With',r,newLine)
                        op = 1
-                       ops.append(r[0] + ' ' + str(r[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)
+                       ops.append('?' + ', s' + personID)
+
+               if top!=ops[-1]:
+                       mainDiv = mainDiv + '</p>'
+                       mainDiv = mainDiv + '<p>'
+                       mainDiv = mainDiv + print_tagged_name('With',r, newLine)
+
+
+               top = ops[-1]
 
                #age when child born
                if row[2] !=0 and bornYear != 0:
                        age = row[2]-bornYear
-                       output = output[:-4] + " at the age of "+str(age) + newLine
+                       thisChild = thisChild[:-4] + \
+                               " at the age of "+str(age) + newLine
+               mainDiv = mainDiv + thisChild
+       mainDiv = mainDiv + '</p>'
 
+       output = '<div id = "main" style = " float:left">';
+       output = output + mainDiv +  "</div>"
 
-       Self = name +' ' + str(personID)
+       output = output + "<div id = 'image' "\
+               +"style = 'float:left; margin-left:20px'>"
 
-       image =  "smallGraph.py?Self="+Self
+       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>"
+
+       Self = name +', ' + str(personID)
+
+       graph =  "smallGraph.py?Self="+Self
        for p in parents:
-               image = image + '&p='+p
+               graph = graph + '&p='+p
        for c in children:
-               image = image + '&c='+c
+               graph = graph + '&c='+c
        for op in ops:
                if op !=None:
-                       image = image + '&op='+op
+                       graph = graph + '&op='+op
        for s in spouses:
                if s !=None:
-                       image = image + '&s='+str(s)
+                       graph = graph + '&s='+str(s)
 
-       image = image.replace(' ','%20')
-       image ="<img src ="+ image + '>'
+       graph = graph.replace(' ','%20')
+       graph ="<img src ="+ graph + '>'
 
-       
-       output = newLine+ output+ image+newLine
+       output = output + "<div id = 'graph' style = 'clear:both'>"
+       output = output +  graph
+       output = output + "</div>"