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

Source Code for Module gavo.web.uploadservice

 1  """ 
 2  Renderers supporting upload cores. 
 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  from nevow import loaders 
13  from nevow import tags as T 
14   
15  from gavo import base 
16  from gavo.web import formrender 
17   
18   
19 -class Uploader(formrender.Form):
20 """A renderer allowing for updates to individual records using file upload. 21 22 This renderer exposes a form with a file widget. It is likely that 23 the interface will change. 24 """ 25 26 name = "upload" 27
28 - def render_uploadInfo(self, ctx, data):
29 if data is None: 30 return T.invisible() 31 else: 32 for key, val in data.original.getPrimaryTable().rows[0].iteritems(): 33 ctx.tag.fillSlots(key, str(val)) 34 return ctx.tag
35 36 docFactory = loaders.stan(T.html[ 37 T.head[ 38 T.title["Upload"], 39 T.invisible(render=T.directive("commonhead")), 40 ], 41 T.body(render=T.directive("withsidebar"))[ 42 T.h1(render=T.directive("meta"))["title"], 43 T.p(class_="procMessage", data=T.directive("result"), 44 render=T.directive("uploadInfo"))[ 45 T.slot(name="nAffected"), 46 " record(s) modified." 47 ], 48 T.invisible(render=T.directive("form genForm")) 49 ] 50 ])
51 52
53 -class MachineUploader(Uploader):
54 """A renderer allowing for updates to individual records using file 55 uploads. 56 57 The difference to Uploader is that no form-redisplay will be done. 58 All errors are reported through HTTP response codes and text strings. 59 It is likely that this renderer will change and/or go away. 60 """ 61 62 name = "mupload" 63
64 - def _handleInputErrors(self, failure, ctx):
65 request = inevow.IRequest(ctx) 66 request.setResponseCode(500) 67 request.setHeader("content-type", "text/plain;charset=utf-8") 68 request.write(failure.getErrorMessage().encode("utf-8")) 69 base.ui.notifyFailure(failure) 70 return ""
71
72 - def _notifyNonModified(self, data, ctx):
73 request = inevow.IRequest(ctx) 74 request.setResponseCode(400) 75 request.setHeader("content-type", "text/plain;charset=utf-8") 76 request.write(("Uploading %s did not change data database.\nThis" 77 " usually happens when the file already existed for an insert" 78 " or did not exist for an update.\n"%( 79 data.inputTable.getParamDict()["File"][0], 80 )).encode("utf-8")) 81 return ""
82
83 - def _formatOutput(self, data, ctx):
84 numAffected = data.original.getPrimaryTable().rows[0]["nAffected"] 85 if numAffected==0: 86 return self._notifyNonModified(data, ctx) 87 request = inevow.IRequest(ctx) 88 request.setResponseCode(200) 89 request.setHeader("content-type", "text/plain;charset=utf-8") 90 request.write(("%s uploaded, %d records modified\n"%( 91 data.inputTable.getParamDict()["File"][0], 92 numAffected)).encode("utf-8")) 93 return ""
94