Home Page Icon
Home Page
Table of Contents for
Asynchronous patterns
Close
Asynchronous patterns
by Arun Ravindran
Django Design Patterns and Best Practices - Second Edition
Title Page
Copyright and Credits
Django Design Patterns and Best Practices Second Edition
PacktPub.com
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Django and Patterns
Why Django?
The story of Django
A framework is born
Removing the magic
Django keeps getting better
How does Django work?
What is a pattern?
Gang of four patterns
Is Django MVC?
Fowler's patterns
Are there more patterns?
Patterns in this book
Criticism of patterns
How to use patterns
Python Zen and Django's design philosophy
Summary
Application Design
How to gather requirements?
Are you a storyteller?
HTML mockups
Designing the application
Dividing a project into apps
Reuse or roll-your-own?
My app sandbox
Which packages made it?
Best practices before starting a project
SuperBook – your mission, should you choose to accept it
Why Python 3?
Which Django Version to use
Starting the project
Summary
Models
M is bigger than V and C
The model hunt
Splitting models.py into multiple files
Structural patterns
Patterns — normalized models
Problem details
Solution details
Three steps of normalization
First normal form (1NF)
Second normal form (2NF)
Third normal form (3NF)
Django models
Performance and denormalization
Should we always normalize?
Pattern — model mixins
Problem details
Solution details
Model mixins
Pattern — user profiles
Problem details
Solution details
Signals
Admin
Multiple profile types
Pattern – service objects
Problem details
Solution details
Retrieval patterns
Pattern — property field
Problem details
Solution details
Cached properties
Pattern — custom model managers
Problem details
Solution details
Set operations on QuerySets
Chaining multiple QuerySets
Migrations
Summary
Views and URLs
A view from the top
Views got classier
Class-based generic views
Class-Based Views are not always Class-Based Generic Views
View mixins
Order of mixins
Decorators
View patterns
Pattern — access controlled views
Problem details
Solution details
Pattern — context enhancers
Problem details
Solution details
Pattern – services
Problem details
Solution details
Designing URLs
URL anatomy
What happens in urls.py?
Simplified URL pattern syntax
Regular expression URL pattern syntax
Can the simplified syntax replace regular expressions?
Names and namespaces
Pattern order
URL pattern styles
Department store URLs
RESTful URLs
React.js, Vue.js, and other view replacements
Summary
Templates
Understanding Django's template language features
Variables
Attributes
Filters
Tags
Philosophy – don't invent a programming language
Jinja2
Organizing templates
How templates work
Using Bootstrap
But they all look the same!
Lightweight alternatives
Template patterns
Pattern — template inheritance tree
Problem details
Solution details
Pattern — the active link
Problem details
Solution details
A template-only solution
Custom tags
Summary
Admin Interface
Using the admin interface
Enhancing models for the admin
Not everyone should be an admin
Admin interface customizations
Changing the heading
Changing the base and stylesheets
Adding a rich-text editor for WYSIWYG editing
Bootstrap-themed admin
Complete overhauls
Protecting the admin
Pattern – feature flags
Problem details
Solution details
Summary
Forms
How forms work
Forms in Django
Why does data need cleaning?
Displaying forms
Time to be crisp
Understanding CSRF
Form processing with class-based views
Form patterns
Pattern – dynamic form generation
Problem details
Solution details
Pattern – user-based forms
Problem details
Solution details
Pattern – multiple form actions per view
Problem details
Solution details
Separate views for separate actions
Same view for separate actions
Pattern – CRUD views
Problem details
Solution details
Summary
Working Asynchronously
Why asynchronous?
Pitfalls of asynchronous code
Asynchronous patterns
Endpoint callback pattern
Publish-subscribe pattern
Polling pattern
Asynchronous solutions for Django
Working with Celery
How Celery works
Celery best practices
Handling failure
Idempotent tasks
Avoid writing to shared or global state
Database updates without race conditions
Avoid passing complex objects to tasks
Understanding asyncio
asyncio versus threads
The classic web-scraper example
Synchronous web-scraping
Asynchronous web-scraping
Concurrency is not parallelism
Entering Channels
Listening to notifications with WebSockets
Differences from Celery
Summary
Creating APIs
RESTful API
API design
Versioning
Django Rest framework
Improving the Public Posts API
Hiding the IDs
API patterns
Pattern – human browsable interface
Problem details
Solution details
Pattern – Infinite Scrolling
Problem details
Solution details
Summary
Dealing with Legacy Code
Finding the Django Version
Activating the virtual environment
Where are the files? This is not PHP
Starting with urls.py
Jumping around the code
Understanding the code base
Creating the big picture
PyGraphviz installation and usage
Incremental change or a full rewrite?
Writing tests before making any changes
Step-by-step process to writing tests
Legacy database integration
Future proofing
Summary
Testing and Debugging
Why write tests?
TDD
Writing a test case
The assert method
Writing better test cases
Mocking
Pattern – Test fixtures and factories
Problem details
Solution details
Learning more about testing
Debugging
Django debug page
A better debug page
The print function
Logging
The Django Debug Toolbar
The Python debugger pdb
Other debuggers
Debugging Django templates
Summary
Security
Cross-site scripting
Why are your cookies valuable?
How Django helps
Where Django might not help
Cross-site request forgery
How Django helps
Where Django might not help
SQL injection
How Django helps
Where Django might not help
Clickjacking
How Django helps
Shell injection
How Django helps
And the web attacks are unending
A handy security checklist
Summary
Production-Ready
The production environment
Choosing a web stack
Components of a stack
Virtual machines or Docker
Microservices
Hosting
Platform as a service
Virtual private servers
Serverless
Other hosting approaches
Deployment tools
Fabric
Typical deployment steps
Configuration management
Monitoring
Improving Performance
Frontend performance
Backend performance
Templates
Database
Caching
Cached session backend
Caching frameworks
Caching patterns
Summary
Python 2 Versus Python 3
Python 3
Python 3 for Djangonauts
Change all __unicode__ methods into __str__
All classes inherit from object
Calling super() is easier
Relative imports must be explicit
HttpRequest and HttpResponse have str and bytes types
f-strings or formatted string literals
Exception syntax changes and improvements
Standard library reorganized
New goodies
Pyvenv and pip are built in
Other changes
Further information
Other Books You May Enjoy
Leave a review - let other readers know what you think
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Pitfalls of asynchronous code
Next
Next Chapter
Endpoint callback pattern
Asynchronous patterns
Let's look at various general patterns that have been used in web applications.
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset