root/SimpleBlog/branches/plone-2.5/MovableTypeAPI.py

Revision 583 (checked in by chervol, 2 years ago)

blog tool merged APIs added

  • Property svn:eol-style set to native
Line 
1
2 from AccessControl import ClassSecurityInfo
3 from Globals import InitializeClass
4 from Products.CMFCore.utils import getToolByName
5 from DateTime import DateTime
6 import xmlrpclib
7 from OFS.SimpleItem import SimpleItem
8
9 XMLRPCTRUE = xmlrpclib.Boolean(1)
10 XMLRPCFALSE = xmlrpclib.Boolean(0)
11
12 authTwoMethods = ['mt/getRecentPostTitles', 'mt/getPostCategories',
13                   'mt/setPostCategories', 'mt/publishPost',
14                   'mt/supportedMethods', 'mt/supportedTextFilters']
15
16
17 def genericBloggerAuthTwo(arg_tuple):
18      return arg_tuple[1],arg_tuple[2],arg_tuple
19
20
21 class MovableTypeAPI(SimpleItem):
22     """Implements the Movable Type API
23
24     See: http://www.movabletype.org/docs/mtmanual_programmatic.html#xmlrpc%20api
25     """
26
27     security = ClassSecurityInfo()
28
29     def __init__(self, RPCAuth = None):
30         ""
31         if RPCAuth:
32             self.setupRPCAuth(RPCAuth)
33
34     security.declarePublic('setupRPCAuth')
35     def setupRPCAuth(self, RPCAuth):
36         RPCAuth.addAuthProvider(authTwoMethods, genericBloggerAuthTwo)
37        
38     security.declarePublic('publishPost')
39     def publishPost(self, postid, username, password):
40         """ Publish a post """
41         self.plone_log('mt/publishPost')
42         print 'mt/publishPost'
43        
44         sbtool = getToolByName(self, 'simpleblog_tool')
45         post = sbtool.getByUID(postid)       
46        
47         if post:
48             # do publishing
49             wf_tool=getToolByName(self, 'portal_workflow')
50             wf_state = wf_tool.getInfoFor(post, 'review_state', '')
51             if wf_state != 'published':
52                 wf_tool.doActionFor(post, 'publish')  # todo
53             return XMLRPCTRUE
54         raise AttributeError, "Entry %s does not exists" % postid
55
56     security.declarePublic('getRecentPostTitles')
57     def getRecentPostTitles(self, blogid, username, password, numberOfPosts=None):
58         """Get a list of posts titles by a user.
59         The number of posts is unlimited."""
60
61         self.plone_log('mt/getRecentPostTitles')
62         print 'mt/getRecentPostTitles'
63         sbtool = getToolByName(self, 'simpleblog_tool')
64         blog = sbtool.getByUID(blogid)
65
66         entries = blog.getFolderListingFolderContents(contentFilter={'portal_type': 'BlogEntry'},)       
67         # todo: what if entries are in subfolders?
68        
69         posts = []
70         for entry in entries:
71             posts.append( { 'dateCreated':entry.created()
72                             , 'userid':entry.Creator()
73                             , 'postid':entry.UID()
74                             , 'title':entry.Title()
75                             , 'description':entry.getBody()
76                             , 'mt_excerpt':entry.Description()
77                               })           
78            
79         if numberOfPosts is not None:
80             return posts[:int(numberOfPosts)]
81         return posts 
82    
83     security.declarePublic('getCategoryList')
84     def getCategoryList(self, blogid, username, password):
85         """ Get a list of available categories """
86
87         self.plone_log('mt/getCategoryList')
88         print 'mt/getCategoryList'
89         sbtool = getToolByName(self, 'simpleblog_tool')
90        
91         blog = sbtool.getByUID(blogid)
92        
93         cats = blog.listCategories()
94        
95         categories = []
96         for cat in cats:
97             categories.append(
98                 {'isPrimary':XMLRPCFALSE, 'categoryId': cat, 'categoryName' : cat})
99
100         return categories   
101    
102     security.declarePublic('getPostCategories')
103     def getPostCategories(self, postid, username, password):
104         """ Return an existing posting categories in the RSS format. """
105
106         self.plone_log('mt/getPostCategories')
107         print 'mt/getPostCategories'
108         sbtool = getToolByName(self, 'simpleblog_tool')
109         post = sbtool.getByUID(postid)
110         if post:
111             cats = post.listCategories()
112             res=[]
113             for c in cats:
114                 res.append({'isPrimary':XMLRPCFALSE, 'categoryId':c, 'categoryName':c})
115             return res
116         else:
117             raise AttributeError, "Post %s was not found" % postid
118
119     security.declarePublic('setPostCategories')
120     def setPostCategories(self, postid, username, password, categories):
121         """ Return an existing posting categories in the RSS format. """
122
123         self.plone_log('mt/setPostCategories')
124         print 'mt/setPostCategories'
125         sbtool = getToolByName(self, 'simpleblog_tool')
126         post = sbtool.getByUID(postid)
127         if post:
128             res=[]
129             categories = [s.get('categoryId', '') for s in categories
130                           if s.get('categoryId', '').strip()]
131             post.setCategories(categories)
132             post.reindexObject()
133             return XMLRPCTRUE
134         else:
135             raise AttributeError, "Post %s was not found" % postid
136
137     security.declarePublic('supportedMethods')
138     def supportedMethods(self):
139         """Return Value: an array of method names supported by the
140         server.
141         """
142         return [m.split('/')[1] for m in authTwoMethods]
143
144     security.declarePublic('supportedTextFilters')
145     def supportedTextFilters(self):
146         """Return Value: an array of structs containing String ``key``
147         and String ``label``. ``key`` is the unique string identifying
148         a text formatting plugin, and ``label`` is the readable
149         description to be displayed to a user. ``key`` is the value
150         that should be passed in the ``mt_convert_breaks`` parameter
151         to newPost and editPost.
152         """
153         # Not actually valid, but you get the idea
154         supported = [{'key':'html',
155                       'label':'HTML'},
156                      {'key':'stx',
157                       'label':'Structured Text'},
158                      {'key':'plain-text',
159                       'label':'Plain Text'}]
160         return supported
161
162     security.declarePublic('getTrackbackPings')
163     def getTrackbackPings(self, postid):
164         """Return Value: an array of structs containing String
165         ``pingTitle`` (the title of the entry sent in the ping),
166         String ``pingURL`` (the URL of the entry), and String
167         ``pingIP`` (the IP address of the host that sent the ping).
168         """
169         return []
170
171 InitializeClass(MovableTypeAPI)
Note: See TracBrowser for help on using the browser.