[patch] pubDate defaulting

Rick Bradley <[email protected]> Sun, 5 Oct 2003 12:47:31 -0500
Newsgroups gmane.comp.web.syncato.general
Message-ID <[email protected]>
This is a patch to dist/scripts/lib/Weblog.py from the 0.5 release of
Syncato to only set the pubDate element of a new document if there is
not already a pubDate element set.  Using XPath to check for element
existence may be a sledgehammer, but that's the only way I could quickly
see to do it.  

I had factored it out for use when editing the document as well but
decided to presume that documents aren't entering the database via means
other than Weblog.py (presuming further that You Know What You're Doing
if such is the case), and so will have a pubDate set if they're in the
database already.


--- Weblog.py	2003-10-05 12:36:08.000000000 -0500
+++ Weblog.py-new	2003-10-05 12:40:44.000000000 -0500
@@ -88,25 +88,26 @@
     def addPost(self, post):        
         try:
             doc = libxml2.parseDoc(post)
-    
-            root = doc.getRootElement()
-                        
-            date = time.time()
-            # ISO8601 Date
-            # TODO this shouldn't really use localtime as the ISO8601 date should be GMT. Just not sure how to 
-            # convert the time using XSL-T otherwise.
-            pubDate = root.newChild(None, "pubDate", time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(date)) + "-07:00")
- 
-            pubDate.setProp("seconds", str(date))
-            
+
+            # add pubDate element if none found
+            if (0 == len(doc.xpathNewContext().xpathEval("//pubDate"))):
+                root = doc.getRootElement()
+                date = time.time()
+                # ISO8601 Date
+                # TODO this shouldn't really use localtime as the ISO8601 date should be GMT. Just not sure how to 
+                # convert the time using XSL-T otherwise.
+                pubDate = root.newChild(None, "pubDate", time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(date)) + "-07:00")
+                pubDate.setProp("seconds", str(date))
+
+            # and add to database
             postID = self.db.addRecord(doc)
-            
             doc.freeDoc()
-            
+
             # Send change notification to other sites           
             self.pingWeblogs()
             
             return postID
+
         except Exception, e:
             print str(e)
             raise e


Rick
-- 
 http://www.rickbradley.com    MUPRN: 730
                       |  will be doing with their
   random email haiku  |  room before and after the
                       |  Installfest, etc). Thanks.


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf