Skip to content

Instantly share code, notes, and snippets.

@pascalmouret
Created November 16, 2012 21:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalmouret/4091188 to your computer and use it in GitHub Desktop.
Save pascalmouret/4091188 to your computer and use it in GitHub Desktop.
Django Management Command to export image_filer as folder-structure
# author: Pascal Mouret
# purpose: Django Management Command to export image_filer as folder-structure
from django.core.management.base import BaseCommand
from django.conf import settings
from image_filer.models import Folder
import os
import shutil
class Command(BaseCommand):
args = 'path'
help = 'reconstructs the image_filer-db-folders in the filesystem, including files'
def handle(self, *args, **options):
path = args[0]
for folder in Folder.objects.filter(parent__isnull=True):
self.export_folder(folder, path)
def export_folder(self, folder, path):
folder_path = os.path.join(path, folder.name)
while(os.path.exists(folder_path)):
folder_path = '%s%s' % (folder_path, '_dub')
print folder_path
os.makedirs(folder_path)
for abst_file in folder.files:
org_name = abst_file.name
if org_name == '':
org_name = abst_file.original_filename
file_path = os.path.join(folder_path, org_name)
while(os.path.exists(file_path)):
split = org_name.split('.')
if len(split) == 1:
new_name = '%s%s' % (org_name, '_dub')
else:
new_name = '%s%s.%s' % ('.'.join(split[:-1]), '_dub', split[-1])
file_path = os.path.join(folder_path, new_name)
file_name = os.path.join(settings.MEDIA_ROOT, abst_file.file.name)
shutil.copy(file_name, file_path)
for child in folder.children.all():
self.export_folder(child, folder_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment