changeset 12:dd8c1c8ae0da

more removing of self.request and general cleanup
author k0s <k0scist@gmail.com>
date Wed, 07 Oct 2009 17:19:32 -0400
parents ebe8dc33c871
children fbf8f198ef67
files bitsyblog/bitsyblog.py
diffstat 1 files changed, 53 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyblog/bitsyblog.py	Tue Oct 06 22:08:12 2009 -0400
+++ b/bitsyblog/bitsyblog.py	Wed Oct 07 17:19:32 2009 -0400
@@ -97,8 +97,8 @@
         return res(environ, start_response)
 
     def get_response(self, text, content_type='text/html'):
+        # XXX to deprecate
         res = Response(content_type=content_type, body=text)
-        res.content_length = len(res.body)
         return res
 
     def get_index(self, request):
@@ -119,13 +119,13 @@
         if path == 'rss':
             if n_posts is None:
                 n_posts = 10
-            return self.get_response(self.site_rss(n_posts), content_type='text/xml')
+            return self.get_response(self.site_rss(request, n_posts), content_type='text/xml')
 
         # site atom
         if path == 'atom':
             if n_posts is None:
                 n_posts = 10
-            return self.get_response(self.atom(self.blog.latest(self.users.users(), n_posts)), content_type='text/xml')
+            return self.get_response(self.atom(request, self.blog.latest(self.users.users(), n_posts)), content_type='text/xml')
 
         ### help
         if path == 'help' and hasattr(self, 'help'):
@@ -161,7 +161,7 @@
         if path == [ 'preferences' ]:
             if check is not None:
                 return check
-            return self.get_response(self.preferences(user))
+            return self.get_response(self.preferences(request, user))
         
         if path == [ 'rss' ]:
             feed = 'rss'
@@ -195,11 +195,11 @@
             return e.wsgi_response
 
         if feed == 'rss':
-            content = self.rss(user, blog) # XXX different signatures
+            content = self.rss(request, user, blog) # XXX different signatures
             return self.get_response(content, content_type='text/xml')
 
         if feed == 'atom':
-            content = self.atom(blog, user) # XXX different signatures
+            content = self.atom(request, blog, user) # XXX different signatures
             return self.get_response(content, content_type='text/xml')
         
         # reverse the blog if necessary
