| 1 |
""" |
| 2 |
Renderers supporting upload cores. |
| 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 template |
| 12 |
from twisted.web.template import tags as T |
| 13 |
|
| 14 |
from gavo import base |
| 15 |
from gavo.svcs import weberrors |
| 16 |
from gavo.web import formrender |
| 17 |
|
| 18 |
|
| 19 |
class Uploader(formrender.FormRenderer): |
| 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. And actually, you probably don't want |
| 24 |
to use this at all. See :samplerd:`citigbot/q` for how Markus does |
| 25 |
this kind of thing today. |
| 26 |
""" |
| 27 |
|
| 28 |
name = "upload" |
| 29 |
|
| 30 |
@template.renderer |
| 31 |
def uploadInfo(self, request, tag): |
| 32 |
data = tag.slotData |
| 33 |
if data is None: |
| 34 |
return T.transparent() |
| 35 |
else: |
| 36 |
for key, val in data.getPrimaryTable().rows[0].items(): |
| 37 |
tag.fillSlots(key, str(val)) |
| 38 |
return tag |
| 39 |
|
| 40 |
loader = template.TagLoader(T.html[ |
| 41 |
T.head[ |
| 42 |
T.title["Upload"], |
| 43 |
T.transparent(render="commonhead"), |
| 44 |
], |
| 45 |
T.body(render="withsidebar")[ |
| 46 |
T.h1(render="meta")["title"], |
| 47 |
T.p(class_="procMessage", data="result", |
| 48 |
render="uploadInfo")[ |
| 49 |
template.slot(name="nAffected"), |
| 50 |
" record(s) modified." |
| 51 |
], |
| 52 |
T.transparent(render="form genForm") |
| 53 |
] |
| 54 |
]) |
| 55 |
|
| 56 |
|
| 57 |
class MachineUploader(Uploader): |
| 58 |
"""A renderer allowing for updates to individual records using file |
| 59 |
uploads. |
| 60 |
|
| 61 |
The difference to Uploader is that no form-redisplay will be done. |
| 62 |
All errors are reported through HTTP response codes and text strings. |
| 63 |
It is likely that this renderer will change and/or go away. |
| 64 |
""" |
| 65 |
|
| 66 |
name = "mupload" |
| 67 |
|
| 68 |
def _handleInputErrors(self, failure, request): |
| 69 |
request.setResponseCode(500) |
| 70 |
request.setHeader("content-type", "text/plain;charset=utf-8") |
| 71 |
request.write(failure.getErrorMessage().encode("utf-8")) |
| 72 |
request.finish() |
| 73 |
base.ui.notifyFailure(failure) |
| 74 |
return weberrors.Silence() |
| 75 |
|
| 76 |
def _notifyNonModified(self, data, request): |
| 77 |
request.setResponseCode(400) |
| 78 |
request.setHeader("content-type", "text/plain;charset=utf-8") |
| 79 |
request.write(("Uploading %s did not change data database.\nThis" |
| 80 |
" usually happens when the file already existed for an insert" |
| 81 |
" or did not exist for an update.\n"%( |
| 82 |
self.queryMeta["inputTable"].getParamDict()["File"][0], |
| 83 |
)).encode("utf-8")) |
| 84 |
request.finish() |
| 85 |
|
| 86 |
def _formatOutput(self, data, request): |
| 87 |
numAffected = data.getPrimaryTable().rows[0]["nAffected"] |
| 88 |
if numAffected==0: |
| 89 |
return self._notifyNonModified(data, request) |
| 90 |
request.setResponseCode(200) |
| 91 |
request.setHeader("content-type", "text/plain;charset=utf-8") |
| 92 |
request.write(("%s uploaded, %d records modified\n"%( |
| 93 |
self.queryMeta["inputTable"].getParamDict()["File"][0], |
| 94 |
numAffected)).encode("utf-8")) |
| 95 |
request.finish() |