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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. Digital music

    View full-size slide

  9. Bar equipment

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. • Leather gloves

    View full-size slide

  16. Nothing is sacred

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. Shipping calculator

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. Customisation

    View full-size slide

  24. DEBUG = True
    settings.py

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. Django looks here first

    View full-size slide

  28. Oscar default

    View full-size slide

  29. oscar/app.py

    View full-size slide

  30. oscar/core/app.py

    View full-size slide

  31. oscar/apps/checkout/app.py

    View full-size slide

  32. Oscar project

    View full-size slide

  33. Replace application
    urls.py

    View full-size slide

  34. myproject/app.py
    Use different app instance

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  37. Oscar default

    View full-size slide

  38. abstract_models.py

    View full-size slide

  39. Oscar project

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  42. templatetags
    management commands
    fixtures
    settings.py

    View full-size slide

  43. model name
    ‘app label’

    View full-size slide

  44. module path class name

    View full-size slide

  45. use same class name as in core

    View full-size slide

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

    View full-size slide

  47. Best practices

    View full-size slide

  48. Log everything
    ...except bankcard details.

    View full-size slide

  49. Audit models

    View full-size slide

  50. Monitor everything
    watch high and low thresholds

    View full-size slide

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

    View full-size slide

  52. View
    Models
    Database
    Load business objects

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  60. 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/23748404@N00/15648677/
    Taxonomy:
    http://www.flickr.com/photos/kqedquest/831547339/
    T-shirts:
    http://www.flickr.com/photos/blazerman/177165473/

    View full-size slide