2022-07-21 19:34:21 +00:00
|
|
|
|
from django.conf import settings
|
2020-10-16 18:20:18 +00:00
|
|
|
|
from django.contrib import admin
|
2022-09-30 18:35:01 +00:00
|
|
|
|
from django.db.models import Count
|
2022-09-30 18:49:26 +00:00
|
|
|
|
from django.db.models.options import Options
|
|
|
|
|
from django.template.loader import render_to_string
|
2022-07-21 19:34:21 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-10-24 14:33:02 +00:00
|
|
|
|
from import_export.admin import ImportExportMixin
|
|
|
|
|
from import_export.resources import ModelResource
|
2020-10-16 18:20:18 +00:00
|
|
|
|
|
2022-07-21 19:34:21 +00:00
|
|
|
|
from inventory.admin.base import BaseUserAdmin, LimitTreeDepthListFilter
|
2022-09-30 18:49:26 +00:00
|
|
|
|
from inventory.models import ItemModel, LocationModel
|
2022-07-21 19:34:21 +00:00
|
|
|
|
from inventory.string_utils import ltruncatechars
|
2020-10-16 18:20:18 +00:00
|
|
|
|
|
|
|
|
|
|
2020-10-24 14:33:02 +00:00
|
|
|
|
class LocationModelResource(ModelResource):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = LocationModel
|
|
|
|
|
|
|
|
|
|
|
2020-10-16 18:20:18 +00:00
|
|
|
|
@admin.register(LocationModel)
|
2020-10-24 14:33:02 +00:00
|
|
|
|
class LocationModelAdmin(ImportExportMixin, BaseUserAdmin):
|
2022-09-30 18:35:01 +00:00
|
|
|
|
@admin.display(ordering='item_count', description=_('ItemModel.verbose_name_plural'))
|
|
|
|
|
def item_count(self, obj):
|
|
|
|
|
return obj.item_count
|
|
|
|
|
|
2022-09-30 18:49:26 +00:00
|
|
|
|
@admin.display(description=_('ItemModel.verbose_name_plural'))
|
|
|
|
|
def items(self, obj):
|
|
|
|
|
item_qs = ItemModel.objects.filter(location=obj)
|
|
|
|
|
opts: Options = ItemModel._meta
|
|
|
|
|
context = {
|
|
|
|
|
'items': item_qs,
|
|
|
|
|
'opts': opts,
|
|
|
|
|
}
|
|
|
|
|
return render_to_string('admin/location/items.html', context)
|
|
|
|
|
|
2022-07-21 19:34:21 +00:00
|
|
|
|
@admin.display(ordering='path_str', description=_('LocationModel.verbose_name'))
|
|
|
|
|
def location(self, obj):
|
|
|
|
|
text = ' › '.join(obj.path)
|
|
|
|
|
text = ltruncatechars(text, max_length=settings.TREE_PATH_STR_MAX_LENGTH)
|
|
|
|
|
return text
|
|
|
|
|
|
2022-09-30 18:35:01 +00:00
|
|
|
|
def get_queryset(self, request):
|
|
|
|
|
qs = super().get_queryset(request)
|
|
|
|
|
qs = qs.annotate(item_count=Count('items'))
|
|
|
|
|
return qs
|
|
|
|
|
|
|
|
|
|
list_display = ('location', 'create_dt', 'update_dt', 'item_count')
|
2022-09-30 18:49:26 +00:00
|
|
|
|
fieldsets = (
|
|
|
|
|
(
|
|
|
|
|
_('Internals'),
|
|
|
|
|
{
|
|
|
|
|
'classes': ('collapse',),
|
|
|
|
|
'fields': (
|
|
|
|
|
('id', 'version'),
|
|
|
|
|
'user',
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
(_('Meta'), {'classes': ('collapse',), 'fields': ('create_dt', 'update_dt')}),
|
|
|
|
|
(
|
|
|
|
|
_('Basic'),
|
|
|
|
|
{
|
|
|
|
|
'fields': (
|
|
|
|
|
'name',
|
|
|
|
|
'description',
|
|
|
|
|
'tags',
|
|
|
|
|
'parent',
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
(_('Items in this Location'), {'fields': ('items',)}),
|
|
|
|
|
)
|
|
|
|
|
readonly_fields = ('id', 'create_dt', 'update_dt', 'user', 'item_count', 'items')
|
2022-07-28 19:07:21 +00:00
|
|
|
|
list_display_links = ('location',)
|
2022-07-21 19:34:21 +00:00
|
|
|
|
list_filter = (LimitTreeDepthListFilter,)
|
2022-09-30 17:20:07 +00:00
|
|
|
|
search_fields = ('name', 'description', 'tags__name')
|
2022-07-21 19:34:21 +00:00
|
|
|
|
ordering = ('path_str',)
|