djangoic approach to implement common web development paradigms

Post on 19-May-2015

7773 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Web development frameworks conceptualize various abstractions to make common web development paradigms easy to implement. These abstractions have to be "transparent" in that they enable developer to override where necessary. Django, a modern python framework provides a full stack framework and has pioneered best practices in defining these abstractions for various web application paradigms such as Authentication, Permissions, Sessions, Caching, Security from CSRF, File Storage, Form Validation, etc. The abstractions provided by django are transparent and extensible and are as such referred to in the community to be "Djangoic"The session covers these abstractions, their defaults and using some of their transparencies to achieve a few non-default common ends such as using cloud storage services like Amazon web services, Implementing LDAP authentication, Implementing caching using Memcached, database and NoSQL data stores. The biggest take away to the developer is the ability to identify and create other Djangoic solutions that are transparent abstractions by examining the ones implemented within django and developed by the community in open source applications.

Transcript

Introduction Paradigms The Admin Interface The Django Advantage

Djangoic approach to implement common webdevelopment paradigms

Lakshman Prasad

April 20, 2011

Concepts and abstractions used to represent elements of a program

CS is neither about computers nor is science - Abelson, SICP, MIT

Web Development Paradigms: Modeling common web patterns

Introduction Paradigms The Admin Interface The Django Advantage

Why

Introduction Paradigms The Admin Interface The Django Advantage

• For building database driven web applications

• Emphasis on Reuse, DRY and simplicity

Introduction Paradigms The Admin Interface The Django Advantage

• For building database driven web applications

• Emphasis on Reuse, DRY and simplicity

Batteries Included

docs.djangoproject.com

Lets examine the batteries

Introduction Paradigms The Admin Interface The Django Advantage

About Me• Active Djangonaut and active in Python world• Part of a few popular open source django applications

github.com/becomingGuru.• Co-Authored an ebook ”django-design-patterns”• Architect and develop django applications at InMobi• Earlier, Consulting and Development via Agiliq Solutions• Developed several custom proprietory django applications

• twitter.com/becomingGuru http://becomingguru.com

Introduction Paradigms The Admin Interface The Django Advantage

A nerd, for a while

consulting since, until recently

Always been developing

Work at InMobi now

Introduction Paradigms The Admin Interface The Django Advantage

IntroductionDefinitionAbout MeAbstractions

ParadigmsFormsAuthentication/Caching/SessionsInernational/Human/Local ..izationGeneric ViewsSorting/Pagination/DataBrowse/GRID/Messages/Other

The Admin InterfaceAdminDatabrowse

The Django AdvantageUsersCommunityEndnotes

Software span a spectrum

Those that interact with hardware

The nuts and bolts

Those that solve a human problem ...

... that are Complex Systems

Frameworks come in between ...

... to achieve balance ...

... and reduce cost of maintainance and development

Common Frameworks

Forms

Save form data to a table

Introduction Paradigms The Admin Interface The Django Advantage

Use a Model Form

>>> from django . forms import ModelForm

# C r e a t e th e form c l a s s .>>> c l a s s A r t i c l e F o r m ( ModelForm ) :. . . c l a s s Meta :. . . model = A r t i c l e

# C r e a t i n g a form to add an a r t i c l e .>>> form = A r t i c l e F o r m ( )

# C r e a t i n g a form to change an e x i s t i n g a r t i c l e .>>> a r t i c l e = A r t i c l e . o b j e c t s . g e t ( pk=1)>>> form = A r t i c l e F o r m ( i n s t a n c e=a r t i c l e )

Introduction Paradigms The Admin Interface The Django Advantage

Save Foreign Keys

Introduction Paradigms The Admin Interface The Django Advantage

Use Model Formset

from d jango . forms . models import mode l f o rm s e t f a c t o r yAuthorFormSet = mode l f o rm s e t f a c t o r y ( Author )f o rmse t = AuthorFormSet ( )

>>> p r i n t f o rmse t<i n pu t type=” h idden ” name=”form−TOTAL FORMS”va l u e=”1” i d=” id fo rm−TOTAL FORMS” /><i n pu t type=” h idden ” name=”form−INITIAL FORMS”va l u e=”0” i d=” id fo rm−INITIAL FORMS” /><i n pu t type=” h idden ” name=”form−MAX NUM FORMS”i d=” id fo rm−MAX NUM FORMS” /><t r><th>< l a b e l f o r=” id fo rm−0−name”>Name:</ l a b e l></th><td><i n pu t i d=” id fo rm−0−name” type=” t e x t ”name=”form−0−name” maxlength=”100” /></td></t r>

