djangocon 09 presentation - pluggable applications

26
Pluggable Applications DjangoCon 2009 Nowell Strite Shawn Rider

Upload: nowell-strite

Post on 13-Jan-2015

5.610 views

Category:

Technology


0 download

DESCRIPTION

Django-Pluggables is a design pattern that endows reusable applications with a few additional features:#. Applications can exist at multiple URL locations (e.g. http://example.com/foo/app/ and http://example.com/bar/app/).#. Applications can be "parented" to other applications or objects which can then deliver specialized context information.#. Posting form data and error handling can happen in locations that make sense to the user, as opposed to the common practice of using templatetags and standalone error or preview pages for form data processing.#. Views and templates remain generic and reusable.

TRANSCRIPT

Page 1: Djangocon 09 Presentation - Pluggable Applications

Pluggable Applications

DjangoCon 2009

Nowell Strite Shawn Rider

Page 2: Djangocon 09 Presentation - Pluggable Applications

•  Background •  Current Approaches •  Our Use Case / Requirements 

•  A Proposed Solu=on •  Some Live Examples 

•  Q & A 

Road Map

Page 3: Djangocon 09 Presentation - Pluggable Applications

A little background

PBS manages several major internal sites:

and more…

Page 4: Djangocon 09 Presentation - Pluggable Applications

A little background

And many of these use Django to some degree:

Page 5: Djangocon 09 Presentation - Pluggable Applications

A little background

Page 6: Djangocon 09 Presentation - Pluggable Applications

A little background

Page 7: Djangocon 09 Presentation - Pluggable Applications

•  Serving a diverse array of sites •  Built on a diverse technical architecture •  Many repe==ve components desired by producers 

and users 

•  Ease of implementa=on is crucial 

•  But highly specific implementa=on details and goals are oIen more important to producers 

PBS needs Django

Page 8: Djangocon 09 Presentation - Pluggable Applications

The Reusable App design paKern helps us: 

–  Easy to share func=onality with other projects –  Quick to get features up and running –  Possible to expand and enhance func=onality 

Reusable Apps Save the Day

Page 9: Djangocon 09 Presentation - Pluggable Applications

The Reusable App design paKern has some limita=ons 

