Mercurial > public > sg101
comparison news/admin.py @ 669:6dba56996a21
For issue #30, provide admin action to fix text encoding issues.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 25 May 2013 17:40:15 -0500 |
parents | ee87ea74d46b |
children | 8386a8ebcbc7 |
comparison
equal
deleted
inserted
replaced
668:644f69e1c1e1 | 669:6dba56996a21 |
---|---|
1 """ | 1 """ |
2 This file contains the automatic admin site definitions for the News models. | 2 This file contains the automatic admin site definitions for the News models. |
3 """ | 3 """ |
4 import datetime | |
5 | |
6 from django.contrib import admin | 4 from django.contrib import admin |
7 from django.conf import settings | 5 from django.conf import settings |
8 | 6 |
9 from news.models import PendingStory | 7 from news.models import PendingStory |
10 from news.models import Story | 8 from news.models import Story |
11 from news.models import Category | 9 from news.models import Category |
12 | 10 |
11 import ftfy | |
12 | |
13 | 13 |
14 class CategoryAdmin(admin.ModelAdmin): | 14 class CategoryAdmin(admin.ModelAdmin): |
15 prepopulated_fields = {'slug': ("title", )} | 15 prepopulated_fields = {'slug': ("title", )} |
16 list_display = ('title', 'slug') | 16 list_display = ['title', 'slug'] |
17 | 17 |
18 | 18 |
19 class PendingStoryAdmin(admin.ModelAdmin): | 19 class PendingStoryAdmin(admin.ModelAdmin): |
20 list_display = ('title', 'date_submitted', 'submitter') | 20 list_display = ['title', 'date_submitted', 'submitter'] |
21 list_filter = ('date_submitted', ) | 21 list_filter = ['date_submitted'] |
22 search_fields = ('title', 'short_text', 'long_text') | 22 search_fields = ['title', 'short_text', 'long_text'] |
23 date_hierarchy = 'date_submitted' | 23 date_hierarchy = 'date_submitted' |
24 actions = ('approve_story', ) | 24 actions = ['approve_story'] |
25 readonly_fields = ('update_date', ) | 25 readonly_fields = ['update_date'] |
26 raw_id_fields = ('submitter', ) | 26 raw_id_fields = ['submitter'] |
27 | 27 |
28 def approve_story(self, request, qs): | 28 def approve_story(self, request, qs): |
29 for pending_story in qs: | 29 for pending_story in qs: |
30 story = Story( | 30 story = Story( |
31 title=pending_story.title, | 31 title=pending_story.title, |
51 class Media: | 51 class Media: |
52 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce'] | 52 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce'] |
53 | 53 |
54 | 54 |
55 class StoryAdmin(admin.ModelAdmin): | 55 class StoryAdmin(admin.ModelAdmin): |
56 list_display = ('title', 'date_submitted', 'submitter', 'category') | 56 list_display = ['title', 'date_submitted', 'submitter', 'category'] |
57 list_filter = ('date_submitted', 'category') | 57 list_filter = ['date_submitted', 'category'] |
58 search_fields = ('title', 'short_text', 'long_text') | 58 search_fields = ['title', 'short_text', 'long_text'] |
59 date_hierarchy = 'date_submitted' | 59 date_hierarchy = 'date_submitted' |
60 readonly_fields = ('update_date', ) | 60 readonly_fields = ['update_date'] |
61 raw_id_fields = ('submitter', ) | 61 raw_id_fields = ['submitter'] |
62 actions = ['fix_text'] | |
63 | |
64 def fix_text(self, request, qs): | |
65 for story in qs: | |
66 story.title = ftfy.fix_text(story.title) | |
67 story.short_text = ftfy.fix_text(story.short_text) | |
68 story.long_text = ftfy.fix_text(story.long_text) | |
69 story.save() | |
70 | |
71 count = len(qs) | |
72 msg = "1 story" if count == 1 else "%d stories" % count | |
73 self.message_user(request, "Text fixed on {}".format(msg)) | |
74 | |
75 fix_text.short_description = "Fix text on selected stories" | |
62 | 76 |
63 class Media: | 77 class Media: |
64 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce'] | 78 js = ['js/news_admin.js'] + settings.GPP_THIRD_PARTY_JS['tiny_mce'] |
65 | 79 |
66 | 80 |