Introduction Paradigms The Admin Interface The Django Advantage

Model Formset options

f o r m s e t = AuthorFormSet ( qs=Author . o b j e c t s . a l l ( ) ,e x t r a =5)

f o r m s e t . i s v a l i d ( )

f o r m s e t . e r r o r s{ ’ name ’ : ’ Th i s f i e l d i s r e q u i r e d ’ }

f o r m s e t . c h a n g e d f o r m s

f o r m s e t . s a v e ( )

Introduction Paradigms The Admin Interface The Django Advantage

Form pre populated

Introduction Paradigms The Admin Interface The Django Advantage

Forms dont have to save to a Model

from django import fo rms

c l a s s ContactForm ( forms . Form ) :s u b j e c t = forms . C h a r F i e l d ( m a x l e n g t h =100)message = forms . C h a r F i e l d ( )s e n d e r = forms . E m a i l F i e l d ( )c c m y s e l f = forms . B o o l e a n F i e l d ( r e q u i r e d=F a l s e )

def s a v e ( s e l f ) :#Do a n y t h i n g. . .

Form Preview

Introduction Paradigms The Admin Interface The Django Advantage

Form Preview

from django . c o n t r i b . f o r m t o o l s . p r e v i e w import FormPreviewfrom myapp . models import SomeModel

#Add a u r l( r ’ ˆ p o s t /$ ’ , SomeModelFormPreview ( SomeModelForm ) ) ,

#D e f i n e form p r e v i e w

c l a s s SomeModelFormPreview ( FormPreview ) :

def done ( s e l f , r e q u e s t , c l e a n e d d a t a ) :# Do someth ing w i t h t he c l e a n e d d a t a , then# r e d i r e c t to a ” s u c c e s s ” page .return H t t p R e s p o n s e R e d i r e c t ( ’ / form / s u c c e s s ’ )

Form Wizard

Introduction Paradigms The Admin Interface The Django Advantage

Form Wizard

c l a s s ContactWizard ( FormWizard ) :def done ( s e l f , r e q u e s t , f o r m l i s t ) :

d o s o m e t h i n g w i t h t h e f o r m d a t a ( f o r m l i s t )return H t t p R e s p o n s e R e d i r e c t ( ’ / r e d i r e c t / ’ )

Introduction Paradigms The Admin Interface The Django Advantage

CSRF Protection

<form a c t i o n=”” method=” p o s t ”>{% c s r f t o k e n %}

from django . v i e w s . d e c o r a t o r s . c s r f import c s r f p r o t e c tfrom django . t e m p l a t e import R e q u e s t C o n t e x t

@ c s r f p r o t e c tdef my view ( r e q u e s t ) :

c = {}# . . .return r e n d e r t o r e s p o n s e ( ” a t e m p l a t e . html ” , c ,

c o n t e x t i n s t a n c e=R e q u e s t C o n t e x t ( r e q u e s t ) )

Introduction Paradigms The Admin Interface The Django Advantage

Authentication: Point to url pattern

u r l p a t t e r n s += p a t t e r n s ( ’ d jango . c o n t r i b . auth . v i e w s ’ ,u r l ( r ’ ˆ l o g i n /$ ’ , ’ l o g i n ’ ) ,u r l ( r ’ ˆ l o g o u t /$ ’ , ’ l o g o u t ’ ) ,u r l ( r ’ ˆ r e g i s t e r /$ ’ , ’ r e g i s t e r ’ ) ,u r l ( r ’ ˆ p a s s r e s e t /$ ’ , ’ p a s s w o r d r e s e t ’ ) ,u r l ( r ’ ˆ p a s s r e s e t 2 /$ ’ , ’ p a s s w o r d r e s e t d o n e ’ ) ,u r l ( r ’ ˆ p a s s r e s e t c o n f i r m /(?P<uidb36 >[−\w]+)/ ’ ,

’ p a s s w o r d r e s e t c o n f i r m ’ ) ,u r l ( r ’ ˆ p a s s r e s e t c o m p l e t e /$ ’ ,

’ p a s s w o r d r e s e t c o m p l e t e ’ ) ,)

Introduction Paradigms The Admin Interface The Django Advantage

Login Required

from django . c o n t r i b . auth . d e c o r a t o r s import \l o g i n r e q u i r e d

@ l o g i n r e q u i r e d ( r e d i r e c t f i e l d n a m e= ’ r e d i r e c t t o ’ )def my view ( r e q u e s t ) :

. . .

Introduction Paradigms The Admin Interface The Django Advantage

Authentication backends

c l a s s S e t t i n g s B a c k e n d :”””A u t h e n t i c a t e a g a i n s t t he s e t t i n g s ADMIN LOGINand ADMIN PASSWORD.”””def a u t h e n t i c a t e ( s e l f , username=None , password=None ) :

i f l o g i n v a l i d and p w d v a l i d :return u s e r

return None

def g e t u s e r ( s e l f , u s e r i d ) :t ry :

return User . o b j e c t s . g e t ( pk=u s e r i d )except User . DoesNotEx i s t :

return None

django-socialauth

Introduction Paradigms The Admin Interface The Django Advantage

Open ID backendc l a s s OpenIdBackend :

def a u t h e n t i c a t e ( s e l f , open id key , r eque s t , p r o v i d e r , u s e r=None ) :t r y :

a s s o c = Us e rA s s o c i a t i o n . o b j e c t s . ge t ( open i d k e y=open i d k e y )r e t u r n a s soc . u s e r

except Us e rA s s o c i a t i o n . DoesNotEx i s t :#f e t c h i f open id p r o v i d e r p r o v i d e s any s imp l e r e g i s t r a t i o n f i e l d si f r e q u e s t . open id and r e q u e s t . open id . s r e g :

ema i l = r e q u e s t . open id . s r e g . ge t ( ’ ema i l ’ )nickname = r e qu e s t . open id . s r e g . ge t ( ’ nickname ’ )f i r s t n ame , l a s tname = ( r e qu e s t . open id . s r e g

. ge t ( ’ f u l l n ame ’ , ’ ’ )r e t u r n u s e r

def GooglesAX ( s e l f , o p e n i d r e s p on s e ) :ema i l = ( op e n i d r e s p on s e . ax

. g e t S i n g l e ( ’ h t tp : // axschema . org / con ta c t / ema i l ’ ) )f i r s t n ame = ( op en i d r e s p on s e . ax

. g e t S i n g l e ( ’ h t tp : // axschema . org /namePerson/ f i r s t ’ ) )l a s tname = ( op en i d r e s p on s e . ax

. g e t S i n g l e ( ’ h t tp : // axschema . org /namePerson/ l a s t ’ ) )r e t u r n l o c a l s ( )

def g e t u s e r ( s e l f , u s e r i d ) :t r y :

u s e r = User . o b j e c t s . ge t ( pk=u s e r i d )r e t u r n u s e r

except User . DoesNotEx i s t :r e t u r n None

”Normalized data is for sissies” - Cal Henderson

Introduction Paradigms The Admin Interface The Django Advantage

Different blocks need different caching durations

Introduction Paradigms The Admin Interface The Django Advantage

Generally Memcache Every page

CACHE BACKEND = ’ memcached : / / 1 2 7 . 0 . 0 . 1 : 1 1 2 1 1 / ’CACHE BACKEND = ’ memcached : / / 1 7 2 . 1 9 . 2 6 . 2 4 0 : 1 1 2 1 1 ;\1 7 2 . 1 9 . 2 6 . 2 4 2 : 1 1 2 1 2 ; 1 7 2 . 1 9 . 2 6 . 2 4 4 : 1 1 2 1 3 / ’

Introduction Paradigms The Admin Interface The Django Advantage

Implement a caching decorator

from django . c o r e . cache import cachedef c a c h e f o r ( seconds , f e t c h =0):

def c a c h e i t ( f u n c ) :def d e c o f u n c ( s e l f ) :

v a l = cache . g e t ( s e l f . g e t c a c h e k e y ( f e t c h ) )i f not v a l :

v a l = f u n c ( s e l f )cache . s e t ( s e l f . g e t c a c h e k e y ( f e t c h ) ,

v a l , s e c o n d s )return v a l

return d e c o f u n creturn c a c h e i t

Introduction Paradigms The Admin Interface The Django Advantage

Apply on the blocks

c l a s s T w i t t e r B l o c k ( T w i t t e r S e a r c h B l o c k ) :

template name = ’ t w i t t e r u s e r b l o c k . html ’a j a x t e m p l a t e = ’ t w e e t s u s e r . html ’

@ c a c h e f o r (60∗60 , f e t c h =1)def f e t c h d a t a ( s e l f ) :

tw = T w i t t e r ( e m a i l= ’ umoja com ’ )u s e r t w e e t s = tw . s t a t u s e s . u s e r t i m e l i n e (

sc reen name=s e l f . data )return u s e r t w e e t s , None

Introduction Paradigms The Admin Interface The Django Advantage

Different cache Backends

• Database

• File System

• Local Memory

• Redis, a NoSQL store

• Dummy- For Development

• Sessions can use any of these, or the database.

Introduction Paradigms The Admin Interface The Django Advantage

Humanization and Localization

19 Apr 2 0 1 0 | n a t u r a l d a y becomes ‘ y e s t e r d a y ‘ .20 Apr 2 0 1 0 | n a t u r a l d a y becomes ‘ today ‘ .

1 | o r d i n a l becomes ‘1 st ‘ .2 | o r d i n a l becomes ‘2 nd ‘ .

1000000 | i n t w o r d becomes ‘ 1 . 0 m i l l i o n ‘ .1200000 | i n t w o r d becomes ‘ 1 . 2 m i l l i o n ‘ .

{{ v a l u e | l o c a l i z e }}

Introduction Paradigms The Admin Interface The Django Advantage

Internationalization

{% b l o c k t r a n s w i t h amount=a r t i c l e . p r i c e %}That w i l l c o s t $ {{ amount }} .{% e n d b l o c k t r a n s %}

{% b l o c k t r a n s w i t h myvar=v a l u e | f i l t e r %}This w i l l have {{ myvar }} i n s i d e .{% e n d b l o c k t r a n s %}

c l a s s in . fo rms . I N S t a t e F i e l dc l a s s in . fo rms . I N Z i p C o d e F i e l dc l a s s in . fo rms . I N S t a t e S e l e c t

Introduction Paradigms The Admin Interface The Django Advantage

Using Generic Views

# some app / v i e w s . pyfrom django . v i e w s . g e n e r i c import TemplateView

c l a s s AboutView ( TemplateView ) :template name = ” about . html ”

# u r l s . pyfrom django . c o n f . u r l s . d e f a u l t s import ∗from some app . v i e w s import AboutView

u r l p a t t e r n s = p a t t e r n s ( ’ ’ ,( r ’ ˆ about / ’ , AboutView . a s v i e w ( ) ) ,

)

Introduction Paradigms The Admin Interface The Django Advantage

List and Object Views

from django . v i e w s . g e n e r i c import L i s t V i e wfrom books . models import Book

c l a s s AcmeBookListView ( L i s t V i e w ) :

c o n t e x t o b j e c t n a m e = ” b o o k l i s t ”q u e r y s e t = Book . o b j e c t s . f i l t e r ( p u b l i s h e r n a m e=”Acme P u b l i s h i n g ” )template name = ” books / a c m e l i s t . html ”

Introduction Paradigms The Admin Interface The Django Advantage

Mixins

from d jango import ht tpfrom d jango . u t i l s import s imp l e j s o n as j s o n

c l a s s JSONResponseMixin ( o b j e c t ) :def r e n d e r t o r e s p o n s e ( s e l f , c on t e x t ) :

” Retu rns a JSON re spon s e c o n t a i n i n g ’ c on t e x t ’ as pay load ”r e t u r n s e l f . g e t j s o n r e s p o n s e ( s e l f . c o n v e r t c o n t e x t t o j s o n ( con t e x t ) )

def g e t j s o n r e s p o n s e ( s e l f , content , ∗∗h t t p r e s p on s e kwa r g s ) :” Cons t r u c t an ‘ HttpResponse ‘ o b j e c t . ”r e t u r n ht tp . HttpResponse ( content ,

c o n t e n t t y p e=’ a p p l i c a t i o n / j s o n ’ ,∗∗h t t p r e s p on s e kwa r g s )

def c o n v e r t c o n t e x t t o j s o n ( s e l f , c on t e x t ) :” Conver t the con t e x t d i c t i o n a r y i n t o a JSON ob j e c t ”r e t u r n j s o n . dumps ( con t e x t )

Introduction Paradigms The Admin Interface The Django Advantage

Any combinations of Mixins

c l a s s Hyb r i dDe ta i lV i ew ( JSONResponseMixin ,S ing l eOb jec tTemp la teResponseMix in ,BaseDeta i lV i ew ) :

def r e n d e r t o r e s p o n s e ( s e l f , c on t e x t ) :# Look f o r a ’ fo rmat=j s o n ’ GET argumenti f s e l f . r e q u e s t .GET. ge t ( ’ fo rmat ’ , ’ html ’ ) == ’ j s o n ’ :

r e t u r n JSONResponseMixin . r e n d e r t o r e s p o n s e ( s e l f , c on t e x t )e l s e :

r e t u r n S ing l eOb j ec tTemp la t eResponseMix i n . r e n d e r t o r e s p o n s e ( s e l f ,c on t e x t )

Introduction Paradigms The Admin Interface The Django Advantage

Types of Generic Views

• Simple: Redirect, Render to Template

• Detail, List View

• Archive View: Date, Month, Year views

• Basic CRUD, with or w/o Auth

Introduction Paradigms The Admin Interface The Django Advantage

Builtin support for Pagination

>>> from django . c o r e . p a g i n a t o r import P a g i n a t o r>>> o b j e c t s = [ ’ j o h n ’ , ’ p a u l ’ , ’ g e o r g e ’ , ’ r i n g o ’ ]>>> p = P a g i n a t o r ( o b j e c t s , 2)

>>> p . count4>>> p . num pages2>>> p . p a g e r a n g e[ 1 , 2 ]

>>> page1 = p . page ( 1 )>>> page1<Page 1 o f 2>>>> page1 . o b j e c t l i s t[ ’ j o h n ’ , ’ p a u l ’ ]

Introduction Paradigms The Admin Interface The Django Advantage

Abstract the pagination in middleware

d j a n g o p a g e n a t i o n

{% a u t o p a g i n a t e o b j e c t l i s t 10 %}

{% p a g i n a t e %}

DataGrid enables user customisable column and sorting

Introduction Paradigms The Admin Interface The Django Advantage

Other common paradigms

• Display Image Thumbnail

• Store data on a CDN, like Amazon S3

• Periodic tasks with Celery

• Decoupled design using Signals

• GeoDjango : Framework for all location based applications

Introduction Paradigms The Admin Interface The Django Advantage

Admin by models alone

Introduction Paradigms The Admin Interface The Django Advantage

Admin Syntax

from django . c o n t r i b import adminfrom models import Post , Comment

c l a s s PostAdmin ( admin . ModelAdmin ) :l i s t d i s p l a y = ( ’ t i t l e ’ , ’ d a t e t i m e ’ )

c l a s s CommentAdmin ( admin . ModelAdmin ) :l i s t d i s p l a y = ( ’ t e x t ’ , )

admin . s i t e . r e g i s t e r ( Post , PostAdmin )admin . s i t e . r e g i s t e r ( Comment , CommentAdmin )

List Page

Add an Entry

Auto Validation

Introduction Paradigms The Admin Interface The Django Advantage

Databrowse enables browsable web complete withhyperlinks

Introduction Paradigms The Admin Interface The Django Advantage

Popular Users

• Media• LA Times• NY Times• Washington Post• Guardian• Hindustan TImes

• Web2.0• Mahalo: 10 million Page views• Many web startups: Convore, Lanyard, Everyblock,

GroupQuality,

Introduction Paradigms The Admin Interface The Django Advantage

Popular Users

• Media• LA Times• NY Times• Washington Post• Guardian• Hindustan TImes

• Web2.0• Mahalo: 10 million Page views• Many web startups: Convore, Lanyard, Everyblock,

GroupQuality,

Django users

Introduction Paradigms The Admin Interface The Django Advantage

NASA uses Django

After an extensive trade study, we selected Django ...as the first and primary application environment for theNebula Cloud.

700 contibutors

More open source projects than you think ...

... one for every size and style

Don’t go where the puck is; go where the puck is going to be

Introduction Paradigms The Admin Interface The Django Advantage

Image Attributions

ht tp : //www. f l i c k r . com/ photos / t e j e d o r o d e l u z /3157690060/ht tp : //www. f l i c k r . com/ photos /23820645@N05/4287681570/ht tp : //www. f l i c k r . com/ photos / a i d a n j o n e s /3575000735/ht tp : // j a c o b i a n . org /h t tp : // s a n j u a n c o l l e g e . edu/ l i b / images / p h i l o s o p h y b r a i n . j pgh t tp : //www. f l i c k r . com/ photos /uhop /105062059/ht tp : // s3 . amazonaws . com/memebox/ up load s /136/ e x p o n e n t i a l g r a p h 2 . j pgh t tp : // geekandpoke . typepad . com/geekandpoke / images /2008/06/03/ s e xp l 1 8 . j pgh t tp : //www. f l i c k r . com/ photos /go /253819/ht tp : // a round the sphe r e . f i l e s . wo rdp re s s . com/2009/05/ sw i s s−army−k n i f e . j pgh t tp : //www. f r e e f o t o . com/ images /41/04/41 04 9−−−Keep−Le f t web . j pgh t tp : //www. f l i c k r . com/ photos / o r i n r o b e r t j o h n /114430223/

hello@lakshmanprasad.com

top related