1. Pluggable Patterns
For Reusable Django Applications
Friday, March 11, 2011
2. An app MyBlog App
should not be • Categories
• Custom Tagging
a monolithic • Custom Comments
pile of code • Comment
Moderation
• Assumption of text
markup type
• Single blogs
For example, most blog • Multiple Sites
“apps” available provide
too much functionality ACME MONOLITHS
Friday, March 11, 2011
4. A “pluggable” app is
Focused
Write programs that do one thing and do it well.
— Doug McIlroy (inventor of UNIX pipes)
Friday, March 11, 2011
5. A “pluggable” app is
Self-Contained
Batteries are included
Dependencies are declared
Friday, March 11, 2011
6. 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.
Friday, March 11, 2011
7. A “pluggable” app is
Easily Installed
pip install coolapp
You did declare your dependencies, right?
Friday, March 11, 2011
8. How do we make a
“pluggable” application?
Friday, March 11, 2011
9. Stop thinking like this
http://upload.wikimedia.org/wikipedia/commons/archive/a/aa/20090315161532!Ferrari_Enzo_Ferrari.JPG
Friday, March 11, 2011
11. Applications can have
very different purposes
http://www.flickr.com/photos/tiemposdelruido/4051083769/
Friday, March 11, 2011
12. Application Types
• Data. Manages specific data and access to it
• Utility. Provide aapplication a specific
problem for any
way of handling
• Decorator.functionality of many applications
aggregates
Adds functionality to one or
Friday, March 11, 2011
13. Situation 1
You want to configure your app
without modifying its code
(e.g. API keys)
Friday, March 11, 2011
14. Configurable Options
Django Supertagging http://github.com/josesoa
Internal Name Setting Name Default Value
Friday, March 11, 2011
36. Adding a manager
Django MPTT http://github.com/django-mptt
from django.db.models import get_model
import django.conf import settings
from coolapp.managers import CustomManager
MODELS = getattr(settings, 'COOLAPP_MODELS', {})
for model_name, mgr_name in MODELS.items():
if not isinstance(model_name, basestring):
continue
model = get_model(*model_name.split('.'))
if not getattr(model, mgr_name, False):
manager = CustomManager()
manager.contribute_to_class(model, mgr_name)
Friday, March 11, 2011
37. Situation 8
You want to customize
a model’s ModelAdmin
(e.g. Change the widget of a field)
Friday, March 11, 2011
38. Lazy Registration of a
Custom ModelAdmin
Django TinyMCE http://github.com/justquick
project’s settings.py
Friday, March 11, 2011
39. Lazy Registration of a
Custom ModelAdmin
Django TinyMCE http://github.com/justquick
Django-TinyMCE’s models.py
Friday, March 11, 2011
40. Lazy Registration of a
Custom ModelAdmin
Django TinyMCE http://github.com/justquick
Django-TinyMCE’s admin.py
Friday, March 11, 2011
41. Lazy Registration of a
Custom ModelAdmin
Django TinyMCE http://github.com/justquick
bottom of Django-TinyMCE’s admin.py
Friday, March 11, 2011