gremmie@1: """This file contains the automatic admin site definitions for the weblinks models""" bgneal@204: import datetime gremmie@1: gremmie@1: from django.contrib import admin gremmie@1: from weblinks.models import Category bgneal@204: from weblinks.models import PendingLink gremmie@1: from weblinks.models import Link gremmie@1: from weblinks.models import FlaggedLink gremmie@1: bgneal@193: bgneal@193: class CategoryAdmin(admin.ModelAdmin): bgneal@675: list_display = ['title', 'slug', 'description', 'count'] bgneal@675: prepopulated_fields = {'slug': ['title', ]} bgneal@675: readonly_fields = ['count'] bgneal@193: bgneal@193: bgneal@204: class PendingLinkAdmin(admin.ModelAdmin): bgneal@675: list_display = ['title', 'url', 'user', 'category', 'date_added'] bgneal@675: raw_id_fields = ['user'] bgneal@675: actions = ['approve_links'] bgneal@675: readonly_fields = ['update_date'] bgneal@204: bgneal@204: def approve_links(self, request, qs): bgneal@204: for pending_link in qs: bgneal@204: link = Link(category=pending_link.category, bgneal@204: title=pending_link.title, bgneal@204: url=pending_link.url, bgneal@204: description=pending_link.description, bgneal@204: user=pending_link.user, bgneal@204: date_added=datetime.datetime.now(), bgneal@204: hits=0, bgneal@204: is_public=True) bgneal@204: link.save() bgneal@204: pending_link.delete() bgneal@204: bgneal@398: count = len(qs) bgneal@398: msg = "1 link" if count == 1 else "%d links" % count bgneal@398: self.message_user(request, "%s approved." % msg) bgneal@398: bgneal@204: approve_links.short_description = "Approve selected links" bgneal@204: bgneal@204: gremmie@1: class LinkAdmin(admin.ModelAdmin): bgneal@675: list_display = ['title', 'url', 'category', 'date_added', 'hits', 'is_public'] bgneal@675: list_filter = ['date_added', 'is_public', 'category'] gremmie@1: date_hierarchy = 'date_added' bgneal@675: ordering = ['-date_added'] bgneal@675: search_fields = ['title', 'description', 'url', 'user__username'] bgneal@675: raw_id_fields = ['user'] bgneal@675: readonly_fields = ['update_date'] gremmie@1: save_on_top = True gremmie@1: bgneal@193: gremmie@1: class FlaggedLinkAdmin(admin.ModelAdmin): bgneal@675: list_display = ['__unicode__', 'url', 'get_link_url', 'user', 'date_flagged'] gremmie@1: date_hierarchy = 'date_flagged' bgneal@675: raw_id_fields = ['user'] bgneal@675: bgneal@675: actions = ['hide_links'] bgneal@675: bgneal@675: def hide_links(self, request, qs): bgneal@675: for flagged_link in qs: bgneal@675: flagged_link.link.is_public = False bgneal@675: flagged_link.link.save() bgneal@675: bgneal@675: count = len(qs) bgneal@675: qs.delete() bgneal@675: msg = "1 link" if count == 1 else "%d links" % count bgneal@675: self.message_user(request, "%s hidden." % msg) bgneal@675: bgneal@675: hide_links.short_description = "Accept flags & hide links" bgneal@675: gremmie@1: bgneal@193: admin.site.register(Category, CategoryAdmin) bgneal@204: admin.site.register(PendingLink, PendingLinkAdmin) gremmie@1: admin.site.register(Link, LinkAdmin) gremmie@1: admin.site.register(FlaggedLink, FlaggedLinkAdmin)