Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing a Django e-commerce framework

Writing a Django e-commerce framework

Django is well-suited for e-commerce, and there are currently several nascent django e-commerce frameworks in development. This talk is about the development of django-oscar, a framework designed to leverage Django's core functionality to allow domain-driven e-commerce applications to be built. This is a very powerful approach.

Writing django apps which are flexible enough to be adapted to a wide variety of
scenarios is a non-trivial problem which will be explored. The talk will also
cover a range of e-commerce best practices and how they can be implemented in
Django.

David Winterbottom

March 02, 2012
Tweet

More Decks by David Winterbottom

Other Decks in Programming

Transcript

  1. Writing a Django
    e-commerce
    framework
    David Winterbottom
    @codeinthehole

    View Slide

  2. Synopsis
    • Background / challenges
    • Why django / python
    • Customising django apps
    • E-commerce tips

    View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. Background

    View Slide

  7. • 5 years of e-commerce projects
    • PHP / Python / Django
    • Lots of war stories

    View Slide

  8. Books

    View Slide

  9. Bookshop domain
    • Millions of products
    • ISBNs, BIC categories, authors, publishers
    • Automated feed-driven processing
    • Catalogue
    • Stock
    • Rich data

    View Slide

  10. Two tier structure
    • Application tier - serves HTTP requests
    • Processing tier
    • Download and import feeds
    • Data cleaning and processing

    View Slide

  11. More books

    View Slide

  12. Different...
    • Fulfilment partners
    • Stock logic
    • Payment partners
    • Multiple payment sources
    • Complicated offers
    • eBooks!

    View Slide

  13. PHP framework
    • Customisation via include path overrides
    • Lots of duplication
    • Hard to upgrade
    • Not-invented-here syndrome

    View Slide

  14. Nailvarnish

    View Slide

  15. T-shirts

    View Slide

  16. Digital music

    View Slide

  17. Bar equipment

    View Slide

  18. New requirements
    • Product variations - colours, sizes etc
    • New fulfilment processes
    • Pricing models

    View Slide

  19. B2B
    • Sales reps
    • Tax rules
    • Managed budgets
    • Integration with “enterprise” software

    View Slide

  20. • Original assumptions all broken
    • Domains vary wildly
    • Lots of work-arounds

    View Slide

  21. View Slide

  22. Requirements
    • Agnostic - few assumptions as possible
    • Models domain
    • without duplication
    • without too much meta-data

    View Slide

  23. EAV
    ENTITY ATTRIBUTE VALUE
    Product Weight 150
    Order Cost centre ZPAN

    View Slide

  24. View Slide

  25. View Slide

  26. • Leather gloves

    View Slide

  27. Bendy bits

    View Slide

  28. Taxonomy

    View Slide

  29. Stock

    View Slide

  30. Shipping

    View Slide

  31. Payment

    View Slide

  32. and also...

    View Slide

  33. View Slide

  34. Nothing is sacred

    View Slide

  35. View Slide

  36. View Slide

  37. django-oscar
    • Django 1.3
    • Experience of last 5 years baked into
    models
    • Lean core
    • Different customisation path

    View Slide

  38. View Slide

  39. Lots of others
    • Satchmo
    • Lightning-Fast-Shop
    • Satchless
    • Django-shop
    • Plata, Mamona, Cartridge, ...
    • http://djangopackages.com/grids/g/ecommerce/

    View Slide

  40. View Slide

  41. Decimals

    View Slide

  42. View Slide

  43. View Slide

  44. View Slide

  45. kwargs

    View Slide

  46. Shipping calculator

    View Slide

  47. PHP

    View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. View Slide

  52. View Slide

  53. View Slide

  54. View Slide

  55. Python

    View Slide

  56. View Slide

  57. View Slide

  58. Web framework
    • Well-thought out structure
    • Templating, HTTP, caching, ...
    • Good test support
    • Security
    • Class-based views

    View Slide

  59. Models
    • Models drive the design
    • Capture domain logic
    • Foundation for rest of application

    View Slide

  60. Eco-system
    • Haystack (search)
    • South (database migrations)
    • Celery (job queue)
    • Internal libraries

    View Slide

  61. View Slide

  62. Customisation

    View Slide

  63. Techniques

    View Slide

  64. DEBUG = True
    settings.py

    View Slide

  65. AUTH_PROFILE_MODULE =
    ‘customer.Profile’
    settings.py

    View Slide

  66. Key idea
    • Having the same name/identifier as your
    parent
    • Subclass and override

    View Slide

  67. Templates

    View Slide

  68. View Slide

  69. View Slide

  70. View Slide

  71. View Slide

  72. Django looks here first

    View Slide

  73. Views

    View Slide

  74. Oscar default

    View Slide

  75. urls.py

    View Slide

  76. oscar/app.py

    View Slide

  77. oscar/core/app.py

    View Slide

  78. oscar/apps/checkout/app.py

    View Slide

  79. Oscar project

    View Slide

  80. Replace application
    urls.py

    View Slide

  81. myproject/app.py
    Use different app instance

    View Slide

  82. myproject/checkout/app.py
    Use different view classes

    View Slide

  83. myproject/checkout/views.py
    Override and extend
    method

    View Slide

  84. Models

    View Slide

  85. Oscar default

    View Slide

  86. settings.py

    View Slide

  87. abstract_models.py

    View Slide

  88. models.py

    View Slide

  89. Oscar project

    View Slide

  90. replaces oscar app
    settings.py
    same ‘app label’

    View Slide

  91. add custom fields
    myproject/voucher/models.py
    import remaining models

    View Slide

  92. templatetags
    management commands
    fixtures
    settings.py

    View Slide

  93. Classes

    View Slide

  94. model name
    ‘app label’

    View Slide

  95. module path class name

    View Slide

  96. use same class name as in core

    View Slide

  97. Recap
    • Bit of a work-around...
    • ...but works well
    • Applicable to other django libraries

    View Slide

  98. Best practices

    View Slide

  99. Log everything
    ...except bankcard details.

    View Slide

  100. Audit models

    View Slide

  101. View Slide

  102. Monitor everything
    watch high and low thresholds

    View Slide

  103. SOA
    • Services make your life easier
    • Keep a layer in front of your models
    • Facade design pattern

    View Slide

  104. View

    View Slide

  105. View
    Models
    Database
    Load business objects

    View Slide

  106. View
    Service
    Models
    Database
    Load business objects
    Business logic here

    View Slide

  107. View
    Service
    Models
    Database
    Load business objects
    Business logic here

    View Slide

  108. View
    Service
    Models
    Database
    Load business objects
    Business logic here

    View Slide

  109. Rules of thumb
    • Keep views lean
    • Business logic in:
    • Models
    • Services
    • Validation logic in forms

    View Slide

  110. View Slide

  111. Danger

    View Slide

  112. Domain pollution
    • When integrating with a 3rd party...
    • they will try and push their shortcomings
    into your platform

    View Slide

  113. View Slide

  114. Anti-corruption layer
    Your platform Their platform
    Anti-corruption layer

    View Slide

  115. Summary
    • Django e-commerce is an emerging force
    • Customising django apps isn’t easy
    • Oscar’s future:
    • NoSQL
    • Table inheritance

    View Slide

  116. Image credits
    Books:
    http://www.flickr.com/photos/th3ph17/3091294342/
    More books:
    http://www.flickr.com/photos/cheryl-j/3965942320/
    Nail varnish:
    http://www.flickr.com/photos/greenzowie/5451402463/
    Leather gloves:
    http://www.flickr.com/photos/theopenseas/175598276/
    Lego shipping:
    http://www.flickr.com/photos/lydiashiningbrightly/5356210011/
    Rubber gloves:
    http://www.flickr.com/photos/recyclethis/139373445/
    Warehouse:
    http://www.flickr.com/photos/[email protected]/15648677/
    Taxonomy:
    http://www.flickr.com/photos/kqedquest/831547339/
    T-shirts:
    http://www.flickr.com/photos/blazerman/177165473/

    View Slide