diff --git a/README.rst b/README.rst index 3e9f6e3..348374e 100644 --- a/README.rst +++ b/README.rst @@ -83,13 +83,14 @@ Option Action ``--force`` Overwrite existing files without the warning prompt. ``-f``, ``--format`` Format to use when generating views and urls. Valid options: ``viewset``, ``apiview``, ``function``, ``modelviewset``. Default: ``viewset``. ``-d``, ``--depth`` Serialization depth for related models. Default: 0 +``-t``, ``--template`` Use templates from a package. Default: internal templates ========================== =================================================== -**Example:** Generate everything for the app ``api`` with function style views, overwriting existing files, with a serialization depth of 2. +**Example:** Generate everything for the app ``api`` from custom templates, with function style views, overwriting existing files, and with a serialization depth of 2. .. code-block:: bash - $ python manage.py generate api --format function --force -- depth=2 + $ python manage.py generate api --template my_drf_template --format function --force --depth=2 ------------------- @@ -240,6 +241,16 @@ Function urls urlpatterns = format_suffix_patterns(urlpatterns) +========= +Templates +========= +Template packages allow you to specify an alternate set of templates for use with the generator. Copy the `drf_generators.templates.* `_. package to `your_app.templates` & modify to suit your needs. Then call with the `--template` option. + +.. code-block:: bash + + $ python manage.py generate api --template your_app.templates + + ===== Tests ===== diff --git a/drf_generators/management/commands/generate.py b/drf_generators/management/commands/generate.py index 9a089cb..986c346 100644 --- a/drf_generators/management/commands/generate.py +++ b/drf_generators/management/commands/generate.py @@ -1,9 +1,20 @@ from django.core.management.base import AppCommand, CommandError +from django.utils.module_loading import import_module from drf_generators.generators import * +from drf_generators import generators as generators_module from optparse import make_option import django +# dict of submodule name => [imported symbols] +template_imports = { + 'serializer': ['SERIALIZER'], + 'apiview': ['API_URL', 'API_VIEW'], + 'viewset': ['VIEW_SET_URL', 'VIEW_SET_VIEW'], + 'function': ['FUNCTION_URL', 'FUNCTION_VIEW'], + 'modelviewset': ['MODEL_URL', 'MODEL_VIEW'], +} + class Command(AppCommand): help = 'Generates DRF API Views and Serializers for a Django app' @@ -28,6 +39,9 @@ class Command(AppCommand): make_option('--urls', dest='urls', action='store_true', help='generate urls only'), + + make_option('-t', '--template', dest='template', + help='package name to use for templates') ) option_list = AppCommand.option_list + base_options @@ -46,6 +60,7 @@ def handle_app_config(self, app_config, **options): serializers = False views = options['views'] if 'views' in options else False urls = options['urls'] if 'urls' in options else False + template = options['template'] if 'template' in options else None elif django.VERSION[1] >= 8: force = options['force'] @@ -54,9 +69,16 @@ def handle_app_config(self, app_config, **options): serializers = options['serializers'] views = options['views'] urls = options['urls'] + template = options['template'] else: raise CommandError('You must be using Django 1.7, 1.8 or 1.9') + if template is not None: + for submodule, symbols in template_imports.items(): + mod = import_module('%s.%s' % (template, submodule)) + for s in symbols: + setattr(generators_module, s, getattr(mod, s)) + if format == 'viewset': generator = ViewSetGenerator(app_config, force) elif format == 'apiview':