Slide 1

Slide 1 text

WRITING, PUBLISHING & MAINTAINING REUSABLE DJANGO APPS DRY all the things! Martin Brochhaus (@mbrochh) PyCon SINGAPORE 2013 Saturday, 15 June, 13

Slide 2

Slide 2 text

Martin Brochhaus Founder of Bitmazk Pte. Ltd. @mbrochh [email protected] Saturday, 15 June, 13

Slide 3

Slide 3 text

DRY? Saturday, 15 June, 13

Slide 4

Slide 4 text

DON’T REPEAT YOURSELF see https://en.wikipedia.org/wiki/Don't_repeat_yourself Saturday, 15 June, 13

Slide 5

Slide 5 text

EXAMPLE Saturday, 15 June, 13

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

WHAT ARE REUSABLE APPS? Saturday, 15 June, 13

Slide 9

Slide 9 text

WHAT ARE REUSABLE APPS? Saturday, 15 June, 13

Slide 10

Slide 10 text

WHAT ARE REUSABLE APPS? Saturday, 15 June, 13

Slide 11

Slide 11 text

WHAT ARE REUSABLE APPS? Saturday, 15 June, 13

Slide 12

Slide 12 text

WHAT ARE REUSABLE APPS? Saturday, 15 June, 13

Slide 13

Slide 13 text

WHAT ARE REUSABLE APPS? Saturday, 15 June, 13

Slide 14

Slide 14 text

DJANGO-FEEDBACK-FORM see https://github.com/bitmazk/django-feedback-form Saturday, 15 June, 13

Slide 15

Slide 15 text

DJANGO-FEEDBACK-FORM see https://github.com/bitmazk/django-feedback-form $ pip install django-feedback-form Saturday, 15 June, 13

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

LITERATURE • Learn from the official Django documentation: https://docs.djangoproject.com/en/dev/intro/reusable-apps/ • Find apps on https://www.djangopackages.com/ Saturday, 15 June, 13

Slide 21

Slide 21 text

LITERATURE • Learn from the official Django documentation: https://docs.djangoproject.com/en/dev/intro/reusable-apps/ • Find apps on https://www.djangopackages.com/ Saturday, 15 June, 13

Slide 22

Slide 22 text

LITERATURE • Learn from the official Django documentation: https://docs.djangoproject.com/en/dev/intro/reusable-apps/ • Find apps on https://www.djangopackages.com/ Saturday, 15 June, 13

Slide 23

Slide 23 text

REUSABLE APP STRUCTURE Saturday, 15 June, 13

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

VERSION # in __init__.py __version__ = '0.1' see http://semver.org/ Saturday, 15 June, 13

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

SOUTH MIGRATIONS see http://south.readthedocs.org/en/latest/tutorial/part1.html Saturday, 15 June, 13

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

PREVIEW YOUR APP IN THE BROWSER $ ./manage.py runserver Saturday, 15 June, 13

Slide 46

Slide 46 text

OBEY PEP8!!! $ flake8 . see http://www.python.org/dev/peps/pep-0008/ Saturday, 15 June, 13

Slide 47

Slide 47 text

PUBLISHING ON THE PYTHON PACKAGE INDEX see http://docs.python.org/3/distutils/packageindex.html Saturday, 15 June, 13

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

DOCUMENTATION see http://docutils.sourceforge.net/docs/user/rst/quickstart.html & http://sphinx-doc.org/tutorial.html Saturday, 15 June, 13

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

Saturday, 15 June, 13

Slide 56

Slide 56 text

HTTPS://GITHUB.COM/ BITMAZK/DJANGO- REUSABLE-APP- TEMPLATE Saturday, 15 June, 13

Slide 57

Slide 57 text

THANK YOU! (https://github.com/bitmazk/django-reusable-app-template) @mbrochh Saturday, 15 June, 13