annotate news/admin.py @ 999:8386a8ebcbc7

WIP News v2.0 admin changes.
author Brian Neal <bgneal@gmail.com>
date Sat, 21 Nov 2015 14:53:29 -0600
parents 6dba56996a21
children c6c3ba5cf6eb
rev   line source
gremmie@1 1 """
gremmie@1 2 This file contains the automatic admin site definitions for the News models.
gremmie@1 3 """
gremmie@1 4 from django.contrib import admin
bgneal@7 5 from django.conf import settings
bgneal@7 6
gremmie@1 7 from news.models import PendingStory
gremmie@1 8 from news.models import Story
gremmie@1 9 from news.models import Category
gremmie@1 10
bgneal@669 11 import ftfy
bgneal@669 12
bgneal@240 13
bgneal@240 14 class CategoryAdmin(admin.ModelAdmin):
bgneal@240 15 prepopulated_fields = {'slug': ("title", )}
bgneal@669 16 list_display = ['title', 'slug']
bgneal@240 17
bgneal@240 18
gremmie@1 19 class PendingStoryAdmin(admin.ModelAdmin):
bgneal@669 20 list_display = ['title', 'date_submitted', 'submitter']
bgneal@669 21 list_filter = ['date_submitted']
bgneal@669 22 search_fields = ['title', 'short_text', 'long_text']
bgneal@204 23 date_hierarchy = 'date_submitted'
bgneal@669 24 actions = ['approve_story']
bgneal@999 25 readonly_fields = ['update_date', 'version']
bgneal@669 26 raw_id_fields = ['submitter']
gremmie@1 27
bgneal@999 28 fieldsets = [
bgneal@999 29 (None, {
bgneal@999 30 'fields': ['title', 'submitter', 'category'],
bgneal@999 31 }),
bgneal@999 32 ('New Markdown Fields', {
bgneal@999 33 'fields': ['short_markup', 'long_markup'],
bgneal@999 34 }),
bgneal@999 35 ('HTML Fields', {
bgneal@999 36 'fields': ['short_text', 'long_text', 'admin_content'],
bgneal@999 37 'classes': ['collapse'],
bgneal@999 38 }),
bgneal@999 39 ('Meta Fields', {
bgneal@999 40 'fields': [
bgneal@999 41 'date_submitted', 'allow_comments', 'tags',
bgneal@999 42 'front_page_expiration', 'priority', 'meta_description',
bgneal@999 43 ]
bgneal@999 44 }),
bgneal@999 45 ('Read-Only Fields', {
bgneal@999 46 'fields': ['update_date', 'version'],
bgneal@999 47 'classes': ['collapse'],
bgneal@999 48 }),
bgneal@999 49 ]
bgneal@999 50
bgneal@204 51 def approve_story(self, request, qs):
bgneal@204 52 for pending_story in qs:
bgneal@204 53 story = Story(
bgneal@204 54 title=pending_story.title,
bgneal@204 55 submitter=pending_story.submitter,
bgneal@204 56 category=pending_story.category,
bgneal@204 57 short_text=pending_story.short_text,
bgneal@204 58 long_text=pending_story.long_text,
bgneal@277 59 date_submitted=pending_story.date_submitted,
bgneal@204 60 allow_comments=pending_story.allow_comments,
bgneal@218 61 tags=pending_story.tags,
bgneal@462 62 front_page_expiration=pending_story.front_page_expiration,
bgneal@486 63 priority=pending_story.priority,
bgneal@999 64 meta_description=pending_story.meta_description,
bgneal@999 65 short_markup=pending_story.short_markup,
bgneal@999 66 long_markup=pending_story.long_markup,
bgneal@999 67 admin_content=pending_story.admin_content)
bgneal@204 68 story.save()
bgneal@204 69 pending_story.delete()
bgneal@204 70
bgneal@398 71 count = len(qs)
bgneal@398 72 msg = "1 story" if count == 1 else "%d stories" % count
bgneal@398 73 self.message_user(request, "%s approved." % msg)
bgneal@398 74
bgneal@204 75 approve_story.short_description = "Approve selected pending stories"
bgneal@204 76
bgneal@204 77 class Media:
bgneal@486 78 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1 79
gremmie@1 80
gremmie@1 81 class StoryAdmin(admin.ModelAdmin):
bgneal@669 82 list_display = ['title', 'date_submitted', 'submitter', 'category']
bgneal@669 83 list_filter = ['date_submitted', 'category']
bgneal@669 84 search_fields = ['title', 'short_text', 'long_text']
bgneal@204 85 date_hierarchy = 'date_submitted'
bgneal@999 86 readonly_fields = ['update_date', 'version']
bgneal@669 87 raw_id_fields = ['submitter']
bgneal@669 88 actions = ['fix_text']
bgneal@669 89
bgneal@999 90 fieldsets = PendingStoryAdmin.fieldsets
bgneal@999 91
bgneal@669 92 def fix_text(self, request, qs):
bgneal@669 93 for story in qs:
bgneal@669 94 story.title = ftfy.fix_text(story.title)
bgneal@669 95 story.short_text = ftfy.fix_text(story.short_text)
bgneal@669 96 story.long_text = ftfy.fix_text(story.long_text)
bgneal@669 97 story.save()
bgneal@669 98
bgneal@669 99 count = len(qs)
bgneal@669 100 msg = "1 story" if count == 1 else "%d stories" % count
bgneal@669 101 self.message_user(request, "Text fixed on {}".format(msg))
bgneal@669 102
bgneal@669 103 fix_text.short_description = "Fix text on selected stories"
gremmie@1 104
bgneal@204 105 class Media:
bgneal@486 106 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1 107
gremmie@1 108
bgneal@240 109 admin.site.register(Category, CategoryAdmin)
gremmie@1 110 admin.site.register(Story, StoryAdmin)
gremmie@1 111 admin.site.register(PendingStory, PendingStoryAdmin)