Package gavo :: Package web :: Module jsonquery
[frames] | no frames]

Source Code for Module gavo.web.jsonquery

  1  """ 
  2  Fast Web->DB->JSON machinery, for now only for the portal page.  It's 
  3  not quite clear to me yet what to do with this kind of thing. 
  4   
  5  It would seem we could do with more of these very lightweight 
  6  things; on the other hand, these bypass all metadata mangement... 
  7  ah, let's see. 
  8  """ 
  9   
 10  #c Copyright 2008-2019, the GAVO project 
 11  #c 
 12  #c This program is free software, covered by the GNU GPL.  See the 
 13  #c COPYING file in the source distribution. 
 14   
 15   
 16  import json 
 17   
 18  from nevow import rend 
 19  from nevow import inevow 
 20   
 21  from gavo import base 
 22   
 23   
 24  # these are the fields necessary for formatting resource headers 
 25  RESMETA_FIELDS = ("title, accessurl, referenceurl," 
 26          " sourcerd, resid, owner, browseable") 
 27   
 28   
29 -class JSONQuery(rend.Page):
30 """A resource returning Json for the database query given in the 31 query class attribute (and potentially some arguments). 32 33 TODO: we should do some more sensible error handling. 34 """
35 - def renderHTTP(self, ctx):
36 queryArgs = dict((key, value[0]) 37 for key, value in inevow.IRequest(ctx).args.iteritems()) 38 39 with base.getTableConn() as conn: 40 res = list(conn.queryToDicts( 41 self.query, queryArgs)) 42 43 request = inevow.IRequest(ctx) 44 request.setHeader("content-type", "text/json") 45 return json.dumps(res)
46 47
48 -class Titles(JSONQuery):
49 query = ( 50 "SELECT "+RESMETA_FIELDS+ 51 " FROM dc.resources" 52 " NATURAL JOIN dc.interfaces" 53 " NATURAL JOIN dc.sets" 54 " WHERE setname='local'" 55 " AND NOT deleted" 56 " ORDER BY title")
57 58
59 -class Subjects(JSONQuery):
60 query = ( 61 "SELECT subject, count(*) as numMatch" 62 " FROM dc.subjects" 63 " NATURAL JOIN dc.sets" 64 " WHERE setname='local'" 65 " AND NOT deleted" 66 " GROUP BY subject" 67 " ORDER BY subject")
68 69
70 -class Authors(JSONQuery):
71 query = ( 72 "SELECT author, count(*) as numMatch" 73 " FROM dc.authors" 74 " NATURAL JOIN dc.sets" 75 " WHERE setname='local'" 76 " AND NOT deleted" 77 " GROUP BY author" 78 " ORDER BY author")
79 80
81 -class ByFulltext(JSONQuery):
82 query = ( 83 "SELECT DISTINCT "+RESMETA_FIELDS+ 84 " FROM dc.resources" 85 " NATURAL JOIN dc.interfaces" 86 " NATURAL JOIN dc.subjects" 87 " NATURAL JOIN dc.sets" 88 " WHERE setname='local'" 89 " AND NOT deleted" 90 " AND (to_tsvector('english', description) || to_tsvector(subject) " 91 " || to_tsvector('english', title) || to_tsvector(authors))" 92 " @@ plainto_tsquery(%(q)s)" 93 " ORDER BY title")
94 95
96 -class BySubject(JSONQuery):
97 query = ( 98 "SELECT "+RESMETA_FIELDS+ 99 " FROM dc.resources" 100 " NATURAL JOIN dc.interfaces" 101 " NATURAL JOIN dc.subjects" 102 " NATURAL JOIN dc.sets" 103 " WHERE setname='local'" 104 " AND subject=%(subject)s" 105 " AND NOT deleted" 106 " ORDER BY title")
107 108
109 -class ByAuthor(JSONQuery):
110 query = ( 111 "SELECT "+RESMETA_FIELDS+ 112 " FROM dc.resources" 113 " NATURAL JOIN dc.interfaces" 114 " NATURAL JOIN dc.authors" 115 " NATURAL JOIN dc.sets" 116 " WHERE setname='local'" 117 " AND author=%(author)s" 118 " AND NOT deleted" 119 " ORDER BY title")
120 121
122 -class ServiceInfo(JSONQuery):
123 query = ( 124 "SELECT title, description, authors," 125 " to_char(dateUpdated, 'YYYY-MM-DD') as lastupdate," 126 " referenceURL, accessURL" 127 " FROM dc.interfaces" 128 " NATURAL JOIN dc.sets" 129 " RIGHT OUTER JOIN dc.resources USING (sourcerd, resid)" 130 " WHERE setname='local'" 131 " AND resId=%(resId)s and sourceRd=%(sourceRD)s")
132 133
134 -class PortalPage(rend.Page):
135 child_titles = Titles() 136 child_subjects = Subjects() 137 child_authors = Authors() 138 child_bySubject = BySubject() 139 child_byAuthor = ByAuthor() 140 child_byFulltext = ByFulltext() 141 child_serviceInfo = ServiceInfo()
142