Merge branch 'taggit' into develop
[philo.git] / philo / admin / pages.py
1 from django import forms
2 from django.conf import settings
3 from django.contrib import admin
4
5 from philo.admin.base import COLLAPSE_CLASSES, TreeEntityAdmin
6 from philo.admin.forms.containers import *
7 from philo.admin.nodes import ViewAdmin
8 from philo.admin.widgets import EmbedWidget
9 from philo.models.fields import TemplateField
10 from philo.models.pages import Page, Template, Contentlet, ContentReference
11
12
13 class ContainerInline(admin.StackedInline):
14         extra = 0
15         max_num = 0
16         can_delete = False
17         classes = ('collapse-open', 'collapse','open')
18         if 'grappelli' in settings.INSTALLED_APPS:
19                 template = 'admin/philo/edit_inline/grappelli_tabular_container.html'
20         else:
21                 template = 'admin/philo/edit_inline/tabular_container.html'
22
23
24 class ContentletInline(ContainerInline):
25         model = Contentlet
26         formset = ContentletInlineFormSet
27         form = ContentletForm
28
29
30 class ContentReferenceInline(ContainerInline):
31         model = ContentReference
32         formset = ContentReferenceInlineFormSet
33         form = ContentReferenceForm
34
35
36 class PageAdmin(ViewAdmin):
37         add_form_template = 'admin/philo/page/add_form.html'
38         fieldsets = (
39                 (None, {
40                         'fields': ('title', 'template')
41                 }),
42         )
43         list_display = ('title', 'template')
44         list_filter = ('template',)
45         search_fields = ['title', 'contentlets__content']
46         inlines = [ContentletInline, ContentReferenceInline] + ViewAdmin.inlines
47         
48         def response_add(self, request, obj, post_url_continue='../%s/'):
49                 # Shamelessly cribbed from django/contrib/auth/admin.py:143
50                 if '_addanother' not in request.POST and '_popup' not in request.POST:
51                         request.POST['_continue'] = 1
52                 return super(PageAdmin, self).response_add(request, obj, post_url_continue)
53
54
55 class TemplateAdmin(TreeEntityAdmin):
56         prepopulated_fields = {'slug': ('name',)}
57         fieldsets = (
58                 (None, {
59                         'fields': ('parent', 'name', 'slug')
60                 }),
61                 ('Documentation', {
62                         'classes': COLLAPSE_CLASSES,
63                         'fields': ('documentation',)
64                 }),
65                 (None, {
66                         'fields': ('code',)
67                 }),
68                 ('Advanced', {
69                         'classes': COLLAPSE_CLASSES,
70                         'fields': ('mimetype',)
71                 }),
72         )
73         formfield_overrides = {
74                 TemplateField: {'widget': EmbedWidget}
75         }
76         save_on_top = True
77         save_as = True
78         list_display = ('__unicode__', 'slug', 'get_path',)
79
80
81 admin.site.register(Page, PageAdmin)
82 admin.site.register(Template, TemplateAdmin)