All checks were successful
Build and Push Docker Image / build (push) Successful in 30s
- Introduced `Subcategory` model with migration and unique slug constraint per category - Enabled subcategory assignment for items with validation to ensure category matching - Updated catalog views to handle subcategories, filtering items accordingly - Enhanced templates (`index.html`, `side-bar.html`, `item-list.html`, `detail.html`) to display subcategories - Added dynamic subcategory dropdown in Django admin with category filtering - Created JavaScript for admin subcategory handling and AJAX population - Wrote comprehensive tests for subcategory functionality in views and admin
84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
from django import forms
|
|
from django.contrib import admin
|
|
from django.http import JsonResponse
|
|
from django.urls import path
|
|
|
|
from .models import Category, Item, ItemImage, Subcategory
|
|
|
|
|
|
def get_subcategory_queryset(category_id):
|
|
if not category_id:
|
|
return Subcategory.objects.none()
|
|
|
|
try:
|
|
category_id = int(category_id)
|
|
except (TypeError, ValueError):
|
|
return Subcategory.objects.none()
|
|
|
|
return Subcategory.objects.filter(category_id=category_id).order_by('name')
|
|
|
|
|
|
@admin.register(Category)
|
|
class CategoryAdmin(admin.ModelAdmin):
|
|
list_display = ('name', 'slug')
|
|
prepopulated_fields = {'slug': ('name',)}
|
|
|
|
|
|
@admin.register(Subcategory)
|
|
class SubcategoryAdmin(admin.ModelAdmin):
|
|
list_display = ('name', 'category', 'slug')
|
|
list_filter = ('category',)
|
|
prepopulated_fields = {'slug': ('name',)}
|
|
|
|
|
|
class ItemAdminForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Item
|
|
fields = '__all__'
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
category_id = self.data.get('category') or getattr(self.instance, 'category_id', None)
|
|
self.fields['subcategory'].queryset = get_subcategory_queryset(category_id)
|
|
|
|
|
|
class ItemImageInline(admin.TabularInline):
|
|
model = ItemImage
|
|
extra = 1
|
|
fields = ('image', 'alt', 'sort_order', 'is_primary')
|
|
|
|
|
|
@admin.register(Item)
|
|
class ItemAdmin(admin.ModelAdmin):
|
|
form = ItemAdminForm
|
|
list_display = ('name', 'category', 'subcategory', 'price', 'is_available', 'created_at')
|
|
list_filter = ('is_available', 'category', 'subcategory', 'created_at')
|
|
prepopulated_fields = {'slug': ('name',)}
|
|
inlines = (ItemImageInline,)
|
|
|
|
class Media:
|
|
js = ('items/admin/item-subcategory.js',)
|
|
|
|
def get_urls(self):
|
|
urls = super().get_urls()
|
|
custom_urls = [
|
|
path(
|
|
'subcategories/',
|
|
self.admin_site.admin_view(self.subcategories_view),
|
|
name='items_item_subcategories',
|
|
),
|
|
]
|
|
return custom_urls + urls
|
|
|
|
def subcategories_view(self, request):
|
|
category_id = request.GET.get('category_id')
|
|
subcategories = get_subcategory_queryset(category_id)
|
|
|
|
return JsonResponse({
|
|
'subcategories': [
|
|
{'id': subcategory.id, 'name': subcategory.name}
|
|
for subcategory in subcategories
|
|
],
|
|
})
|