annotate news/admin.py @ 1003:fa52bfb28217

Set id on submit news form for image related js.
author Brian Neal <bgneal@gmail.com>
date Thu, 26 Nov 2015 10:47:24 -0600
parents c6c3ba5cf6eb
children 9fc12bbc8c81
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@1001 82 admin_content=pending_story.admin_content)
bgneal@1001 83 story.save()
bgneal@1001 84 pending_story.delete()
bgneal@1001 85
bgneal@1001 86 # Create comment thread if configured to do so.
bgneal@1001 87 forum_slug = story.category.forum_slug
bgneal@1001 88 if story.allow_comments and forum_slug:
bgneal@1001 89 post_body = COMMENT_THREAD_BODY.format(title=story.title,
bgneal@1001 90 url=story.get_absolute_url())
bgneal@1001 91 topic = create_topic(forum_slug, story.submitter, story.title, post_body)
bgneal@1001 92 story.forums_topic = topic
bgneal@1001 93 story.save()
bgneal@204 94
bgneal@204 95 class Media:
bgneal@486 96 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1 97
gremmie@1 98
gremmie@1 99 class StoryAdmin(admin.ModelAdmin):
bgneal@669 100 list_display = ['title', 'date_submitted', 'submitter', 'category']
bgneal@669 101 list_filter = ['date_submitted', 'category']
bgneal@669 102 search_fields = ['title', 'short_text', 'long_text']
bgneal@204 103 date_hierarchy = 'date_submitted'
bgneal@999 104 readonly_fields = ['update_date', 'version']
bgneal@669 105 raw_id_fields = ['submitter']
bgneal@669 106 actions = ['fix_text']
bgneal@669 107
bgneal@999 108 fieldsets = PendingStoryAdmin.fieldsets
bgneal@999 109
bgneal@669 110 def fix_text(self, request, qs):
bgneal@669 111 for story in qs:
bgneal@669 112 story.title = ftfy.fix_text(story.title)
bgneal@669 113 story.short_text = ftfy.fix_text(story.short_text)
bgneal@669 114 story.long_text = ftfy.fix_text(story.long_text)
bgneal@669 115 story.save()
bgneal@669 116
bgneal@669 117 count = len(qs)
bgneal@669 118 msg = "1 story" if count == 1 else "%d stories" % count
bgneal@669 119 self.message_user(request, "Text fixed on {}".format(msg))
bgneal@669 120
bgneal@669 121 fix_text.short_description = "Fix text on selected stories"
gremmie@1 122
bgneal@204 123 class Media:
bgneal@486 124 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1 125
gremmie@1 126
bgneal@240 127 admin.site.register(Category, CategoryAdmin)
gremmie@1 128 admin.site.register(Story, StoryAdmin)
gremmie@1 129 admin.site.register(PendingStory, PendingStoryAdmin)