From ccbc6afd1abd42b5c45c82413d8042e286366db0 Mon Sep 17 00:00:00 2001 From: Peter Fein Date: Thu, 11 Feb 2016 20:20:25 +0000 Subject: [PATCH 1/3] stub support for specifying a source of templates on the commandline. Refs #22. --- .../management/commands/generate.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drf_generators/management/commands/generate.py b/drf_generators/management/commands/generate.py index 9a089cb..a40f288 100644 --- a/drf_generators/management/commands/generate.py +++ b/drf_generators/management/commands/generate.py @@ -1,9 +1,19 @@ 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 +38,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 +59,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 +68,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': From 8eb8cb10b976f06ba9c1528d27569a94e3e87ba9 Mon Sep 17 00:00:00 2001 From: Peter Fein Date: Tue, 7 Jun 2016 16:45:07 -0400 Subject: [PATCH 2/3] fix pep-8 problems --- drf_generators/management/commands/generate.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drf_generators/management/commands/generate.py b/drf_generators/management/commands/generate.py index a40f288..986c346 100644 --- a/drf_generators/management/commands/generate.py +++ b/drf_generators/management/commands/generate.py @@ -15,6 +15,7 @@ 'modelviewset': ['MODEL_URL', 'MODEL_VIEW'], } + class Command(AppCommand): help = 'Generates DRF API Views and Serializers for a Django app' @@ -38,7 +39,7 @@ 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') ) From 4a0ab2c3270ae7c2d8c91a38d8283b5f9aee1251 Mon Sep 17 00:00:00 2001 From: Peter Fein Date: Tue, 7 Jun 2016 16:59:45 -0400 Subject: [PATCH 3/3] Update readme for templates --- README.rst | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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 =====