Pluggable Patterns For Reusable Django Applications
Project
Project Configuration URL routing Templates
Project Application Application Application Application Application Application Application Application Application Configuration URL routing Templates
Here there be power! Project Application Application Application Application Application Application Application Application Application Configuration URL routing Templates
We need a better definition for Django Application
An app should not be a monolithic pile of code For example, most blog apps available provide too much functionality MyBlog App Categories Custom Tagging Custom Comments Comment Moderation Assumption of text markup type Single blogs Multiple Sites ACME MONOLITHS
An application should be pluggable
A pluggable app is Focused Write programs that do one thing and do it well. Doug McIlroy (inventor of UNIX pipes)
A pluggable app is Self-Contained Batteries are included Dependencies are declared
A pluggable app is Easily Adaptable Corey s Law: The less adaptable you make your code, the sooner you will be tasked to adapt it.
A pluggable app is Easily Installed pip install coolapp You did declare your dependencies, right?
How do we make a pluggable application?
Stop thinking like this http://upload.wikimedia.org/wikipedia/commons/archive/a/aa/20090315161532!ferrari_enzo_ferrari.jpg
and think like this http://www.flickr.com/photos/motagirl2/4301276868/
Applications can have very different purposes http://www.photoshoproadmap.com/imagenes/blog/lego-brushes/lego-bricks-high-resolution.jpg
Application Types Data. Manages specific data and access to it Utility. Provide a way of handling a specific problem for any application Decorator. Adds functionality to one or aggregates functionality of many applications
Data Apps http://www.geeky-gadgets.com/wp-content/uploads/2009/08/lego-usb-hub_2.jpg
Situation 1 Lots of variations Each implementation is different (e.g. blogs)
Abstract Models GLAMKit http://www.glamkit.org/ EntryBase FeaturableEntryMixin StatusableEntryMixin TaggableEntryMixin HTMLFormattableEntryMixin
Situation 2 A few, well-known of variations (e.g. Use django.contrib.sites?)
Optional Field Settings
Situation 3 Optionally use another application (e.g. Use django-tagging?)
Optional Integration
Optional Integration
Situation 3 You want to reference different models (e.g. Customizable author field)
Configurable Foreign Keys Viewpoint http://github.com/washingtontimes
Situation 3 You want to provide hooks for other applications (e.g. While processing in a view)
Emit Signals Satchmo http://www.satchmoproject.com/
Utility Apps http://www.1000steine.com/brickset/images/852759-1.jpg
Utility apps are just code libraries
Projects with template tags need one
Does it need to be in? Yes, if you have: Templates Template Tags Management Commands
Decorator Apps http://trendland.net/wp-content/uploads/2008/12/lego-ring.jpg
CoolApp New Method Custom Manager New Field New Admin
Registering for Decoration
Simple Model Registry
Model-Data Registry
Lazy Registration
Situation 4 You want to add a field to a model (e.g. Link to another model)
Simple Model Registry
Situation 5 You want to add a method to a model (e.g. Link to another model)
Adding methods
Situation 6 You want to customize a model s ModelAdmin (e.g. Change the widget of a field)
Lazy Registration of a Custom ModelAdmin
Lazy Registration of a Custom ModelAdmin
Lazy Registration of a Custom ModelAdmin
Touch Points
Touch Points of an App The parts of an application that usually need tweaking URLs Templates Configurable options View responses
Situation 7 You want the URLs of your app to live under any prefix (e.g. /blogs/ vs. /weblogs/)
Name your URLs
Reference your URLs by name
Situation 8 You want your templates to be easily overridable
Namespace Templates coolapp templates coolapp base.html
Namespace Templates coolapp templates All templates in a template name space coolapp base.html
Namespace Templates coolapp templates coolapp All templates extend a single template base.html
Namespace Templates coolapp templates coolapp This template simply base.html
Import your blocks Allows you to override any of the templates extra_head.html content.html base.html
Situation 9 You want to configure your app without modifying its code (e.g. API keys)
Configurable Options Django Supertagging http://github.com/josesoa Internal Name Setting Name Default Value
Configurable Options Django Debug Toolbar http://github.com/robhudson
Define a Storage Option
Situation 10 You want to alter the data your views use (e.g. Extra context, different template)
Add keyword arguments Django Registration http://bitbucket.org/ubernostrum
Class-based Views http://github.com/bfirsh/django-class-based-views http://github.com/pegasus/django-baseviews http://code.djangoproject.com/ticket/6735 http://codysoyland.com/2010/feb/3/thread-safe-objectoriented-views-django/ http://www.toddreed.name/content/django-view-class/
To be continued
My Info coreyoordt@gmail.com @coordt github.com/coordt github.com/washingtontimes opensource.washingtontimes.com