Add upload support
authorBernardo Innocenti <bernie@codewiz.org>
Tue, 12 Feb 2008 05:05:28 +0000 (15:05 +1000)
committerBernardo Innocenti <bernie@codewiz.org>
Tue, 12 Feb 2008 05:05:28 +0000 (15:05 +1000)
geekigeeki.py

index 764f3665de6fff91f4e773fc918cdae5adb65a7a..eef3d6c6736ff76f06e0b44f428f359a7eeab9ac 100755 (executable)
@@ -144,7 +144,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error'):
         print text + '</b> '
     print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink')
     print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
-    if globals().has_key('history_url'):
+    if 'history_url' in globals():
         print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
         if name:
             print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
@@ -227,17 +227,21 @@ def do_raw(pagename):
 
 def do_edit(pagename):
     pg = Page(pagename)
-    if 'preview' in form:
-        pg.send_editor(form['savetext'].value)
-    elif 'save' in form:
-        pg.save_text(form['savetext'].value)
+    if 'save' in form:
+        if form['file'].value:
+            pg.save(form['file'].file.read())
+        else:
+            pg.save(form['savetext'].value.replace('\r\n', '\n'))
         pg.send_page()
     elif 'cancel' in form:
         pg.msg_text = 'Editing canceled'
         pg.msg_type = 'notice'
         pg.send_page()
     else:
-        pg.send_editor()
+        text = None
+        if 'preview' in form:
+            text = form['savetext'].value
+        pg.send_editor(text)
 
 def make_index_key():
     links = map(lambda ch: '<a href="#%s">%s</a>' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz')
@@ -249,7 +253,7 @@ def page_list():
 def send_footer(name, mod_string=None):
     if globals().get('debug_cgi', False):
         cgi.print_arguments()
-        cgi.print_form(cgi.FieldStorage())
+        cgi.print_form(form)
         cgi.print_environ()
     print '<div id="footer"><hr />'
     print ('<p class="copyright"><span class="benchmark">generated in %0.3fs</span>' +
@@ -268,10 +272,9 @@ def _macro_FullSearch(*vargs):
     return _macro_search("fullsearch")
 
 def _macro_search(type):
-    if form.has_key('value'):
-        default = form["value"].value
-    else:
-        default = ''
+    default = ''
+    if 'value' in form:
+        default = form['value'].value
     return """<form method="get"><input name="%s" size="30" value="%s" /><input type="submit" value="Search" /></form>""" % (type, default)
 
 def _macro_WordIndex(*vargs):
@@ -591,7 +594,7 @@ class Page:
             raise er
 
     def get_attrs(self):
-        if self.__dict__.has_key('attrs'):
+        if 'attrs' in self.__dict__:
             return self.attrs
         self.attrs = {}
         try:
@@ -621,7 +624,8 @@ class Page:
                     return action in perms.split(',')
             return False
         except Exception, er:
-            pass
+            if acl:
+                self.msg_text = 'Illegal acl line: ' + acl
         return default
 
     def can_write(self):
@@ -662,13 +666,18 @@ class Page:
             send_guru("Write access denied by ACLs", "error")
             return
 
+        file = ''
+        if 'file' in form:
+            file = form['file'].value
+
         print ('<p><b>Editing ' + self.page_name
             + ' for ' + cgi.escape(remote_user())
             + ' from ' + cgi.escape(get_hostname(remote_host()))
             + '</b></p>')
-        print '<div class="editor"><form method="post" action="%s">' % relative_url(self.page_name)
+        print '<div class="editor"><form method="post" enctype="multipart/form-data" action="%s">' % relative_url(self.page_name)
         print '<input type="hidden" name="edit" value="%s">' % (self.page_name)
-        print """<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100">%s</textarea>""" % (preview or self.get_raw_body())
+        print '<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100">%s</textarea>' % (preview or self.get_raw_body())
+        print 'Or upload a file: <input type="file" name="file" value="%s" />' % file
         print """
             <br />
             <input type="submit" name="save" value="Save" />
@@ -692,25 +701,25 @@ class Page:
         else:
             send_title(None, msg_text='Read access denied by ACLs', msg_type='notice')
 
-    def _write_file(self, text):
+    def _write_file(self, data):
         tmp_filename = self._tmp_filename()
-        open(tmp_filename, 'wt').write(text.replace('\r\n', '\n'))
-        text = self._text_filename()
+        open(tmp_filename, 'wb').write(data)
+        name = self._text_filename()
         if os.name == 'nt':
             # Bad Bill!  POSIX rename ought to replace. :-(
             try:
-                os.remove(text)
+                os.remove(name)
             except OSError, er:
                 if er.errno != errno.ENOENT: raise er
-        os.rename(tmp_filename, text)
+        os.rename(tmp_filename, name)
 
-    def save_text(self, newtext):
+    def save(self, newdata):
         if not self.can_write():
             self.msg_text = 'Write access denied by ACLs'
             self.msg_type = 'error'
             return
 
-        self._write_file(newtext)
+        self._write_file(newdata)
         rc = 0
         if post_edit_hook:
             # FIXME: what's the std way to perform shell quoting in python?
@@ -743,7 +752,7 @@ try:
     }
 
     for cmd in handlers.keys():
-        if form.has_key(cmd):
+        if cmd in form:
             apply(handlers[cmd], (form[cmd].value,))
             break
     else:
@@ -759,6 +768,7 @@ try:
             else:
                 from mimetypes import MimeTypes
                 type, encoding = MimeTypes().guess_type(query)
+                type = type or 'text/plain'
                 Page(query).send_raw(mimetype=type)
         else:
             print "Status: 404 Not Found"