Add a comments view to the InfluenceAdmin.
[matthijs/projects/xerxes.git] / influences / admin.py
1 from django.contrib import admin
2 from django.contrib.admin.util import unquote
3 from django.shortcuts import get_object_or_404, render_to_response
4 from django.template import RequestContext
5 from xerxes.influences.models import Character, Influence
6 from django.contrib.contenttypes import generic
7 from threadedcomments.models import ThreadedComment
8 from django.utils.safestring import mark_safe
9
10 from views import get_influence_comment_form, quote_reply
11
12 class CharacterAdmin(admin.ModelAdmin):
13     list_filter=('status', 'player')
14     search_fields=('name',)
15     list_display=('player', 'name', 'status') 
16
17 admin.site.register(Character, CharacterAdmin)
18
19 class InfluenceAdmin(admin.ModelAdmin):
20     list_filter=('character', 'status', 'longterm')
21     search_fields=('character', 'summary', 'description', 'contact')
22     list_display=('character', 'contact', 'summary', 'longterm', 'status') 
23
24     class Media:
25         js = ('base/js/yahoo-dom-event.js', 'base/js/logger-debug.js')
26
27     def __call__(self, request, url):
28         if (url and url.endswith('/comments')):
29             return self.comments_view(request, unquote(url[:-9]))
30         else:
31             return super(InfluenceAdmin, self).__call__(request, url)
32     
33     def comments_view(self, request, object_id):
34         model = self.model
35         opts = model._meta
36         obj = get_object_or_404(model, pk=object_id)
37
38         comments = ThreadedComment.objects.get_tree(obj)
39         # Annotate each comment with a proper reply form
40         for comment in comments:
41             initial = { 'comment' : quote_reply(comment) }
42             comment.reply_form = get_influence_comment_form(request.user.is_staff, comment)(initial=initial)
43
44         context = {
45             'root_path'     : self.admin_site.root_path,
46             'app_label'     : self.model._meta.app_label,
47             'object'        : obj,
48             'opts'          : opts,
49             'comments'      : comments,
50             'media'         : mark_safe(self.media),
51         }
52         return render_to_response('admin/influences/influence/comments.html', context, RequestContext(request))
53         
54 admin.site.register(Influence, InfluenceAdmin)