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

Source Code for Module gavo.web.productrender

 1  """ 
 2  Code dealing with product (i.e., fits file) delivery. 
 3  """ 
 4   
 5  #c Copyright 2008-2019, the GAVO project 
 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 nevow import inevow 
12   
13  from gavo import base 
14  from gavo import svcs 
15  from gavo.protocols import products 
16  from gavo.web import grend 
17   
18   
19 -class ProductRenderer(grend.ServiceBasedPage):
20 """The renderer used for delivering products. 21 22 This will only work with a ProductCore since the resulting 23 data set has to contain products.Resources. Thus, you probably 24 will not use this in user RDs. 25 """ 26 name = "get" 27 pathFromSegments = "" 28
29 - def renderHTTP(self, ctx):
30 request = inevow.IRequest(ctx) 31 try: 32 data = {"accref": 33 products.RAccref.fromRequest(self.pathFromSegments, request)} 34 except base.NotFoundError: 35 raise base.ui.logOldExc(svcs.UnknownURI("No product specified")) 36 37 # deferring here and going to all the trouble of running a core 38 # is probably overkill; currently, the main thing that'd require 39 # handwork is figuring out authentication in parallel with what 40 # needs to be done for tar files. We should do better, indded. 41 return self.runServiceWithFormalData(data, ctx 42 ).addCallback(self._deliver, ctx)
43
44 - def _deliver(self, result, ctx):
45 doPreview = result.queryMeta.ctxArgs.get("preview") 46 # SvcResult madness; clean up SvcResult and remove this. 47 result = getattr(result, "original", result) 48 product = result[0] 49 request = inevow.IRequest(ctx) 50 51 # TODO: figure out a good way to see whether what we've got already is a 52 # preview. 53 if doPreview and not "Preview" in product.__class__.__name__: 54 return products.PreviewCacheManager.getPreviewFor(product 55 ).addCallback(self._deliverPreview, product, request 56 ).addErrback(self._deliverPreviewFailure, request) 57 58 else: 59 return product
60
61 - def _deliverPreview(self, content, product, request):
62 previewMime = product.pr["preview_mime"] or "image/jpeg" 63 request.setHeader("content-type", str(previewMime)) 64 request.setHeader("content-length", len(content)) 65 return content
66
67 - def _deliverPreviewFailure(self, failure, request):
68 failure.printTraceback() 69 data = "Not an image (preview generation failed, please report)" 70 request.setResponseCode(500) 71 request.setHeader("content-type", "text/plain") 72 request.setHeader("content-length", str(len(data))) 73 return data
74
75 - def locateChild(self, ctx, segments):
76 if segments: 77 self.pathFromSegments = "/".join(segments) 78 return self, ()
79