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, COLLAPSE_CLASSES
7 from philo.admin.widgets import EmbedWidget
8 from philo.contrib.penfield.models import BlogEntry, Blog, BlogView, Newsletter, NewsletterArticle, NewsletterIssue, NewsletterView
9 from philo.models.fields import TemplateField
12 class DelayedDateForm(forms.ModelForm):
15 def __init__(self, *args, **kwargs):
16 super(DelayedDateForm, self).__init__(*args, **kwargs)
17 self.fields[self.date_field].required = False
20 class BlogAdmin(EntityAdmin):
21 prepopulated_fields = {'slug': ('title',)}
22 list_display = ('title', 'slug')
25 class BlogEntryAdmin(EntityAdmin):
26 form = DelayedDateForm
27 list_filter = ['author', 'blog']
28 date_hierarchy = 'date'
29 search_fields = ('content',)
30 list_display = ['title', 'date', 'author']
31 raw_id_fields = ('author',)
34 'fields': ('title', 'author', 'blog')
37 'fields': ('content', 'excerpt', 'tags'),
40 'fields': ('slug', 'date'),
41 'classes': COLLAPSE_CLASSES
44 related_lookup_fields = {'fk': raw_id_fields}
45 prepopulated_fields = {'slug': ('title',)}
46 formfield_overrides = {
47 TemplateField: {'widget': EmbedWidget}
51 class BlogViewAdmin(EntityAdmin):
57 'fields': ('index_page', 'entry_page', 'tag_page')
60 'fields': ('entry_archive_page', 'tag_archive_page')
62 ('General Settings', {
63 'fields': ('entry_permalink_style', 'entry_permalink_base', 'tag_permalink_base', 'entries_per_page'),
64 'classes': COLLAPSE_CLASSES
67 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'feed_length', 'item_title_template', 'item_description_template',),
68 'classes': COLLAPSE_CLASSES
71 raw_id_fields = ('index_page', 'entry_page', 'tag_page', 'entry_archive_page', 'tag_archive_page', 'item_title_template', 'item_description_template',)
72 related_lookup_fields = {'fk': raw_id_fields}
75 class NewsletterAdmin(EntityAdmin):
76 prepopulated_fields = {'slug': ('title',)}
77 list_display = ('title', 'slug')
80 class NewsletterArticleAdmin(EntityAdmin):
81 form = DelayedDateForm
82 filter_horizontal = ('authors',)
83 list_filter = ('newsletter',)
84 date_hierarchy = 'date'
85 search_fields = ('title', 'authors__name',)
86 list_display = ['title', 'date', 'author_names']
89 'fields': ('title', 'authors', 'newsletter')
92 'fields': ('full_text', 'lede', 'tags')
95 'fields': ('slug', 'date'),
96 'classes': COLLAPSE_CLASSES
99 actions = ['make_issue']
100 prepopulated_fields = {'slug': ('title',)}
101 formfield_overrides = {
102 TemplateField: {'widget': EmbedWidget}
105 def author_names(self, obj):
106 return ', '.join([author.get_full_name() for author in obj.authors.all()])
107 author_names.short_description = "Authors"
109 def make_issue(self, request, queryset):
110 opts = NewsletterIssue._meta
111 info = opts.app_label, opts.module_name
112 url = reverse("admin:%s_%s_add" % info)
113 return HttpResponseRedirect("%s?articles=%s" % (url, ",".join([str(a.pk) for a in queryset])))
114 make_issue.short_description = u"Create issue from selected %(verbose_name_plural)s"
117 class NewsletterIssueAdmin(EntityAdmin):
118 filter_horizontal = ('articles',)
119 prepopulated_fields = {'slug': ('title',)}
120 list_display = ('title', 'slug')
123 class NewsletterViewAdmin(EntityAdmin):
126 'fields': ('newsletter',)
129 'fields': ('index_page', 'article_page', 'issue_page')
132 'fields': ('article_archive_page', 'issue_archive_page')
135 'fields': ('article_permalink_style', 'article_permalink_base', 'issue_permalink_base'),
136 'classes': COLLAPSE_CLASSES
139 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'feed_length', 'item_title_template', 'item_description_template',),
140 'classes': COLLAPSE_CLASSES
143 raw_id_fields = ('index_page', 'article_page', 'issue_page', 'article_archive_page', 'issue_archive_page', 'item_title_template', 'item_description_template',)
144 related_lookup_fields = {'fk': raw_id_fields}
147 admin.site.register(Blog, BlogAdmin)
148 admin.site.register(BlogEntry, BlogEntryAdmin)
149 admin.site.register(BlogView, BlogViewAdmin)
150 admin.site.register(Newsletter, NewsletterAdmin)
151 admin.site.register(NewsletterArticle, NewsletterArticleAdmin)
152 admin.site.register(NewsletterIssue, NewsletterIssueAdmin)
153 admin.site.register(NewsletterView, NewsletterViewAdmin)