| 1 |
""" |
| 2 |
A renderer that queries a single field in a service. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
from twisted.web import resource |
| 12 |
from twisted.web import server |
| 13 |
from twisted.web.template import tags as T |
| 14 |
|
| 15 |
from gavo import base |
| 16 |
from gavo import formal |
| 17 |
from gavo import svcs |
| 18 |
from gavo.svcs import streaming |
| 19 |
from gavo.web import common |
| 20 |
from gavo.web import grend |
| 21 |
from gavo.svcs import weberrors |
| 22 |
|
| 23 |
|
| 24 |
NT = formal.addNevowAttributes |
| 25 |
|
| 26 |
|
| 27 |
class VOTableResource(resource.Resource): |
| 28 |
# A quick hack to support VOTable responses. |
| 29 |
# Kill this in favour of serviceresults. |
| 30 |
def __init__(self, result, queryMeta): |
| 31 |
resource.Resource.__init__(self) |
| 32 |
self.result, self.queryMeta = result, queryMeta |
| 33 |
|
| 34 |
def render(self, request): |
| 35 |
if base.getMetaText(self.result.getPrimaryTable(), "_queryStatus" |
| 36 |
)=="OVERFLOW": |
| 37 |
fName = "truncated_votable.xml" |
| 38 |
else: |
| 39 |
fName = "votable.xml" |
| 40 |
request.setHeader("content-type", base.votableType) |
| 41 |
request.setHeader('content-disposition', |
| 42 |
'attachment; filename=%s'%fName) |
| 43 |
return streaming.streamVOTable(request, self.result, self.queryMeta) |
| 44 |
|
| 45 |
|
| 46 |
class QPRenderer(grend.HTMLResultRenderMixin, |
| 47 |
grend.CustomTemplateMixin, |
| 48 |
grend.ServiceBasedPage): |
| 49 |
"""The Query Path renderer extracts a query argument from the query path. |
| 50 |
|
| 51 |
Basically, whatever segments are left after the path to the renderer |
| 52 |
are taken and fed into the service. The service must cooperate by |
| 53 |
setting a queryField property which is the key the parameter is assigned |
| 54 |
to. |
| 55 |
|
| 56 |
QPRenderers cannot do forms, of course, but they can nicely share a |
| 57 |
service with the form renderer. |
| 58 |
|
| 59 |
To adjust the results' appreance, you can override resultline (for when |
| 60 |
there's just one result row) and resulttable (for when there is more |
| 61 |
than one result row) templates. In the templates, you can retrieve |
| 62 |
the input parameter's value as the inPar data, for instance, like this:: |
| 63 |
|
| 64 |
<n:invisible n:data="inPar" n:render="string"/> |
| 65 |
""" |
| 66 |
name = "qp" |
| 67 |
queryValue = None |
| 68 |
defaultQueryFormat = "HTML" |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def isCacheable(self, segments, request): |
| 72 |
return False # That's the default, but let's be sure here... |
| 73 |
|
| 74 |
def render(self, request): |
| 75 |
if not self.queryValue: |
| 76 |
raise svcs.UnknownURI("This page is a root page for a" |
| 77 |
" query-based service. You have to give a valid value in the" |
| 78 |
" path.") |
| 79 |
|
| 80 |
args = request.strargs.copy() |
| 81 |
args[self.service.getProperty("queryField")] = [self.queryValue] |
| 82 |
self.runAsync(args |
| 83 |
).addCallback(self._formatOutput, request |
| 84 |
).addErrback(self._handleError, request |
| 85 |
).addErrback(weberrors.renderDCErrorPage, request) |
| 86 |
return server.NOT_DONE_YET |
| 87 |
|
| 88 |
def _formatOutput(self, res, request): |
| 89 |
self.result = res |
| 90 |
|
| 91 |
# Hm... can we get rid of this hack? Just support proper RESPONSEFORMAT, |
| 92 |
# perhaps? |
| 93 |
if "vot" in self.queryMeta.get("format", "").lower(): |
| 94 |
return VOTableResource(self.result, self.queryMeta).render(request) |
| 95 |
|
| 96 |
# Hm... I suppose this and the corresponding stuff in |
| 97 |
# UnifiedDALRenderer should be merged. |
| 98 |
if isinstance(self.result, tuple): |
| 99 |
# core returned a complete document (mime and string) |
| 100 |
mime, payload = self.result |
| 101 |
request.setHeader("content-type", mime) |
| 102 |
return streaming.streamOut( |
| 103 |
lambda f: f.write(payload), request, self.queryMeta) |
| 104 |
|
| 105 |
nMatched = self.queryMeta.get("Matched") |
| 106 |
if nMatched==0: |
| 107 |
raise svcs.UnknownURI("No record matching %s."%( |
| 108 |
self.queryValue)) |
| 109 |
elif nMatched==1: |
| 110 |
self.customTemplate = self.getTemplate("resultline") |
| 111 |
else: |
| 112 |
self.customTemplate = self.getTemplate("resulttable") |
| 113 |
return super(QPRenderer, self).render(request) |
| 114 |
|
| 115 |
def _handleError(self, flr, request): |
| 116 |
# all errors except svcs.Errors (which are handled by weberrors) |
| 117 |
# are translated to 404s (and logged). |
| 118 |
if isinstance(flr.value, (base.ExecutiveAction, svcs.Error)): |
| 119 |
return flr |
| 120 |
|
| 121 |
base.ui.notifyFailure(flr, "A core raised '%s' into a qp renderer." |
| 122 |
" Translating into a 404."%(flr.value)) |
| 123 |
raise svcs.UnknownURI("The query initiated by your URL failed," |
| 124 |
" yielding a message '%s'."%flr.getErrorMessage()) |
| 125 |
|
| 126 |
def data_inPar(self, request, tag): |
| 127 |
return self.queryValue |
| 128 |
|
| 129 |
def getChild(self, name, request): |
| 130 |
# if we're here, we are the responsible resource and just stuff |
| 131 |
# the remaining segments into the query value |
| 132 |
self.queryValue = "/".join(request.popSegments(name)) |
| 133 |
return self |
| 134 |
|
| 135 |
def getTemplate(self, resultFormat): |
| 136 |
if resultFormat in self.service.templates: |
| 137 |
return self.service.getTemplate(resultFormat) |
| 138 |
return common.doctypedStan( |
| 139 |
T.html[ |
| 140 |
T.head(render="commonhead")[ |
| 141 |
T.title(render="meta")['title'], |
| 142 |
T.style(type="text/css", render="servicestyle"),], |
| 143 |
T.body(render="withsidebar")[ |
| 144 |
T.h1(render="meta")['title'], |
| 145 |
NT(T.div, data="result")(class_="result") [ |
| 146 |
T.transparent(render=resultFormat)]]]) |