annotate forums/admin.py @ 1090:71685387dd11

Reduce database usage on poll index page.
author Brian Neal <bgneal@gmail.com>
date Mon, 09 May 2016 19:48:51 -0500
parents 3e1905e523be
children
rev   line source
bgneal@75 1 """
bgneal@75 2 This file contains the admin definitions for the forums application.
bgneal@75 3 """
bgneal@75 4 from django.contrib import admin
bgneal@75 5
bgneal@75 6 from forums.models import Category
bgneal@75 7 from forums.models import Forum
bgneal@75 8 from forums.models import Topic
bgneal@75 9 from forums.models import Post
bgneal@98 10 from forums.models import FlaggedPost
bgneal@307 11 from forums.models import ForumLastVisit
bgneal@113 12 from forums.models import TopicLastVisit
bgneal@562 13 from forums.signals import (notify_new_topic, notify_updated_topic,
bgneal@562 14 notify_new_post, notify_updated_post)
bgneal@562 15
bgneal@204 16 import bio.badges
bgneal@75 17
bgneal@75 18
bgneal@75 19 class CategoryAdmin(admin.ModelAdmin):
bgneal@75 20 list_display = ('name', 'position', )
bgneal@206 21 list_editable = ('position', )
bgneal@81 22 prepopulated_fields = { 'slug': ('name', ) }
bgneal@81 23 save_on_top = True
bgneal@75 24
bgneal@75 25
bgneal@75 26 class ForumAdmin(admin.ModelAdmin):
bgneal@75 27 list_display = ('name', 'category', 'position', 'topic_count', 'post_count')
bgneal@206 28 list_editable = ('position', )
bgneal@75 29 prepopulated_fields = { 'slug': ('name', ) }
bgneal@75 30 raw_id_fields = ('last_post', )
bgneal@206 31 ordering = ('category', )
bgneal@81 32 save_on_top = True
bgneal@81 33
bgneal@75 34
bgneal@75 35 class TopicAdmin(admin.ModelAdmin):
bgneal@102 36 list_display = ('name', 'forum', 'creation_date', 'update_date', 'user', 'sticky', 'locked',
bgneal@75 37 'post_count')
bgneal@232 38 raw_id_fields = ('user', 'last_post', 'subscribers', 'bookmarkers')
bgneal@75 39 search_fields = ('name', )
bgneal@75 40 date_hierarchy = 'creation_date'
bgneal@75 41 list_filter = ('creation_date', 'update_date', )
bgneal@81 42 save_on_top = True
bgneal@75 43
bgneal@562 44 # override save_model() to update the search index
bgneal@562 45 def save_model(self, request, obj, form, change):
bgneal@562 46 obj.save()
bgneal@562 47
bgneal@562 48 if change:
bgneal@562 49 notify_updated_topic(obj)
bgneal@562 50 else:
bgneal@562 51 notify_new_topic(obj)
bgneal@562 52
bgneal@75 53
bgneal@75 54 class PostAdmin(admin.ModelAdmin):
bgneal@552 55 list_display = ('user', 'creation_date', 'update_date', 'user_ip', 'summary')
bgneal@75 56 raw_id_fields = ('topic', 'user', )
bgneal@75 57 exclude = ('html', )
bgneal@75 58 search_fields = ('body', )
bgneal@75 59 date_hierarchy = 'creation_date'
bgneal@75 60 list_filter = ('creation_date', 'update_date', )
bgneal@97 61 ordering = ('-creation_date', )
bgneal@81 62 save_on_top = True
bgneal@75 63
bgneal@740 64 def get_queryset(self, request):
bgneal@552 65 return Post.objects.select_related('user')
bgneal@552 66
bgneal@562 67 # override save_model() to update the search index
bgneal@562 68 def save_model(self, request, obj, form, change):
bgneal@562 69 obj.save()
bgneal@562 70
bgneal@562 71 if change:
bgneal@562 72 notify_updated_post(obj)
bgneal@562 73 else:
bgneal@562 74 notify_new_post(obj)
bgneal@562 75
bgneal@75 76
bgneal@98 77 class FlaggedPostAdmin(admin.ModelAdmin):
bgneal@465 78 list_display = ['__unicode__', 'flag_date', 'get_post_url']
bgneal@465 79 actions = ['accept_flags']
bgneal@465 80 raw_id_fields = ['post', 'user', ]
bgneal@204 81
bgneal@204 82 def accept_flags(self, request, qs):
bgneal@204 83 """This admin action awards a security pin to the user who reported
bgneal@204 84 the post and then deletes the flagged post object.
bgneal@204 85 """
bgneal@204 86 for flag in qs:
bgneal@204 87 bio.badges.award_badge(bio.badges.SECURITY_PIN, flag.user)
bgneal@204 88 flag.delete()
bgneal@204 89
bgneal@204 90 accept_flags.short_description = "Accept selected flagged posts"
bgneal@98 91
bgneal@98 92
bgneal@307 93 class ForumLastVisitAdmin(admin.ModelAdmin):
bgneal@307 94 raw_id_fields = ('user', 'forum')
bgneal@307 95 list_display = ('user', 'forum', 'begin_date', 'end_date')
bgneal@307 96
bgneal@307 97
bgneal@113 98 class TopicLastVisitAdmin(admin.ModelAdmin):
bgneal@113 99 raw_id_fields = ('user', 'topic')
bgneal@113 100 list_display = ('user', 'topic', 'last_visit')
bgneal@113 101
bgneal@113 102
bgneal@75 103 admin.site.register(Category, CategoryAdmin)
bgneal@75 104 admin.site.register(Forum, ForumAdmin)
bgneal@75 105 admin.site.register(Topic, TopicAdmin)
bgneal@75 106 admin.site.register(Post, PostAdmin)
bgneal@98 107 admin.site.register(FlaggedPost, FlaggedPostAdmin)
bgneal@307 108 admin.site.register(ForumLastVisit, ForumLastVisitAdmin)
bgneal@113 109 admin.site.register(TopicLastVisit, TopicLastVisitAdmin)