diff gpp/comments/models.py @ 14:7b6540b185d9

Added get_absolute_url() for my comments. Had to add a get_absolute_url() function for downloads in the process.
author Brian Neal <bgneal@gmail.com>
date Sat, 18 Apr 2009 19:14:17 +0000
parents dbd703f7d63a
children d0d779dd0832
line wrap: on
line diff
--- a/gpp/comments/models.py	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/comments/models.py	Sat Apr 18 19:14:17 2009 +0000
@@ -7,6 +7,7 @@
 from django.contrib.contenttypes import generic
 from django.contrib.auth.models import User
 from django.template.loader import render_to_string
+from django.core import urlresolvers
 
 
 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
@@ -43,17 +44,29 @@
     # Attach manager
     objects = CommentManager()
 
+    class Meta:
+        ordering = ('creation_date', )
+
     def __unicode__(self):
         return u'%s: %s...' % (self.user.username, self.comment[:50])
 
-    class Meta:
-        ordering = ('creation_date', )
-
     def save(self, force_insert=False, force_update=False):
         html = render_to_string('comments/markdown.html', {'data': self.comment})
         self.html = html.strip()
         super(Comment, self).save(force_insert, force_update)
 
+    def get_absolute_url(self):
+        return self.get_content_object_url() + ('#c%s' % self.id)
+
+    def get_content_object_url(self):
+        """
+        Get a URL suitable for redirecting to the content object.
+        """
+        return urlresolvers.reverse(
+            "comments-url-redirect",
+            args=(self.content_type_id, self.object_id)
+        )
+
 
 class CommentFlag(models.Model):
     """This model represents a user flagging a comment as inappropriate."""