source: products/SimpleBlog/trunk/BloggerAPI.py

Last change on this file was 1, checked in by myroslav, 18 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1#
2# Authors: Tom von Schwerdtner <tvon@etria.com>
3#          Brian Skahan <bskahan@etria.com>
4#
5# Copyright 2004, Etria, LLP
6#
7# This file is part of Quills
8#
9# Quills is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# Foobar is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with Quills; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23from OFS.SimpleItem import SimpleItem
24from Products.CMFCore.utils import getToolByName
25from Products.CMFDefault.utils import parseHeadersBody
26from AccessControl import ClassSecurityInfo
27import DateTime
28
29authOneMethods = [
30    'blogger/getTemplate',
31    'blogger/setTemplate',
32    'blogger/deletePost',
33    'blogger/editPost',
34    'blogger/newPost',
35    'blogger/getRecentPosts'
36    ]
37
38def genericBloggerAuthOne(args):
39    return args[2],args[3],args
40
41
42authTwoMethods = [
43    'blogger/getUsersBlogs',
44    'blogger/getUserInfo'
45    ]
46
47def genericBloggerAuthTwo(args):
48    return args[1],args[2],args
49
50class BloggerAPI(SimpleItem):
51    """http://www.blogger.com/developers/api/1_docs/"""
52    security = ClassSecurityInfo()
53
54    def __init__(self, RPCAuth = None):
55        ""
56        if RPCAuth:
57            self.setupRPCAuth(RPCAuth)
58
59    security.declarePublic('setupRPCAuth')
60    def setupRPCAuth(self, RPCAuth):
61        RPCAuth.addAuthProvider(authOneMethods, genericBloggerAuthOne)
62        RPCAuth.addAuthProvider(authTwoMethods, genericBloggerAuthTwo)
63
64    security.declarePublic('newPost')
65    def newPost(self, appkey, blogid, username, password, content, publish):
66        """blogger.newPost makes a new post to a designated blog. Optionally,
67        will publish the blog after making the post. On success, it returns the
68        unique ID of the new post.  On error, it will return some error
69        message."""
70        self.plone_log('blogger/newPost')
71
72        sbtool = getToolByName(self, 'simpleblog_tool')
73        blog = sbtool.getByUID(blogid)
74
75        post = content.split('\n')
76        title = post[0]
77        if len(title)>25:
78            title = str(DateTime.DateTime())
79        body = '\n'.join(post[1:])
80
81        #headers, body = parseHeadersBody(content)
82        id = sbtool.idFromTitle(title)
83        blog.invokeFactory('BlogEntry', id = id, title = title, body = body)
84        entry = getattr(blog, id)
85
86        wf_tool = getToolByName(self, 'portal_workflow')
87        if publish:
88            state = sbtool.getPublishedState()
89            entry.setEffectiveDate(DateTime.DateTime())
90
91            # todo xxxxxxxxxx
92            wf_tool.doActionFor(entry,'publish', None)
93
94        return entry.UID()
95
96    security.declarePublic('editPost')
97    def editPost(self, appkey, postid, username, password, content, publish):
98        """blogger.editPost changes the contents of a given post. Optionally,
99        will publish the blog the post belongs to after changing the post. On
100        success, it returns a boolean true value. On error, it will return a
101        fault with an error message."""
102        self.plone_log('blogger/editPost')
103
104        sbtool = getToolByName(self, 'simpleblog_tool')
105        entry = sbtool.getByUID(postid)
106        entry.setBody(content, mimetype='text/html')
107
108        if publish:
109            wf_tool = getToolByName(self, 'portal_workflow')
110            entry.setEffectiveDate(DateTime.DateTime())
111            wf_tool.doActionFor(entry, 'publish', None)
112
113        return True
114
115    security.declarePublic('deletePost')
116    def deletePost(self, appkey, postid, username, password, publish):
117        "Returns true on success, fault on failure"
118        self.plone_log('blogger/deletePost')
119
120        sbtool = getToolByName(self, 'simpleblog_tool')
121        entry = sbtool.getByUID(postid)
122        entry.aq_inner.aq_parent.manage_delObjects(entry.getId())
123
124        return True
125
126    security.declarePublic('getRecentPosts')
127    def getRecentPosts(self, appkey, blogid, username, password, num):
128        "Return 'num' recent posts to specified blog"
129        self.plone_log('blogger/getRecentPosts')
130        sbtool = getToolByName(self, 'simpleblog_tool')
131        blog = sbtool.getByUID(blogid)
132        brains = blog.getFolderContents(contentFilter={'portal_type': 'BlogEntry'},)
133
134        posts = []
135        for b in brains:
136            entry = b.getObject()
137            body = entry.getBody()
138            posts.append( { 'dateCreated':b.created
139                            , 'userid':b.Creator
140                            , 'postid':entry.UID()
141                            , 'title':b.Title
142                            , 'description':body
143                            , 'excerpt':b.Description
144                            , 'content':body
145                              })
146        if num is not None:
147            return posts[:int(num)]
148        return posts
149
150    security.declarePublic('getUsersBlogs')
151    def getUsersBlogs(self, appkey, username, password):
152        """blogger.getUsersBlogs returns information about all the blogs a
153        given user is a member of. Data is returned as an array of <struct>'s
154        containing the ID (blogid), name (blogName), and URL (url) of each
155        blog."""
156        self.plone_log('blogger/getUsersBlogs')
157        catalog = getToolByName(self, 'portal_catalog')
158        results = catalog(meta_type='Blog')
159
160        blogs = []
161        for item in results:
162            o = item.getObject()
163            if o.portal_membership.checkPermission('Modify portal content', o):
164                blogs.append(
165                        {'url': o.absolute_url(),
166                         'blogid' : o.UID(),
167                         'blogName' : o.title_or_id()}
168                        )
169
170        return blogs
171
172    security.declarePublic('getUserInfo')
173    def getUserInfo(self, appkey, username, password):
174        """blogger.getUserInfo returns returns a struct containing user's
175        userid, firstname, lastname, nickname, email, and url."""
176        self.plone_log('blogger/getUserInfo')
177
178        membership=getToolByName(self, 'portal_membership')
179        info={'name':'no name','email':'no email','userid':'no user id'
180               ,'firstname':'no first name','lastname':'no last name'
181               ,'url':'no url'}
182        member=membership.getAuthenticatedMember()
183        if member:
184            for key,value in info.items():
185                info[key] = getattr(member,key,None) or value
186        return info
187
188    security.declarePublic('getTemplate')
189    def getTemplate(self):
190        """blogger.getTemplate returns text of the main or archive index
191        template for a given blog."""
192        # Not implementing
193        self.plone_log('blogger/getTemplate')
194        pass
195
196    security.declarePublic('setTemplate')
197    def setTemplate(self):
198        """blogger.setTemplate changes the template for a given blog. Can
199        change either main or archive index template."""
200        # Not implementing
201        self.plone_log('blogger/setTemplate')
202        pass
Note: See TracBrowser for help on using the repository browser.