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.admin.forms.attributes import AttributeForm, AttributeInlineFormSet
9 from philo.admin.widgets import TagFilteredSelectMultiple
10 from mptt.admin import MPTTModelAdmin
13 COLLAPSE_CLASSES = ('collapse', 'collapse-closed', 'closed',)
16 class AttributeInline(generic.GenericTabularInline):
17 ct_field = 'entity_content_type'
18 ct_fk_field = 'entity_object_id'
22 classes = COLLAPSE_CLASSES
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'
29 template = 'admin/philo/edit_inline/tabular_attribute.html'
32 class EntityAdmin(admin.ModelAdmin):
33 inlines = [AttributeInline]
37 class TreeAdmin(MPTTModelAdmin):
41 class TreeEntityAdmin(TreeAdmin, EntityAdmin):
45 class TagAdmin(admin.ModelAdmin):
46 list_display = ('name', 'slug')
47 prepopulated_fields = {"slug": ("name",)}
48 search_fields = ["name"]
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.
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)
57 class AddTagAdmin(admin.ModelAdmin):
58 def formfield_for_manytomany(self, db_field, request=None, **kwargs):
60 Get a form Field for a ManyToManyField.
62 # If it uses an intermediary model that isn't auto created, don't show
64 if not db_field.rel.through._meta.auto_created:
67 if db_field.rel.to == Tag and db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
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)
73 return super(AddTagAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
76 admin.site.register(Tag, TagAdmin)