Pluggable Patterns                           For Reusable Django ApplicationsFriday, March 11, 2011
An app                           MyBlog App        should not be               • Categories                               ...
An application should                            be “pluggable”Friday, March 11, 2011
A “pluggable” app is                               Focused                         Write programs that do one thing and do...
A “pluggable” app is                            Self-Contained                               Batteries are included       ...
A “pluggable” app is                             Easily Adaptable                         Corey’s Law: The less adaptable ...
A “pluggable” app is                            Easily Installed                                     pip install coolapp  ...
How do we make a                 “pluggable” application?Friday, March 11, 2011
Stop thinking like this                     http://upload.wikimedia.org/wikipedia/commons/archive/a/aa/20090315161532!Ferr...
and think like thisFriday, March 11, 2011
Applications can have                     very different purposes                                  http://www.flickr.com/p...
Application Types                     • Data. Manages specific data and access to it                     • Utility. Provid...
Situation 1                   You want to configure your app                     without modifying its code               ...
Configurable Options                         Django Supertagging http://github.com/josesoa                  Internal Name ...
Configurable Options               Django Debug Toolbar http://github.com/robhudsonFriday, March 11, 2011
Data Apps                         http://www.flickr.com/photos/29276244@N03/3200630853/Friday, March 11, 2011
Situation 2                       Lots of variations                Each implementation is different                      ...
Abstract Models                                  GLAMKit http://www.glamkit.org/                              EntryBase   ...
Situation 3                  A few, well-known of variations                   (e.g. Use django.contrib.sites?)Friday, Mar...
Optional Field SettingsFriday, March 11, 2011
Situation 4                          Optionally use another                                 application                   ...
Optional IntegrationFriday, March 11, 2011
Situation 5                             You want to reference                                different models             ...
Runtime Configurable                             Foreign Keys                         Viewpoint http://github.com/washingt...
Runtime Configurable                             Foreign Keys                         Viewpoint http://github.com/washingt...
Utility Apps                         http://www.flickr.com/photos/s8/3638531205/Friday, March 11, 2011
Required for                     template tags or                  management commandsFriday, March 11, 2011
Decorator Apps                          http://www.flickr.com/photos/yum9me/2109549869/Friday, March 11, 2011
New Method   New Field       Custom Manager           New AdminFriday, March 11, 2011
Situation 6                         You want to add a                          field to a modelFriday, March 11, 2011
Lazy Field Insertion             Django Categories http://github.com/washingtontimesFriday, March 11, 2011
Lazy Field Insertion             Django Categories http://github.com/washingtontimesFriday, March 11, 2011
Lazy Field Insertion             Django Categories http://github.com/washingtontimesFriday, March 11, 2011
Situation 7                              You want to add a                         custom manager to a modelFriday, March ...
Lazy Manager Insertion                         Django MPTT http://github.com/django-mpttFriday, March 11, 2011
Adding a manager                         Django MPTT http://github.com/django-mptt                   from django.db.models...
Situation 8                        You want to customize                       a model’s ModelAdmin                  (e.g....
Lazy Registration of a                         Custom ModelAdmin                         Django TinyMCE http://github.com/...
Lazy Registration of a                         Custom ModelAdmin                         Django TinyMCE http://github.com/...
Lazy Registration of a                         Custom ModelAdmin                         Django TinyMCE http://github.com/...
Lazy Registration of a                         Custom ModelAdmin                         Django TinyMCE http://github.com/...
Touch PointsFriday, March 11, 2011
Touch Points of an App                         The parts of an application that                         usually need tweak...
Situation 9              You want the URLs of your app to                    live under any prefix                  (e.g. ...
Name your URLsFriday, March 11, 2011
Name your URLs                  url Function     nameFriday, March 11, 2011
Reference your                         URLs by nameFriday, March 11, 2011
Situation 10                         You want your templates                          to be easily overridableFriday, Marc...
“Namespace” Templates                                               All templates in                         coolapp      ...
“Namespace” Templates                         coolapp                               templates                             ...
Extend one template                         site_base.html   base.html                         summary.html     index.html...
Extend one template                         site_base.html   base.html                                          base.html ...
Extend one template                         site_base.html   base.html                                          base.html ...
Extend one template                             coolapp/base.htmlFriday, March 11, 2011
Extend one template                             coolapp/base.htmlFriday, March 11, 2011
Import your blocks       Allows you to override any of the templates                                       extra_head.html...
Situation 11                         You want flexibility storing                              uploaded filesFriday, March...
Define a Storage OptionFriday, March 11, 2011
Situation 12                            You want to alter the                            data your views use              ...
100% more class-                           based views!                          django-cbv for                           ...
My Info                     • coreyoordt@gmail.com                     • @coordt                     • github.com/coordt  ...
Upcoming SlideShare
Loading in...5
×

Pluggable Django Application Patterns PyCon 2011

7,133

Published on

12 Ways to get your apps pluggable or reusable given at PyCon 2011

Published in: Technology

Pluggable Django Application Patterns PyCon 2011

  1. 1. Pluggable Patterns For Reusable Django ApplicationsFriday, March 11, 2011
  2. 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 MONOLITHSFriday, March 11, 2011
  3. 3. An application should be “pluggable”Friday, March 11, 2011
  4. 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. 5. A “pluggable” app is Self-Contained Batteries are included Dependencies are declaredFriday, March 11, 2011
  6. 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. 7. A “pluggable” app is Easily Installed pip install coolapp You did declare your dependencies, right?Friday, March 11, 2011
  8. 8. How do we make a “pluggable” application?Friday, March 11, 2011
  9. 9. Stop thinking like this http://upload.wikimedia.org/wikipedia/commons/archive/a/aa/20090315161532!Ferrari_Enzo_Ferrari.JPGFriday, March 11, 2011
  10. 10. and think like thisFriday, March 11, 2011
  11. 11. Applications can have very different purposes http://www.flickr.com/photos/tiemposdelruido/4051083769/Friday, March 11, 2011
  12. 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 orFriday, March 11, 2011
  13. 13. Situation 1 You want to configure your app without modifying its code (e.g. API keys)Friday, March 11, 2011
  14. 14. Configurable Options Django Supertagging http://github.com/josesoa Internal Name Setting Name Default ValueFriday, March 11, 2011
  15. 15. Configurable Options Django Debug Toolbar http://github.com/robhudsonFriday, March 11, 2011
  16. 16. Data Apps http://www.flickr.com/photos/29276244@N03/3200630853/Friday, March 11, 2011
  17. 17. Situation 2 Lots of variations Each implementation is different (e.g. blogs)Friday, March 11, 2011
  18. 18. Abstract Models GLAMKit http://www.glamkit.org/ EntryBase FeaturableEntryMixin StatusableEntryMixin TaggableEntryMixin HTMLFormattableEntryMixinFriday, March 11, 2011
  19. 19. Situation 3 A few, well-known of variations (e.g. Use django.contrib.sites?)Friday, March 11, 2011
  20. 20. Optional Field SettingsFriday, March 11, 2011
  21. 21. Situation 4 Optionally use another application (e.g. Use django-tagging?)Friday, March 11, 2011
  22. 22. Optional IntegrationFriday, March 11, 2011
  23. 23. Situation 5 You want to reference different models (e.g. Customizable author field)Friday, March 11, 2011
  24. 24. Runtime Configurable Foreign Keys Viewpoint http://github.com/washingtontimesFriday, March 11, 2011
  25. 25. Runtime Configurable Foreign Keys Viewpoint http://github.com/washingtontimesFriday, March 11, 2011
  26. 26. Utility Apps http://www.flickr.com/photos/s8/3638531205/Friday, March 11, 2011
  27. 27. Required for template tags or management commandsFriday, March 11, 2011
  28. 28. Decorator Apps http://www.flickr.com/photos/yum9me/2109549869/Friday, March 11, 2011
  29. 29. New Method New Field Custom Manager New AdminFriday, March 11, 2011
  30. 30. Situation 6 You want to add a field to a modelFriday, March 11, 2011
  31. 31. Lazy Field Insertion Django Categories http://github.com/washingtontimesFriday, March 11, 2011
  32. 32. Lazy Field Insertion Django Categories http://github.com/washingtontimesFriday, March 11, 2011
  33. 33. Lazy Field Insertion Django Categories http://github.com/washingtontimesFriday, March 11, 2011
  34. 34. Situation 7 You want to add a custom manager to a modelFriday, March 11, 2011
  35. 35. Lazy Manager Insertion Django MPTT http://github.com/django-mpttFriday, March 11, 2011
  36. 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. 37. Situation 8 You want to customize a model’s ModelAdmin (e.g. Change the widget of a field)Friday, March 11, 2011
  38. 38. Lazy Registration of a Custom ModelAdmin Django TinyMCE http://github.com/justquick project’s settings.pyFriday, March 11, 2011
  39. 39. Lazy Registration of a Custom ModelAdmin Django TinyMCE http://github.com/justquick Django-TinyMCE’s models.pyFriday, March 11, 2011
  40. 40. Lazy Registration of a Custom ModelAdmin Django TinyMCE http://github.com/justquick Django-TinyMCE’s admin.pyFriday, March 11, 2011
  41. 41. Lazy Registration of a Custom ModelAdmin Django TinyMCE http://github.com/justquick bottom of Django-TinyMCE’s admin.pyFriday, March 11, 2011
  42. 42. Touch PointsFriday, March 11, 2011
  43. 43. Touch Points of an App The parts of an application that usually need tweaking • URLs • Templates • View responsesFriday, March 11, 2011
  44. 44. Situation 9 You want the URLs of your app to live under any prefix (e.g. /blogs/ vs. /weblogs/)Friday, March 11, 2011
  45. 45. Name your URLsFriday, March 11, 2011
  46. 46. Name your URLs url Function nameFriday, March 11, 2011
  47. 47. Reference your URLs by nameFriday, March 11, 2011
  48. 48. Situation 10 You want your templates to be easily overridableFriday, March 11, 2011
  49. 49. “Namespace” Templates All templates in coolapp a template “name space” templates coolapp base.htmlFriday, March 11, 2011
  50. 50. “Namespace” Templates coolapp templates coolapp Referenced as base.html “coolapp/base.html”Friday, March 11, 2011
  51. 51. Extend one template site_base.html base.html summary.html index.html detail.htmlFriday, March 11, 2011
  52. 52. Extend one template site_base.html base.html base.html summary.html index.html detail.htmlFriday, March 11, 2011
  53. 53. Extend one template site_base.html base.html base.html summary.html index.html detail.htmlFriday, March 11, 2011
  54. 54. Extend one template coolapp/base.htmlFriday, March 11, 2011
  55. 55. Extend one template coolapp/base.htmlFriday, March 11, 2011
  56. 56. Import your blocks Allows you to override any of the templates extra_head.html coolapp/detail.htmlFriday, March 11, 2011
  57. 57. Situation 11 You want flexibility storing uploaded filesFriday, March 11, 2011
  58. 58. Define a Storage OptionFriday, March 11, 2011
  59. 59. Situation 12 You want to alter the data your views use (e.g. Extra context, different template)Friday, March 11, 2011
  60. 60. 100% more class- based views! django-cbv for backwards compatibility!Friday, March 11, 2011
  61. 61. My Info • coreyoordt@gmail.com • @coordt • github.com/coordt • github.com/washingtontimes • opensource.washingtontimes.comFriday, March 11, 2011
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×