My favorites
▼
|
Sign in
django-mptt
Utilities for implementing Modified Preorder Tree Traversal
Project Home
Downloads
Wiki
Issues
Source
Export to GitHub
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
72
attachment: mptt-admin.diff
(2.7 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Index: mptt/admin.py
===================================================================
--- mptt/admin.py (revision 0)
+++ mptt/admin.py (revision 0)
@@ -0,0 +1,42 @@
+from django.conf import settings
+from django.contrib import admin
+from django.utils.translation import ugettext_lazy as _
+from forms import SafeMPTTAdminForm
+
+ModelAdmin = admin.ModelAdmin
+
+if getattr(settings, 'MPTT_USE_FEINCMS', False):
+ try:
+ from feincms.admin import editor
+ ModelAdmin = editor.TreeEditor
+ except ImportError:
+ pass
+
+class MPTTModelAdmin(ModelAdmin):
+ """
+ see: http://magicrebirth.wordpress.com/2009/08/18/django-admin-and-mptt-2/
+ this extends the method already found in (FeinCMS) TreeEditor class
+ """
+ form = SafeMPTTAdminForm
+
+ def _actions_column(self, obj):
+ actions = super(MPTTModelAdmin, self)._actions_column(obj)
+ actions.insert(0,
+ u'<a href="add/?%s=%s" title="%s"><img src="%simg/admin/icon_addlink.gif" alt="%s" /></a>' % (getattr(self.model._meta,'parent_attr','parent'), obj.pk, _('Add child'), settings.ADMIN_MEDIA_PREFIX, _('Add child'))
+ )
+ actions.insert(0,
+ u'<a href="%s" title="%s" target="_blank"><img src="%simg/admin/selector-search.gif" alt="%s" /></a>' % (obj.get_absolute_url(), _('View on site'), settings.ADMIN_MEDIA_PREFIX, _('View on site'))
+ )
+ return actions
+
+ def delete_selected_tree(self, modeladmin, request, queryset):
+ n = 0
+ for obj in queryset:
+ obj.delete()
+ n += 1
+ self.message_user(request, _("Successfully deleted %s items." % n))
+
+ def get_actions(self, request):
+ actions = super(MPTTModelAdmin, self).get_actions(request)
+ actions['delete_selected'] = (self.delete_selected_tree, 'delete_selected', _("Delete selected %(verbose_name_plural)s"))
+ return actions
Index: mptt/forms.py
===================================================================
--- mptt/forms.py (revision 121)
+++ mptt/forms.py (working copy)
@@ -127,3 +127,13 @@
except InvalidMove, e:
self.errors[NON_FIELD_ERRORS] = ErrorList(e)
raise
+
+class SafeMPTTAdminForm(forms.ModelForm):
+ def clean_parent(self):
+ parent = self.cleaned_data.get('parent')
+ if self.instance and parent and parent != self.instance.parent:
+ # TODO: use custom field names for tree_id etc if defined
+ if self.instance.tree_id == parent.tree_id \
+ and self.instance.lft <= parent.lft <= self.instance.rght:
+ raise forms.ValidationError('Invalid parent')
+ return parent
\ No newline at end of file
Powered by
Google Project Hosting