annotate gpp/weblinks/admin.py @ 318:c550933ff5b6

Fix a bug where you'd get an error when trying to delete a forum thread (topic does not exist). Apparently when you call topic.delete() the posts would get deleted, but the signal handler for each one would run, and it would try to update the topic's post count or something, but the topic was gone? Reworked the code a bit and explicitly delete the posts first. I also added a sync() call on the parent forum since post counts were not getting adjusted.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 Feb 2011 21:46:52 +0000
parents d424b8bae71d
children 701730b2fcda
rev   line source
gremmie@1 1 """This file contains the automatic admin site definitions for the weblinks models"""
bgneal@204 2 import datetime
gremmie@1 3
gremmie@1 4 from django.contrib import admin
gremmie@1 5 from weblinks.models import Category
bgneal@204 6 from weblinks.models import PendingLink
gremmie@1 7 from weblinks.models import Link
gremmie@1 8 from weblinks.models import FlaggedLink
gremmie@1 9
bgneal@193 10
bgneal@193 11 class CategoryAdmin(admin.ModelAdmin):
bgneal@242 12 list_display = ('title', 'slug', 'description', 'count')
bgneal@242 13 prepopulated_fields = {'slug': ('title', )}
bgneal@193 14 readonly_fields = ('count', )
bgneal@193 15
bgneal@193 16
bgneal@204 17 class PendingLinkAdmin(admin.ModelAdmin):
bgneal@204 18 list_display = ('title', 'url', 'user', 'category', 'date_added')
bgneal@204 19 raw_id_fields = ('user', )
bgneal@204 20 actions = ('approve_links', )
bgneal@277 21 readonly_fields = ('update_date', )
bgneal@204 22
bgneal@204 23 def approve_links(self, request, qs):
bgneal@204 24 for pending_link in qs:
bgneal@204 25 link = Link(category=pending_link.category,
bgneal@204 26 title=pending_link.title,
bgneal@204 27 url=pending_link.url,
bgneal@204 28 description=pending_link.description,
bgneal@204 29 user=pending_link.user,
bgneal@204 30 date_added=datetime.datetime.now(),
bgneal@204 31 hits=0,
bgneal@204 32 is_public=True)
bgneal@204 33 link.save()
bgneal@204 34 pending_link.delete()
bgneal@204 35
bgneal@204 36 approve_links.short_description = "Approve selected links"
bgneal@204 37
bgneal@204 38
gremmie@1 39 class LinkAdmin(admin.ModelAdmin):
gremmie@1 40 list_display = ('title', 'url', 'category', 'date_added', 'hits', 'is_public')
gremmie@1 41 list_filter = ('date_added', 'is_public', 'category')
gremmie@1 42 date_hierarchy = 'date_added'
gremmie@1 43 ordering = ('-date_added', )
gremmie@1 44 search_fields = ('title', 'description', 'url', 'user__username')
gremmie@1 45 raw_id_fields = ('user', )
bgneal@277 46 readonly_fields = ('update_date', )
gremmie@1 47 save_on_top = True
gremmie@1 48
bgneal@193 49
gremmie@1 50 class FlaggedLinkAdmin(admin.ModelAdmin):
bgneal@165 51 list_display = ('__unicode__', 'url', 'get_link_url', 'user', 'date_flagged')
gremmie@1 52 date_hierarchy = 'date_flagged'
gremmie@1 53 raw_id_fields = ('user', )
gremmie@1 54
bgneal@193 55 admin.site.register(Category, CategoryAdmin)
bgneal@204 56 admin.site.register(PendingLink, PendingLinkAdmin)
gremmie@1 57 admin.site.register(Link, LinkAdmin)
gremmie@1 58 admin.site.register(FlaggedLink, FlaggedLinkAdmin)