Alessandro Molina avatar Alessandro Molina committed 0c243e0

Support commenting things which are not SQLA entities

Comments (0)

Files changed (2)

tgcomments/controllers/root.py

 """Main Controller"""
 
 from tg import TGController
-from tg import expose, flash, require, url, lurl, request, redirect, validate
+from tg import expose, flash, require, url, lurl, request, redirect, validate, config
 from tg.exceptions import HTTPForbidden, HTTPRedirection
 from tg.i18n import ugettext as _, lazy_ugettext as l_
 
 from tgcomments import model
 from tgcomments.model import DBSession, Comment
-from tgcomments.lib import get_user_gravatar, notify_comment_on_facebook
+from tgcomments.lib import get_user_gravatar, notify_comment_on_facebook, make_fake_comment_entity, FakeCommentEntity
 
 from tgext.pluggable import app_model
 
         if entity_type is None or entity_id is None:
             return back_to_referer()
 
-        entity = DBSession.query(entity_type).get(entity_id)
-        if not entity:
-            return back_to_referer()
+        if issubclass(entity_type, FakeCommentEntity):
+            entity = make_fake_comment_entity(entity_type, entity_id)
+        else:
+            entity = DBSession.query(entity_type).get(entity_id)
+            if not entity:
+                return back_to_referer()
 
         if not request.identity:
             try:
                 user = {'name':String(not_empty=True).to_python(kw.get('author')),
                         'avatar':get_user_gravatar(Email(not_empty=True).to_python(kw.get('email')))}
-            except Invalid, e:
-                print e
+            except Invalid:
                 return back_to_referer()
         else:
             user = request.identity['user']

tgcomments/lib/__init__.py

 import json
 from urllib import urlopen, urlencode
 from contextlib import closing
+from collections import namedtuple
 
 def get_user_gravatar(user):
     if not isinstance(user, basestring):
     with closing(urlopen(url, urlencode(data))) as fbanswer:
         return json.loads(fbanswer.read())
 
+class FakeCommentEntity(object):
+    class __mapper__(object):
+        primary_key = [namedtuple('FakeColumn', ['key'])('uid')]
+
+    def __init__(self, uid):
+        self.uid = uid
+
+def make_fake_comment_entity(entity_type, entity_id):
+    fake_comment_type = type(entity_type.__name__, (FakeCommentEntity,), {})
+    return fake_comment_type(entity_id)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.