From e839b8f71b5353339b354ea78248bd5c74ab348a Mon Sep 17 00:00:00 2001
From: "James E. Blair" <corvus@gnu.org>
Date: Mon, 10 Aug 2009 21:39:55 -0700
Subject: Remove separate linkback table (just variants of comments now). Add
 function to generate trackback RDF.

---
 quoins/controllers.py | 42 +++++++++++++++++++++++++++++++++---------
 quoins/model/blog.py  | 38 ++++----------------------------------
 2 files changed, 37 insertions(+), 43 deletions(-)

diff --git a/quoins/controllers.py b/quoins/controllers.py
index 5605c61..ac3cef7 100644
--- a/quoins/controllers.py
+++ b/quoins/controllers.py
@@ -294,13 +294,15 @@ class Pingback(TGController):
             return xmlrpclib.Fault(0x20, 'Post not found.')
         elif not post.allow_comments:
             return xmlrpclib.Fault(0x31, 'Comments are closed on this post.')
-        for lb in post.linkbacks:
-            if lb.url == sourceURI:
+        for lb in post.comments:
+            if lb.url and lb.url == sourceURI:
                 return xmlrpclib.Fault(0x30, 'Pingback already registered.')
-        lb = LinkBack()
-        DBSession.add(lb)
+        lb = Comment()
+        lb.approved = True
+        lb.type = 'pingback'
         lb.post = post
         lb.url = sourceURI
+        post.comments.append(lb)
         return 'Linkback recorded.'
 
 def post_paginate(start, posts, size):
@@ -360,6 +362,25 @@ class BlogController(TGController):
     def get_html(self, data):
         return HTML(data)
 
+    def get_trackback_rdf(self, post, comment=True):
+        rdf = """
+    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+             xmlns:dc="http://purl.org/dc/elements/1.1/"
+             xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
+    <rdf:Description
+        rdf:about="%s"
+        dc:identifier="%s"
+        dc:title="%s"
+        trackback:ping="%s"
+    </rdf:RDF>\n"""
+        rdf = rdf % (self.absolute_url('/'),
+                     self.absolute_url(post),
+                     post.title,
+                     self.absolute_url('/trackback/%s'%post.id))
+        if comment:
+            rdf = "<!-- "+rdf+"    -->"
+        return HTML(rdf)
+       
     def send_comment_email(self, comment):
         post = comment.post
         blog = post.blog
@@ -480,6 +501,7 @@ Comment:
     def post(self, id):
         post = DBSession.query(Post).get(id)
         if not post: abort(404)
+        print pylons.request.headers
         pylons.response.headers['X-Pingback']=self.absolute_url('/pingback/')
         return dict(quoins = self,
                     blog = post.blog,
@@ -550,8 +572,8 @@ Comment:
                 pass
 
         c = Comment()
-        c.post = post
         post.comments.append(c)
+        c.post = post
         c.body = body
         if request.identity:
             c.author=request.identity['user']
@@ -869,17 +891,19 @@ Comment:
             message = 'Post not found.'
         elif not post.allow_comments:
             message = 'Comments are closed on this post.'
-        for lb in post.linkbacks:
-            if lb.url == url:
+        for lb in post.comments:
+            if lb.url and lb.url == url:
                 message = 'Trackback already registered.'
         if not message:
-            lb = LinkBack()
-            DBSession.add(lb)
+            lb = Comment()
+            lb.approved = True
+            lb.type = 'trackback'
             lb.post = post
             lb.url = url
             lb.title = title
             lb.name = blog_name
             lb.body = excerpt
+            post.comments.append(lb)
         if message:
             error = 1
             message = "<message>%s</message>\n" % message
diff --git a/quoins/model/blog.py b/quoins/model/blog.py
index fb1fa27..a7415d6 100644
--- a/quoins/model/blog.py
+++ b/quoins/model/blog.py
@@ -89,17 +89,7 @@ comment_table = Table('comment', metadata,
     Column('body', TEXT),
     Column('created', DateTime, nullable=False, default=datetime.now),
     Column('approved', Boolean, nullable=False, default=False, index=True),
-)
-
-linkback_table = Table('linkback', metadata,
-    Column('id', Integer, primary_key=True),
-    Column('post_id', Integer, ForeignKey('post.id',
-        onupdate="CASCADE", ondelete="CASCADE"), nullable=False, index=True),
-    Column('url', String(255)),
-    Column('title', Unicode(255)),
-    Column('body', Unicode(255)),
-    Column('name', Unicode(255)),
-    Column('created', DateTime, nullable=False, default=datetime.now),
+    Column('type', String(16), nullable=False, default='comment', index=True),
 )
 
 class Blog(object):
@@ -176,25 +166,15 @@ class Post(object):
             DBSession.delete(t)
 	self.tags.remove(t)
 
-    def get_comments_and_linkbacks(self, trackbacks=1, pingbacks=1):
-        objects = self.approved_comments[:]
-        for x in self.linkbacks:
-            if (trackbacks and x.body) or (pingbacks and not x.body):
-                objects.append(x)
-        objects.sort(lambda a,b: cmp(a.created, b.created))
-        return objects
-    comments_and_links = property(get_comments_and_linkbacks)
-
-
 class Media(object):
     pass
 
 class Tag(object):
     pass
 
-class BaseComment(object):
+class Comment(object):
     def get_author_name(self):
-        if hasattr(self, 'author') and self.author:            
+        if self.author:            
             return self.author.display_name
         if self.name:
             return self.name
@@ -202,12 +182,6 @@ class BaseComment(object):
             return self.url
         return 'Anonymous'
     author_name = property(get_author_name)
-        
-class Comment(BaseComment):
-    comment_type = 'comment'
-
-class LinkBack(BaseComment):
-    comment_type = 'linkback'
 
 mapper(Blog, blog_table,
        properties=dict(posts=relation(Post,
@@ -243,13 +217,9 @@ mapper(Post, post_table,
                        unapproved_comments=relation(Comment,
                                                     primaryjoin=and_(comment_table.c.post_id==post_table.c.id,
                                                                      comment_table.c.approved==False)),
-                       media=relation(Media),
-                       linkbacks=relation(LinkBack)))
+                       media=relation(Media)))
 
 mapper(Comment, comment_table,
        order_by=comment_table.c.created,
        properties=dict(post=relation(Post),
                        author=relation(TGUser)))
-
-mapper(LinkBack, linkback_table,
-       properties=dict(post=relation(Post)))
-- 
cgit v1.2.3