gremmie@1: """
gremmie@1: This file contains the automatic admin site definitions for the News models.
gremmie@1: """
gremmie@1: from django.contrib import admin
bgneal@7: from django.conf import settings
bgneal@7: 
bgneal@1001: from forums.tools import create_topic
gremmie@1: from news.models import PendingStory
gremmie@1: from news.models import Story
gremmie@1: from news.models import Category
gremmie@1: 
bgneal@669: import ftfy
bgneal@669: 
bgneal@240: 
bgneal@1027: COMMENT_THREAD_BODY = (u"This topic was automatically created to discuss the "
bgneal@1027:         u"news story [{title}]({url}).")
bgneal@1001: 
bgneal@1001: 
bgneal@240: class CategoryAdmin(admin.ModelAdmin):
bgneal@240:     prepopulated_fields = {'slug': ("title", )}
bgneal@1001:     list_display = ['title', 'slug', 'forum_slug']
bgneal@1001:     list_editable = ['forum_slug']
bgneal@240: 
bgneal@240: 
gremmie@1: class PendingStoryAdmin(admin.ModelAdmin):
bgneal@669:     list_display = ['title', 'date_submitted', 'submitter']
bgneal@669:     list_filter = ['date_submitted']
bgneal@669:     search_fields = ['title', 'short_text', 'long_text']
bgneal@204:     date_hierarchy = 'date_submitted'
bgneal@1001:     actions = ['approve_stories']
bgneal@999:     readonly_fields = ['update_date', 'version']
bgneal@669:     raw_id_fields = ['submitter']
gremmie@1: 
bgneal@999:     fieldsets = [
bgneal@999:         (None, {
bgneal@999:             'fields': ['title', 'submitter', 'category'],
bgneal@999:             }),
bgneal@999:         ('New Markdown Fields', {
bgneal@999:             'fields': ['short_markup', 'long_markup'],
bgneal@999:             }),
bgneal@999:         ('HTML Fields', {
bgneal@999:             'fields': ['short_text', 'long_text', 'admin_content'],
bgneal@999:             'classes': ['collapse'],
bgneal@999:             }),
bgneal@999:         ('Meta Fields', {
bgneal@999:             'fields': [
bgneal@999:                 'date_submitted', 'allow_comments', 'tags',
bgneal@999:                 'front_page_expiration', 'priority', 'meta_description',
bgneal@999:                 ]
bgneal@999:             }),
bgneal@999:         ('Read-Only Fields', {
bgneal@999:             'fields': ['update_date', 'version'],
bgneal@999:             'classes': ['collapse'],
bgneal@999:             }),
bgneal@999:     ]
bgneal@999: 
bgneal@1001:     def approve_stories(self, request, qs):
bgneal@204:         for pending_story in qs:
bgneal@1001:             self._approve_story(pending_story)
bgneal@204: 
bgneal@398:         count = len(qs)
bgneal@398:         msg = "1 story" if count == 1 else "%d stories" % count
bgneal@398:         self.message_user(request, "%s approved." % msg)
bgneal@398: 
bgneal@1001:     approve_stories.short_description = "Approve selected pending stories"
bgneal@1001: 
bgneal@1001:     def _approve_story(self, pending_story):
bgneal@1001:         story = Story(
bgneal@1001:                 title=pending_story.title,
bgneal@1001:                 submitter=pending_story.submitter,
bgneal@1001:                 category=pending_story.category,
bgneal@1001:                 short_text=pending_story.short_text,
bgneal@1001:                 long_text=pending_story.long_text,
bgneal@1001:                 date_submitted=pending_story.date_submitted,
bgneal@1001:                 allow_comments=pending_story.allow_comments,
bgneal@1001:                 tags=pending_story.tags,
bgneal@1001:                 front_page_expiration=pending_story.front_page_expiration,
bgneal@1001:                 priority=pending_story.priority,
bgneal@1001:                 meta_description=pending_story.meta_description,
bgneal@1001:                 short_markup=pending_story.short_markup,
bgneal@1001:                 long_markup=pending_story.long_markup,
bgneal@1005:                 admin_content=pending_story.admin_content,
bgneal@1005:                 version=pending_story.version)
bgneal@1001:         story.save()
bgneal@1001:         pending_story.delete()
bgneal@1001: 
bgneal@1001:         # Create comment thread if configured to do so.
bgneal@1001:         forum_slug = story.category.forum_slug
bgneal@1001:         if story.allow_comments and forum_slug:
bgneal@1001:             post_body = COMMENT_THREAD_BODY.format(title=story.title,
bgneal@1001:                                                    url=story.get_absolute_url())
bgneal@1001:             topic = create_topic(forum_slug, story.submitter, story.title, post_body)
bgneal@1001:             story.forums_topic = topic
bgneal@1001:             story.save()
bgneal@204: 
bgneal@204:     class Media:
bgneal@486:         js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1: 
gremmie@1: 
gremmie@1: class StoryAdmin(admin.ModelAdmin):
bgneal@669:     list_display = ['title', 'date_submitted', 'submitter', 'category']
bgneal@669:     list_filter = ['date_submitted', 'category']
bgneal@669:     search_fields = ['title', 'short_text', 'long_text']
bgneal@204:     date_hierarchy = 'date_submitted'
bgneal@999:     readonly_fields = ['update_date', 'version']
bgneal@1036:     raw_id_fields = ['submitter', 'forums_topic']
bgneal@669:     actions = ['fix_text']
bgneal@669: 
bgneal@1036:     fieldsets = PendingStoryAdmin.fieldsets + [
bgneal@1036:         ('Comments', {
bgneal@1036:             'fields': ['forums_topic'],
bgneal@1036:             })
bgneal@1036:     ]
bgneal@999: 
bgneal@669:     def fix_text(self, request, qs):
bgneal@669:         for story in qs:
bgneal@669:             story.title = ftfy.fix_text(story.title)
bgneal@669:             story.short_text = ftfy.fix_text(story.short_text)
bgneal@669:             story.long_text = ftfy.fix_text(story.long_text)
bgneal@669:             story.save()
bgneal@669: 
bgneal@669:         count = len(qs)
bgneal@669:         msg = "1 story" if count == 1 else "%d stories" % count
bgneal@669:         self.message_user(request, "Text fixed on {}".format(msg))
bgneal@669: 
bgneal@669:     fix_text.short_description = "Fix text on selected stories"
gremmie@1: 
bgneal@204:     class Media:
bgneal@486:         js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce']
gremmie@1: 
gremmie@1: 
bgneal@240: admin.site.register(Category, CategoryAdmin)
gremmie@1: admin.site.register(Story, StoryAdmin)
gremmie@1: admin.site.register(PendingStory, PendingStoryAdmin)