2016-09-29 20:20:10 +00:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import admin
|
|
|
|
|
2018-04-27 20:53:38 +00:00
|
|
|
from django_select2.forms import ModelSelect2Widget, Select2MultipleWidget
|
2017-10-08 16:01:03 +00:00
|
|
|
|
2018-10-10 19:18:25 +00:00
|
|
|
|
2018-04-27 20:53:38 +00:00
|
|
|
from .models import Item, ItemImage, Category, Label
|
|
|
|
from .widgets import ItemSelectWidget, PropsSelectWidget
|
2017-10-08 20:39:23 +00:00
|
|
|
|
2020-05-14 21:16:53 +00:00
|
|
|
|
2023-07-11 13:34:35 +00:00
|
|
|
class ModelAdminMixin(object):
|
2020-05-14 21:16:53 +00:00
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
return request.user.is_authenticated()
|
|
|
|
|
|
|
|
has_change_permission = has_add_permission
|
|
|
|
has_delete_permission = has_add_permission
|
|
|
|
has_module_permission = has_add_permission
|
|
|
|
|
2017-10-08 20:39:23 +00:00
|
|
|
|
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 = {
|
2023-07-11 13:34:35 +00:00
|
|
|
"parent": ItemSelectWidget(model=Item),
|
|
|
|
"categories": Select2MultipleWidget,
|
|
|
|
"props": PropsSelectWidget,
|
|
|
|
}
|
2016-09-29 20:20:10 +00:00
|
|
|
|
2018-04-27 20:53:38 +00:00
|
|
|
|
2020-05-14 21:16:53 +00:00
|
|
|
class ItemImageInline(ModelAdminMixin, admin.TabularInline):
|
2016-09-29 20:20:10 +00:00
|
|
|
model = ItemImage
|
|
|
|
extra = 1
|
|
|
|
|
2018-04-27 20:53:38 +00:00
|
|
|
|
2020-05-14 21:16:53 +00:00
|
|
|
class LabelInline(ModelAdminMixin, admin.TabularInline):
|
2017-04-28 12:14:27 +00:00
|
|
|
model = Label
|
|
|
|
|
2018-04-27 20:53:38 +00:00
|
|
|
|
2020-05-12 18:58:10 +00:00
|
|
|
class ItemAdmin(ModelAdminMixin, admin.ModelAdmin):
|
2023-07-11 13:34:35 +00:00
|
|
|
list_display = ("_name",)
|
|
|
|
list_filter = ("categories",)
|
2016-09-29 20:20:10 +00:00
|
|
|
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):
|
2023-07-11 13:34:35 +00:00
|
|
|
return "-" * obj.get_level() + "> " + obj.name
|
2017-02-15 03:01:47 +00:00
|
|
|
|
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:
|
2023-07-11 13:34:35 +00:00
|
|
|
request.session["last-parent"] = str(obj.parent.uuid)
|
2017-02-16 15:14:37 +00:00
|
|
|
else:
|
2023-07-11 13:34:35 +00:00
|
|
|
request.session["last-parent"] = str(obj.uuid)
|
2017-02-16 15:14:37 +00:00
|
|
|
|
|
|
|
def get_changeform_initial_data(self, request):
|
|
|
|
data = {
|
2023-07-11 13:34:35 +00:00
|
|
|
"parent": request.GET.get("parent") or request.session.get("last-parent")
|
|
|
|
}
|
2017-10-07 22:08:00 +00:00
|
|
|
|
2017-02-16 15:14:37 +00:00
|
|
|
data.update(super(ItemAdmin, self).get_changeform_initial_data(request))
|
|
|
|
return data
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
js = (
|
|
|
|
# Required by select2
|
2023-07-11 13:34:35 +00:00
|
|
|
"https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js",
|
|
|
|
)
|
|
|
|
css = {"all": ("css/admin.css",)}
|
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)
|
|
|
|
|
2018-04-27 20:53:38 +00:00
|
|
|
|
2020-05-12 18:58:10 +00:00
|
|
|
class NormalModelAdmin(ModelAdminMixin, admin.ModelAdmin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-07-11 13:34:35 +00:00
|
|
|
admin.site.site_title = "Hackerspace Storage Admin"
|
|
|
|
admin.site.site_header = "Hackerspace Storage Admin"
|
2020-05-11 18:26:59 +00:00
|
|
|
|
2016-09-29 20:20:10 +00:00
|
|
|
admin.site.register(Item, ItemAdmin)
|
2020-05-12 18:58:10 +00:00
|
|
|
admin.site.register(Category, NormalModelAdmin)
|
2018-04-27 22:32:45 +00:00
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.auth.models import Group
|
|
|
|
|
2023-07-11 13:34:35 +00:00
|
|
|
User.add_to_class("get_short_name", User.get_username)
|
|
|
|
User.add_to_class("get_full_name", User.get_username)
|
2018-04-28 19:25:08 +00:00
|
|
|
|
2018-04-27 22:32:45 +00:00
|
|
|
admin.site.unregister(User)
|
|
|
|
admin.site.unregister(Group)
|
2018-10-10 19:18:25 +00:00
|
|
|
|
|
|
|
from social_django.admin import UserSocialAuth, Nonce, Association
|
2023-07-11 13:34:35 +00:00
|
|
|
|
2018-10-10 19:18:25 +00:00
|
|
|
admin.site.unregister(UserSocialAuth)
|
|
|
|
admin.site.unregister(Nonce)
|
|
|
|
admin.site.unregister(Association)
|