Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
Pluggable Patterns
  For Reusable Django Applications
Project
Project




Configuration   URL routing   Templates
Project
 Application    Application   Application




 Application    Application   Application




 Application    Applic...
Here there be power!
                  Project
 Application      Application   Application




 Application      Applicati...
We need a better definition
 for “Django Application”
An app                          MyBlog App
should not be              • Categories
                           • Custom Tag...
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 pi...
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 wil...
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...
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-h...
Application Types
• Data. Manages specific data and access to it
• Utility. Provide aapplication a specific
  problem for ...
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


   StatusableE...
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                New Field
Custom Manager             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...
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
                        All templates in
  coolapp                  a template
                     ...
“Namespace” Templates
  coolapp
                          All templates
        templates        extend a single
         ...
“Namespace” Templates
  coolapp

        templates       This template simply

              coolapp

                    ...
Import your blocks
Allows you to override any of the templates

                              extra_head.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...
To be continued…
My Info

• coreyoordt@gmail.com
• @coordt
• github.com/coordt
• github.com/washingtontimes
• opensource.washingtontimes.com
Próxima SlideShare
Cargando en…5
×

Pluggable patterns

2.291 visualizaciones

Publicado el

  • Sé el primero en comentar

Pluggable patterns

  1. 1. Pluggable Patterns For Reusable Django Applications
  2. 2. Project
  3. 3. Project Configuration URL routing Templates
  4. 4. Project Application Application Application Application Application Application Application Application Application Configuration URL routing Templates
  5. 5. Here there be power! Project Application Application Application Application Application Application Application Application Application Configuration URL routing Templates
  6. 6. We need a better definition for “Django Application”
  7. 7. 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
  8. 8. An application should be “pluggable”
  9. 9. A “pluggable” app is Focused Write programs that do one thing and do it well. — Doug McIlroy (inventor of UNIX pipes)
  10. 10. A “pluggable” app is Self-Contained Batteries are included Dependencies are declared
  11. 11. 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.
  12. 12. A “pluggable” app is Easily Installed pip install coolapp You did declare your dependencies, right?
  13. 13. How do we make a “pluggable” application?
  14. 14. Stop thinking like this http://upload.wikimedia.org/wikipedia/commons/archive/a/aa/20090315161532!Ferrari_Enzo_Ferrari.JPG
  15. 15. and think like this http://www.flickr.com/photos/motagirl2/4301276868/
  16. 16. Applications can have very different purposes http://www.photoshoproadmap.com/imagenes/blog/lego-brushes/lego-bricks-high-resolution.jpg
  17. 17. 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
  18. 18. Data Apps http://www.geeky-gadgets.com/wp-content/uploads/2009/08/lego-usb-hub_2.jpg
  19. 19. Situation 1 Lots of variations Each implementation is different (e.g. blogs)
  20. 20. Abstract Models GLAMKit http://www.glamkit.org/ EntryBase FeaturableEntryMixin StatusableEntryMixin TaggableEntryMixin HTMLFormattableEntryMixin
  21. 21. Situation 2 A few, well-known of variations (e.g. Use django.contrib.sites?)
  22. 22. Optional Field Settings
  23. 23. Situation 3 Optionally use another application (e.g. Use django-tagging?)
  24. 24. Optional Integration
  25. 25. Optional Integration
  26. 26. Situation 3 You want to reference different models (e.g. Customizable author field)
  27. 27. Configurable Foreign Keys Viewpoint http://github.com/washingtontimes
  28. 28. Situation 3 You want to provide hooks for other applications (e.g. While processing in a view)
  29. 29. Emit Signals Satchmo http://www.satchmoproject.com/
  30. 30. Utility Apps http://www.1000steine.com/brickset/images/852759-1.jpg
  31. 31. Utility apps are just code libraries
  32. 32. Projects with template tags need one
  33. 33. Does it need to be in ? Yes, if you have: • Templates • Template Tags • Management Commands
  34. 34. Decorator Apps http://trendland.net/wp-content/uploads/2008/12/lego-ring.jpg
  35. 35. CoolApp New Method New Field Custom Manager New Admin
  36. 36. Registering for Decoration
  37. 37. Simple Model Registry
  38. 38. Model-Data Registry
  39. 39. Lazy Registration
  40. 40. Situation 4 You want to add a field to a model (e.g. Link to another model)
  41. 41. Simple Model Registry
  42. 42. Situation 5 You want to add a method to a model (e.g. Link to another model)
  43. 43. Adding methods
  44. 44. Situation 6 You want to customize a model’s ModelAdmin (e.g. Change the widget of a field)
  45. 45. Lazy Registration of a Custom ModelAdmin
  46. 46. Lazy Registration of a Custom ModelAdmin
  47. 47. Lazy Registration of a Custom ModelAdmin
  48. 48. Touch Points
  49. 49. Touch Points of an App The parts of an application that usually need tweaking • URLs • Templates • Configurable options • View responses
  50. 50. Situation 7 You want the URLs of your app to live under any prefix (e.g. /blogs/ vs. /weblogs/)
  51. 51. Name your URLs
  52. 52. Reference your URLs by name
  53. 53. Situation 8 You want your templates to be easily overridable
  54. 54. “Namespace” Templates coolapp templates coolapp base.html
  55. 55. “Namespace” Templates All templates in coolapp a template “name space” templates coolapp base.html
  56. 56. “Namespace” Templates coolapp All templates templates extend a single template coolapp base.html
  57. 57. “Namespace” Templates coolapp templates This template simply coolapp base.html
  58. 58. Import your blocks Allows you to override any of the templates extra_head.html content.html base.html
  59. 59. Situation 9 You want to configure your app without modifying its code (e.g. API keys)
  60. 60. Configurable Options Django Supertagging http://github.com/josesoa Internal Name Setting Name Default Value
  61. 61. Configurable Options Django Debug Toolbar http://github.com/robhudson
  62. 62. Define a Storage Option
  63. 63. Situation 10 You want to alter the data your views use (e.g. Extra context, different template)
  64. 64. Add keyword arguments Django Registration http://bitbucket.org/ubernostrum
  65. 65. 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-object- oriented-views-django/ http://www.toddreed.name/content/django-view-class/
  66. 66. To be continued…
  67. 67. My Info • coreyoordt@gmail.com • @coordt • github.com/coordt • github.com/washingtontimes • opensource.washingtontimes.com

×