| 1 |
""" |
| 2 |
Renderer not reacting too strongly on user input. |
| 3 |
|
| 4 |
There's StaticRender just delivering certain files from within a service, |
| 5 |
and there's FixedPageRenderer that just formats a defined template. |
| 6 |
""" |
| 7 |
|
| 8 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 9 |
#c |
| 10 |
#c This program is free software, covered by the GNU GPL. See the |
| 11 |
#c COPYING file in the source distribution. |
| 12 |
|
| 13 |
|
| 14 |
import os |
| 15 |
|
| 16 |
from twisted.web import server |
| 17 |
from twisted.web import static |
| 18 |
|
| 19 |
from gavo import base |
| 20 |
from gavo import svcs |
| 21 |
from gavo.web import grend |
| 22 |
from gavo.web import ifpages |
| 23 |
|
| 24 |
|
| 25 |
class StaticRenderer(grend.ServiceBasedPage): |
| 26 |
"""A renderer that just hands through files. |
| 27 |
|
| 28 |
The standard operation here is to set a staticData property pointing |
| 29 |
to a resdir-relative directory used to serve files for. Indices |
| 30 |
for directories are created. |
| 31 |
|
| 32 |
You can define a root resource by giving an indexFile property on |
| 33 |
the service. Note in particular that you can use an index file |
| 34 |
with an extension of shtml. This lets you use nevow templates, but |
| 35 |
since metadata will be taken from the global context, that's |
| 36 |
probably not terribly useful. You are probably looking for the fixed |
| 37 |
renderer if you find yourself needing this. |
| 38 |
""" |
| 39 |
name = "static" |
| 40 |
defaultType = "text/html" |
| 41 |
|
| 42 |
def __init__(self, request, service): |
| 43 |
grend.ServiceBasedPage.__init__(self, request, service) |
| 44 |
try: |
| 45 |
self.indexFile = os.path.join(service.rd.resdir, |
| 46 |
service.getProperty("indexFile")) |
| 47 |
except base.NotFoundError: |
| 48 |
self.indexFile = None |
| 49 |
try: |
| 50 |
self.staticPath = os.path.join(service.rd.resdir, |
| 51 |
service.getProperty("staticData")) |
| 52 |
except base.NotFoundError: |
| 53 |
self.staticPath = None |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def isBrowseable(self, service): |
| 57 |
return service.getProperty("indexFile", None) |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def isCacheable(cls, segments, request): |
| 61 |
return False |
| 62 |
|
| 63 |
def render(self, request): |
| 64 |
if not request.uri.endswith(b"/"): |
| 65 |
# only render these as directories |
| 66 |
raise svcs.WebRedirect(request.uri+b"/") |
| 67 |
|
| 68 |
if self.indexFile: |
| 69 |
try: |
| 70 |
return ifpages.StaticFile(self.indexFile, self.rd).render(request) |
| 71 |
except os.error as ex: |
| 72 |
raise base.NotFoundError(self.indexFile, "File", "file system", |
| 73 |
hint=str(ex)) |
| 74 |
else: |
| 75 |
if self.staticPath is None: |
| 76 |
raise svcs.ForbiddenURI("No static data on this service") |
| 77 |
|
| 78 |
return ifpages.DirectoryLister(self.staticPath).render(request) |
| 79 |
|
| 80 |
def getChild(self, name, request): |
| 81 |
segments = request.popSegments(name) |
| 82 |
if segments==[""]: |
| 83 |
return self |
| 84 |
|
| 85 |
if self.staticPath is None: |
| 86 |
raise svcs.ForbiddenURI("No static data on this service") |
| 87 |
|
| 88 |
relPath = "/".join(segments) |
| 89 |
destName = os.path.join(self.staticPath, relPath) |
| 90 |
|
| 91 |
# make sure destName actually is below staticPath so stupid |
| 92 |
# tricks with .. or similar don't work on us |
| 93 |
if not os.path.abspath(destName).startswith( |
| 94 |
os.path.abspath(self.staticPath)): |
| 95 |
raise svcs.ForbiddenURI("%s is not located below staticData"% |
| 96 |
destName) |
| 97 |
|
| 98 |
if os.path.isdir(destName): |
| 99 |
if not destName.endswith("/"): |
| 100 |
raise svcs.WebRedirect(request.uri+b"/") |
| 101 |
return static.File(destName).directoryListing() |
| 102 |
|
| 103 |
elif os.path.isfile(destName): |
| 104 |
return ifpages.StaticFile(destName, self.rd, |
| 105 |
defaultType=self.defaultType) |
| 106 |
|
| 107 |
else: |
| 108 |
raise svcs.UnknownURI("No %s available here."%relPath) |
| 109 |
|
| 110 |
|
| 111 |
class HiPSRenderer(StaticRenderer): |
| 112 |
"""A static renderer with a few amenities for HiPS trees. |
| 113 |
|
| 114 |
To make this work, set the service's staticData property |
| 115 |
""" |
| 116 |
name = "hips" |
| 117 |
# for the benefit of properties |
| 118 |
defaultType = "text/plain" |
| 119 |
urlUse = "base" |
| 120 |
|
| 121 |
def __init__(self, request, service): |
| 122 |
super().__init__(request, service) |
| 123 |
self.indexFile = os.path.join(self.staticPath, "index.html") |
| 124 |
|
| 125 |
@classmethod |
| 126 |
def isCacheable(cls, segments, request): |
| 127 |
return False |
| 128 |
|
| 129 |
@classmethod |
| 130 |
def isBrowseable(self, service): |
| 131 |
return True |
| 132 |
|
| 133 |
|
| 134 |
class FixedPageRenderer( |
| 135 |
grend.CustomTemplateMixin, |
| 136 |
grend.ServiceBasedPage, |
| 137 |
grend.HTMLResultRenderMixin): |
| 138 |
"""A renderer that renders a single template. |
| 139 |
|
| 140 |
Use something like ``<template key="fixed">res/ft.html</template>`` |
| 141 |
in the enclosing service to tell the fixed renderer where to get |
| 142 |
this template from. |
| 143 |
|
| 144 |
In the template, you can fetch parameters from the URL using |
| 145 |
something like ``<n:invisible n:data="parameter FOO" n:render="string"/>``; |
| 146 |
you can also define new render and data functions on the |
| 147 |
service using customRF and customDF. |
| 148 |
|
| 149 |
This is, in particular, used for the data center's root page. |
| 150 |
|
| 151 |
The fixed renderer is intended for non- or slowly changing content. |
| 152 |
It is annotated as cacheable, which means that DaCHS will in general |
| 153 |
only render it once and then cache it. If the render functions |
| 154 |
change independently of the RD, use the volatile renderer. |
| 155 |
|
| 156 |
During development, users must add ?nocache=True to a fixed page |
| 157 |
URI to force DaCHS to reload the template. |
| 158 |
|
| 159 |
Built-in services for such browser apps should go through the //run |
| 160 |
RD. |
| 161 |
""" |
| 162 |
name = "fixed" |
| 163 |
|
| 164 |
def __init__(self, request, service): |
| 165 |
grend.ServiceBasedPage.__init__(self, request, service) |
| 166 |
self.table = None |
| 167 |
self.customTemplate = None |
| 168 |
try: |
| 169 |
self.customTemplate = self.service.getTemplate( |
| 170 |
self.name, "nocache" in request.strargs) |
| 171 |
except KeyError: |
| 172 |
raise base.ui.logOldExc( |
| 173 |
svcs.UnknownURI("%s renderer needs a '%s' template"%( |
| 174 |
self.name, self.name))) |
| 175 |
|
| 176 |
def _formatOutput(self, result, request): |
| 177 |
self.table = result and result.getPrimaryTable() |
| 178 |
grend.ServiceBasedPage.render(self, request) |
| 179 |
|
| 180 |
def render(self, request): |
| 181 |
self.runAsync(request.strargs |
| 182 |
).addCallback( |
| 183 |
self._formatOutput, request |
| 184 |
).addErrback(svcs.renderDCErrorPage, request) |
| 185 |
return server.NOT_DONE_YET |
| 186 |
|
| 187 |
def data_result(self, request, tag): |
| 188 |
return self.table |
| 189 |
|
| 190 |
def data_parameter(self, parName): |
| 191 |
"""lets you insert an URL parameter into the template. |
| 192 |
|
| 193 |
Non-existing parameters are returned as an empty string. |
| 194 |
""" |
| 195 |
def getParameter(request, tag): |
| 196 |
val = request.strargs.get(parName, [""])[0] |
| 197 |
if isinstance(val, bytes): |
| 198 |
val = val.decode("utf-8", "ignore") |
| 199 |
else: |
| 200 |
val = str(val) |
| 201 |
return val |
| 202 |
return getParameter |
| 203 |
|
| 204 |
@classmethod |
| 205 |
def isCacheable(cls, segments, request): |
| 206 |
return True |
| 207 |
|
| 208 |
@classmethod |
| 209 |
def isBrowseable(self, service): |
| 210 |
return True |
| 211 |
|
| 212 |
|
| 213 |
class VolatilePageRenderer(FixedPageRenderer): |
| 214 |
"""A renderer rendering a single template with fast-changing results. |
| 215 |
|
| 216 |
This is like the fixed renderer, except that the results are not cached. |
| 217 |
""" |
| 218 |
name = "volatile" |
| 219 |
|
| 220 |
@classmethod |
| 221 |
def isCacheable(cls, segments, request): |
| 222 |
return False |