Refactoring a Django App as a Stand-Alone Module

From Littledamien Wiki
Revision as of 21:22, 16 February 2016 by Video8 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview[edit]

This page catalogs the workflow involved in taking an app that is part of a Django project and refactoring it as a stand-alone module.

Reusable Django Packages/Apps contains an overview of the principles of creating stand-alone Django modules

Workflow[edit]

App set up[edit]

  • Move project files to directory outside Django project, e.g. ~/develop/django/apps
- project_root_dir
    |-- app_dir
    |    |-- tests/
    |    |-- __init__.py
    |    |-- admin.py
    |    |-- app.py
    |    `-- models.py
    |-- .gitignore
    |-- LICENSE
    |-- MANIFEST.IN
    |-- README.md
    `-- setup.py

Configure app files[edit]

  • Add package config files
    • LICENSE
    • MANIFEST.in
    • README.md
    • setup.py
  • Delete migrations directory
  • Add project properties in the app directory
    • __init__.py
      • __author__
      • __version__
      • default_app_config = 'app_name.apps.AppNameConfig'
    • apps.py defines a class named AppNameConfig with the following properties:
      • name - app token, e.g. the name of the directory containing the app
      • verbose_name - Plain english description of the app
  • Consider moving views defined in the app out of the app and into the actual Django web applications that references the app.
  • N.B. that the Grappelli admin app does not handle models that are broken out into separate files.
  • Update MANIFEST.in to include any files that are not in the top level of the app directory.
  • Update README.md with information about installing, configuring, and using the app.
  • Update setup.py with configuration for creating distribution files out of the app.
  • Exclude Mac OS files. PyCharm project files, and distribution files in .gitignore.
  • Create a distribution for the app: python setup.py sdist --formats=gztar,zip

Configure Django project to use the stand-alone app[edit]

  • Add app identifier to INSTALLED_APPS in webapp/settings.py
  • Add app distribution to requirements.txt
  • Incorporate database changes:
    • python manage.py makemigrations app_name
    • python manage.py migrate app_name

Distributing the app[edit]

  • Upload the app's distribution files to the nrose-django-packages Amazon S3 bucket.
  • Create GitHub repo for the app and push code there.