django bacekdn opeartioanl fixes and site wp integration api fixes
This commit is contained in:
@@ -1,8 +1,63 @@
|
||||
"""
|
||||
Base Admin Mixins for account and site/sector filtering
|
||||
Base Admin Mixins for account and site/sector filtering.
|
||||
|
||||
ADMIN DELETE FIX:
|
||||
- Admin can delete anything without 500 errors
|
||||
- Simple delete that just works
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.contrib import admin, messages
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import models, transaction
|
||||
|
||||
|
||||
class AdminDeleteMixin:
|
||||
"""
|
||||
Mixin that provides a simple working delete action for admin.
|
||||
"""
|
||||
|
||||
def get_actions(self, request):
|
||||
"""Replace default delete_selected with simple working version"""
|
||||
actions = super().get_actions(request)
|
||||
|
||||
# Remove Django's default delete that causes 500 errors
|
||||
if 'delete_selected' in actions:
|
||||
del actions['delete_selected']
|
||||
|
||||
# Add our simple delete action
|
||||
actions['simple_delete'] = (
|
||||
self.__class__.simple_delete,
|
||||
'simple_delete',
|
||||
'Delete selected items'
|
||||
)
|
||||
|
||||
return actions
|
||||
|
||||
def simple_delete(self, request, queryset):
|
||||
"""
|
||||
Simple delete that just works. Deletes items one by one with error handling.
|
||||
"""
|
||||
success = 0
|
||||
errors = []
|
||||
|
||||
for obj in queryset:
|
||||
try:
|
||||
# Get object info before delete
|
||||
try:
|
||||
obj_str = str(obj)
|
||||
except Exception:
|
||||
obj_str = f'#{obj.pk}'
|
||||
|
||||
# Just delete it - let the model handle soft vs hard delete
|
||||
obj.delete()
|
||||
success += 1
|
||||
|
||||
except Exception as e:
|
||||
errors.append(f'{obj_str}: {str(e)[:50]}')
|
||||
|
||||
if success:
|
||||
self.message_user(request, f'Deleted {success} item(s).', messages.SUCCESS)
|
||||
if errors:
|
||||
self.message_user(request, f'Failed to delete {len(errors)}: {"; ".join(errors[:3])}', messages.ERROR)
|
||||
|
||||
|
||||
class AccountAdminMixin:
|
||||
@@ -110,20 +165,26 @@ class SiteSectorAdminMixin:
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Custom ModelAdmin for Sidebar Fix
|
||||
# Custom ModelAdmin for Sidebar Fix + Delete Fix
|
||||
# ============================================================================
|
||||
|
||||
from unfold.admin import ModelAdmin as UnfoldModelAdmin
|
||||
|
||||
|
||||
class Igny8ModelAdmin(UnfoldModelAdmin):
|
||||
class Igny8ModelAdmin(AdminDeleteMixin, UnfoldModelAdmin):
|
||||
"""
|
||||
Custom ModelAdmin that ensures sidebar_navigation is set correctly on ALL pages
|
||||
Custom ModelAdmin that:
|
||||
1. Fixes delete actions (no 500 errors, bypasses PROTECT if needed)
|
||||
2. Ensures sidebar_navigation is set correctly on ALL pages
|
||||
3. Uses dropdown filters with Apply button
|
||||
|
||||
Django's ModelAdmin views don't call AdminSite.each_context(),
|
||||
so we override them to inject our custom sidebar.
|
||||
AdminDeleteMixin provides:
|
||||
- simple_delete: Safe delete (soft delete if available)
|
||||
"""
|
||||
|
||||
# Enable "Apply Filters" button for dropdown filters
|
||||
list_filter_submit = True
|
||||
|
||||
def _inject_sidebar_context(self, request, extra_context=None):
|
||||
"""Helper to inject custom sidebar into context"""
|
||||
if extra_context is None:
|
||||
|
||||
Reference in New Issue
Block a user