2016-09-29 20:20:10 +00:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import admin
|
2017-04-28 12:14:27 +00:00
|
|
|
from .models import Item, ItemImage, Category, Label
|
2017-02-16 15:14:37 +00:00
|
|
|
from django_select2.forms import Select2Widget, Select2MultipleWidget
|
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
|
|
|
|
class ItemForm(forms.ModelForm):
|
|
|
|
name = forms.CharField(widget=forms.TextInput())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Item
|
|
|
|
exclude = []
|
2017-02-16 15:14:37 +00:00
|
|
|
widgets = {
|
|
|
|
'parent': Select2Widget,
|
|
|
|
'categories': Select2MultipleWidget
|
|
|
|
}
|
2016-09-29 20:20:10 +00:00
|
|
|
|
|
|
|
class ItemImageInline(admin.TabularInline):
|
|
|
|
model = ItemImage
|
|
|
|
extra = 1
|
|
|
|
|
2017-04-28 12:14:27 +00:00
|
|
|
class LabelInline(admin.TabularInline):
|
|
|
|
model = Label
|
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
class ItemAdmin(admin.ModelAdmin):
|
2017-02-28 23:16:10 +00:00
|
|
|
list_display = ('_name',)
|
2016-09-29 20:20:10 +00:00
|
|
|
list_filter = ('categories',)
|
|
|
|
form = ItemForm
|
2017-04-28 12:14:27 +00:00
|
|
|
inlines = [ItemImageInline, LabelInline]
|
2017-02-28 23:16:10 +00:00
|
|
|
save_on_top = True
|
2016-09-29 20:20:10 +00:00
|
|
|
|
2017-02-15 03:01:47 +00:00
|
|
|
def _name(self, obj):
|
|
|
|
return '-' * obj.get_level() + '> ' + obj.name
|
|
|
|
|
2017-02-16 15:14:37 +00:00
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
super(ItemAdmin, self).save_model(request, obj, form, change)
|
|
|
|
|
|
|
|
# Store last input parent to use as default on next creation
|
|
|
|
if obj.parent:
|
|
|
|
request.session['last-parent'] = str(obj.parent.uuid)
|
|
|
|
else:
|
|
|
|
request.session['last-parent'] = str(obj.uuid)
|
|
|
|
|
|
|
|
def get_changeform_initial_data(self, request):
|
|
|
|
data = {
|
|
|
|
'parent': request.session.get('last-parent')
|
|
|
|
}
|
|
|
|
data.update(super(ItemAdmin, self).get_changeform_initial_data(request))
|
|
|
|
return data
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
js = (
|
|
|
|
# Required by select2
|
2017-06-19 13:31:05 +00:00
|
|
|
'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
|
2017-02-16 15:14:37 +00:00
|
|
|
)
|
2016-09-29 20:20:10 +00:00
|
|
|
|
2017-05-29 13:39:28 +00:00
|
|
|
def response_action(self, request, queryset):
|
|
|
|
with Item.disabled_tree_trigger():
|
|
|
|
return super(ItemAdmin, self).response_action(request, queryset)
|
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
admin.site.register(Item, ItemAdmin)
|
|
|
|
admin.site.register(Category)
|