comparison bitsyblog/bitsyblog.py @ 70:f6a6a4b072e7

complete overhaul to allow event handlers
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 07 Jul 2010 16:18:19 -0700
parents 0f25fe665367
children 0c98d1c2c6df
comparison
equal deleted inserted replaced
69:bcc3a59713c3 70:f6a6a4b072e7
58 } 58 }
59 59
60 60
61 cooked_bodies = {} 61 cooked_bodies = {}
62 62
63 def __init__(self, **kw): 63 def __init__(self, kw, handler_args):
64 for key in self.defaults: 64 for key in self.defaults:
65 setattr(self, key, kw.get(key, self.defaults[key])) 65 setattr(self, key, kw.get(key, self.defaults[key]))
66 self.n_links = int(self.n_links) # could be a string from the .ini 66 self.n_links = int(self.n_links) # could be a string from the .ini
67 self.response_functions = { 'GET': self.get, 67 self.response_functions = { 'GET': self.get,
68 'POST': self.post, 68 'POST': self.post,
71 71
72 # abstract attributes 72 # abstract attributes
73 from user import FilespaceUsers 73 from user import FilespaceUsers
74 74
75 self.users = FilespaceUsers(self.file_dir) 75 self.users = FilespaceUsers(self.file_dir)
76 self.blog = FileBlog(self.file_dir) 76 self.blog = FileBlog(self, self.file_dir)
77 self.cooker = self.restructuredText 77 self.cooker = self.restructuredText
78 self.feed_items = int(self.feed_items) 78 self.feed_items = int(self.feed_items)
79 79
80 # template renderer 80 # template renderer
81 self.auto_reload = self.auto_reload.lower()=='true' 81 self.auto_reload = self.auto_reload.lower()=='true'
102 self.header = file(self.header).read() 102 self.header = file(self.header).read()
103 self.header = Markup(self.header) 103 self.header = Markup(self.header)
104 104
105 # for BitsyAuth 105 # for BitsyAuth
106 self.newuser = self.users.new 106 self.newuser = self.users.new
107
108 # post handlers
109 self.handlers = [] # handlers for blog post event
110 for entry_point in iter_entry_points('bitsyblog.listeners'):
111 try:
112 handler = entry_point.load()(self, **handler_args.get(entry_point.name, {}))
113 self.handlers.append(handler)
114 except:
115 print 'Cant load entry point %s' entry_point.name
116 continue
117
107 118
108 ### methods dealing with HTTP 119 ### methods dealing with HTTP
109 120
110 def __call__(self, environ, start_response): 121 def __call__(self, environ, start_response):
111 request = Request(environ) 122 request = Request(environ)
324 privacy = request.GET.get('privacy') or request.POST.get('privacy') or 'public' 335 privacy = request.GET.get('privacy') or request.POST.get('privacy') or 'public'
325 336
326 # write the file 337 # write the file
327 now = utils.datestamp(datetime.datetime.now()) 338 now = utils.datestamp(datetime.datetime.now())
328 location = self.user_url(request, user, now) 339 location = self.user_url(request, user, now)
329 self.blog.post(user, now, body, privacy) 340 blog_entry = self.blog.post(user, now, body, privacy)
341
342 # fire event handlers
343 # XXX could be done asynchronously
344 for handler in self.handlers:
345 handler(blog_entry)
330 346
331 # point the user at the post 347 # point the user at the post
332 return exc.HTTPSeeOther("Post blogged by bitsy", location=location) 348 return exc.HTTPSeeOther("Post blogged by bitsy", location=location)
333 349
334 def put(self, request): 350 def put(self, request):