from importlib import import_module from django.core.management import BaseCommand from django.db import transaction from django.conf import settings from boards.models.user import UserSettings SessionStore = import_module(settings.SESSION_ENGINE).SessionStore __author__ = 'neko259' class Command(BaseCommand): help = 'Remove user settings that do not have their sessions.' @transaction.atomic def handle(self, *args, **options): count = 0 for setting in UserSettings.objects.all(): session = SessionStore(session_key=setting.session_key) if session.is_empty(): setting.delete() count += 1 print('Removed {} empty setting objects.'.format(count))