|
| 1 | +from optparse import make_option |
| 2 | +import os |
| 3 | + |
| 4 | +from django.core.management.base import CommandError |
| 5 | +from django.core.management.templates import TemplateCommand |
| 6 | +from django.utils.importlib import import_module |
| 7 | + |
| 8 | +import horizon |
| 9 | + |
| 10 | + |
| 11 | +class Command(TemplateCommand): |
| 12 | + template = os.path.join(horizon.__path__[0], "conf", "dash_template") |
| 13 | + option_list = TemplateCommand.option_list + ( |
| 14 | + make_option('--target', |
| 15 | + dest='target', |
| 16 | + action='store', |
| 17 | + default=None, |
| 18 | + help='The directory in which the panel ' |
| 19 | + 'should be created. Defaults to the ' |
| 20 | + 'current directory. The value "auto" ' |
| 21 | + 'may also be used to automatically ' |
| 22 | + 'create the panel inside the specified ' |
| 23 | + 'dashboard module.'),) |
| 24 | + help = ("Creates a Django app directory structure for a new dashboard " |
| 25 | + "with the given name in the current directory or optionally in " |
| 26 | + "the given directory.") |
| 27 | + |
| 28 | + def handle(self, dash_name=None, **options): |
| 29 | + if dash_name is None: |
| 30 | + raise CommandError("You must provide a dashboard name.") |
| 31 | + |
| 32 | + # Use our default template if one isn't specified. |
| 33 | + if not options.get("template", None): |
| 34 | + options["template"] = self.template |
| 35 | + |
| 36 | + # We have html templates as well, so make sure those are included. |
| 37 | + options["extensions"].extend(["html", "js", "css"]) |
| 38 | + |
| 39 | + # Check that the app_name cannot be imported. |
| 40 | + try: |
| 41 | + import_module(dash_name) |
| 42 | + except ImportError: |
| 43 | + pass |
| 44 | + else: |
| 45 | + raise CommandError("%r conflicts with the name of an existing " |
| 46 | + "Python module and cannot be used as an app " |
| 47 | + "name. Please try another name." % dash_name) |
| 48 | + |
| 49 | + super(Command, self).handle('dash', dash_name, **options) |
0 commit comments