Re: [PATCH] Trackback+akismetcomments

Michael Guntsche <mike-Z92qn3yYq0hWk0Htik3J/[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
On Dec 29, 2007, at 3:58, Ryan Barrett wrote:

> i didn't include the parts that you said you were redoing. if/when  
> you check
> those parts in, would you mind adding tests for them? thanks in  
> advance...
>

Hello Ryan,

Please take a look at the following patch. It adds the pending  
changes and also fixes a problem with non-ASCII characters in  
comments. I removed all json related stuff for now.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
Pyblosxom-devel mailing list
Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
trackback.patch (application/octet-stream, 7.2 KB)
Index: plugins/comments/plugins/comments.js
===================================================================
--- plugins/comments/plugins/comments.js	(revision 1180)
+++ plugins/comments/plugins/comments.js	(working copy)
@@ -66,7 +67,7 @@
   for (i = 0; i < form.elements.length; i++) {
     elem = form.elements[i];
     if (elem.type != 'submit' && elem.type != 'button')
-      post_data += '&' + elem.name + '=' + escape(elem.value);
+      post_data += '&' + elem.name + '=' + encodeURIComponent(elem.value);
   }
 
   // send the request and tell the user.
Index: plugins/comments/plugins/akismetcomments.py
===================================================================
--- plugins/comments/plugins/akismetcomments.py	(revision 1180)
+++ plugins/comments/plugins/akismetcomments.py	(working copy)
@@ -48,6 +48,7 @@
 
 import sys
 import time
+from Pyblosxom import tools
 
 def verify_installation(request):
     try:
@@ -86,22 +87,31 @@
     form = request.getForm()
     reqdata = request.getData()
     http = request.getHttp()
+    
+    fields = {
+              # For Comments  
+              'body' : 'comment',
+              'email' : 'comment_author_email',
+              'author' : 'comment_author',
+              
+              # For Trackbacks
+              'excerpt' : 'comment',
 
-    fields = { 'comment' : 'body',
-               'comment_author_email' : 'email',
-               'comment_author' : 'author',
-               'comment_author_url' : 'url' }
+              # Common
+              'url': 'comment_author_url'
+              }
+            
     data = {}
-    for field in fields:
-        if form.has_key(fields[field]):
-            data[field] = ""
-            for char in list(form[fields[field]].value):
+    for field, entry in fields.items():
+        if form.has_key(field):
+            data[entry] = ""
+            for char in list(form[field].value):
                 try:
                     char.encode('ascii')
                 except:
-                    data[field] = data[field] + "&#" + str(ord(char)) + ";"
+                    data[entry] = data[entry] + "&#" + str(ord(char)) + ";"
                 else:
-                    data[field] = data[field] + char
+                    data[entry] = data[entry] + char
 
     if 'comment' not in data or not data['comment']:
         return False
@@ -118,19 +128,22 @@
     api = Akismet(api_key, base_url, agent='PyBlosxom/1.3')
 
     if not api.verify_key():
-        print >>sys.stderr, "Could not verify akismet API key. Comments accepted.",
+        logger = tools.getLogger()
+        logger.error("Could not verify akismet API key. Comments accepted.")
         return False
 
     # false is ham, true is spam
     try:
         if api.comment_check(body, data):
-            print >>sys.stderr, "Rejecting comment",
+            logger = tools.getLogger()
+            logger.error("Rejecting comment")
             return (True, 'I\'m sorry, but your comment was rejected by the <a href="http://akismet.com/">Akismet</a> spam filtering system.')
 
         else:
             return False
     except AkismetError:
-        print >>sys.stderr, "Rejecting comment (AkismetError)",
+        logger = tools.getLogger()
+        logger.error("Rejecting comment (AkismetError)")
         return (True, "Missing essential data (e.g., a UserAgent string).")
 
 
Index: plugins/comments/plugins/comments.py
===================================================================
--- plugins/comments/plugins/comments.py	(revision 1180)
+++ plugins/comments/plugins/comments.py	(working copy)
@@ -734,7 +734,11 @@
         "body" in form and posting):
 
         encoding = config.get('blog_encoding', 'iso-8859-1')
-        decode_form(form, encoding)
+        if form.has_key('ajax'):
+            #Ajax is ALWAYS UTF-8
+            decode_form(form, 'UTF-8')
+        else:
+            decode_form(form, encoding)
 
         body = form['body'].value
         author = form['author'].value
@@ -773,7 +777,7 @@
 
         # record the comment's timestamp, so we can extract it and send it
         # back alone, without the rest of the page, if the request was ajax.
-        data['cmt_time'] = float(cdict['pubDate'])
+        data['cmt_time'] = cmt_time 
 
         argdict = { "request": request, "comment": cdict }
         reject = tools.run_callback("comment_reject",
@@ -800,6 +804,8 @@
         blosxom.Renderer.__init__(self, request, out)
         self._ajax_type = request.getHttp()['form']['ajax'].value
         self._data = data
+        
+        self.encoding = request.getConfiguration().get("blog_encoding","UTF-8")
 
     def __shouldOutput(self, entry, template_name):
         """ Return whether we should output this template, depending on the
@@ -822,6 +828,14 @@
         if self.__shouldOutput(entry, template_name):
             blosxom.Renderer._outputFlavour(self, entry, template_name)
 
+    def render(self, headers=0):
+        """
+        We need to add the headers here because certain browsers use plain ascii if the charset is not set.
+        Thank you Safari
+        """
+        self._request.getResponse().addHeader("Content-type", "text/plain;charset=%s" % (self.encoding,) )
+        blosxom.Renderer.render(self, headers)
+        
 def cb_renderer(args):
     request = args['request']
     config = request.getConfiguration()
@@ -1001,6 +1015,16 @@
                 renderer.outputTemplate(output, comment_entry, 'comment')
             if ('preview' in form
                 and 'comment-preview' in renderer.flavour):
+                
+        
+                if form.has_key('ajax'):
+                    #Ajax is ALWAYS UTF-8
+                    encoding = 'UTF-8'
+                else:
+                    encoding = config.get('blog_encoding', 'iso-8859-1')
+
+                decode_form(form, encoding)
+
                 com = build_preview_comment(form, entry, config)
                 renderer.outputTemplate(output, com, 'comment-preview')
             elif ('rejected' in data):
@@ -1008,6 +1032,12 @@
                 msg = '<span class="error">%s</span>' % data["comment_message"]
                 rejected['cmt_description'] = msg
                 rejected['cmt_description_escaped'] = escape(msg)
+                
+                # If this is an ajax comment we need to overwrite 'cmt_time' so the rejected message shows up
+                if form.has_key('ajax'):
+                    rejected['cmt_time'] = data['cmt_time']
+                        
+                
                 renderer.outputTemplate(output, rejected, 'comment')
             renderer.outputTemplate(output, entry, 'comment-form')
         args['template'] = template +u"".join(output)
Index: plugins/comments/plugins/trackback.py
===================================================================
--- plugins/comments/plugins/trackback.py	(revision 1180)
+++ plugins/comments/plugins/trackback.py	(working copy)
@@ -105,6 +105,8 @@
                       'ipaddress': pyhttp.get('REMOTE_ADDR', ''),
                       }
 
+            cdict['ipaddress'] = pyhttp.get('REMOTE_ADDR', '')
+
             argdict = { "request": request, "comment": cdict }
             reject = tools.run_callback("trackback_reject",
                                         argdict,
smime.p7s (application/pkcs7-signature, 2.4 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.