Login

Copy media files to central location for easier sharing

Author:
jtiai
Posted:
May 17, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

I was in need to have pluggable components that all have more or less some media files. I didn't want to pollute my config with dozens of media paths so I wrote this custom command that copies contents <appdir>/app_media to your MEDIA_ROOT/<appname> path.

In template you will refer your media files like {{MEDIA_URL}}/appname/<path to media>

 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 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.

#

Please login first before commenting.