changeset 740:0ec051831d52

Add listAttachments, getAttachment, and putAttachment to the xmlrpc api. (Thanks to Matthew Gilbert)
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Mon, 05 Jun 2006 10:57:46 +0200
parents 8802d6935b6f
children 2c837d2c554e
files MoinMoin/xmlrpc/__init__.py docs/CHANGES
diffstat 2 files changed, 68 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/xmlrpc/__init__.py	Fri Jun 02 21:48:03 2006 +0200
+++ b/MoinMoin/xmlrpc/__init__.py	Mon Jun 05 10:57:46 2006 +0200
@@ -27,12 +27,13 @@
 
 modules = pysupport.getPackageModules(__file__)
 
-import sys, time, xmlrpclib
+import os, sys, time, xmlrpclib
 
 from MoinMoin import config, user, wikiutil
 from MoinMoin.Page import Page
 from MoinMoin.PageEditor import PageEditor
 from MoinMoin.logfile import editlog
+from MoinMoin.action import AttachFile
 
 _debug = 0
 
@@ -397,6 +398,71 @@
                  self._outstr(results.formatContext(hit, 180, 1)))
                 for hit in results.hits]
 
+    def xmlrpc_listAttachments(self, pagename):
+        """ Get all attachments associated with pagename
+        
+        @param pagename: pagename (utf-8)
+        @rtype: list
+        @return: a list of utf-8 attachment names
+        """    
+        pagename = self._instr(pagename)
+        # User may read page?
+        if not self.request.user.may.read(pagename):
+            return self.notAllowedFault()
+        
+        result = AttachFile._get_files(self.request, pagename)
+        return result
+        
+    def xmlrpc_getAttachment(self, pagename, attachname):
+        """ Get attachname associated with pagename
+        
+        @param pagename: pagename (utf-8)
+        @param attachname: attachment name (utf-8)
+        @rtype base64
+        @return base64 data
+        """
+        pagename = self._instr(pagename)
+        # User may read page?
+        if not self.request.user.may.read(pagename):
+            return self.notAllowedFault()
+        
+        filename = wikiutil.taintfilename(filename)
+        filename = AttachFile.getFilename(self.request, pagename, attachname)
+        if not os.path.isfile(filename):
+            return self.noSuchPageFault()
+        return self._outlob(open(filename, 'rb').read())
+        
+    def xmlrpc_putAttachment(self, pagename, attachname, data):
+        """ Set attachname associated with pagename to data
+        
+        @param pagename: pagename (utf-8)
+        @param attachname: attachment name (utf-8)
+        @param data: file data (base64)
+        @rtype boolean
+        @return True if attachment was set
+        """
+        pagename = self._instr(pagename)
+        # User may read page?
+        if not self.request.user.may.read(pagename):
+            return self.notAllowedFault()
+
+        if not self.request.cfg.xmlrpc_putpage_enabled:
+            return xmlrpclib.Boolean(0)
+        if self.request.cfg.xmlrpc_putpage_trusted_only and not self.request.user.trusted:
+            return xmlrpclib.Fault(1, "You are not allowed to edit this page")
+        # also check ACLs
+        if not self.request.user.may.write(pagename):
+            return xmlrpclib.Fault(1, "You are not allowed to edit this page")
+        
+        attachname = wikiutil.taintfilename(attachname)
+        filename = AttachFile.getFilename(self.request, pagename, attachname)
+        if os.path.exists(filename) and not os.path.isfile(filename):
+            return self.noSuchPageFault()
+        open(filename, 'wb+').write(data.data)
+        os.chmod(filename, 0666 & config.umask)
+        AttachFile._addLogEntry(self.request, 'ATTNEW', pagename, filename)
+        return xmlrpclib.Boolean(1)
+
     def process(self):
         """ xmlrpc v1 and v2 dispatcher """
         try:
--- a/docs/CHANGES	Fri Jun 02 21:48:03 2006 +0200
+++ b/docs/CHANGES	Mon Jun 05 10:57:46 2006 +0200
@@ -77,6 +77,7 @@
     * You can have a common cache_dir for your farm (will save a bit space
       and cpu time as it shares some stuff).
       You need to set "cache_dir = '/some/farm/cachedir' in your farmconfig.
+    * Added XMLRPC methods for attachment handling. Thanks to Matthew Gilbert.
 
   Bugfixes:
     * on action "info" page, "revert" link will not be displayed for empty page