Module: check_mk
Branch: master
Commit: 100477d0c3f5bb4c2c1b2fa1083e057318b8960d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=100477d0c3f5bb…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Nov 19 16:56:10 2013 +0100
htmllib can now handle file uploads more comfortable ; added upload valuespec
---
web/htdocs/html_mod_python.py | 9 ++++++++-
web/htdocs/htmllib.py | 3 ++-
web/htdocs/valuespec.py | 21 +++++++++++++++++++++
3 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/web/htdocs/html_mod_python.py b/web/htdocs/html_mod_python.py
index 1a3b786..401b9bc 100644
--- a/web/htdocs/html_mod_python.py
+++ b/web/htdocs/html_mod_python.py
@@ -41,8 +41,9 @@ class html_mod_python(htmllib.html):
self.cookies = Cookie.get_cookies(self.req)
def read_get_vars(self):
- self.vars = {}
+ self.vars = {}
self.listvars = {} # for variables with more than one occurrance
+ self.uploads = {}
fields = util.FieldStorage(self.req, keep_blank_values = 1)
for field in fields.list:
varname = field.name
@@ -64,6 +65,12 @@ class html_mod_python(htmllib.html):
# URL simpler.
self.vars[varname] = value
+ # put uploaded file infos into separate storage
+ if field.filename is not None:
+ self.uploads[varname] = (field.filename, field.type, field.value)
+
+ def uploaded_file(self, varname, default = None):
+ return self.uploads.get(varname, default)
def lowlevel_write(self, text):
if self.io_error:
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index 3964300..0d21314 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -1241,4 +1241,5 @@ class html:
self.load_tree_states()
self.treestates[tree] = val
-
+ def uploaded_file(self, varname, default = None):
+ raise MKGeneralException("uploaded_file not implemented")
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 251df0b..ab287b1 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -2569,4 +2569,25 @@ class PasswordSpec(TextAscii):
html.icon_button("#", _(u"Randomize password"),
"random",
onclick="vs_passwordspec_randomize(this);")
+class FileUpload(ValueSpec):
+ def __init__(self, **kwargs):
+ ValueSpec.__init__(self, **kwargs)
+ self._allow_empty = kwargs.get('allow_empty', True)
+
+ def canonical_value(self):
+ if self._allow_empty:
+ return None
+ else:
+ return ''
+ def validate_value(self, value, varprefix):
+ if not self._allow_empty and value == None:
+ raise MKUserError(varprefix, _('Please select a file.'))
+
+ def render_input(self, varprefix, value):
+ html.upload_file(varprefix)
+
+ def from_html_vars(self, varprefix):
+ x = html.uploaded_file(varprefix)
+ html.debug(x)
+ return x