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.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(AddTagAdmin):
26 form = DelayedDateForm
27 filter_horizontal = ['tags']
28 list_filter = ['author', 'blog']
29 date_hierarchy = 'date'
30 search_fields = ('content',)
31 list_display = ['title', 'date', 'author']
32 raw_id_fields = ('author',)
35 'fields': ('title', 'author', 'blog')
38 'fields': ('content', 'excerpt', 'tags'),
41 'fields': ('slug', 'date'),
42 'classes': COLLAPSE_CLASSES
45 related_lookup_fields = {'fk': raw_id_fields}
46 prepopulated_fields = {'slug': ('title',)}
47 formfield_overrides = {
48 TemplateField: {'widget': EmbedWidget}
52 class BlogViewAdmin(EntityAdmin):
58 'fields': ('index_page', 'entry_page', 'tag_page')
61 'fields': ('entry_archive_page', 'tag_archive_page')
63 ('General Settings', {
64 'fields': ('entry_permalink_style', 'entry_permalink_base', 'tag_permalink_base', 'entries_per_page'),
65 'classes': COLLAPSE_CLASSES
68 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'feed_length', 'item_title_template', 'item_description_template',),
69 'classes': COLLAPSE_CLASSES
72 raw_id_fields = ('index_page', 'entry_page', 'tag_page', 'entry_archive_page', 'tag_archive_page', 'item_title_template', 'item_description_template',)
73 related_lookup_fields = {'fk': raw_id_fields}
76 class NewsletterAdmin(EntityAdmin):
77 prepopulated_fields = {'slug': ('title',)}
78 list_display = ('title', 'slug')
81 class NewsletterArticleAdmin(AddTagAdmin):
82 form = DelayedDateForm
83 filter_horizontal = ('tags', 'authors')
84 list_filter = ('newsletter',)
85 date_hierarchy = 'date'
86 search_fields = ('title', 'authors__name',)
87 list_display = ['title', 'date', 'author_names']
90 'fields': ('title', 'authors', 'newsletter')
93 'fields': ('full_text', 'lede', 'tags')
96 'fields': ('slug', 'date'),
97 'classes': COLLAPSE_CLASSES
100 actions = ['make_issue']
101 prepopulated_fields = {'slug': ('title',)}
102 formfield_overrides = {
103 TemplateField: {'widget': EmbedWidget}
106 def author_names(self, obj):
107 return ', '.join([author.get_full_name() for author in obj.authors.all()])
108 author_names.short_description = "Authors"
110 def make_issue(self, request, queryset):
111 opts = NewsletterIssue._meta
112 info = opts.app_label, opts.module_name
113 url = reverse("admin:%s_%s_add" % info)
114 return HttpResponseRedirect("%s?articles=%s" % (url, ",".join([str(a.pk) for a in queryset])))
115 make_issue.short_description = u"Create issue from selected %(verbose_name_plural)s"
118 class NewsletterIssueAdmin(EntityAdmin):
119 filter_horizontal = ('articles',)
120 prepopulated_fields = {'slug': ('title',)}
121 list_display = ('title', 'slug')
124 class NewsletterViewAdmin(EntityAdmin):
127 'fields': ('newsletter',)
130 'fields': ('index_page', 'article_page', 'issue_page')
133 'fields': ('article_archive_page', 'issue_archive_page')
136 'fields': ('article_permalink_style', 'article_permalink_base', 'issue_permalink_base'),
137 'classes': COLLAPSE_CLASSES
140 'fields': ( 'feeds_enabled', 'feed_suffix', 'feed_type', 'feed_length', 'item_title_template', 'item_description_template',),
141 'classes': COLLAPSE_CLASSES
144 raw_id_fields = ('index_page', 'article_page', 'issue_page', 'article_archive_page', 'issue_archive_page', 'item_title_template', 'item_description_template',)
145 related_lookup_fields = {'fk': raw_id_fields}
148 admin.site.register(Blog, BlogAdmin)
149 admin.site.register(BlogEntry, BlogEntryAdmin)
150 admin.site.register(BlogView, BlogViewAdmin)
151 admin.site.register(Newsletter, NewsletterAdmin)
152 admin.site.register(NewsletterArticle, NewsletterArticleAdmin)
153 admin.site.register(NewsletterIssue, NewsletterIssueAdmin)
154 admin.site.register(NewsletterView, NewsletterViewAdmin)