from django.conf import settings from django.core.management.base import BaseCommand import fstools import pygal from pygal.models import Item, TYPE_FOLDER, get_item_by_rel_path class Command(BaseCommand): def handle(self, *args, **options): # rebuild existing non folder items # for item in Item.objects.all().exclude(type=TYPE_FOLDER): item.__set_model_fields_from_file__() item.save() # rebuild folder items based on file system structure (not existing items will be created) folder_list = fstools.dirlist(settings.ITEM_ROOT, rekursive=True) folder_list.append(settings.ITEM_ROOT) folder_list.sort(reverse=True) for full_path in folder_list: item = get_item_by_rel_path(pygal.get_rel_path(full_path)) if item is not None: item.__set_model_fields_from_file__() item.save() self.stdout.write(self.style.SUCCESS('Updated cached data of %d items.' % len(Item.objects.all().exclude(type=TYPE_FOLDER)))) self.stdout.write(self.style.SUCCESS('Updated cached data of %d folders.' % len(Item.objects.filter(type=TYPE_FOLDER))))