•  Apps are expected to live at one URL  (e.g. hKp://example.com/comments/) 

•  Current conven=on of adding ‘extra_context’ params are not sa=sfactory 

•  Conven=onal template replacement is not always flexible enough  

Reusable Apps Save the Day… Sort of…

Page 10: Djangocon 09 Presentation - Pluggable Applications

Template Tags allow apps to expose func=onality inside templates for other apps and site 

components 

Template Tags to the Rescue

Page 11: Djangocon 09 Presentation - Pluggable Applications

But template tags are also limited: 

–  Form pos=ng becomes complicated 

–  Generic inser=on of data into page context can be dangerous 

–  Addi=onal logic, such as complex permissions checks, are difficult to enforce or bypass via generic template tags 

Template Tags to the Rescue … Sort of…

Page 12: Djangocon 09 Presentation - Pluggable Applications

The Use Case: PBS TeacherLine Peer Connection

Peer Connection features:

1. Access to a robust database of curated resources 2. Community features to allow educators to share and discuss these resources

Because discussion is so important in this setting, many objects in Peer Connection support discussions.

Given that the purpose of the site is partially to learn to use technology, a clean user experience is essential.

Page 13: Djangocon 09 Presentation - Pluggable Applications

The Use Case: PBS TeacherLine Peer Connection

Our Requirements:

•  Sensible URLs that follow our patterns elsewhere on the site •  Allow discussions app to interact with other apps and objects in expected ways •  Allow posting of data and error handling in the same location – no redirects to stand-alone pages to fix a message post. •  Flexibility to enforce permissions and other variations of display in an ad-hoc way.

Page 14: Djangocon 09 Presentation - Pluggable Applications

By making an app “pluggable” we gain some extra features: 

–  Apps can be presented at various URLs throughout the site 

–  Each presenta=on can define context in a clean, DRY manner 

–  Form pos=ng and error handling can happen in the logical loca=on for the user 

Our Solution: Make the Reusable App Pluggable

Page 15: Djangocon 09 Presentation - Pluggable Applications

The Use Case: PBS TeacherLine Peer Connection

Two examples of how discussions appear in Peer Connection:

/content/module/216/discuss/

/community/network/pbs-staff/group/tl-technology/discuss/

Page 16: Djangocon 09 Presentation - Pluggable Applications

The Use Case: PBS TeacherLine Peer Connection

Two examples of how discussions appear in Peer Connection:

/content/module/216/discuss/

/community/network/pbs-staff/group/tl-technology/discuss/

Page 17: Djangocon 09 Presentation - Pluggable Applications

The Use Case: PBS TeacherLine Peer Connection

Two examples of how discussions appear in Peer Connection:

/content/module/216/discuss/

/community/network/pbs-staff/group/tl-technology/discuss/

Page 18: Djangocon 09 Presentation - Pluggable Applications

The Use Case: PBS TeacherLine Peer Connection

/content/module/216/discuss/

/community/network/pbs-staff/group/tl-technology/discuss/

Content generated by the pluggable app:

Page 19: Djangocon 09 Presentation - Pluggable Applications

The Use Case: PBS TeacherLine Peer Connection

/content/module/216/discuss/

/community/network/pbs-staff/group/tl-technology/discuss/

Content required by the presenting app templates:

Page 20: Djangocon 09 Presentation - Pluggable Applications

Make it So

By making our discussions app pluggable, we solved our problems and fulfilled our requirements.

But what kind of work does it take to get there?

Page 21: Djangocon 09 Presentation - Pluggable Applications

Make it So

Parts of Pluggable Apps

• Pluggable URL definition

• Pluggable application views definition

•  Subclass / instantiation of Pluggable application

Page 22: Djangocon 09 Presentation - Pluggable Applications

Pluggable URL Definition

frompluggablesimportPluggableApp,url,include,patterns

classComplaintsApp(PluggableApp): urlpatterns=patterns('complaints.views.traditional', url(r'^$','index',name='complaints_index'), url(r'^create/$','edit',name='complaints_create'), url(r'^(?P<complaint_id>\d+)/edit/$','edit',name='complaints_edit'), url(r'^(?P<complaint_id>\d+)/delete/$','delete',name='complaints_delete'), )

Page 23: Djangocon 09 Presentation - Pluggable Applications

Pluggable Application Views Definition

defindex(request): complaints=Complaint.objects.pluggable(request) form=ComplaintForm() template=request.pluggable.config.get('template’) base_template=request.pluggable.config.get('base_template’)

returnrender_to_response(template,{ 'complaints':complaints, 'form':form, 'base_template':base_template, },context_instance=RequestContext(request))

Page 24: Djangocon 09 Presentation - Pluggable Applications

Subclass / Instantiation of Pluggable Application

classSillyWalkComplaintsApp(ComplaintsApp): defpluggable_config(self,request,walk_slug=None): return{'base_template':'sillywalks/view.html'}

defpluggable_view_context(self,request,walk_slug): try: sillywalk=SillyWalk.objects.get(slug=walk_slug) exceptSillyWalk.DoesNotExist: raiseHttp404 returnsillywalk

defpluggable_template_context(self,request,walk_slug): return{'sillywalk':request.pluggable.view_context}

complaints_app=SillyWalkComplaintsApp('sillywalks')

Page 25: Djangocon 09 Presentation - Pluggable Applications

And Now For Something Completely Different

Let’s see some live examples…

Page 26: Djangocon 09 Presentation - Pluggable Applications

Thank You For Your Time

Code referenced in this presentation is available at: http://bit.ly/django-pluggables

Nowell Strite Twitter: @nowells

Email: [email protected]

Shawn Rider Twitter: @shawnr

Email: [email protected]