changeset 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 777451a98f9d
children 32b1ca8e2940
files gpp/comments/models.py gpp/comments/urls.py gpp/downloads/models.py gpp/potd/models.py gpp/potd/urls.py gpp/potd/views.py gpp/templates/comments/comment.html gpp/templates/potd/view.html
diffstat 8 files changed, 50 insertions(+), 7 deletions(-) [+]
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."""
--- a/gpp/comments/urls.py	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/comments/urls.py	Sat Apr 18 19:14:17 2009 +0000
@@ -8,3 +8,9 @@
     url(r'^markdown/$', 'markdown_preview', name='comments-markdown_preview'),
     url(r'^post/$', 'post_comment', name='comments-post'),
 )
+
+urlpatterns += patterns('',
+    url(r'^cr/(\d+)/(\d+)/$',
+        'django.contrib.contenttypes.views.shortcut',
+        name='comments-url-redirect'),
+)
--- a/gpp/downloads/models.py	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/downloads/models.py	Sat Apr 18 19:14:17 2009 +0000
@@ -67,6 +67,10 @@
     def __unicode__(self):
         return self.title
 
+    @models.permalink
+    def get_absolute_url(self):
+        return ('downloads-download', [str(self.id)])
+
     def save(self, force_insert=False, force_update=False):
         html = render_to_string('downloads/markdown.html', {'data': self.description})
         self.html = html.strip()
--- a/gpp/potd/models.py	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/potd/models.py	Sat Apr 18 19:14:17 2009 +0000
@@ -34,11 +34,15 @@
     date_added = models.DateField(auto_now_add=True)
     potd_count = models.IntegerField(default=0)
 
+    class Meta:
+        ordering = ('-date_added', '-caption')
+
     def __unicode__(self):
         return u'%s (%s)' % (self.caption, self.pk)
 
-    class Meta:
-        ordering = ('-date_added', '-caption')
+    @models.permalink
+    def get_absolute_url(self):
+        return ('potd-archive', [str(self.id)])
 
     def save(self, force_insert=False, force_update=False):
 
--- a/gpp/potd/urls.py	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/potd/urls.py	Sat Apr 18 19:14:17 2009 +0000
@@ -5,4 +5,5 @@
 
 urlpatterns = patterns('potd.views',
     url(r'^$', 'view', name='potd-view'),
+    url(r'^archive/(\d+)/$', 'archive', name='potd-archive'),
 )
--- a/gpp/potd/views.py	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/potd/views.py	Sat Apr 18 19:14:17 2009 +0000
@@ -3,14 +3,25 @@
 """
 
 from django.shortcuts import render_to_response
+from django.shortcuts import get_object_or_404
 from django.template import RequestContext
 
 from potd.models import Current
+from potd.models import Photo
 
 
 def view(request):
     potd = Current.objects.get_current_photo()
     return render_to_response('potd/view.html', {
         'potd': potd,
+        'is_current': True,
         },
         context_instance = RequestContext(request))
+
+def archive(request, id):
+    photo = get_object_or_404(Photo, pk=id)
+    return render_to_response('potd/view.html', {
+        'potd': photo,
+        'is_current': False,
+        },
+        context_instance = RequestContext(request))
--- a/gpp/templates/comments/comment.html	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/templates/comments/comment.html	Sat Apr 18 19:14:17 2009 +0000
@@ -2,12 +2,12 @@
 {% load markup %}
 {% load smiley_tags %}
 <li>
-<div class="comment">
+<div class="comment" id="c{{ comment.id }}">
 <div class="comment-avatar">
 <a href="{% url bio-view_profile username=comment.user.username%}">{% avatar comment.user %}</a>
 </div>
 {% if comment.is_removed %}
-<div class="comment-text-removed"><p>This comment has been removed.</p></div>
+<div class="comment-text-removed"><p><em>This comment has been removed.</em></p></div>
 {% else %}
 <div class="comment-text">{{ comment.html|safe }}</div>
 {% endif %}
--- a/gpp/templates/potd/view.html	Thu Apr 16 02:00:17 2009 +0000
+++ b/gpp/templates/potd/view.html	Sat Apr 18 19:14:17 2009 +0000
@@ -11,8 +11,12 @@
 <script type="text/javascript" src="{{ MEDIA_URL }}js/comments.js"></script>
 {% endblock %}
 {% block content %}
+{% if is_current %}
 <h2>Photo Of The Day</h2>
 <h3>{% now "l, F d, Y" %}</h3>
+{% else %}
+<h2>Photo Of The Day Archives</h2>
+{% endif %}
 <div class="potd-details">
 {% if potd %}
 <img src="{{ potd.photo.url }}" alt="{{ potd.caption }}" title="{{ potd.caption }}" />