
import os
from shutil import copy

from django.core.management.base import CommandError
from django.core.management.commands.startapp import Command as StartAppCommand
from django.template.loader import find_template_source
from django.utils.importlib import import_module

from mezzanine.conf import settings


class Command(StartAppCommand):
    """
    Creates a theme directory which is a Django app plus all existing 
    templates in the current project.
    """

    help = ("Creates a theme directory which is a Django app plus all "
            "existing templates in the current project.")
    args = "[theme_name]"
    label = "theme name"
    can_import_settings = True

    def handle_label(self, theme_name, **options):
        """
        Create a new Django app and copy all available templates into it.
        """
        super(Command, self).handle_label(theme_name, **options)
        # Build a unique list of template names from ``INSTALLED_APPS`` and 
        # ``TEMPLATE_DIRS`` then use Django's ``find_template_source`` to 
        # determine which to copy.
        templates = set()
        for app in settings.INSTALLED_APPS:
            if app.startswith("django.") or app in settings.OPTIONAL_APPS:
                continue
            app_module = import_module(app)
            app_path = os.path.dirname(os.path.abspath(app_module.__file__))
            templates_path = os.path.join(app_path, "templates")
            for (root, dirs, files) in os.walk(templates_path):
                for f in files:
                    template = os.path.join(root, f)[len(templates_path):]
                    if not template.startswith("/admin/"):
                        templates.add(template.lstrip("/"))
        os.mkdir(os.path.join(theme_name, "templates"))
        for template in templates:
            path_from = unicode(find_template_source(template)[1])
            path_to = os.path.join(theme_name, "templates", template)
            try:
                os.makedirs(os.path.dirname(path_to))
            except OSError:
                pass
            copy(path_from, path_to)

@@ -166,64 +166,8 @@
 and%0A
-from django.template.loader import find_template_source%0A
 from
@@ -250,16 +250,1016 @@
 tings%0A%0A%0A
+def template_path(template):%0A    %22%22%22%0A    Django 1.2 and higher moved to class-based template loaders and %0A    deprecated %60%60find_template_source%60%60 - there's a %60%60find_template%60%60 %0A    function that is similar but always returns %60%60None%60%60 for the %0A    template path so it appears to be broken. This function works as far %0A    as retrieving the template path goes which is all we're concerned %0A    with.%0A    %22%22%22%0A    from django import VERSION%0A    if VERSION %3C (1, 2, 0):%0A        from django.template.loader import find_template_source%0A        return find_template_source(template)%5B1%5D%0A    from django.template.loader import find_template_loader, %5C%0A                                       TemplateDoesNotExist%0A    for loader_name in settings.TEMPLATE_LOADERS:%0A        loader = find_template_loader(loader_name)%0A        if loader is not None:%0A            try:%0A                return loader.load_template_source(template, None)%5B1%5D%0A            except TemplateDoesNotExist:%0A                pass%0A    return None%0A%0A%0A
 class Co
@@ -2792,21 +2792,16 @@
 unicode(
-find_
 template
@@ -2793,38 +2793,36 @@
 nicode(template_
-source
+path
 (template)%5B1%5D)%0A 
@@ -2815,19 +2815,16 @@
 emplate)
-%5B1%5D
 )%0A      

Allow newer versions of Django to determine full paths for templates in the start_theme command.