Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

-------------------

Expand Down Expand Up @@ -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.* <https://github.com/Brobin/drf-generators/tree/master/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
=====
Expand Down
22 changes: 22 additions & 0 deletions drf_generators/management/commands/generate.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand All @@ -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']
Expand All @@ -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':
Expand Down