delete_users: only project models; Django admin history (LogEntry.user_id is staff) was being counted

Account deletion also keeps the status log of the payment records it keeps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-02 06:20:05 +03:30
parent 63cd1af6f7
commit 2f1e257533
3 changed files with 28 additions and 2 deletions

View File

@@ -34,11 +34,25 @@ USER_COLUMNS = ('user_id', 'referred_user_id')
def user_scoped_models():
"""[(model, column)] برای هر مدلی که به کاربر ارجاع می‌دهد، جز خودِ User."""
"""
[(model, column)] برای هر مدلِ خودِ پروژه که به کاربرِ اپ ارجاع می‌دهد، جز خودِ User.
فقط اپ‌هایی که داخلِ پوشه‌ی پروژه‌اند. `admin.LogEntry` هم ستونِ user_id دارد ولی آن
کارمندِ ادمینِ جنگوست، نه کاربرِ اپ؛ بارِ اول که این دستور روی سرور گزارش گرفت، همان را
هم شمرده بود و اگر اجرا می‌شد تاریخچه‌ی ادمینِ شماره‌ی ۱ پاک می‌شد.
"""
from pathlib import Path
from django.conf import settings
base = Path(settings.BASE_DIR).resolve()
found = []
for model in apps.get_models():
if model is User:
continue
app_path = Path(model._meta.app_config.path).resolve()
if base not in app_path.parents or 'site-packages' in app_path.parts:
continue
names = {f.attname for f in model._meta.concrete_fields}
found += [(model, column) for column in USER_COLUMNS if column in names]
return found