root/SimpleBlog/trunk/MovableTypeAPI.py

Revision 790 (checked in by mylan, 2 years ago)

Clear blogging APIs modules, up version.

  • 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         sbtool = getToolByName(self, 'simpleblog_tool')
43         post = sbtool.getByUID(postid)       
44
45         if post:
46             # do publishing
47             wf_tool=getToolByName(self, 'portal_workflow')
48             wf_state = wf_tool.getInfoFor(post, 'review_state', '')
49             if wf_state != 'published':
50                 wf_tool.doActionFor(post, 'publish')  # todo
51             return XMLRPCTRUE
52
53         raise AttributeError, "Entry %s does not exists" % postid
54
55     security.declarePublic('getRecentPostTitles')
56     def getRecentPostTitles(self, blogid, username, password, numberOfPosts=None):
57         """Get a list of posts titles by a user.
58         The number of posts is unlimited."""
59         self.plone_log('mt/getRecentPostTitles')
60         sbtool = getToolByName(self, 'simpleblog_tool')
61         blog = sbtool.getByUID(blogid)
62
63         brains = blog.getFolderContents(contentFilter={'portal_type': 'BlogEntry'},)
64         # todo: what if entries are in subfolders?
65         posts = []
66         for b in brains:
67             entry = b.getObject()
68             posts.append( { 'dateCreated':b.created
69                             , 'userid':b.Creator
70                             , 'postid':entry.UID()
71                             , 'title':b.Title
72                             , 'description':entry.getBody()
73                             , 'mt_excerpt':b.Description
74                               })
75         if numberOfPosts is not None:
76             return posts[:int(numberOfPosts)]
77
78         return posts
79
80     security.declarePublic('getCategoryList')
81     def getCategoryList(self, blogid, username, password):
82         """ Get a list of available categories """
83         self.plone_log('mt/getCategoryList')
84         sbtool = getToolByName(self, 'simpleblog_tool')
85         blog = sbtool.getByUID(blogid)
86         cats = blog.listCategories()
87         categories = []
88         for cat in cats:
89             categories.append(
90                 {'isPrimary':XMLRPCFALSE, 'categoryId': cat, 'categoryName' : cat})
91
92         return categories
93
94     security.declarePublic('getPostCategories')
95     def getPostCategories(self, postid, username, password):
96         """ Return an existing posting categories in the RSS format. """
97         self.plone_log('mt/getPostCategories')
98         sbtool = getToolByName(self, 'simpleblog_tool')
99         post = sbtool.getByUID(postid)
100         if post:
101             cats = post.listCategories()
102             res=[]
103             for c in cats:
104                 res.append({'isPrimary':XMLRPCFALSE, 'categoryId':c, 'categoryName':c})
105             return res
106         else:
107             raise AttributeError, "Post %s was not found" % postid
108
109     security.declarePublic('setPostCategories')
110     def setPostCategories(self, postid, username, password, categories):
111         """ Return an existing posting categories in the RSS format. """
112         self.plone_log('mt/setPostCategories')
113         sbtool = getToolByName(self, 'simpleblog_tool')
114         post = sbtool.getByUID(postid)
115         if post:
116             res=[]
117             categories = [s.get('categoryId', '') for s in categories
118                           if s.get('categoryId', '').strip()]
119             post.setCategories(categories)
120             post.reindexObject()
121             return XMLRPCTRUE
122         else:
123             raise AttributeError, "Post %s was not found" % postid
124
125     security.declarePublic('supportedMethods')
126     def supportedMethods(self):
127         """Return Value: an array of method names supported by the
128         server.
129         """
130         return [m.split('/')[1] for m in authTwoMethods]
131
132     security.declarePublic('supportedTextFilters')
133     def supportedTextFilters(self):
134         """Return Value: an array of structs containing String ``key``
135         and String ``label``. ``key`` is the unique string identifying
136         a text formatting plugin, and ``label`` is the readable
137         description to be displayed to a user. ``key`` is the value
138         that should be passed in the ``mt_convert_breaks`` parameter
139         to newPost and editPost.
140         """
141         # Not actually valid, but you get the idea
142         supported = [{'key':'html',
143                       'label':'HTML'},
144                      {'key':'stx',
145                       'label':'Structured Text'},
146                      {'key':'plain-text',
147                       'label':'Plain Text'}]
148         return supported
149
150     security.declarePublic('getTrackbackPings')
151     def getTrackbackPings(self, postid):
152         """Return Value: an array of structs containing String
153         ``pingTitle`` (the title of the entry sent in the ping),
154         String ``pingURL`` (the URL of the entry), and String
155         ``pingIP`` (the IP address of the host that sent the ping).
156         """
157         return []
158
159 InitializeClass(MovableTypeAPI)
Note: See TracBrowser for help on using the browser.