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
5 from philo.admin import EntityAdmin, AddTagAdmin, COLLAPSE_CLASSES
6 from philo.contrib.penfield.models import BlogEntry, Blog, BlogView, Newsletter, NewsletterArticle, NewsletterIssue, NewsletterView
9 class DelayedDateForm(forms.ModelForm):
12 def __init__(self, *args, **kwargs):
13 super(DelayedDateForm, self).__init__(*args, **kwargs)
14 self.fields[self.date_field].required = False
17 class TitledAdmin(EntityAdmin):
18 prepopulated_fields = {'slug': ('title',)}
19 list_display = ('title', 'slug')
22 class BlogAdmin(TitledAdmin):
26 class BlogEntryAdmin(TitledAdmin, AddTagAdmin):
27 form = DelayedDateForm
28 filter_horizontal = ['tags']
29 list_filter = ['author', 'blog']
30 date_hierarchy = 'date'
31 search_fields = ('content',)
32 list_display = ['title', 'date', 'author']
33 raw_id_fields = ('author',)
36 'fields': ('title', 'author', 'blog')
39 'fields': ('content', 'excerpt', 'tags'),
42 'fields': ('slug', 'date'),
43 'classes': COLLAPSE_CLASSES
46 related_lookup_fields = {'fk': raw_id_fields}
49 class BlogViewAdmin(EntityAdmin):
55 'fields': ('index_page', 'entry_page', 'tag_page')
58 'fields': ('entry_archive_page', 'tag_archive_page')
60 ('General Settings', {
61 'fields': ('entry_permalink_style', 'entry_permalink_base', 'tag_permalink_base', 'entries_per_page'),
62 'classes': COLLAPSE_CLASSES
65 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'item_title_template', 'item_description_template',),
66 'classes': COLLAPSE_CLASSES
69 raw_id_fields = ('index_page', 'entry_page', 'tag_page', 'entry_archive_page', 'tag_archive_page', 'item_title_template', 'item_description_template',)
70 related_lookup_fields = {'fk': raw_id_fields}
73 class NewsletterAdmin(TitledAdmin):
77 class NewsletterArticleAdmin(TitledAdmin, AddTagAdmin):
78 form = DelayedDateForm
79 filter_horizontal = ('tags', 'authors')
80 list_filter = ('newsletter',)
81 date_hierarchy = 'date'
82 search_fields = ('title', 'authors__name',)
83 list_display = ['title', 'date', 'author_names']
86 'fields': ('title', 'authors', 'newsletter')
89 'fields': ('full_text', 'lede', 'tags')
92 'fields': ('slug', 'date'),
93 'classes': COLLAPSE_CLASSES
96 actions = ['make_issue']
98 def author_names(self, obj):
99 return ', '.join([author.get_full_name() for author in obj.authors.all()])
100 author_names.short_description = "Authors"
102 def make_issue(self, request, queryset):
103 opts = NewsletterIssue._meta
104 info = opts.app_label, opts.module_name
105 url = reverse("admin:%s_%s_add" % info)
106 return HttpResponseRedirect("%s?articles=%s" % (url, ",".join([str(a.pk) for a in queryset])))
107 make_issue.short_description = u"Create issue from selected %(verbose_name_plural)s"
110 class NewsletterIssueAdmin(TitledAdmin):
111 filter_horizontal = TitledAdmin.filter_horizontal + ('articles',)
114 class NewsletterViewAdmin(EntityAdmin):
117 'fields': ('newsletter',)
120 'fields': ('index_page', 'article_page', 'issue_page')
123 'fields': ('article_archive_page', 'issue_archive_page')
126 'fields': ('article_permalink_style', 'article_permalink_base', 'issue_permalink_base'),
127 'classes': COLLAPSE_CLASSES
130 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'feed_length', 'item_title_template', 'item_description_template',),
131 'classes': COLLAPSE_CLASSES
134 raw_id_fields = ('index_page', 'article_page', 'issue_page', 'article_archive_page', 'issue_archive_page', 'item_title_template', 'item_description_template',)
135 related_lookup_fields = {'fk': raw_id_fields}
138 admin.site.register(Blog, BlogAdmin)
139 admin.site.register(BlogEntry, BlogEntryAdmin)
140 admin.site.register(BlogView, BlogViewAdmin)
141 admin.site.register(Newsletter, NewsletterAdmin)
142 admin.site.register(NewsletterArticle, NewsletterArticleAdmin)
143 admin.site.register(NewsletterIssue, NewsletterIssueAdmin)
144 admin.site.register(NewsletterView, NewsletterViewAdmin)