@@ -222,8 +222,10 @@
         display the blog or respond to a get request
         """
 
-        # GENSHI: global data dictionary
-        request.environ['data'] = { 'site_name': self.site_name }
+        # GENSHI: data dictionary
+        request.environ['data'] = { 'site_name': self.site_name,
+                                    'request': request,
+                                    'link': self.liin}
 
         # front matter of the site
         index = self.get_index()
@@ -267,9 +269,9 @@
                 
                 errors = self.users.write_settings(user, **settings)
                 if errors: # re-display form with errors                    
-                    return self.get_response(self.preferences(user, errors))
+                    return self.get_response(self.preferences(request, user, errors))
                 
-                return self.get_response(self.preferences(user, message='Changes saved'))
+                return self.get_response(self.preferences(request, user, message='Changes saved'))
             elif len(path) == 1 and self.isentry(path[0]):
                 entry = self.blog.entry(user, path[0], roles['author'])
                 if entry is None:
@@ -292,7 +294,7 @@
                                     location='/%s' % self.user_url(request, user, 'post'))
 
         # determine if the post is secret or private
-        privacy = request.GET.get('privacy') or self.request.POST.get('privacy') or 'public'
+        privacy = request.GET.get('privacy') or request.POST.get('privacy') or 'public'
 
         # write the file
         now = utils.datestamp(datetime.datetime.now())
@@ -338,13 +340,14 @@
         methods += ' and %s' % self.response_functions.keys()[-1] 
         return exc.HTTPMethodNotAllowed("Only %s operations are allowed" % methods)
 
-    ### auth functions
+    ### auth/auth functions
 
     def passwords(self):
         return self.users.passwords()
 
     def authenticated(self, request):
         """return authenticated user"""
+        # XXX needed? 
         return request.environ.get('REMOTE_USER')
 
     def check_user(self, user, request):
@@ -385,11 +388,6 @@
             name = None
         return name, path
 
-    ### date methods
-
-    def isentry(self, string): # TODO -> blog.py
-        return (len(string) == len(''.join(utils.timeformat))) and string.isdigit()
-
     def user_url(self, request, user, *args, **kw):
         permalink = kw.get('permalink')
         if permalink:
@@ -399,17 +397,27 @@
         _args.extend(args)
         return '/'.join([str(arg) for arg in _args])
 
+    ### 
+
+    def isentry(self, string): # TODO -> blog.py
+        return (len(string) == len(''.join(utils.timeformat))) and string.isdigit()
+
     def permalink(self, request, blogentry):
         return self.user_url(request, blogentry.user, blogentry.datestamp(), permalink=True)
 
-    def entry_subject(self, blogentry):
-        if hasattr(self.request, 'user') and self.request.user.name == blogentry.user:
-            prefs = self.request.user.settings
+    def link(self, request, path, permanant=False):
+        if isinstance(path, basestring):
+            path = [ path ]
+        path = [ i.strip('/') for i in path ]
+        if permanant:
+            path = [ request.application_url ] + list(path)
+            return '/'.join(path)
         else:
-            prefs = self.users[blogentry.user].settings
-        subject = prefs.get('Subject', self.subject)
-        date_format = prefs.get('Date format', self.date_format)
-        return subject % { 'date': blogentry.date.strftime(date_format) }
+            if request.path:
+                path = [ request.path ] + list(path)
+            return '/%s' % ('/'.join(path))
+
+    ### mangled URLs
 
     def mangledurl(self, request, blogentry):
         return self.user_url(request, blogentry.user, 'x%x' % (int(blogentry.datestamp()) * self.users.secret(blogentry.user)), permalink=True)
@@ -578,13 +586,13 @@
 
         entries = []
         for entry in blog:
-            id = entry.datestamp()
+            blog_id = entry.datestamp()
             format = prefs.get('Date format', self.date_format)
             datestamp = entry.date.strftime(format)
             synopsis = entry.title()
             if synopsis:
                 synopsis = ': %s' % cgi.escape(synopsis)
-            entries.append(markup.link("%s#%s" % (path, id), datestamp) + synopsis)
+            entries.append(markup.link("%s#%s" % (path, blog_id), datestamp) + synopsis)
 
         print >> retval, markup.listify(entries)
         print >> retval, more
@@ -676,15 +684,16 @@
                 
         return etree.tostring(foo)
 
+
     ### feeds
 
-    def site_rss(self, n_items=10):
+    def site_rss(self, request, n_items=10):
         blog = self.blog.latest(list(self.users.users()), n_items)
         title = self.site_name + ' - rss'
-        link = self.request.host_url # XXX should be smarter
+        link = request.application_url 
         description = "latest scribblings on %s" % self.site_name
         lastBuildDate = datetime.datetime.now()
-        items = [ self.rss_item(entry.user, entry) for entry in blog ]
+        items = [ self.rss_item(request, entry.user, entry) for entry in blog ]
         rss = PyRSS2Gen.RSS2(title=title, 
                              link=link,
                              description=description,
@@ -692,18 +701,18 @@
                              items=items)
         return rss.to_xml()
 
-    def rss(self, user, blog):
+    def rss(self, request, user, blog):
         """
         rss feed for a user's blog
         done with PyRSS2Gen:
         http://www.dalkescientific.com/Python/PyRSS2Gen.html
         """        
         title = "%s's blog" % user
-        link = os.path.split(self.request.url)[0]
+        link = os.path.split(request.url)[0]
         description = "latest blog entries for %s on %s" % (user, self.site_name)
         lastBuildDate = datetime.datetime.now() # not sure what this means
         
-        items = [ self.rss_item(user, entry) for entry in blog ]
+        items = [ self.rss_item(request, user, entry) for entry in blog ]
         rss = PyRSS2Gen.RSS2(title=title, 
                              link=link,
                              description=description,
@@ -711,9 +720,9 @@
                              items=items)
         return rss.to_xml()
 
-    def rss_item(self, user, entry):
-        if hasattr(self.request, 'user') and self.request.user.name == user:
-            prefs = self.request.user.settings
+    def rss_item(self, request, user, entry):
+        if hasattr(request, 'user') and request.user.name == user:
+            prefs = request.user.settings
         else:
             prefs = self.users[user].settings
         subject = prefs.get('Subject', self.subject)
@@ -727,17 +736,17 @@
                                  pubDate=entry.date)
 
 
-    def atom(self, blog, author=None):
+    def atom(self, request, blog, author=None):
         retval = StringIO()
         print >> retval, """<?xml version="1.0" encoding="utf-8"?>
 <feed xmlns="http://www.w3.org/2005/Atom">
 """
         if author:
             title = "%s's blog" % author
-            link = self.request.host_url + '/' + author
+            link = request.host_url + '/' + author
         else:
             title = self.site_name + ' - atom'
-            link = self.request.host_url
+            link = request.application_url
             
         date = blog[0].date.isoformat()
 
@@ -773,7 +782,7 @@
         print >> retval, '</form>'
         return self.render(request, retval.getvalue())
 
-    def preferences_form(self, user):
+    def preferences_form(self, request, user):
         prefs = self.request.user.settings
         form = Form()
 
@@ -816,9 +825,9 @@
         
         return form
 
-    def preferences(self, user, errors=None, message=None):
+    def preferences(self, request, user, errors=None, message=None):
         """user preferences form"""
-        body = self.preferences_form(user)(errors)
+        body = self.preferences_form(request, user)(errors)
         if message:
             body = '%s\n%s' % ( markup.p(markup.strong(message)), body )
         return self.render(request, body, title='preferences')
@@ -837,7 +846,9 @@
     def get(self, request):
 
         # GENSHI: global data dictionary
-        request.environ['data'] = { 'site_name': self.site_name }
+        request.environ['data'] = { 'site_name': self.site_name,
+                                    'request': request,
+                                    'link': self.link }
 
         ### user space
         user, path = self.userpath(request)