annotate news/admin.py @ 1005:9fc12bbc8c81

Tweaks for news v2.0 with old news pending stories present.
author Brian Neal <bgneal@gmail.com>
date Thu, 26 Nov 2015 23:16:07 -0600
parents c6c3ba5cf6eb
children cd4db27c90e3
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
bgneal@1001 7 from forums.tools import create_topic
gremmie@1 8 from news.models import PendingStory
gremmie@1 9 from news.models import Story
gremmie@1 10 from news.models import Category
gremmie@1 11
bgneal@669 12 import ftfy
bgneal@669 13
bgneal@240 14
bgneal@1001 15 COMMENT_THREAD_BODY = ("This topic was automatically created to discuss the "
bgneal@1001 16 "news story [{title}]({url}).")
bgneal@1001 17
bgneal@1001 18
bgneal@240 19 class CategoryAdmin(admin.ModelAdmin):
bgneal@240 20 prepopulated_fields = {'slug': ("title", )}
bgneal@1001 21 list_display = ['title', 'slug', 'forum_slug']
bgneal@1001 22 list_editable = ['forum_slug']
bgneal@240 23
bgneal@240 24
gremmie@1 25 class PendingStoryAdmin(admin.ModelAdmin):
bgneal@669 26 list_display = ['title', 'date_submitted', 'submitter']
bgneal@669 27 list_filter = ['date_submitted']
bgneal@669 28 search_fields = ['title', 'short_text', 'long_text']
bgneal@204 29 date_hierarchy = 'date_submitted'
bgneal@1001 30 actions = ['approve_stories']
bgneal@999 31 readonly_fields = ['update_date', 'version']
bgneal@669 32 raw_id_fields = ['submitter']
gremmie@1 33
bgneal@999 34 fieldsets = [
bgneal@999 35 (None, {
bgneal@999 36 'fields': ['title', 'submitter', 'category'],
bgneal@999 37 }),
bgneal@999 38 ('New Markdown Fields', {
bgneal@999 39 'fields': ['short_markup', 'long_markup'],
bgneal@999 40 }),
bgneal@999 41 ('HTML Fields', {
bgneal@999 42 'fields': ['short_text', 'long_text', 'admin_content'],
bgneal@999 43 'classes': ['collapse'],
bgneal@999 44 }),
bgneal@999 45 ('Meta Fields', {
bgneal@999 46 'fields': [
bgneal@999 47 'date_submitted', 'allow_comments', 'tags',
bgneal@999 48 'front_page_expiration', 'priority', 'meta_description',
bgneal@999 49 ]
bgneal@999 50 }),
bgneal@999 51 ('Read-Only Fields', {
bgneal@999 52 'fields': ['update_date', 'version'],
bgneal@999 53 'classes': ['collapse'],
bgneal@999 54 }),
bgneal@999 55 ]
bgneal@999 56
bgneal@1001 57 def approve_stories(self, request, qs):
bgneal@204 58 for pending_story in qs:
bgneal@1001 59 self._approve_story(pending_story)
bgneal@204 60
bgneal@398 61 count = len(qs)
bgneal@398 62 msg = "1 story" if count == 1 else "%d stories" % count
bgneal@398 63 self.message_user(request, "%s approved." % msg)
bgneal@398 64
bgneal@1001 65 approve_stories.short_description = "Approve selected pending stories"
bgneal@1001 66
bgneal@1001 67 def _approve_story(self, pending_story):
bgneal@1001 68 story = Story(
bgneal@1001 69 title=pending_story.title,
bgneal@1001 70 submitter=pending_story.submitter,
bgneal@1001 71 category=pending_story.category,
bgneal@1001 72 short_text=pending_story.short_text,
bgneal@1001 73 long_text=pending_story.long_text,
bgneal@1001 74 date_submitted=pending_story.date_submitted,
bgneal@1001 75 allow_comments=pending_story.allow_comments,
bgneal@1001 76 tags=pending_story.tags,
bgneal@1001 77 front_page_expiration=pending_story.front_page_expiration,
bgneal@1001 78 priority=pending_story.priority,
bgneal@1001 79 meta_description=pending_story.meta_description,
bgneal@1001 80 short_markup=pending_story.short_markup,
bgneal@1001 81 long_markup=pending_story.long_markup,
bgneal@1005 82 admin_content=pending_story.admin_content,
bgneal@1005 83 version=pending_story.version)
bgneal@1001 84 story.save()
bgneal@1001 85 pending_story.delete()
bgneal@1001 86
bgneal@1001 87 # Create comment thread if configured to do so.
bgneal@1001 88 forum_slug = story.category.forum_slug
bgneal@1001 89 if story.allow_comments and forum_slug:
bgneal@1001 90 post_body = COMMENT_THREAD_BODY.format(title=story.title,
bgneal@1001 91 url=story.get_absolute_url())
bgneal@1001 92 topic = create_topic(forum_slug, story.submitter, story.title, post_body)
bgneal@1001 93 story.forums_topic = topic
bgneal@1001 94 story.save()
bgneal@204 95
bgneal@204 96 class Media:
bgneal@486 97 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1 98
gremmie@1 99
gremmie@1 100 class StoryAdmin(admin.ModelAdmin):
bgneal@669 101 list_display = ['title', 'date_submitted', 'submitter', 'category']
bgneal@669 102 list_filter = ['date_submitted', 'category']
bgneal@669 103 search_fields = ['title', 'short_text', 'long_text']
bgneal@204 104 date_hierarchy = 'date_submitted'
bgneal@999 105 readonly_fields = ['update_date', 'version']
bgneal@669 106 raw_id_fields = ['submitter']
bgneal@669 107 actions = ['fix_text']
bgneal@669 108
bgneal@999 109 fieldsets = PendingStoryAdmin.fieldsets
bgneal@999 110
bgneal@669 111 def fix_text(self, request, qs):
bgneal@669 112 for story in qs:
bgneal@669 113 story.title = ftfy.fix_text(story.title)
bgneal@669 114 story.short_text = ftfy.fix_text(story.short_text)
bgneal@669 115 story.long_text = ftfy.fix_text(story.long_text)
bgneal@669 116 story.save()
bgneal@669 117
bgneal@669 118 count = len(qs)
bgneal@669 119 msg = "1 story" if count == 1 else "%d stories" % count
bgneal@669 120 self.message_user(request, "Text fixed on {}".format(msg))
bgneal@669 121
bgneal@669 122 fix_text.short_description = "Fix text on selected stories"
gremmie@1 123
bgneal@204 124 class Media:
bgneal@486 125 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1 126
gremmie@1 127
bgneal@240 128 admin.site.register(Category, CategoryAdmin)
gremmie@1 129 admin.site.register(Story, StoryAdmin)
gremmie@1 130 admin.site.register(PendingStory, PendingStoryAdmin)