Further polished embedding system - allowed for context-dependent embed nodes to...
[philo.git] / admin / base.py
1 from django.conf import settings
2 from django.contrib import admin
3 from django.contrib.contenttypes import generic
4 from django.http import HttpResponse
5 from django.utils import simplejson as json
6 from django.utils.html import escape
7 from philo.models import Tag, Attribute
8 from philo.forms import AttributeForm, AttributeInlineFormSet
9 from philo.admin.widgets import TagFilteredSelectMultiple
10 from mptt.admin import MPTTModelAdmin
11
12
13 COLLAPSE_CLASSES = ('collapse', 'collapse-closed', 'closed',)
14
15
16 class AttributeInline(generic.GenericTabularInline):
17         ct_field = 'entity_content_type'
18         ct_fk_field = 'entity_object_id'
19         model = Attribute
20         extra = 1
21         allow_add = True
22         classes = COLLAPSE_CLASSES
23         form = AttributeForm
24         formset = AttributeInlineFormSet
25         fields = ['key', 'value_content_type']
26         if 'grappelli' in settings.INSTALLED_APPS:
27                 template = 'admin/philo/edit_inline/grappelli_tabular_attribute.html'
28         else:
29                 template = 'admin/philo/edit_inline/tabular_attribute.html'
30
31
32 class EntityAdmin(admin.ModelAdmin):
33         inlines = [AttributeInline]
34         save_on_top = True
35
36
37 class TreeAdmin(MPTTModelAdmin):
38         pass
39
40
41 class TreeEntityAdmin(TreeAdmin, EntityAdmin):
42         pass
43
44
45 class TagAdmin(admin.ModelAdmin):
46         list_display = ('name', 'slug')
47         prepopulated_fields = {"slug": ("name",)}
48         search_fields = ["name"]
49         
50         def response_add(self, request, obj, post_url_continue='../%s/'):
51                 # If it's an ajax request, return a json response containing the necessary information.
52                 if request.is_ajax():
53                         return HttpResponse(json.dumps({'pk': escape(obj._get_pk_val()), 'unicode': escape(obj)}))
54                 return super(TagAdmin, self).response_add(request, obj, post_url_continue)
55
56
57 class AddTagAdmin(admin.ModelAdmin):
58         def formfield_for_manytomany(self, db_field, request=None, **kwargs):
59                 """
60                 Get a form Field for a ManyToManyField.
61                 """
62                 # If it uses an intermediary model that isn't auto created, don't show
63                 # a field in admin.
64                 if not db_field.rel.through._meta.auto_created:
65                         return None
66                 
67                 if db_field.rel.to == Tag and db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
68                         opts = Tag._meta
69                         if request.user.has_perm(opts.app_label + '.' + opts.get_add_permission()):
70                                 kwargs['widget'] = TagFilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
71                                 return db_field.formfield(**kwargs)
72                 
73                 return super(AddTagAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
74
75
76 admin.site.register(Tag, TagAdmin)