Copy media files to central location for easier sharing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django.core.management.base import BaseCommand
from django.utils.importlib import import_module
from django.conf import settings
import os
import shutil

class Command(BaseCommand):
    def handle(self, *args, **options): 
        basedir = os.path.abspath(os.path.curdir)

        for app in settings.INSTALLED_APPS:
            m = import_module(app)
            media_dir = os.path.join(os.path.dirname(m.__file__), 'app_media')
            app_name = app.split('.')[-1]
            dest_dir = os.path.join(settings.MEDIA_ROOT, app_name)
            if os.path.exists(media_dir):
                print "Copying %s/* to %s/" % (media_dir, dest_dir)
                shutil.copytree(media_dir, dest_dir)

More like this

  1. Serve static media files from app/media subdirectory by adamlofts 5 years, 8 months ago
  2. Use the WSGIAccessScript Directive to secure static files based on the Django session by LuckiDog 2 years, 11 months ago
  3. collectmedia command: Copy or link media files from installed apps by exogen 5 years, 7 months ago
  4. Cacheable resources by jbrisbin 5 years, 8 months ago
  5. Serve static media and indexes from app directories [Python2.5, Development only] by adamlofts 5 years, 8 months ago

Comments

carljm (on May 18, 2009):

Good idea, but would be better handled by symlinking rather than copying. Unless you're on Windows, I guess.

#

(Forgotten your password?)