Add a comments view to the InfluenceAdmin.
authorMatthijs Kooijman <matthijs@stdin.nl>
Fri, 31 Oct 2008 10:36:30 +0000 (11:36 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Fri, 31 Oct 2008 10:36:30 +0000 (11:36 +0100)
influences/admin.py

index afdf6d7351b4e1a77d23819540483080ca2cbdba..f3e882801f67314c8420580179a4df52921337c4 100644 (file)
@@ -1,5 +1,13 @@
 from django.contrib import admin
 from django.contrib import admin
+from django.contrib.admin.util import unquote
+from django.shortcuts import get_object_or_404, render_to_response
+from django.template import RequestContext
 from xerxes.influences.models import Character, Influence
 from xerxes.influences.models import Character, Influence
+from django.contrib.contenttypes import generic
+from threadedcomments.models import ThreadedComment
+from django.utils.safestring import mark_safe
+
+from views import get_influence_comment_form, quote_reply
 
 class CharacterAdmin(admin.ModelAdmin):
     list_filter=('status', 'player')
 
 class CharacterAdmin(admin.ModelAdmin):
     list_filter=('status', 'player')
@@ -13,5 +21,34 @@ class InfluenceAdmin(admin.ModelAdmin):
     search_fields=('character', 'summary', 'description', 'contact')
     list_display=('character', 'contact', 'summary', 'longterm', 'status') 
 
     search_fields=('character', 'summary', 'description', 'contact')
     list_display=('character', 'contact', 'summary', 'longterm', 'status') 
 
-admin.site.register(Influence, InfluenceAdmin)
+    class Media:
+        js = ('base/js/yahoo-dom-event.js', 'base/js/logger-debug.js')
+
+    def __call__(self, request, url):
+        if (url and url.endswith('/comments')):
+            return self.comments_view(request, unquote(url[:-9]))
+        else:
+            return super(InfluenceAdmin, self).__call__(request, url)
     
     
+    def comments_view(self, request, object_id):
+        model = self.model
+        opts = model._meta
+        obj = get_object_or_404(model, pk=object_id)
+
+        comments = ThreadedComment.objects.get_tree(obj)
+        # Annotate each comment with a proper reply form
+        for comment in comments:
+            initial = { 'comment' : quote_reply(comment) }
+            comment.reply_form = get_influence_comment_form(request.user.is_staff, comment)(initial=initial)
+
+        context = {
+            'root_path'     : self.admin_site.root_path,
+            'app_label'     : self.model._meta.app_label,
+            'object'        : obj,
+            'opts'          : opts,
+            'comments'      : comments,
+            'media'         : mark_safe(self.media),
+        }
+        return render_to_response('admin/influences/influence/comments.html', context, RequestContext(request))
+        
+admin.site.register(Influence, InfluenceAdmin)