Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing, Publishing & Maintaining Reusable Djan...

Writing, Publishing & Maintaining Reusable Django Apps

At Bitmazk we have created more than 60 reusable Django apps in 2012 and 2013. This talk describes best practices and patterns for creating reusable Django apps and introduces a template repository for kickstarting new apps within a few seconds.

Presented at PyCon Singapore 2013
Video: http://youtu.be/a4S1kTJfezA
Github: https://github.com/bitmazk/django-reusable-app-template

Martin Brochhaus

June 15, 2013
Tweet

More Decks by Martin Brochhaus

Other Decks in Programming

Transcript

  1. WRITING, PUBLISHING & MAINTAINING REUSABLE DJANGO APPS DRY all the

    things! Martin Brochhaus (@mbrochh) PyCon SINGAPORE 2013 Saturday, 15 June, 13
  2. EXAMPLE # some code ... if path == ‘$HOME/myapp/bin’: #

    do something # some more code ... if path == ‘$HOME/myapp/lib’: # do something else Saturday, 15 June, 13
  3. EXAMPLE # some code ... if path == ‘$HOME/myapp/bin’: #

    do something # some more code ... if path == ‘$HOME/myapp/lib’: # do something else import os BASE_PATH = ‘$HOME/myapp’ # some code ... if path == os.path.join(BASE_PATH, ‘bin’): # do something # some more code ... if path == os.path.join(BASE_PATH, ‘lib’): # do something else Saturday, 15 June, 13
  4. DJANGO-FEEDBACK-FORM see https://github.com/bitmazk/django-feedback-form $ pip install django-feedback-form # in settings.py

    INSTALLED_APPS = [ ... 'feedback_form', ] $ ./manage.py migrate feedback_form Saturday, 15 June, 13
  5. DJANGO-FEEDBACK-FORM see https://github.com/bitmazk/django-feedback-form $ pip install django-feedback-form # in settings.py

    INSTALLED_APPS = [ ... 'feedback_form', ] $ ./manage.py migrate feedback_form # in urls.py urlpatterns = ( ... url(r'^feedback/', include('feedback_form.urls')), ) Saturday, 15 June, 13
  6. DJANGO-FEEDBACK-FORM see https://github.com/bitmazk/django-feedback-form $ pip install django-feedback-form # in settings.py

    INSTALLED_APPS = [ ... 'feedback_form', ] $ ./manage.py migrate feedback_form # in urls.py urlpatterns = ( ... url(r'^feedback/', include('feedback_form.urls')), ) # in your html template {% load feedback_tags %} {% feedback_form %} Saturday, 15 June, 13
  7. REUSABLE APP STRUCTURE - root folder/ - AUTHORS - CHANGELOG.txt

    - DESCRIPTION - LICENSE - MANIFEST.in - README.rst - manage.py - test_requirements.txt - setup.py Saturday, 15 June, 13
  8. REUSABLE APP STRUCTURE - root folder/ - AUTHORS - CHANGELOG.txt

    - DESCRIPTION - LICENSE - MANIFEST.in - README.rst - manage.py - test_requirements.txt - setup.py - package_name/ - __init__.py - admin.py - models.py - urls.py - app_settings.py - views.py - static/package_name/ - templates/package_name/ Saturday, 15 June, 13
  9. REUSABLE APP STRUCTURE - root folder/ - AUTHORS - CHANGELOG.txt

    - DESCRIPTION - LICENSE - MANIFEST.in - README.rst - manage.py - test_requirements.txt - setup.py - package_name/ - __init__.py - admin.py - models.py - urls.py - app_settings.py - views.py - static/package_name/ - templates/package_name/ - tests/ - runtests.py - test_settings.py - models_tests.py etc. - test_app/ Saturday, 15 June, 13
  10. AUTHORS Current or previous core committers Martin Brochhaus (mbrochh) Contributors

    (in alphabetical order) * Your name could stand here :) see https://github.com/django/django/blob/master/AUTHORS Saturday, 15 June, 13
  11. CHANGELOG === ongoing === - Added this cool feature ===

    1.0 === - Public API stable and released - Backwards incompatible changes released === 0.2.1 === - Fixed annoying bug - Fixed typo === 0.2 === - Added some cool features === 0.1 === - Initial commit see https://github.com/divio/django-cms/blob/develop/CHANGELOG.txt Saturday, 15 June, 13
  12. LICENSE The MIT License (MIT) Copyright (c) 2012 Martin Brochhaus

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. see http://opensource.org/licenses/MIT Saturday, 15 June, 13
  13. README Title Of The App ================ Short description, what problem

    does it solve? Installation ============ You can use code-examples:: pip install app-name Usage ===== Settings / Templatetags ======================= Contribute ========== see https://github.com/bitmazk/django-social-media-links Saturday, 15 June, 13
  14. MANIFEST include AUTHORS include LICENSE include DESCRIPTION include CHANGELOG.txt include

    README.md graft package_name global-exclude *.orig *.pyc *.log *.swp prune package_name/tests/coverage prune package_name/.ropeproject see http://docs.python.org/2/distutils/sourcedist.html Saturday, 15 June, 13
  15. SETUP.PY setup( name="package_name", version=app.__version__, description=read('DESCRIPTION'), long_description=read('README.rst'), license='The MIT License', platforms=['OS

    Independent'], keywords='django, reusable, app, feedback', author='Martin Brochhaus', author_email='[email protected]', url="https://github.com/username/package_name", packages=find_packages(), include_package_data=True, install_requires=[ 'django>=1.3', ], ) see http://docs.python.org/2/distutils/setupscript.html & http://guide.python-distribute.org/quickstart.html Saturday, 15 June, 13
  16. TEST REQUIREMENTS django-nose coverage django-coverage django-jasmine ipdb flake8 fabric factory_boy

    mock selenium see http://www.pip-installer.org/en/latest/requirements.html Saturday, 15 June, 13
  17. REUSABLE APP STRUCTURE - root folder/ - AUTHORS - CHANGELOG.txt

    - DESCRIPTION - LICENSE - MANIFEST.in - README.rst - manage.py - test_requirements.txt - setup.py - package_name/ - __init__.py - admin.py - models.py - urls.py - app_settings.py - views.py - static/package_name/ - templates/package_name/ - tests/ - runtests.py - test_settings.py - models_tests.py etc. - test_app/ Saturday, 15 June, 13
  18. APP SETTINGS # in app_settings.py from django.conf import settings FORM_COLOR

    = getattr(settings, ‘FEEDBACK_FORM_COLOR’, ‘red’) # somewhere in your app from .app_settings import FORM_COLOR self.color = FORM_COLOR see http://blog.muhuk.com/2010/01/26/developing-reusable-django-apps-app-settings.html Saturday, 15 June, 13
  19. STATIC & TEMPLATES - root - package_name - static -

    package_name - css - styles.css - js - some_script.js - root - package_name - templates - package_name - feedback_form.html see https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types & https://docs.djangoproject.com/en/dev/ref/settings/#staticfiles-finders Saturday, 15 June, 13
  20. REUSABLE APP STRUCTURE - root folder/ - AUTHORS - CHANGELOG.txt

    - DESCRIPTION - LICENSE - MANIFEST.in - README.rst - manage.py - test_requirements.txt - setup.py - package_name/ - __init__.py - admin.py - models.py - urls.py - app_settings.py - views.py - static/package_name/ - templates/package_name/ - tests/ - runtests.py - test_settings.py - models_tests.py etc. - test_app/ Saturday, 15 June, 13
  21. TEST DRIVEN DEVELOPMENT • test_settings.py simulates a Django project •

    test_app • an app within the app?! • further simulates a Django project • can be used to define Dummy models • Execute ./runtests.py see https://speakerdeck.com/mbrochh/tdd-with-django Saturday, 15 June, 13
  22. SOUTH MIGRATIONS # before you publish your app $ ./manage.py

    schemamigration package_name --initial see http://south.readthedocs.org/en/latest/tutorial/part1.html Saturday, 15 June, 13
  23. SOUTH MIGRATIONS # before you publish your app $ ./manage.py

    schemamigration package_name --initial # when you make changes to models.py $ ./manage.py schemamigration package_name --auto see http://south.readthedocs.org/en/latest/tutorial/part1.html Saturday, 15 June, 13
  24. SOUTH MIGRATIONS # before you publish your app $ ./manage.py

    schemamigration package_name --initial # when you make changes to models.py $ ./manage.py schemamigration package_name --auto # when you make changes that require datamigrations $ ./manage.py datamigration package_name --auto see http://south.readthedocs.org/en/latest/tutorial/part1.html Saturday, 15 June, 13
  25. SOUTH MIGRATIONS # before you publish your app $ ./manage.py

    schemamigration package_name --initial # when you make changes to models.py $ ./manage.py schemamigration package_name --auto # when you make changes that require datamigrations $ ./manage.py datamigration package_name --auto # all your users need to do is $ ./manage.py migrate package_name see http://south.readthedocs.org/en/latest/tutorial/part1.html Saturday, 15 June, 13
  26. PUBLISHING ON THE PYTHON PACKAGE INDEX # check if MANIFEST.in

    includes the correct files $ python setup.py sdist see http://docs.python.org/3/distutils/packageindex.html Saturday, 15 June, 13
  27. PUBLISHING ON THE PYTHON PACKAGE INDEX # check if MANIFEST.in

    includes the correct files $ python setup.py sdist # register your app on pypi.python.org $ python setup.py register see http://docs.python.org/3/distutils/packageindex.html Saturday, 15 June, 13
  28. PUBLISHING ON THE PYTHON PACKAGE INDEX # check if MANIFEST.in

    includes the correct files $ python setup.py sdist # register your app on pypi.python.org $ python setup.py register # upload your app $ python setup.py sdist upload see http://docs.python.org/3/distutils/packageindex.html Saturday, 15 June, 13
  29. DOCUMENTATION # in the docs folder $ mkvirtualenv -p python2.7

    yourapp $ pip install Sphinx $ deactivate && workon $ sphinx-quickstart see http://docutils.sourceforge.net/docs/user/rst/quickstart.html & http://sphinx-doc.org/tutorial.html Saturday, 15 June, 13
  30. DOCUMENTATION # in the docs folder $ mkvirtualenv -p python2.7

    yourapp $ pip install Sphinx $ deactivate && workon $ sphinx-quickstart # in docs/source/index.rst Contents: .. toctree:: :maxdepth: 2 installation see http://docutils.sourceforge.net/docs/user/rst/quickstart.html & http://sphinx-doc.org/tutorial.html Saturday, 15 June, 13
  31. DOCUMENTATION # in the docs folder $ mkvirtualenv -p python2.7

    yourapp $ pip install Sphinx $ deactivate && workon $ sphinx-quickstart # in docs/source/index.rst Contents: .. toctree:: :maxdepth: 2 installation # in docs/source/installation.rst Installation ============ Your documentation goes here... see http://docutils.sourceforge.net/docs/user/rst/quickstart.html & http://sphinx-doc.org/tutorial.html Saturday, 15 June, 13