1 from django import forms
2 from django.contrib import admin
3 from django.core.urlresolvers import reverse
4 from django.http import HttpResponseRedirect, QueryDict
6 from philo.admin import EntityAdmin, AddTagAdmin, COLLAPSE_CLASSES
7 from philo.contrib.penfield.models import BlogEntry, Blog, BlogView, Newsletter, NewsletterArticle, NewsletterIssue, NewsletterView
10 class DelayedDateForm(forms.ModelForm):
13 def __init__(self, *args, **kwargs):
14 super(DelayedDateForm, self).__init__(*args, **kwargs)
15 self.fields[self.date_field].required = False
18 class TitledAdmin(EntityAdmin):
19 prepopulated_fields = {'slug': ('title',)}
20 list_display = ('title', 'slug')
23 class BlogAdmin(TitledAdmin):
27 class BlogEntryAdmin(TitledAdmin, AddTagAdmin):
28 form = DelayedDateForm
29 filter_horizontal = ['tags']
30 list_filter = ['author', 'blog']
31 date_hierarchy = 'date'
32 search_fields = ('content',)
33 list_display = ['title', 'date', 'author']
34 raw_id_fields = ('author',)
37 'fields': ('title', 'author', 'blog')
40 'fields': ('content', 'excerpt', 'tags'),
43 'fields': ('slug', 'date'),
44 'classes': COLLAPSE_CLASSES
47 related_lookup_fields = {'fk': raw_id_fields}
50 class BlogViewAdmin(EntityAdmin):
56 'fields': ('index_page', 'entry_page', 'tag_page')
59 'fields': ('entry_archive_page', 'tag_archive_page')
61 ('General Settings', {
62 'fields': ('entry_permalink_style', 'entry_permalink_base', 'tag_permalink_base', 'entries_per_page'),
63 'classes': COLLAPSE_CLASSES
66 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'feed_length', 'item_title_template', 'item_description_template',),
67 'classes': COLLAPSE_CLASSES
70 raw_id_fields = ('index_page', 'entry_page', 'tag_page', 'entry_archive_page', 'tag_archive_page', 'item_title_template', 'item_description_template',)
71 related_lookup_fields = {'fk': raw_id_fields}
74 class NewsletterAdmin(TitledAdmin):
78 class NewsletterArticleAdmin(TitledAdmin, AddTagAdmin):
79 form = DelayedDateForm
80 filter_horizontal = ('tags', 'authors')
81 list_filter = ('newsletter',)
82 date_hierarchy = 'date'
83 search_fields = ('title', 'authors__name',)
84 list_display = ['title', 'date', 'author_names']
87 'fields': ('title', 'authors', 'newsletter')
90 'fields': ('full_text', 'lede', 'tags')
93 'fields': ('slug', 'date'),
94 'classes': COLLAPSE_CLASSES
97 actions = ['make_issue']
99 def author_names(self, obj):
100 return ', '.join([author.get_full_name() for author in obj.authors.all()])
101 author_names.short_description = "Authors"
103 def make_issue(self, request, queryset):
104 opts = NewsletterIssue._meta
105 info = opts.app_label, opts.module_name
106 url = reverse("admin:%s_%s_add" % info)
107 return HttpResponseRedirect("%s?articles=%s" % (url, ",".join([str(a.pk) for a in queryset])))
108 make_issue.short_description = u"Create issue from selected %(verbose_name_plural)s"
111 class NewsletterIssueAdmin(TitledAdmin):
112 filter_horizontal = TitledAdmin.filter_horizontal + ('articles',)
115 class NewsletterViewAdmin(EntityAdmin):
118 'fields': ('newsletter',)
121 'fields': ('index_page', 'article_page', 'issue_page')
124 'fields': ('article_archive_page', 'issue_archive_page')
127 'fields': ('article_permalink_style', 'article_permalink_base', 'issue_permalink_base'),
128 'classes': COLLAPSE_CLASSES
131 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'feed_length', 'item_title_template', 'item_description_template',),
132 'classes': COLLAPSE_CLASSES
135 raw_id_fields = ('index_page', 'article_page', 'issue_page', 'article_archive_page', 'issue_archive_page', 'item_title_template', 'item_description_template',)
136 related_lookup_fields = {'fk': raw_id_fields}
139 admin.site.register(Blog, BlogAdmin)
140 admin.site.register(BlogEntry, BlogEntryAdmin)
141 admin.site.register(BlogView, BlogViewAdmin)
142 admin.site.register(Newsletter, NewsletterAdmin)
143 admin.site.register(NewsletterArticle, NewsletterArticleAdmin)
144 admin.site.register(NewsletterIssue, NewsletterIssueAdmin)
145 admin.site.register(NewsletterView, NewsletterViewAdmin)