Debian Codegraph @

gavo/web/customrender.py

raw
1 """
2 User-defined renderers.
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 gavo import svcs
12 from gavo.web import grend
13
14
15 class CustomRenderer(grend.ServiceBasedPage):
16 """A renderer defined in a python module.
17
18 To define a custom renderer write a python module and define a
19 class MainPage inheriting from gavo.web.ServiceBasedPage.
20
21 This class basically is a gavo.formal.nevowc TemplatedPage, i.e., you
22 can define loader, getChild, render, and so on.
23
24 To use it, you have to define a service with the resdir-relative path
25 to the module in the customPage attribute and probably a nullCore. You
26 also have to allow the custom renderer (but you may have other renderers,
27 e.g., static).
28
29 If the custom page is for display in web browsers, define a
30 class method isBrowseable(cls, service) returning true. This is
31 for the generation of links like "use this service from your browser"
32 only; it does not change the service's behaviour with your renderer.
33
34 In general, avoid custom renderers. If you can't, see the upstream
35 twisted documentation on twisted.web.resource for how to write them.
36 """
37 name = "custom"
38
39 def __init__(self, request, service):
40 grend.ServiceBasedPage.__init__(self, request, service)
41 if not service.customPage:
42 raise svcs.UnknownURI("No custom page defined for this service.")
43 pageClass, self.reloadInfo = service.customPageCode
44 self.realPage = pageClass(request, service)
45
46 @classmethod
47 def isBrowseable(self, service):
48 return getattr(service, "customPageCode", None
49 ) and service.customPageCode[0].isBrowseable(service)
50
51 def render(self, request):
52 return self.realPage.render(request)
53
54 def getChild(self, name, request):
55 return self.realPage.getChild(name, request)