diff --git a/Makefile b/Makefile
index 1e66e6cde..5919324c5 100644
--- a/Makefile
+++ b/Makefile
@@ -3,18 +3,22 @@ run:
cp -R static-html/* generated/updated_site/
cp -R redirects/* generated/updated_site/
cp -R static/* generated/updated_site/
- sed -i '' 's/\(^.*~~.*$$\)/\1<\/span>/g' generated/updated_site/pages/*.html
- sed -i '' 's/~~//g' generated/updated_site/pages/*.html
- cp generated/updated_site/pages/* generated/updated_site/
- rm -rf generated/updated_site/pages/
+ sed -i '' 's/\(^.*~~.*$$\)/\1<\/span>/g' generated/updated_site/pages/*.html
sed -i '' 's/\(^.*~~.*$$\)/\1<\/span>/g' generated/updated_site/blog/*.html
+ sed -i '' 's/~~//g' generated/updated_site/pages/*.html
+ cp generated/updated_site/pages/* generated/updated_site/
sed -i '' 's/~~//g' generated/updated_site/blog/*.html
+prod: run
+ rm -rf generated/updated_site/pages/
+ sed -i '' 's/\(.[a-zA-Z0-9()_]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_.~*=&;/-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 ()_,&;:#{}]*\)<\/span>/\1/g; s/\(.[0-9]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9()_]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_.]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_.]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_.]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_.]*\)<\/span>/\1/g; s/\(.[a-zA-Z0-9 ()_]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 ()_,&;:#{}]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^;:+#{}*!%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^;:+#{}*!%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^;:+#{}*!%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^;:+#{}*!%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^\\\;:+#{}~*@`!?%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^;:+#{}*!%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^\\\;:+#{}~*@`!?%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /\\\()_,.&$^;:+#{}|=~*@`!?%-]*\)<\/span>/\1/g; s/\(.[][a-zA-Z0-9 /()_,.&^;:+#{}*@`!%-]*\)<\/span>/\1/g; s/<\/span>//g; s/<\/span>//g' generated/updated_site/*.html generated/updated_site/blog/*.html
+
+
bookbuild:
- mkdir tempcontent
- cp -R content/pages tempcontent/pages
- rm -rf tempcontent/pages/examples
+ mkdir -p tempcontent/pages
+ cp -R content/pages/0* tempcontent/pages
+ cp -R content/pages/meta tempcontent/pages
python transform_book.py pdf
pelican -t theme -s book_settings.py -o generated/book tempcontent
cp -R static-html/* generated/book/
diff --git a/content/pages/01-introduction/01-learning-programming.markdown b/content/pages/01-introduction/01-learning-programming.markdown
index 217546782..08f676d8e 100644
--- a/content/pages/01-introduction/01-learning-programming.markdown
+++ b/content/pages/01-introduction/01-learning-programming.markdown
@@ -125,11 +125,6 @@ repositories and sites with practice problems and solutions:
* [TeachCraft](https://teachcraft.net/) combines Minecraft with Python to
learn coding.
-* [500 Data Structures and Algorithms practice problems and their solutions](https://techiedelight.quora.com/500-Data-Structures-and-Algorithms-practice-problems-and-their-solutions)
- covers a large swath of the computer science space. It is not important
- to know all of these algorithms and data structures but experience with
- many of them will be greatly beneficial in becoming a better developer.
-
### First-hand advice
These articles are written by programmers who explain how they learned to
@@ -145,11 +140,6 @@ give example paths you can think about taking as a beginner:
including persistence, respecting others and considering ideas that are
outside your comfort zone.
-* [Mastering programming](https://www.facebook.com/notes/kent-beck/mastering-programming/1184427814923414)
- by [Kent Beck](https://en.wikipedia.org/wiki/Kent_Beck) contains
- patterns and observations for how experienced programmers he has worked
- with in the past became great software developers.
-
* [This Picture Will Change the Way You Learn to Code](https://dev.to/nextdotxyz/this-picture-will-change-the-way-you-learn-tocode-4kmh)
covers a well done graphics of many up-to-date concepts and tools that
developers use. The post reminds you that you will not and should not learn
@@ -170,10 +160,6 @@ your teaching experience:
is an awesome resource that explains how you can use simple but fun
drawings to teach otherwise difficult technical concepts to students.
-* [Teaching programming to working professionals](http://pgbovine.net/PG-Podcast-21-Trey-Hunner.htm)
- is a video podcast with [Trey Hunter](https://twitter.com/treyhunner)
- about his experience teaching Python to experienced professionals.
-
* [Teach Yourself Computer Science](https://teachyourselfcs.com/) is
intended as a self-teaching tool with many resources that are classic
computer science textbooks. There are also nice explanations for why
diff --git a/content/pages/01-introduction/02-python-programming-language.markdown b/content/pages/01-introduction/02-python-programming-language.markdown
index c99bf04ff..a3b046bb1 100644
--- a/content/pages/01-introduction/02-python-programming-language.markdown
+++ b/content/pages/01-introduction/02-python-programming-language.markdown
@@ -79,9 +79,6 @@ Dictionary comprehension:
### General Python language resources
-* The [online Python tutor](http://www.pythontutor.com/) visually walks
- through code and shows how it executes on the Python interpreter.
-
* [Python Module of the Week](http://pymotw.com/2/index.html) is a tour
through the Python standard library.
@@ -108,9 +105,6 @@ Dictionary comprehension:
* Armin Roacher presented [things you didn't know about Python](https://speakerdeck.com/mitsuhiko/didntknow)
at PyCon South Africa in 2012.
-* [Writing idiomatic Python](http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/)
- is a guide for writing Pythonic code.
-
### Python ecosystem resources
There's an entire page on [best Python resources](/best-python-resources.html)
@@ -153,7 +147,7 @@ the very beginner topics.
[comprehensions](http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Comprehensions.html)
including code examples and diagrams to explain how they work.
-* [Comprehensions in Python the Jedi way](https://gist.github.com/bearfrieze/a746c6f12d8bada03589)
+* [Comprehensions in Python the Jedi way](https://gist.github.com/mjhea0/1c0031bd6fcd9263f844)
shows off comprehensions with a Star Wars theme to walk through the nuts
and bolts. All examples use Python 3.5.
@@ -167,10 +161,6 @@ the very beginner topics.
covers what the code for list comprehensions looks like and gives some
example code to show how they work.
-* [An Introduction to Python Lists](http://effbot.org/zone/python-list.htm)
- is a solid overview of Python lists in general and tangentially covers
- list comprehensions.
-
### Python generator resources
* This blog post entitled
diff --git a/content/pages/01-introduction/03-why-use-python.markdown b/content/pages/01-introduction/03-why-use-python.markdown
index 63405e70c..86a0e3ecd 100644
--- a/content/pages/01-introduction/03-why-use-python.markdown
+++ b/content/pages/01-introduction/03-why-use-python.markdown
@@ -33,10 +33,7 @@ The IEEE ranked Python as the
[#1 programming language in 2019](https://spectrum.ieee.org/computing/software/the-top-programming-languages-2019),
which continued its hot streak
after ranking it
-[#1 in 2018](https://spectrum.ieee.org/at-work/innovation/the-2018-top-programming-languages),
-[#1 in 2017](https://spectrum.ieee.org/at-work/innovation/the-2017-top-programming-languages)
-and
-[#3 top programming language in 2016](http://spectrum.ieee.org/computing/software/the-2016-top-programming-languages).
+[#1 in 2018](https://spectrum.ieee.org/at-work/innovation/the-2018-top-programming-languages) and 2017.
[RedMonk's June 2019 ranking](https://redmonk.com/sogrady/2019/07/18/language-rankings-6-19/)
had Python at #3, which held consistent from previous years' rankings in
[2018](https://redmonk.com/sogrady/2018/08/10/language-rankings-6-18/)
@@ -47,9 +44,9 @@ Stack Overflow's community-created question and answer data confirms the
[incredible growth of the Python ecosystem](https://stackoverflow.blog/2017/09/06/incredible-growth-python/)
and tries to determine
[why it growing so quickly](https://stackoverflow.blog/2017/09/14/python-growing-quickly/)
-with their own analysis. In the 2018 Stack Overflow developer survey the
+with their own analysis. In the 2020 Stack Overflow developer survey the
data indicated that
-[Python was the fastest growing major programming language](https://insights.stackoverflow.com/survey/2018/#technology-programming-scripting-and-markup-languages)
+[Python was the fastest growing major programming language](https://insights.stackoverflow.com/survey/2020/#technology-programming-scripting-and-markup-languages)
and that there is a close alignment between the languages and tools that
developers choose to learn and the usage in developers' professional work.
@@ -133,13 +130,6 @@ language.
system versus statically typed languages, be sure to
[read this thorough explanation of the topic](http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html).
-* [Why I swapped C#.NET for Python as my default language and platform (and won’t be going back)](https://medium.com/@anthonypjshaw/why-i-swapped-c-net-for-python-as-my-default-language-and-platform-and-wont-be-going-back-e0063a25e491)
- provides a viewpoint from someone who is not a professional developer but
- uses coding to hack out some projects. He found Microsoft's .NET ecosystem
- lacking when it came to satisfying his needs and Python filled the gap for
- him with its wide array of open source code libraries, package management
- and ability to work well on platforms other than Windows.
-
* [Python, Machine Learning, and Language Wars](http://sebastianraschka.com/blog/2015/why-python.html)
compares Python with R, MATLAB and Julia for data science work. While
Python is great for [deployment automation](/deployment.html) and
diff --git a/content/pages/01-introduction/04-python-2-or-3.markdown b/content/pages/01-introduction/04-python-2-or-3.markdown
index eaf166982..a362e7b49 100644
--- a/content/pages/01-introduction/04-python-2-or-3.markdown
+++ b/content/pages/01-introduction/04-python-2-or-3.markdown
@@ -78,7 +78,7 @@ gone through the process and have advice for making it less painful.
implementations. There is also a
[quick reference for writting code with Python 2 and 3 compatibility](https://wiki.python.org/moin/PortingToPy3k/BilingualQuickRef).
-* [Upgrading to Python 3 with Zero Downtime](https://tech.yplanapp.com/2016/08/24/upgrading-to-python-3-with-zero-downtime/)
+* [Upgrading to Python 3 with Zero Downtime](https://adamj.eu/tech/2016/08/24/upgrading-yplan-to-python-3-with-zero-downtime/)
supplies advice on transitioning a large existing Python 2 web application
to Python 3. Their process involved upgrading dependencies, testing and
deploying the new version before going back to clean up unnecessary code
diff --git a/content/pages/01-introduction/05-enterprise-python.markdown b/content/pages/01-introduction/05-enterprise-python.markdown
index d00e8fbe0..c50052847 100644
--- a/content/pages/01-introduction/05-enterprise-python.markdown
+++ b/content/pages/01-introduction/05-enterprise-python.markdown
@@ -39,12 +39,14 @@ frameworks when otherwise they should not make technical design decisions.
## Why are there misconceptions about Python in enterprise environments?
Traditionally large organizations building enterprise software have used
-statically typed languages such as C++, .NET and Java. Throughout the 1980s
-and 1990s large companies such as Microsoft, Sun Microsystems and Oracle
-marketed these languages as "enterprise grade". The inherent snub to other
-languages was that they were not appropriate for CIOs' difficult technical
-environments. Languages other than Java, C++ and .NET were seen as risky and
-therefore not worthy of investment.
+statically typed languages and platforms such as C++, C# and Java.
+Throughout the 1990s and early 2000s, large companies such as
+Microsoft, Sun Microsystems and Oracle marketed these languages as
+"enterprise grade". The inherent message about other programming
+ecosystem was that they were not appropriate for CIOs' difficult
+technical environments. Languages other than Java, C++ and C# (along
+with its broader .NET platform) were seen as risky and therefore not
+worthy of investment.
In addition, "scripting languages" such as Python, Perl and Ruby were not
yet robust enough in the 1990s because their core standard libraries were
@@ -68,7 +70,7 @@ best maintained and fully featured pieces of code for any language.
Meanwhile, some of the traditional enterprise software development languages
such as Java have languished due to underinvestment by their major corporate
-backers. When [Oracle purchased Sun Microsystems in 2009](http://www.oracle.com/us/corporate/press/018363)
+backers. When Oracle purchased Sun Microsystems in 2009,
there was a long lag time before Java was enhanced with new language features
in Java 7. Oracle also
[bundles unwanted adware with the Java installation](http://www.engadget.com/2015/03/06/java-adware-mac/),
diff --git a/content/pages/01-introduction/06-community.markdown b/content/pages/01-introduction/06-community.markdown
index 1baec5f4f..a5f463319 100644
--- a/content/pages/01-introduction/06-community.markdown
+++ b/content/pages/01-introduction/06-community.markdown
@@ -77,7 +77,7 @@ resources provide perspective on offline events like
and
[PyCon US 2016](http://www.dreisbach.us/blog/pycon-2016/). There are
many other retrospectives for other
- [community-led conferences such as EuroPython](http://www.artima.com/weblogs/viewpost.jsp?thread=261930).
+ [community-led conferences such as EuroPython](https://www.artima.com/weblogs/viewpost.jsp?thread=261930).
These summaries can be a great way to get a slice of the experience
before purchasing a ticket and booking a trip.
@@ -112,16 +112,11 @@ the community.
provides a starter page with links to community-run newsletters, resources
and conferences.
-* There are many large active online communities on
- [Reddit](https://www.reddit.com/r/python) and
- [IRC channels](https://freenode.net/) such as #python, #python-dev
- and #distutils.
-
* The Python community has a concept known as "Benevolent Dictator For Life"
that may appear odd to newcomers. Essentially, Guido Van Rossum created
the language and still has the ability to decide community arguments one
way or the other. This post on the
- [origin of BDFL](http://www.artima.com/weblogs/viewpost.jsp?thread=235725)
+ [origin of BDFL](https://www.artima.com/weblogs/viewpost.jsp?thread=235725)
has more context about Guido's role.
* [Python Community and Python at Dropbox](https://talkpython.fm/episodes/show/30/python-community-and-python-at-dropbox)
diff --git a/content/pages/01-introduction/07-companies.markdown b/content/pages/01-introduction/07-companies.markdown
index cbd58c14a..c4e02cfc8 100644
--- a/content/pages/01-introduction/07-companies.markdown
+++ b/content/pages/01-introduction/07-companies.markdown
@@ -37,7 +37,7 @@ below).
at job descriptions on sites like
[Glassdoor with "Python Goldman Sachs" keywords](https://www.glassdoor.com/Jobs/Goldman-Sachs-python-Jobs-EI_IE2800.0,13_KO14,20.htm)
and
- [Indeed for JP Morgan Chase](https://www.indeed.com/salaries/Python-Developer-Salaries-at-JPMorgan-Chase).
+ [Indeed for JP Morgan Chase](https://www.indeed.com/cmp/JPMorgan-Chase/salaries/Python-Developer).
Salaries and responsibilities vary widely based on the role and whether
Python is used for data analysis,
[web application development](/web-development.html) or DevOps.
@@ -145,7 +145,7 @@ like the CIA, FBI and NSA.
[SEC uses Python and proposes organizations use Python to comply with regulations](http://jsdelfino.blogspot.com/2010/05/security-exchange-commission-python.html).
* A quick search for government jobs that require or recommend Python
- [via USAJobs](https://sec.usajobs.gov/Search/?k=python&p=1)
+ [via USAJobs](https://www.usajobs.gov/Search/Results?k=python)
turns up numerous listings at organizations such as the Smithsonian
Institution, Department of Education, Department of the Navy and
National Institute of Standards and Technology (NIST).
diff --git a/content/pages/01-introduction/08-best-python-resources.markdown b/content/pages/01-introduction/08-best-python-resources.markdown
index 81113487a..6677ba3d2 100644
--- a/content/pages/01-introduction/08-best-python-resources.markdown
+++ b/content/pages/01-introduction/08-best-python-resources.markdown
@@ -47,13 +47,6 @@ should skip down to the next section for "experienced developers".
and problems rather than jumping into a specific language that's recommended
to you by a friend.
-* [A Python Crash Course](https://www.grahamwheeler.com/posts/python-crash-course.html)
- gives an awesome overview of the history of Python, what drives the
- programming community and dives into example code. You will likely need
- to read this in combination with other resources to really let the syntax
- sink in, but it's a great article to read several times over as you
- continue to learn.
-
* The [Python projects tag](https://www.twilio.com/blog/tag/python) on the
Twilio blog is constantly updated with fun tutorials you can build to
learn Python, such as the
@@ -61,9 +54,6 @@ should skip down to the next section for "experienced developers".
[Choose Your Own Adventures Presentations using Flask and WebSockets](https://www.twilio.com/blog/2014/11/choose-your-own-adventure-presentations-with-reveal-js-python-and-websockets.html)
and [Martianify Photos with OpenCV](https://www.twilio.com/blog/2015/11/getting-started-with-opencv-and-python-featuring-the-martian-2.html).
-* [A Byte of Python](http://www.swaroopch.com/notes/python/) is a beginner's
- tutorial for the Python language.
-
* Google put together a great compilation of materials and subjects you
should read and learn from if you want to be a
[professional programmer](https://www.google.com/about/careers/students/guide-to-technical-development.html).
diff --git a/content/pages/01-introduction/09-best-python-videos.markdown b/content/pages/01-introduction/09-best-python-videos.markdown
index b265bc34b..14be9bccd 100644
--- a/content/pages/01-introduction/09-best-python-videos.markdown
+++ b/content/pages/01-introduction/09-best-python-videos.markdown
@@ -163,6 +163,7 @@ like [PyCon US](https://us.pycon.org/) and
Python language.
* PyCon US videos from
+ [2020](https://www.youtube.com/playlist?list=PL2Uw4_HvXqvbpFquYIE57BEAqkQWk-iFg),
[2019](https://www.youtube.com/channel/UCxs2IIVXaEHHA4BtTiWZ2mQ/videos),
[2018](https://www.youtube.com/channel/UCsX05-2sVSH7Nx3zuk3NYuQ/videos),
[2017](https://www.youtube.com/channel/UCrJhliKNQ8g0qoE_zvL8eVg/videos),
@@ -172,6 +173,7 @@ like [PyCon US](https://us.pycon.org/) and
are all available online for free.
* All of the talk videos are available on YouTube for
+ [EuroPython 2020](https://www.youtube.com/playlist?list=PL8uoeex94UhHgMD9GOCbEHWku7pEPx9fW0),
[EuroPython 2019](https://www.youtube.com/playlist?list=PL8uoeex94UhHFRew8gzfFJHIpRFWyY4YW),
[EuroPython 2018](https://www.youtube.com/watch?v=LoRq9yGeBWY&list=PL8uoeex94UhFrNUV2m5MigREebUms39U5),
[EuroPython 2017](https://www.youtube.com/watch?v=OCHrzW-R3QI&list=PL8uoeex94UhG9QAoRICebFpeKK2M0Herh),
diff --git a/content/pages/01-introduction/10-best-python-podcasts.markdown b/content/pages/01-introduction/10-best-python-podcasts.markdown
index b52a474a5..a2c528fee 100644
--- a/content/pages/01-introduction/10-best-python-podcasts.markdown
+++ b/content/pages/01-introduction/10-best-python-podcasts.markdown
@@ -38,15 +38,19 @@ listen and learn.
on [testing](/testing.html) and related topics such as mocking and
[code metrics](/code-metrics.html).
+* [Django Riffs](https://djangoriffs.com/) is a podcast for learning
+ [web application development](/web-development.html) in Python using the
+ [Django web framework](/django.html).
+
+* [The Real Python Podcast](https://realpython.com/podcasts/rpp/) is a weekly
+ podcast with interviews, coding tips, and conversation with guests from the
+ Python community.
+
* [Teaching Python](https://www.teachingpython.fm/) is a podcast by two
teachers about their adventures teaching middle school computer science,
problem solving, handling failure, frustration, and success with
teaching Python programming.
-* Professor Philip Guo has a video podcast called
- [PG Podcast](http://pgbovine.net/PG-Podcast.htm), typically on
- Python subjects.
- [All episodes are listed on a separate page](http://pgbovine.net/PG-Podcast-summary.htm).
## Favorite podcast episodes
diff --git a/content/pages/02-development-environments/00-development-environments.markdown b/content/pages/02-development-environments/00-development-environments.markdown
index 1a71cbc68..4b7dc8b94 100644
--- a/content/pages/02-development-environments/00-development-environments.markdown
+++ b/content/pages/02-development-environments/00-development-environments.markdown
@@ -8,17 +8,21 @@ meta: Development environments allow programmers to read, write and work with co
A development environment is a combination of a
-[text editor](/text-editors-ides.html) and a Python runtime environment.
-The text editor allows you to write the code. The runtime environment
-implementation such as [CPython](https://github.com/python/cpython)
-or [PyPy](https://pypy.org/) provides the method for executing your code.
+[text editor](/text-editors-ides.html) and a Python runtime implementation.
+The text editor allows you to write code for your applications. The runtime
+implementation, such as [CPython](https://github.com/python/cpython)
+or [PyPy](https://pypy.org/), provides the method for executing your code.
-A text editor can be as simple as Notepad on Windows or more complicated
-as a complete integrated development environment (IDE) such as
-[PyCharm](https://www.jetbrains.com/pycharm/) which runs on any major
-operating system.
+A text editor can be as simple as Notepad running on Windows or a more
+complicated
+[integrated development environment (IDE)](/text-editors-ides.html) with
+syntax checking, integrated [test runner](/testing.html) and code highlighting.
+A couple of common IDEs for Python development are
+[PyCharm](https://www.jetbrains.com/pycharm/) and
+[VSCode](https://code.visualstudio.com/), both of which runs on any major
+[operating system](/operating-systems.html).
## Why is a development environment necessary?
@@ -89,7 +93,7 @@ getting started and then require payment as you scale up your application.
[Stdlib](https://stdlib.com/) that can integrate with external
[web APIs](/application-programming-interfaces.html).
-* [GitLab Web IDE](https://about.gitlab.com/2018/06/15/introducing-gitlab-s-integrated-development-environment/)
+* [GitLab Web IDE](https://docs.gitlab.com/ee/user/project/web_ide/)
is integrated into the GitLab web application for modifying your
[Git](/git.html) repository files directly in your browser.
@@ -117,12 +121,6 @@ configuration as a starting point and customize it from there.
[which version of Python to use](/python-2-or-3.html) and adding
[code metrics](/code-metrics.html) libraries for checking syntax.
-* [Three Ways to Install Python on your Windows Computer](http://blog.yhat.com/posts/installing-python-on-windows.html)
- provides multiple avenues for Windows users to get Python on their machine
- before setting up the rest of their development environment. Unlike
- macOS and Linux, the Windows [operating system](/operating-systems.html)
- does not include Python with its default installation.
-
* [PyCharm: The Good Parts](http://nafiulis.me/pycharm-the-good-parts-i.html)
shows you how to be more efficient and productive with that IDE if it's
your choice for writing Python code.
diff --git a/content/pages/02-development-environments/01-text-editors-ides.markdown b/content/pages/02-development-environments/01-text-editors-ides.markdown
index 9f23de412..f179ee45d 100644
--- a/content/pages/02-development-environments/01-text-editors-ides.markdown
+++ b/content/pages/02-development-environments/01-text-editors-ides.markdown
@@ -78,7 +78,7 @@ gathering [code metrics](/code-metrics.html), running
free editions for students and open source projects.
* [Thonny](http://thonny.org/) is an
- [open source](https://bitbucket.org/plas/thonny/src) Python IDE for new
+ [open source](https://github.com/thonny/thonny) Python IDE for new
programmers. The tool bakes in syntax highlighting, code completion, a
simple debugger, a beginner-friendly shell and in situ documentation to
assist new developers who are just starting to code.
diff --git a/content/pages/02-development-environments/02-vim.markdown b/content/pages/02-development-environments/02-vim.markdown
index 1d2ee09b5..f37c04a5d 100644
--- a/content/pages/02-development-environments/02-vim.markdown
+++ b/content/pages/02-development-environments/02-vim.markdown
@@ -20,7 +20,10 @@ Vim's philosophy is that developers are more productive when they avoid
taking their hands off the keyboard. Code should flow naturally from the
developer's thoughts through the keyboard and onto the screen. Using a mouse
or other peripheral is a detriment to the rate at which a developer's
-thoughts become code.
+thoughts become code. This "efficiency by keyboard"
+[keeps Vim as one of the most popular text editors](https://pragmaticpineapple.com/how-did-vim-become-so-popular/)
+despite having been around for decades. Few programming tools have that kind
+of staying power.
Vim has a logical, structured command language. When a beginner is learning
the editor she may feel like it is impossible to understand all the key
@@ -88,6 +91,31 @@ If a Vimrc file does not already exist, just create it within the user's
home directory and it will be picked up by Vim the next time you open the
editor.
+The following are a few resources for learning what to put in, and how to
+structure a `.vimrc` file. I recommend adding configuration options one
+at a time to test them individually instead of going whole hog with a Vimrc
+you are unfamiliar with.
+
+* [A Good Vimrc](http://dougblack.io/words/a-good-vimrc.html) is a fantastic,
+ detailed overview and opinionated guide to configuring Vim. Highly
+ recommended for new and experienced Vim users.
+
+* [5 lines for a blank .vimrc](https://swordandsignals.com/2020/12/13/5-lines-in-vimrc.html)
+ shows settings for case insensitive search, highlighting as you search,
+ disabling swap, and a couple more "must have" enhancements to the
+ default configuration.
+
+* [Vim and Python](https://justin.abrah.ms/vim/vim_and_python.html) shows
+ and explains many Python-specific .vimrc options.
+
+* This
+ [repository's folder with Vimrc files](https://github.com/amix/vimrc/tree/master/vimrcs)
+ has example configurations that are well commented and easy to learn from.
+
+* For people who are having trouble getting started with Vim, check out this
+ blog post on the
+ [two simple steps that helped this author learn Vim](http://adamdelong.com/two-simple-steps-helped-me-learn-vim/).
+
### Vim tutorials
Vim has a reputation for a difficult learning curve, but it's much easier
@@ -139,11 +167,6 @@ to get started with these tutorials.
* [Vim Adventures](http://vim-adventures.com/) is a cute, fun browser-based
game that helps you learn Vim commands by playing through the adventure.
-* In [Vim: revisited](http://mislav.uniqpath.com/2011/12/vim-revisited/) the
- author explains his on-again off-again relationship with using Vim. He then
- shows how he configures and uses the editor so it sticks as his primary
- code editing tool.
-
* [Things About Vim I Wish I Knew Earlier](https://blog.petrzemek.net/2016/04/06/things-about-vim-i-wish-i-knew-earlier/)
explores the lessons one developer learned while exclusively using Vim
for several years. The author includes using relative instead of absolute
@@ -156,31 +179,6 @@ to get started with these tutorials.
habits.
-### Vimrc resources
-These are a few resources for learning how to structure a `.vimrc` file. I
-recommend adding configuration options one at a time to test them
-individually instead of going whole hog with a Vimrc you are unfamiliar with.
-
-* [A Good Vimrc](http://dougblack.io/words/a-good-vimrc.html) is a fantastic,
- detailed overview and opinionated guide to configuring Vim. Highly
- recommended for new and experienced Vim users.
-
-* [Vim and Python](https://justin.abrah.ms/vim/vim_and_python.html) shows
- and explains many Python-specific .vimrc options.
-
-* [Vim as a Python IDE](http://liuchengxu.org/posts/use-vim-as-a-python-ide/)
- shows a slew of plugins and configuration options for coding with Python
- in Vim.
-
-* This
- [repository's folder with Vimrc files](https://github.com/amix/vimrc/tree/master/vimrcs)
- has example configurations that are well commented and easy to learn from.
-
-* For people who are having trouble getting started with Vim, check out this
- blog post on the
- [two simple steps that helped this author learn Vim](http://adamdelong.com/two-simple-steps-helped-me-learn-vim/).
-
-
### Vim installation guides
These installation guides will help you get Vim up and running on Mac OS X,
Linux and Windows.
diff --git a/content/pages/02-development-environments/04-sublime-text.markdown b/content/pages/02-development-environments/04-sublime-text.markdown
index 841687737..196df3b51 100644
--- a/content/pages/02-development-environments/04-sublime-text.markdown
+++ b/content/pages/02-development-environments/04-sublime-text.markdown
@@ -68,10 +68,6 @@ links should get your editor customized with linters,
is a spectacular tutorial that covers installing Sublime Text and
configuring a multitude of helpful Python programming plugins.
-* [Sublime Text 3 Heaven](https://www.kennethreitz.org/essays/sublime-text-3-heaven)
- is a quick overview of the extensions, packages and bonus toys that
- one developer uses for his own Sublime Text development setup.
-
* [Sublime Tutor](https://sublimetutor.com/) is an interactive in-editor
keyboard shortcuts tutorial that plugs into Sublime so you can learn and
become more productive as you use the editor.
diff --git a/content/pages/02-development-environments/05-pycharm.markdown b/content/pages/02-development-environments/05-pycharm.markdown
index c6810f26c..c464d6e87 100644
--- a/content/pages/02-development-environments/05-pycharm.markdown
+++ b/content/pages/02-development-environments/05-pycharm.markdown
@@ -29,10 +29,6 @@ Python code.
is a solid discussion thread with different developers' perspectives
on using PyCharm for coding their applications.
-* [How to Get Started with PyCharm and Have a Productive Python IDE](https://pedrokroger.net/getting-started-pycharm-python-ide/)
- covers the basics of configuring PyCharm for running code within
- virtualenvs, macros, using the console and code completion.
-
* [Using PyCharm with Pyramid](https://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/development_tools/pycharm.html)
is specific to developing and debugging
[with the Pyramid web framework](/pyramid.html).
diff --git a/content/pages/02-development-environments/06-jupyter-notebook.markdown b/content/pages/02-development-environments/06-jupyter-notebook.markdown
index 838763258..58c9c32b6 100644
--- a/content/pages/02-development-environments/06-jupyter-notebook.markdown
+++ b/content/pages/02-development-environments/06-jupyter-notebook.markdown
@@ -73,6 +73,18 @@ your own productive workflow.
data science notebook projects such as MATLAB and Mathematica to set the stage
for IPython and Jupyter's creation.
+* [How to use Jupyter Notebooks in 2020 (Part 1: The data science landscape)](https://ljvmiranda921.github.io/notebook/2020/03/06/jupyter-notebooks-in-2020/)
+ is a high-level overview post that starts a series on Jupyter Notebooks.
+ This first post covers why a tool like Jupyter Notebook is needed in
+ the broader landscape of data science.
+ [Part 2](https://ljvmiranda921.github.io/notebook/2020/03/16/jupyter-notebooks-in-2020-part-2/)
+ examines the growth of the Jupyter ecosystem and the jump from exploratory
+ analysis notebooks to production notebooks.
+
+* [Jupyter Notebook Best Practices](https://levelup.gitconnected.com/jupyter-notebook-best-practices-fc326eb5cd22)
+ contains tips for beginners such as learning shortcuts and properly
+ documenting the analysis you work on.
+
* [How to Version Control Jupyter Notebooks](https://nextjournal.com/schmudde/how-to-version-control-jupyter)
explains how Jupyter Notebooks are stored in JSON, the issues with that
format for [source control](/source-control.html) and how to get
@@ -134,6 +146,13 @@ like advanced interactive visualizations.
code that is suitable for [deployment](/deployment.html) to a production
environment.
+* [How to use ipywidgets to make your Jupyter notebook interactive](https://www.wrighters.io/use-ipywidgets-with-jupyter-notebooks/)
+ is a tutorial on how to use the
+ [ipywidgets](https://ipywidgets.readthedocs.io/en/latest/) library
+ to make Jupyter Notebooks respond to users' input and go beyond
+ simply presenting data into having users be able to do some additional
+ analysis themselves.
+
* [28 Jupyter Notebook tips, tricks and shortcuts](https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/)
explains many of the lesser-known keyboard shortcuts and mechanisms
to output settings.
@@ -143,10 +162,6 @@ like advanced interactive visualizations.
you will eventually want to handle within your notebooks as you get
comfortable in the environment.
-* [Hacking my way to a Jupyter notebook powered blog](https://nipunbatra.github.io/blog/2017/Jupyter-powered-blog.html)
- explores how the author created a blog using Jupyter but ran into some
- issues along the way, along with how to solve those problems.
-
* [Making Publication Ready Python Notebooks](http://blog.juliusschulz.de/blog/ultimate-ipython-notebook)
explores the plugins that the author uses when creating and exporting
reports from Jupyter.
diff --git a/content/pages/02-development-environments/07-shells.markdown b/content/pages/02-development-environments/07-shells.markdown
index 845728809..aa5ac671f 100644
--- a/content/pages/02-development-environments/07-shells.markdown
+++ b/content/pages/02-development-environments/07-shells.markdown
@@ -45,3 +45,7 @@ environment named `fullstackpython` within the macOS Terminal application.
little tool that shows how input and arguments in the shell break
down and are interpreted by commands. The data is pulled from the
[Ubuntu](/ubuntu.html) `man` pages.
+
+* [Shell productivity tips and tricks](https://blog.balthazar-rouberol.com/shell-productivity-tips-and-tricks.html)
+ covers how to increase your effectiveness on the shell across topics such as
+ navigating history, autocompletion, and pattern matching.
diff --git a/content/pages/02-development-environments/08-bash-shell.markdown b/content/pages/02-development-environments/08-bash-shell.markdown
index abf95629b..22c7877bc 100644
--- a/content/pages/02-development-environments/08-bash-shell.markdown
+++ b/content/pages/02-development-environments/08-bash-shell.markdown
@@ -20,12 +20,150 @@ during Python software development as part of a programmer's
-### Bash resources
+### How do Python developers use Bash?
+If you are programming in the terminal on [macOS](/macos.html)
+or [Linux](/ubuntu.html), or using the
+[Windows Subsystem for Linux on Windows 10](https://docs.microsoft.com/en-us/windows/wsl/install-win10),
+you can easily gain access to Bash if it is not already
+your default shell.
+
+You can show what shell you are currently using by echoing the
+`SHELL` environment variable, like so:
+
+```
+$ echo "$SHELL"
+```
+
+Which will then print the shell you are currently using. For example,
+on macOS I am using Bash by default so the echo command prints:
+
+```
+/bin/bash
+```
+
+How much you use Bash or any shell will likely depend on your
+[development environment](/development-environments.html), especially
+if you are using an editor like [Vim](/vim.html) instead of an
+IDE like [PyCharm](/pycharm.html), because it is often easier to do
+certain tasks in the shell. For example, most developers I know who
+use PyCharm will search for some instance of source code right in
+their IDE, whereas I use a combination of Vim and [tmux](/tmux.html)
+so I frequently flip between panes to use commands like `grep` to
+do my source code searches.
+
+There is no right way to perform a task like source code searching, it's
+really just what works for your brain as a developer that will guide
+how often you interact with the Bash shell.
+
+
+### Getting started with Bash
+Working with a shell, Bash or otherwise, is intimidating the first time
+you try to get started. You are staring at the `$` prompt without a
+whole lot of direction.
+
+When you are completely new to using Bash, it is a good idea to at least
+scan, if not take some additional time for in-depth reading of the
+documentation for commands that every developer uses. The following
+commands are used so frequently in Bash that an experienced developer
+probably does not even think about them anymore, they become just a
+natural part of your workflow:
+
+* `echo`: [print text to the command line](https://man7.org/linux/man-pages/man1/echo.1.html)
+* `ls`: [list the contents of a directory](https://man7.org/linux/man-pages/man1/ls.1.html)
+* `cd`: [change the working directory](https://man7.org/linux/man-pages/man1/cd.1p.html)
+* `cp`: [copy a file or directory](https://man7.org/linux/man-pages/man1/cp.1.html)
+* `mv`: [move one or more files](https://man7.org/linux/man-pages/man1/mv.1.html)
+* `rm`: [delete one or more files or directories](https://man7.org/linux/man-pages/man1/rm.1.html)
+
+If you know how to use the above commands then you will at least be able
+to move around the file system, create, move and update files and know
+what is on your storage device(s).
+
+The following commands are somewhat more advanced but also frequently
+used by developers:
+
+* `su`: [run comamnds as different users or groups](https://man7.org/linux/man-pages/man1/su.1.html)
+* `whoami`: [print which user you are currently logged in as](https://man7.org/linux/man-pages/man1/whoami.1.html)
+* `grep`: [searches for patterns in files](https://man7.org/linux/man-pages/man1/grep.1.html)
+
+The above lists are not even close to exhaustive for what commands
+you need to know when working with Bash. Read some of the following
+introductory tutorials to gain a better understanding of working
+with this shell:
+
+* [The Linux command line for beginner](https://ubuntu.com/tutorials/command-line-for-beginners)
+ by [Ubuntu](/ubuntu.html) will provide you with context for how to
+ use the command line, working with files and directories, and handling
+ superuser commands.
+
* [Bash Guide for beginners](http://www.tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html)
- is an entire book for those new to working with commandlines. It covers
+ is an entire book for those new to working with command lines. It covers
commands, paths, Bash shell scripting, variables and many other critical
topics that are necessary to move from beginner to advanced Bash user.
+* [101 Bash Commands and Tips for Beginners to Experts](https://dev.to/awwsmm/101-bash-commands-and-tips-for-beginners-to-experts-30je)
+ gives a well-done laundry list of tricks to explore.
+
+* [Bash Quick References](https://shellmagic.xyz/) is a cheat sheet for
+ common operators and signals that come up when working with scripts.
+
+
+### Bash scripting
+Bash is used not only as an interactive prompt but also for scripting, which
+makes it possible to execute one or more Bash commands stored within a file.
+These scripts can be short, with only a single command, or very complicated
+with control-flow logic, for loops, and almost anything you want to automate
+or compute because
+[Bash is a Turing-complete programming language](https://www.quora.com/Is-Bash-Turing-complete).
+
+Complex Bash scripts sometimes get a negative reputation because they can be
+difficult to read and understand if you are not the original author (or you
+are reading your own script after a significant period of time has elapsed).
+There are many ways to accomplish the same tasks with Bash so the files are
+often confusing to read unless the author of a script included clear
+documentation. This readability problem is typically less of an issue with
+Python scripts because spacing is enforced and the standard library
+encapsulates common tasks.
+
+It's a good idea to think about how you want to structure your Bash scripts
+as they grow larger. The following resources provide insight into what you
+should consider while coding Bash scripts.
+
+* This [minimal safe Bash template](https://betterdev.blog/minimal-safe-bash-script-template/)
+ contains an 86-line Bash script that the author claims once you
+ understand and use it as a base then it will make your scripts
+ easier to maintain over time.
+
+* [Creating a bash completion script](https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial.html)
+ is a great tutorial that walks you through a reasonably complex Bash
+ script for completing syntax in other Bash shell scripts.
+
+* [Anybody can write good bash (with a little effort)](https://blog.yossarian.net/2020/01/23/Anybody-can-write-good-bash-with-a-little-effort)
+ covers the basics of shell scripting and provides some recommendations
+ for creating more maintainable scripts such as using linters and
+ formatters.
+
+* Google's [Shell Style Guide](https://google.github.io/styleguide/shell.xml)
+ covers how to write consistent, maintainable shell scripts, which is
+ particularly important if you have ever tried to debug a hacky shell
+ script that was never meant to be used by anyone other than the original
+ author.
+
+* [Bash scripting quirks & safety tips](https://jvns.ca/blog/2017/03/26/bash-quirks/)
+ explains Bash basic programming constructs like `for` loops and variable
+ assignment then goes into ways to avoid weird issues in your code.
+
+* If all else fails when you're trying to use Bash scripts, this article
+ on [replacing Bash scripts with Python](https://github.com/ninjaaron/replacing-bash-scripting-with-python)
+ is a guide on swapping in Python for administrative scripting, including
+ what to do about replacing invaluable command line tools such as `awk`,
+ `sed` and `grep`.
+
+
+### Additional Bash resources
+The following resources cover more advanced Bash use cases and what pitfalls
+to try to avoid as you work with the shell or write scripts.
+
* [Advancing in the Bash shell](http://samrowe.com/wordpress/advancing-in-the-bash-shell/)
covers important concepts such as bang syntax, movement commands,
tab completion and aliases.
@@ -46,19 +184,6 @@ during Python software development as part of a programmer's
`PROMPT_COMMAND`, `CDPATH` and `REPLY` which can simplify your
scripts by using values that Bash already has stored for you.
-* Google's [Shell Style Guide](https://google.github.io/styleguide/shell.xml)
- covers how to write consistent, maintainable shell scripts, which is
- particularly important if you have ever tried to debug a hacky shell
- script that was never meant to be used by anyone other than the original
- author.
-
-* [101 Bash Commands and Tips for Beginners to Experts](https://dev.to/awwsmm/101-bash-commands-and-tips-for-beginners-to-experts-30je)
- is a well-done laundry list of tricks to explore.
-
-* [Bash scripting quirks & safety tips](https://jvns.ca/blog/2017/03/26/bash-quirks/)
- explains Bash basic programming constructs like `for` loops and variable
- assignment then goes into ways to avoid weird issues in your code.
-
* [Safe ways to do things in bash](https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md)
shows you how to not shoot yourself in the foot by using safe coding
practices with your shell scripts.
@@ -76,19 +201,10 @@ during Python software development as part of a programmer's
application that generates a hostable, customizable status page for your
services.
-* [Replacing Bash scripts with Python](https://github.com/ninjaaron/replacing-bash-scripting-with-python)
- is a guide on using using Python for administrative scripting, including
- what to do about replacing invaluable command line tools such as `awk`,
- `sed` and `grep`.
-
* [Using Aliases to Speed Up Your Git Workflow](https://dev.to/robertcoopercode/using-aliases-to-speed-up-your-git-workflow-2f5a)
has a bunch of shell aliases that make it easier for you to execute
complicated or uncommon [Git](/git.html) commands.
-* [Creating a bash completion script](https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial.html)
- is a great tutorial that walks you through a reasonably complex Bash
- script for completing syntax in other Bash shell scripts.
-
* [6 Tips Before You Write Your Next Bash Cronjob](https://yasoob.me/posts/6-tips-before-you-write-your-next-bash-cronjob/)
covers starting your scripts with shebang, redirecting output, timeouts
and sudo privileges.
@@ -103,10 +219,15 @@ during Python software development as part of a programmer's
their destructive abilities by reading through the descriptions provided
by the author.
-* [Bash Quick References](https://shellmagic.xyz/) is a cheat sheet for
- common operators and signals that come up when working with scripts.
-
-* [Anybody can write good bash (with a little effort)](https://blog.yossarian.net/2020/01/23/Anybody-can-write-good-bash-with-a-little-effort)
- covers the basics of shell scripting and provides some recommendations
- for creating more maintainable scripts such as using linters and
- formatters.
+* [Faster bash startup](https://danpker.com/posts/faster-bash-startup/)
+ and
+ [Even faster bash startup](https://work.lisk.in/2020/11/20/even-faster-bash-startup.html)
+ are two great tutorials that will save you a bunch of time if you frequently
+ open new Bash shells. On many systems you can easily cut down the startup
+ time for the shell which can be unnecessarily sluggish.
+
+* [Bash HTTP monitoring dashboard](https://raymii.org/s/software/Bash_HTTP_Monitoring_Dashboard.html)
+ ([source code](https://github.com/RaymiiOrg/bash-http-monitoring))
+ is a useful application fully written in Bash shell scripts that
+ monitors the health of one or more websites to make sure they are
+ up and running.
diff --git a/content/pages/02-development-environments/10-powershell.markdown b/content/pages/02-development-environments/10-powershell.markdown
index 6c399fb52..d4449544b 100644
--- a/content/pages/02-development-environments/10-powershell.markdown
+++ b/content/pages/02-development-environments/10-powershell.markdown
@@ -39,6 +39,13 @@ of a Python programmer's
give historical perspective on how the Windows shell has evolved from
MS-DOS days into the current Windows 10 world.
+* [Learning a New REST API with PowerShell](https://www.cloudsavvyit.com/2454/learning-a-new-rest-api-with-powershell/)
+ covers how to create the common GET, POST, PUT and DELETE requests that are
+ used to work with [RESTful APIs](/application-programming-interfaces.html).
+ The the tutorial also contains some tips and tricks for reading API
+ documentation and how to use PowerShell more effectively in these
+ situations.
+
* [PowerShell in Azure Functions](http://www.brianbunke.com/blog/2018/02/27/powershell-in-azure-functions/)
shows you how to use PowerShell scripting language code in your Azure
Functions. The language is only in experimental mode on Azure Functions
@@ -55,3 +62,9 @@ of a Python programmer's
* [Invoke-Build](https://github.com/nightroman/Invoke-Build) is a build
automation tool. There is also extensive documentation on their
[wiki](https://github.com/nightroman/Invoke-Build/wiki).
+
+* [Two months with Powershell on a UNIX](https://code.joejag.com/2020/a-month-with-powershell.html)
+ examines one developer's experience using PowerShell not on Windows
+ but on a \*nix [operating system](/operating-systems.html). The author
+ covers the advantages and disadvantages he found during his experience
+ and some of the bugs he hopes are fixed in PowerShell 7.
diff --git a/content/pages/02-development-environments/12-tmux.markdown b/content/pages/02-development-environments/12-tmux.markdown
index 4da1c3016..4b95fba6c 100644
--- a/content/pages/02-development-environments/12-tmux.markdown
+++ b/content/pages/02-development-environments/12-tmux.markdown
@@ -28,14 +28,16 @@ easier to use many shells at once and attaching to both local and remote
tmux+Vim user myself, I can attest to how great these two tools complement
each other.
+* [Writing & Coding Workflow](http://jacobzelko.com/workflow/) shows one
+ developer's configuration that combines [Vim](/vim.html) and several plugins
+ with tmux for a productive setup.
+
* [Making tmux Pretty and Usable - A Guide to Customizing your tmux.conf](http://www.hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/)
* [Tmux Pairing Anywhere: On Your Box](http://iamvery.com/2013/11/16/tmux-pairing-anywhere-on-your-box.html)
* [Using tmux Properly](http://danielallendeutsch.com/blog/16-using-tmux-properly.html)
-* [Differences between tmux vs screen](https://wtanaka.com/node/8136)
-
* [The Power Of tmux Hooks](https://devel.tech/tips/n/tMuXz2lj/the-power-of-tmux-hooks/)
* There are a slew of "cheat sheets" for tmux out there, here are a few
diff --git a/content/pages/02-development-environments/14-environment-configuration.markdown b/content/pages/02-development-environments/14-environment-configuration.markdown
index 2c87e88fb..d65114eb7 100644
--- a/content/pages/02-development-environments/14-environment-configuration.markdown
+++ b/content/pages/02-development-environments/14-environment-configuration.markdown
@@ -34,6 +34,13 @@ Either one can be used in your applications but there are slight differences
that can make one better than the other in various situations.
Other useful environment variables resources:
+* [How to Set Environment Variables in Linux and Mac: The Missing Manual](https://doppler.com/blog/how-to-set-environment-variables-in-linux-and-mac)
+ is a wonderfully detailed guide with many tips and tricks throughout
+ the walkthrough such as quickly setting environment variables for a
+ single command, passing environment variables through when using sudo
+ and executing a command in a "clean" environment without everything
+ you have already set interfering or being accessible to that script.
+
* [The Twelve-Factor App](https://12factor.net/) describes a method for
securing environment data for your applications. The twelve factors are
commonly referenced across many programming ecosystems, not just Python,
diff --git a/content/pages/02-development-environments/15-application-dependencies.markdown b/content/pages/02-development-environments/15-application-dependencies.markdown
index a01f2eba9..927a3720d 100644
--- a/content/pages/02-development-environments/15-application-dependencies.markdown
+++ b/content/pages/02-development-environments/15-application-dependencies.markdown
@@ -39,9 +39,6 @@ all yourself. A few of the best collections of Python libraries are
shows the open source Python projects trending today, this week, and this
month.
-* This list of [20 Python libraries you can’t live without](http://freepythontips.wordpress.com/2013/07/30/20-python-libraries-you-cant-live-without/)
- is a wide-ranging collection from data analysis to testing tools.
-
* Wikipedia actually has an extensive
[page dedicated to Python libraries](http://en.wikipedia.org/wiki/List_of_Python_software)
grouped by categories.
@@ -161,16 +158,6 @@ so far to get up to speed on building and releasing your own packages.
provides a collection of resources to understand how to package and
distribute Python code libraries.
-* [Alice in Python projectland](https://veekaybee.github.io/2017/09/26/python-packaging/)
- is an amazing post that takes the reader from simple Python script
- into a complete Python package.
-
-* [Perils of packaging](https://malramsay.com/post/perils_of_packaging/)
- covers several edge cases that come up when trying to put together
- pieces like Travis CI, PyPI and conda. The post walks through the errors
- and how to get around them until they are smoothed out by updates to the
- tools.
-
* [How to Publish Your Package on PyPI](https://blog.jetbrains.com/pycharm/2017/05/how-to-publish-your-package-on-pypi/)
is for developers who have created a code library they would like to
share and make installable for other developers.
@@ -230,10 +217,6 @@ developers.
dependency management tools including newer ones such as pipenv and
Poetry.
-* Occasionally arguments about using Python's dependency manager versus
- one of Linux's dependency managers comes up. This provides
- [one perspective on that debate](http://notes.pault.ag/debian-python/).
-
* [Open source trust scaling](http://lucumr.pocoo.org/2016/3/24/open-source-trust-scaling/)
is a good piece for the [Python community](/python-community.html)
(and other programming communities) that is based on the
diff --git a/content/pages/02-development-environments/17-localhost-tunnels.markdown b/content/pages/02-development-environments/17-localhost-tunnels.markdown
index 4cfe9d86e..f82b2c81e 100644
--- a/content/pages/02-development-environments/17-localhost-tunnels.markdown
+++ b/content/pages/02-development-environments/17-localhost-tunnels.markdown
@@ -23,7 +23,11 @@ experience with to the ones I have not used.
* [ngrok](https://ngrok.com/) is the service I use most often. It is
easy and worth the small fee to upgrade your account with a few
- extra features such as fixed, customizable subdomains.
+ extra features such as fixed, customizable subdomains. There is
+ also a
+ [Python wrapper for ngrok called pyngrok](https://github.com/alexdlaird/pyngrok)
+ that makes it easy to programmatically access the ngrok client
+ from Python applications.
* [Localtunnel](https://localtunnel.github.io/www/) is a localhost
tunnel written in Node.js.
diff --git a/content/pages/02-development-environments/18-source-control.markdown b/content/pages/02-development-environments/18-source-control.markdown
index 064f9729b..c96745a51 100644
--- a/content/pages/02-development-environments/18-source-control.markdown
+++ b/content/pages/02-development-environments/18-source-control.markdown
@@ -118,10 +118,6 @@ control services are:
part of an ongoing deployment guide written by the folks at
[Rainforest](https://www.rainforestqa.com/).
-* This lighthearted guide to the
- [ten astonishments in version control history](http://www.flourish.org/2011/12/astonishments-ten-in-the-history-of-version-control/)
- is a fun way to learn how systems developed over the past several decades.
-
* [A visual guide to version control](http://betterexplained.com/articles/a-visual-guide-to-version-control/)
is a detailed article with real-life examples for why version control is
necessary in software development.
@@ -198,9 +194,9 @@ implementation.
free online version of the O'Reilly
[Version Control with Subversion](https://www.amazon.com/dp/B002SR2QIW/) book.
-* [How to use Subversion (SVN)](https://deveo.com/svn-tutorial/) lays out
- the basic concepts and provides the first few steps for getting started
- tracking files.
+* [How to Host SVN Repositories](https://www.perforce.com/blog/vcs/how-host-subversion-svn)
+ lays out the basic concepts and provides the first few steps for getting
+ started tracking files.
* [10 Most Used SVN Commands with Examples](http://www.thegeekstuff.com/2011/04/svn-command-examples/)
is a good refresher list if you've used SVN in the past but it has been
diff --git a/content/pages/02-development-environments/19-git.markdown b/content/pages/02-development-environments/19-git.markdown
index 3233bff32..5bffca262 100644
--- a/content/pages/02-development-environments/19-git.markdown
+++ b/content/pages/02-development-environments/19-git.markdown
@@ -141,6 +141,12 @@ workflow. These resources will come in handy for specific Git subjects.
important when working with others on a long-lasting project where you
dive through the commit history via `git log` and related commands.
+* [How to squash Git commits](https://gitbetter.substack.com/p/how-to-squash-git-commits)
+ explains how to use the `git rebase` command in interactive mode to
+ consolidated the number of commits in your history. This technique is
+ useful when a group of commits are related and it's easier to understand
+ them as a single commit rather than a collection of smaller commits.
+
* [Oh shit, Git!](http://ohshitgit.com/) is a profanity-filled description
of tips to get you out of binds you may find yourself in when you get too
tricky with Git commands.
@@ -163,9 +169,10 @@ workflow. These resources will come in handy for specific Git subjects.
the Git history, and good practices for using Git based on the
knowledge of how it works internally.
-* [How Microsoft uses Git](https://docs.microsoft.com/en-us/azure/devops/learn/devops-at-microsoft/use-git-microsoft)
- gives a high-level overview of their repository structure and hosting
- at the extremely large scale organization.
+* [Chasing a bad commit](https://vishaltelangre.com/chasing-a-bad-commit/)
+ examines the `git bisect` command and how it can be used in either
+ interactive mode or on its own with `git bisect run` to find the
+ problematic code commit that needs to be fixed.
* [GitTips](https://git.wiki.kernel.org/index.php/GitTips) is a list of
pro tips to clean up common issues and how to dive through Git history
@@ -184,11 +191,6 @@ workflow. These resources will come in handy for specific Git subjects.
through example Git commands. This is a highly recommended read after you've
grasped the basics and are looking to go deeper with Git.
-* [How To Host Your Own Private Git Repositories](https://eklitzke.org/how-to-how-your-own-private-git-repositories)
- provides the steps for handling private Git repositories on your own
- server. This setup is great for either mirroring GitHub repositories
- or just getting away from hosted services entirely.
-
* [How I configure my git in a new computer](https://medium.com/@Tiagojdferreira/how-i-set-up-my-git-in-a-new-computer-85bb461b089f)
shows how to handle a `.gitconfig` file, with
[an example Gist](https://gist.github.com/Tiagojdferreira/115ecac229e176e48d520c59b022e4fb)
@@ -217,6 +219,11 @@ workflow. These resources will come in handy for specific Git subjects.
system. This is an awesome read to get a view on how Git works under the
commands you're using to manipulate these objects.
+* [How to Undo Mistakes With Git Using the Command Line](https://www.youtube.com/watch?v=lX9hsdsAeTk)
+ is a video that covers topics like resetting a file to an old revision,
+ recovering deleted commits, squashing multiple commits into one with
+ interactive rebase and recovering deleted branches.
+
## Git Workflows
Teams of developers can use Git in varying workflows because of Git's
@@ -233,10 +240,6 @@ minimize merge conflicts.
why at GitHub they do not use the git-flow model and provides an
alternative that solves some of the issues they found with git-flow.
-* [Git Workflows That Work](http://blog.endpoint.com/2014/05/git-workflows-that-work.html)
- is a helpful post with diagrams to show how teams can create a Git workflow
- that will help their development process.
-
* [Comparing workflows](https://www.atlassian.com/git/tutorials/comparing-workflows)
provides a slew of examples for how developers on a team can handle merge
conflicts and other situations that commonly arise when using Git.
diff --git a/content/pages/03-data/00-data.markdown b/content/pages/03-data/00-data.markdown
index 2ef2bbdc8..39a767500 100644
--- a/content/pages/03-data/00-data.markdown
+++ b/content/pages/03-data/00-data.markdown
@@ -12,7 +12,7 @@ subsections, including (in no particular order):
* data processing / wrangling
* machine learning
-* data analysis
+* [data analysis](/data-analysis.html)
* [visualization](/data-visualization.html)
* geospatial mapping
* persistence via [relational databases](/databases.html) and
@@ -154,6 +154,17 @@ sets.
have evolved over the past 20ish years based on his first-hand experience
as a leader and member in that community.
+* The State of [Python Speech Recognition](https://www.assemblyai.com/blog/the-state-of-python-speech-recognition-in-2021)
+ in 2021 is a practical overview of a specific area in data: extracting text
+ from voice recording data. Looking at verticals like this one can make it
+ easier to understand changes that are occurring in some parts of data and
+ programming that could be applied to other areas.
+
+* [Automated Data Wrangling](https://catalyst.coop/2021/05/23/automated-data-wrangling/)
+ covers cleaning, labeling, and automating the bunch of activities
+ that are typically necessary before analysis and data usage can
+ begin for a project.
+
* The [Open Source Data Science Masters](http://datasciencemasters.org/)
is a well-crafted free curriculum and set of resources for students who
want to learn both the theory and technologies for working with data.
@@ -163,3 +174,7 @@ sets.
[Jupyter Notebook](/jupyter-notebook.html) that [Stripe](/stripe.html)
for their data analysis across the company.
+* [The Definitive Data Scientist Environment Setup](https://davidadrian.cc/definitive-data-scientist-setup/)
+ explains how to set up both a hardware and software configuration that
+ is conducive to data science research and analysis.
+
diff --git a/content/pages/03-data/01-databases.markdown b/content/pages/03-data/01-databases.markdown
index 5eed45185..bad071a2c 100644
--- a/content/pages/03-data/01-databases.markdown
+++ b/content/pages/03-data/01-databases.markdown
@@ -100,7 +100,7 @@ Find out about Python applications with a MySQL backed on the dedicated
To work with a relational database using Python, you need to use a code
library. The most common libraries for relational databases are:
-* [psycopg2](http://initd.org/psycopg/)
+* [psycopg](https://www.psycopg.org/)
([source code](https://github.com/psycopg/psycopg2))
for PostgreSQL.
@@ -112,8 +112,6 @@ library. The most common libraries for relational databases are:
* [cx\_Oracle](https://oracle.github.io/python-cx_Oracle/index.html) for
Oracle Database ([source code](https://github.com/oracle/python-cx_Oracle)).
- Oracle moved their
- [open source driver code from SourceForge to GitHub in 2017](https://blogs.oracle.com/developers/oracle-database-python-driver-now-on-github).
SQLite support is built into Python 2.7+ and therefore a separate library
@@ -183,7 +181,7 @@ speed on SQL if you have never previously used it.
elaborates on one of the trickiest parts of writing SQL statements
that bridge one or more tables: the `JOIN`.
-* [Writing better SQL](http://www.craigkerstiens.com/2016/01/08/writing-better-sql/)
+* [Writing more legible SQL](https://www.craigkerstiens.com/2016/01/08/writing-more-legible-sql/)
is a short code styling guide to make your queries easier to read.
* [SQL Intermediate](https://www.dataquest.io/blog/sql-intermediate/) is a
@@ -265,7 +263,7 @@ speed on SQL if you have never previously used it.
1. Install PostgreSQL on your server. Assuming you went with Ubuntu run
``sudo apt-get install postgresql``.
-1. Make sure the [psycopg2](http://initd.org/psycopg/) library is in your
+1. Make sure the [psycopg](https://www.psycopg.org/) library is in your
application's dependencies.
1. Configure your web application to connect to the PostgreSQL instance.
diff --git a/content/pages/03-data/02-postgresql.markdown b/content/pages/03-data/02-postgresql.markdown
index d20271e9c..5e700895a 100644
--- a/content/pages/03-data/02-postgresql.markdown
+++ b/content/pages/03-data/02-postgresql.markdown
@@ -12,7 +12,7 @@ pronounced "Poss-gres", is an open source
[relational database](/databases.html) implementation frequently used by
Python applications as a backend for data storage and retrieval.
-
+
## How does PostgreSQL fit within the Python stack?
@@ -57,7 +57,7 @@ architecture.
To work with relational databases in Python you need to use a database
driver, which is also referred to as a database connector. The most common
driver library for working with PostgreSQL is
-[psycopg2](http://initd.org/psycopg/). There is
+[psycopg](https://www.psycopg.org/). There is
[a list of all drivers on the PostgreSQL wiki](https://wiki.postgresql.org/wiki/Python),
including several libraries that are no longer maintained. If you're
working with the
@@ -126,17 +126,17 @@ walkthroughs I've read.
* This article explains how and why PostgreSQL can handle
[full text searching](http://blog.lostpropertyhq.com/postgres-full-text-search-is-good-enough/)
- for many use cases. If you're going down this route, read
- [this blog post that explains how one developer implemented PostgreSQL full text search with SQLAlchemy](http://blog.garage-coding.com/2015/12/18/postgres-fulltext-search.html).
-
-* [django-postgres-copy](http://django-postgres-copy.californiacivicdata.org/en/latest/)
- is a tool for bulk loading data into a PostgreSQL database based on Django models.
- [Say hello to our new open-source software for loading bulk data into PostgreSQL](http://www.californiacivicdata.org/2015/07/17/hello-django-postgres-copy/)
- is an introduction to using the tool in your own projects.
+ for many use cases.
* [How to speed up tests in Django and PostgreSQL](http://nemesisdesign.net/blog/coding/how-to-speed-up-tests-django-postgresql/)
explains some hacks for making your schema migration-backed run quicker.
+* [Thinking psycopg3](https://www.varrazzo.com/blog/2020/03/06/thinking-psycopg3/)
+ is written by a developer who has worked on this critical Python library
+ for interacting with PostgreSQL since 2005. The author writes up thoughts
+ on what should change if backwards-incompatible changes are ever introduced
+ in a new hypothetical future version.
+
* [Records](https://pypi.org/project/records/) is a wrapper around the psycopg2
driver that allows easy access to direct SQL access. It's worth a look if
you prefer writing SQL over using an
@@ -169,9 +169,21 @@ for properly handling your data.
past five years. It's amazing to see how far this project has come and
how it continues to evolve.
+* [The Internals of PostgreSQL](http://www.interdb.jp/pg/) is a book
+ that goes into how PostgreSQL works, including core topics such as
+ [query processing](http://www.interdb.jp/pg/pgsql03.html),
+ [concurrency control](http://www.interdb.jp/pg/pgsql05.html) and the
+ [layout of heap table files](http://www.interdb.jp/pg/pgsql01.html).
+
* [PostgreSQL Weekly](http://postgresweekly.com/) is a weekly newsletter of
PostgreSQL content from around the web.
+* [My Favorite PostgreSQL Extensions - Part One](https://severalnines.com/database-blog/my-favorite-postgresql-extensions-part-one)
+ and
+ [part two](https://severalnines.com/database-blog/my-favorite-postgresql-extensions-part-two)
+ are roundups of useful PostgreSQL extensions that augment the
+ standard PostgreSQL functionality.
+
* [An introduction to PostgreSQL physical storage](http://rachbelaid.com/introduction-to-postgres-physical-storage/)
provides a solid walkthrough of where PostgreSQL files are located on
disk, how the files store your data and what mappings are important for
diff --git a/content/pages/03-data/03-mysql.markdown b/content/pages/03-data/03-mysql.markdown
index 87edda6a3..27c52b9c4 100644
--- a/content/pages/03-data/03-mysql.markdown
+++ b/content/pages/03-data/03-mysql.markdown
@@ -133,6 +133,10 @@ A handful of the best of these tutorials are listed below.
goes into details for why the company Userlike migrated from their MySQL
database setup to PostgreSQL.
+* [MySQL sharding at Quora](https://www.quora.com/q/quoraengineering/MySQL-sharding-at-Quora)
+ provides details behind Quora's at-scale infrastructure and
+ how their MySQL sharding evolved over time.
+
* [Growing up with MySQL](https://nylas.com/blog/growing-up-with-mysql/) is
a story about how one company went through dramatic growth and had to keep
up with it by quickly scaling their MySQL database.
diff --git a/content/pages/03-data/04-sqlite.markdown b/content/pages/03-data/04-sqlite.markdown
index 2cb4cef26..5c7f3fd00 100644
--- a/content/pages/03-data/04-sqlite.markdown
+++ b/content/pages/03-data/04-sqlite.markdown
@@ -86,6 +86,10 @@ tutorials will help you get started.
It's a great short read which shows that the code is well-tested and
maintained.
+* [SQLite is not a toy database](https://antonz.org/sqlite-is-not-a-toy-database/)
+ is a whirlwind overview of some of the best aspects of SQLite and why
+ you should use it.
+
* [Data Analysis of 8.2 Million Rows with Python and SQLite](https://plot.ly/ipython-notebooks/big-data-analytics-with-pandas-and-sqlite/)
explains how you can load a large dataset in to SQLite and visualize it
using the Plotly service.
@@ -139,6 +143,9 @@ you are having with SQLite rather than going through a general tutorial.
digs into the internals of SQLite and shows some bugs found (and
since fixed) while the author was researching the SQLite source code.
+* [How to Store Multimedia Files in a SQLite3 Database with Python](https://www.twilio.com/blog/intro-multimedia-file-upload-python-sqlite3-database)
+ goes through the Python code for storing and accessing BLOB-type objects.
+
* [Going Fast with SQLite and Python](http://charlesleifer.com/blog/going-fast-with-sqlite-and-python/)
shares essential knowledge for working effectively with SQLite in Python,
particularly when it comes to transactions, concurrency and commits.
@@ -148,11 +155,6 @@ you are having with SQLite rather than going through a general tutorial.
[object-relational mapper (ORM)](/object-relational-mappers-orms.html)
to implement virtual tables and aggregates on top of SQLite.
-* [Use SQLite with Django On AWS Lambda with Zappa](https://blog.zappa.io/posts/use-sqlite-with-django-on-aws-lambda-with-zappa)
- provides an example `dev_settings.py` file for
- locally testing a [Django](/django.html) application intended for
- [AWS Lambda](/aws-lambda.html).
-
* [SQLite Database Authorization and Access Control with Python](http://charlesleifer.com/blog/sqlite-database-authorization-and-access-control-with-python/)
covers how to control access to the SQLite database connection and
file even though SQLite normally allows unauthorized access by design.
diff --git a/content/pages/03-data/05-object-relational-mappers.markdown b/content/pages/03-data/05-object-relational-mappers.markdown
index a642889cc..6451908e8 100644
--- a/content/pages/03-data/05-object-relational-mappers.markdown
+++ b/content/pages/03-data/05-object-relational-mappers.markdown
@@ -8,7 +8,7 @@ meta: Object-relational mappers (ORMs) bridge relational databases and data repr
An object-relational mapper (ORM) is a code library that automates the
-transfer of data stored in relational databases tables into objects that
+transfer of data stored in relational database tables into objects that
are more commonly used in application code.
@@ -64,7 +64,7 @@ there was a pressing reason.
Python ORM libraries are not required for accessing relational
databases. In fact, the low-level access is typically provided by another
library called a *database connector*, such as
-[psycopg](http://initd.org/psycopg/) (for PostgreSQL)
+[psycopg](https://www.psycopg.org/) (for PostgreSQL)
or [MySQL-python](https://pypi.org/project/MySQL-python/1.2.5) (for
MySQL). Take a look at the table below which shows how ORMs can work with
different web frameworks and connectors and relational databases.
@@ -137,7 +137,8 @@ There are numerous ORM implementations written in Python, including
1. [The Django ORM](/django-orm.html)
1. [PonyORM](/pony-orm.html)
1. [SQLObject](http://sqlobject.org/)
-1. [Tortoise ORM](https://tortoise-orm.readthedocs.io/en/latest/)
+1. [Tortoise ORM](https://tortoise-orm.readthedocs.io/en/latest/)
+ ([source code](https://github.com/tortoise/tortoise-orm/))
There are other ORMs, such as Canonical's
[Storm](https://storm.canonical.com/), but most of them do not appear to
diff --git a/content/pages/03-data/06-sqlalchemy.markdown b/content/pages/03-data/06-sqlalchemy.markdown
index af57ed512..f4c9abf9d 100644
--- a/content/pages/03-data/06-sqlalchemy.markdown
+++ b/content/pages/03-data/06-sqlalchemy.markdown
@@ -166,6 +166,10 @@ edge cases.
gives a wonderful walkthrough of how to build your own online commenting
system in Python using SQLAlchemy.
+* [Master SQLAlchemy Relationships in a Performance Friendly Way](https://blog.theodo.com/2020/03/sqlalchemy-relationship-performance/)
+ dives into code that shows how to improve performance when setting and
+ accessing relationship-based data in your models.
+
* [SQLAlchemy and data access in Python](https://talkpython.fm/episodes/show/5/sqlalchemy-and-data-access-in-python)
is a podcast interview with the creator of SQLAlchemy that covers the
project's history and how it has evolved over the past decade.
@@ -193,6 +197,11 @@ implementations. Several open source projects and articles are listed here
to make it a bit easier to understand the differences between these
implementations.
+* [Introduction to SQLAlchemy ORM for Django Developers](https://apirobot.me/posts/introduction-to-sqlalchemy-orm-for-django-developers)
+ is written by a developer who typically used the [Django ORM](/django-orm.html)
+ at work and then had a chance to try SQLAlchemy for one project. He covers
+ differences in how each one handles transactions, models and queries.
+
* [SQLAlchemy vs Other ORMs](http://www.pythoncentral.io/sqlalchemy-vs-orms/)
provides a detailed comparison of SQLAlchemy against alternatives.
@@ -212,3 +221,9 @@ implementations.
[which is better and why: Django ORM or SQLALchemy](https://www.quora.com/Which-is-better-and-why-Djangos-ORM-or-SQLAlchemy)
based on various developers' experiences.
+
+## Open source code for learning SQLAlchemy
+Many open source projects rely on SQLAlchemy. A great way to learn
+how to properly work with this tool is to read the code that shows
+how those projects use SQLAlchemy. This section alphabetically lists
+these code examples by class and function in the SQLAlchemy code base.
diff --git a/content/pages/03-data/08-django-orm.markdown b/content/pages/03-data/08-django-orm.markdown
index c5ce776ed..59bb49f8b 100644
--- a/content/pages/03-data/08-django-orm.markdown
+++ b/content/pages/03-data/08-django-orm.markdown
@@ -39,6 +39,10 @@ that have been added throughout the project's history.
[Django Silk](http://mtford.co.uk/blog/2/) is another inspection tool and
has capabilities to do more than just SQL inspection.
+* [Django QuerySet Examples (with SQL code included)](https://davit.tech/django-queryset-examples/)
+ teaches how QuerySets work and shows the corresponding SQL code behind the
+ Python code you write to use the ORM.
+
* [Making a specific Django app faster](http://reinout.vanrees.org/weblog/2014/05/06/making-faster.html)
is a Django performance blog post with some tips on measuring performance
and optimizing based on the measured results.
@@ -176,3 +180,18 @@ following resources should get you past the initial hurdles.
added to an existing table, and another where a `Many-to-Many` field needs
to be converted to a standard `ForeignKey` column while retaining all
of the data.
+
+* [Double-checked locking with Django ORM](https://lukeplant.me.uk/blog/posts/double-checked-locking-with-django-orm/)
+ shows how you can implement a double-checking locking pattern in the
+ Django ORM with [PostgreSQL](/postgresql.html), which is useful
+ when you want to prevent multiple processes from accessing the same
+ data at the same time.
+
+* [Using Django Check Constraints for the Sum of Percentage Fields](https://adamj.eu/tech/2020/03/10/django-check-constraints-sum-percentage-fields/)
+ shows how you can combine several `PositiveIntegerField` model
+ fields with a checking constraint and a web form that ensures
+ all of the fields sum up to a precise amount, such as 100%.
+
+* [Learn Django ORM - Query and Filters](https://www.youtube.com/playlist?list=PLOLrQ9Pn6cazjoDEnwzcdWWf4SNS0QZml)
+ is a video tutorials series that gives an overview of the ORM's
+ querying and filtering capabilities.
diff --git a/content/pages/03-data/10-no-sql-datastore.markdown b/content/pages/03-data/10-no-sql-datastore.markdown
index 05e47b705..c6c9b4683 100644
--- a/content/pages/03-data/10-no-sql-datastore.markdown
+++ b/content/pages/03-data/10-no-sql-datastore.markdown
@@ -189,10 +189,6 @@ representing a person could have a property of "female" or "male".
* [CAP Theorem overview](http://natishalom.typepad.com/nati_shaloms_blog/2010/10/nocap.html)
presents the basic constraints all databases must trade off in operation.
-* This post on [What is a NoSQL database? Learn By Writing One in Python](http://jeffknupp.com/blog/2014/09/01/what-is-a-nosql-database-learn-by-writing-one-in-python/)
- is a detailed article that breaks the mystique behind what some forms
- of NoSQL databases are doing under the covers.
-
* The [CAP Theorem series](http://blog.thislongrun.com/2015/03/the-cap-theorem-series.html)
explains concepts related to NoSQL such as what is ACID compared to CAP, CP
versus CA and high availability in large scale deployments.
diff --git a/content/pages/03-data/11-redis.markdown b/content/pages/03-data/11-redis.markdown
index c05d04741..3600ddae2 100644
--- a/content/pages/03-data/11-redis.markdown
+++ b/content/pages/03-data/11-redis.markdown
@@ -84,10 +84,6 @@ Redis should be customized out of its default configuration to secure it
against unauthorized and unauthenticated users. These resources provide
some advice on Reids security and guarding against data breaches.
-* [Pentesting Redis servers](http://averagesecurityguy.info/2015/09/17/pentesting-redis-servers/)
- shows that security is important not only on your application but also
- the databases you're using as well.
-
* Redis, just as with any relational or NoSQL database, needs to be secured
based on [security guidelines](http://www.antirez.com/news/96). There is
also a post where the main author of Redis
diff --git a/content/pages/03-data/12-mongodb.markdown b/content/pages/03-data/12-mongodb.markdown
index 407cb1c30..665144fec 100644
--- a/content/pages/03-data/12-mongodb.markdown
+++ b/content/pages/03-data/12-mongodb.markdown
@@ -67,6 +67,12 @@ command line and query language.
[Google Cloud Function](/google-cloud-functions.html)
to store persistent data while running on a serverless platform.
+* [Everything You Know About MongoDB is Wrong!](https://developer.mongodb.com/article/everything-you-know-is-wrong)
+ lists many of the common thoughts developers have about MongoDB and
+ why some of them are misconceptions. This is a good read for
+ developers who used MongoDB several years ago and want to know what
+ major improvements have been made since then.
+
### MongoDB security
NoSQL databases can be a weak spot in a production deployment environment,
@@ -93,11 +99,6 @@ security controls so make sure to lock down your instances.
installing and using MongoDB on your own instance. The post covers
authentication, SSL and firewalls.
-* [Securing MongoDB using Let's Encrypt certificate](https://zohaib.me/securing-mongodb-using-lets-encrypt/)
- gives a configuration that encrypts that traffic coming from and
- going to your MongoDB instances using free
- [Let's Encrypt certificates](https://letsencrypt.org/).
-
* This 4 post securing MongoDB series covers
[Data Security Requirements for Regulatory Compliance](https://www.mongodb.com/blog/post/securing-mongodb-part-1-data-security-requirements-for-regulatory-compliance),
[Database Access Control](https://www.mongodb.com/blog/post/securing-mongodb-part-2-database-access-control),
@@ -105,11 +106,6 @@ security controls so make sure to lock down your instances.
and
[Environmental Control & Database Management](https://www.mongodb.com/blog/post/securing-mongodb-part-4-environmental-control-and-database-management).
-* Lightweight Directory Access Protocol (LDAP) is common in many
- established company environments for security. This post on
- [How to Configure LDAP Authentication for MongoDB](https://www.mongodb.com/blog/post/how-to-configure-LDAP-authentication-for-mongodb)
- goes over how to authenticate users via LDAP who are using MongoDB.
-
### Python with MongoDB resources
MongoDB is straightforward to use in a Python application when a driver
@@ -120,10 +116,6 @@ configure and start using MongoDB with Python.
shows how to use Python to interface with MongoDB via PyMongo and
MongoEngine.
-* A [Gentle Introduction to MongoDB using Pymongo](http://altons.github.io/python/2013/01/21/gentle-introduction-to-mongodb-using-pymongo/)
- reviews a connection configuration file, the basics for data insertion,
- aggregation and updating records.
-
* [How To Set Up Flask with MongoDB and Docker](https://www.digitalocean.com/community/tutorials/how-to-set-up-flask-with-mongodb-and-docker)
combines the [Flask](/flask.html) web framework with MongoDB then
containerizes the example application with [Docker](/docker.html).
diff --git a/content/pages/03-data/14-neo4j.markdown b/content/pages/03-data/14-neo4j.markdown
index 13f4cf29c..d53b5711c 100644
--- a/content/pages/03-data/14-neo4j.markdown
+++ b/content/pages/03-data/14-neo4j.markdown
@@ -35,11 +35,6 @@ of the database.
Transform and Load (ETL) tool to move your data from one database such as
[MySQL](/mysql.html) into Neo4j.
-* [Migrating from MySQL to Neo4j](https://engineering.logicgate.com/migrating-from-mysql-to-neo4j-2f6cb63a73c8)
- gives specific advice about how your data can change during a migration from
- a relational database to a graph database which the author did not know
- before undertaking their own migration.
-
### Neo4j resources
* [Building a Recommendation Engine with Neo4j and Python](https://www.youtube.com/watch?v=ILjTikVhT9k)
diff --git a/content/pages/03-data/16-pandas.markdown b/content/pages/03-data/16-pandas.markdown
index b3ccee0d7..a0750eaae 100644
--- a/content/pages/03-data/16-pandas.markdown
+++ b/content/pages/03-data/16-pandas.markdown
@@ -49,6 +49,14 @@ is a data structures and analysis library.
shows how to use the `crosstab` function in pandas so you can summarize
and group data.
+* [Calculating streaks in pandas](https://joshdevlin.com/blog/calculate-streaks-in-pandas/)
+ shows how to measure and report on streaks in data, which is where
+ several events happen in a row consecutively.
+
+* [How to Convert a Python Dictionary to a Pandas DataFrame](https://www.marsja.se/how-to-convert-a-python-dictionary-to-a-pandas-dataframe/)
+ is a straightforward tutorial with example code for loading and adding
+ data stored in a typical Python dictionary into a DataFrame.
+
* This two-part series on loading data into a pandas DataFrame presents
what to do
[when CSV files do not match your expectations](http://blog.enthought.com/python/with-and-without-the-canopy-data-import-tool-loading-data-theres-no-such-thing-as-a-simple-csv-file/)
@@ -62,15 +70,18 @@ is a data structures and analysis library.
and charts that show the pay off period broken down by interest and
principal.
+* [Efficiently cleaning text with pandas](https://pbpython.com/text-cleaning.html)
+ provides a really great practical tutorial on different approaches
+ for cleaning a large data set so that you can begin to do your analysis.
+ The tutorial also shows how to use the
+ [sidetable](https://github.com/chris1610/sidetable) library, which
+ creates summary tables of a DataFrame.
+
* [tabula-py: Extract table from PDF into Python DataFrame](https://blog.chezo.uno/tabula-py-extract-table-from-pdf-into-python-dataframe-6c7acfa5f302)
presents how to use the Python wrapper for the
[Tabula](https://tabula.technology/) library that makes it easier to
extract table data from PDF files.
-* [Analyzing Browser History Using Python and Pandas](https://applecrazy.github.io/blog/2017-11-12/analyzing-browser-hist-using-python)
- shows how to take data from Google Chrome and start to visualize it
- with pandas and [matplotlib](/matplotlib.html).
-
* [Time Series Forecast Case Study with Python: Monthly Armed Robberies in Boston](https://machinelearningmastery.com/time-series-forecast-case-study-python-monthly-armed-robberies-boston/)
walks through the data wrangling, analysis and visualization steps
with a public data set of murders in Boston from 1966 to 1975. This
@@ -110,3 +121,32 @@ is a data structures and analysis library.
explains that pandas operations can often be parallelized for better
performance using the Pandas UDFs feature in PySpark version 2.3
or greater.
+
+* [How to use Pandas read_html to Scrape Data from HTML Tables](https://www.marsja.se/how-to-use-pandas-read_html-to-scrape-data-from-html-tables/)
+ has a bunch of great code examples that show how to load
+ data from HTML directly into your DataFrames.
+
+* [How to download fundamentals data with Python](http://theautomatic.net/2020/05/05/how-to-download-fundamentals-data-with-python/)
+ shows how to obtain and use financial data, such as balance sheets,
+ stock prices, and various ratios to perform your own analysis on.
+
+* [How to convert JSON to Excel with Python and pandas](https://www.marsja.se/how-to-convert-json-to-excel-python-pandas/)
+ provides instructions for creating a spreadsheet out of JSON file.
+
+* [Loading large datasets in Pandas](https://towardsdatascience.com/loading-large-datasets-in-pandas-11bdddd36f7b)
+ explains how to get around the `MemoryError` issue that occurs
+ when using `read_csv` because the data set is larger than the
+ available memory on a machine. You can use chunking with
+ the `read_csv` function to divide the data set into smaller parts that
+ each can be loaded into memory. Alternatively, you can use a
+ [SQLite database](/sqlite.html) to create a [relational database](/databases.html)
+ with the data then use SQL queries or an
+ [object-relational mapper (ORM)](/object-relational-mappers-orms.html)
+ to load the data and perform analysis in pandas.
+
+* Real-world Excel spreadsheets are often a mess of unstructured data, so
+ this tutorial on
+ [Reading Poorly Structured Excel Files with Pandas](https://pbpython.com/pandas-excel-range.html)
+ gives example code for extracting only part of a file as well
+ as reading ranges and tables.
+
diff --git a/content/pages/03-data/17-scipy-numpy.markdown b/content/pages/03-data/17-scipy-numpy.markdown
index b74612684..00763c2d0 100644
--- a/content/pages/03-data/17-scipy-numpy.markdown
+++ b/content/pages/03-data/17-scipy-numpy.markdown
@@ -44,11 +44,6 @@ following resources are broader walkthroughs for the SciPy ecosystem:
plotter. This is a very cool example project that ties together
the scientific world and the art world.
-* [Lectures in Quantitative Economics: SciPy](https://lectures.quantecon.org/py/scipy.html)
- provides a good overview of SciPy compared to the specific NumPy
- project, as well as explanations for the wrappers SciPy provides
- over lower-level FORTRAN libraries.
-
* [A plea for stability in the SciPy ecosystem](http://blog.khinsen.net/posts/2017/11/16/a-plea-for-stability-in-the-scipy-ecosystem/)
presents concerns from one scientist's perspective about how fast the
Python programming ecosystem changes and that code can become backwards
@@ -67,9 +62,19 @@ following resources are broader walkthroughs for the SciPy ecosystem:
computation and how to perform operations that get the results you need
for your data analysis.
-* [101 NumPy Exercises for Data Analysis](https://www.machinelearningplus.com/101-numpy-exercises-python/)
+* [Scientific Computing in Python: Introduction to NumPy and Matplotlib](https://sebastianraschka.com/blog/2020/numpy-intro.html)
+ is a detailed tutorial that goes through the basics for NumPy and
+ then connects it to [Matplotlib](/matplotlib.html).
+
+* [Math to Code](https://mathtocode.com/) provides an interactive
+ tutorial to learn how to implement math in NumPy.
+
+* [101 NumPy Exercises for Data Analysis](https://www.machinelearningplus.com/python/101-numpy-exercises-python/)
+ has a bunch of questions and answers to common ways to work with NumPy
+ and is useful to understand what you can do with this library.
* [NumPy: creating and manipulating numerical data](http://www.scipy-lectures.org/intro/numpy/index.html)
+ contains many code examples for common operations.
* [Python NumPy Array Tutorial](https://www.datacamp.com/community/tutorials/python-numpy-tutorial)
is a starter tutorial specifically focused on using and working
diff --git a/content/pages/03-data/18-data-visualization.markdown b/content/pages/03-data/18-data-visualization.markdown
index 62002fcc0..e56c2b2dc 100644
--- a/content/pages/03-data/18-data-visualization.markdown
+++ b/content/pages/03-data/18-data-visualization.markdown
@@ -36,7 +36,6 @@ easier for humans to see patterns, trends and other useful information.
* [Python & OpenGL for Scientific Visualization](https://www.labri.fr/perso/nrougier/python-opengl/)
is a free book that shows how to combine open source tools such as
- [Pyglet](https://pyglet.readthedocs.io/en/stable/) and
[PyOpenGL](http://pyopengl.sourceforge.net/) with Python
[data analysis](/data-analysis.html) libraries to generate interactive
scientific data visualizations.
@@ -92,11 +91,6 @@ Sometimes you need inspiration from other sources to figure out what
you want to build. The following links have made me excited about data
visualization and gave me ideas for what to build.
-* [Roads to Rome](http://roadstorome.moovellab.com/) is a beautiful
- visualization showing the data behind the expression "all roads lead
- to Rome" and whether or not there is a "Rome" central city in every
- country.
-
* [Monarchs](https://thebackend.dev/monarchs/) is a wonderful 1,000 year
history visual of European rulers. The developer also wrote an in-depth
article on
@@ -108,10 +102,6 @@ visualization and gave me ideas for what to build.
and dark sides, the main characters, various bits about the Force
and other data extracted from the movies.
-* [Big League Graphs](https://bigleaguegraphs.com/) presents a bunch of
- creative ways to view data for sports such as basketball, baseball and
- hockey.
-
* [What do numbers look like?](https://johnhw.github.io/umap_primes/index.md.html)
is a Python 3 dimensional visualization of millions of integers, colored
by special factors such as prime and Fibonacci numbers.
@@ -142,6 +132,12 @@ visualization and gave me ideas for what to build.
### Data visualization resources
+* [Guides for visualizing data](https://flowingdata.com/2020/06/01/guides-for-visualizing-reality/)
+ provides the thought processes that you can use when you are
+ showing uncertainty, incompletet data, differences, outliers,
+ and other common scenarios that occur when your data is messy -
+ and it usually is!
+
* [Data visualization, from 1987 to today](https://medium.economist.com/data-visualisation-from-1987-to-today-65d0609c6017)
is a wonderful reference about the pre-computer age era of visualization
which was a combination of cartography, art and statistics rather than
@@ -157,8 +153,7 @@ visualization and gave me ideas for what to build.
* [Engineering Intelligence Through Data Visualization at Uber](https://eng.uber.com/data-viz-intel/)
explains how Uber's data visualization team grew from 1 person to 15
and the output they created along the way, including the open source
- tools [react-vis](https://uber.github.io/react-vis/) and
- [react-map-gl](https://uber.github.io/react-map-gl/).
+ tool [react-vis](https://uber.github.io/react-vis/).
* The Practitioner's Guide to System Dashboard Design series covers a
lot of ground for what you should consider when building one form
@@ -168,3 +163,9 @@ visualization and gave me ideas for what to build.
* [Part 2: Presentation and Accessibility](http://onemogin.com/observability/dashboards/practitioners-guide-to-system-dashboard-design-p2.html)
* [Part 3: What Charts to Use](http://onemogin.com/observability/dashboards/practitioners-guide-to-system-dashboard-design-p3.html)
* [Part 4: Context Improvement](http://onemogin.com/observability/dashboards/practitioners-guide-to-system-dashboard-design-p4.html)
+
+* [Truncating the Y-Axis: Threat or Menace?](https://engineering.tableau.com/truncating-the-y-axis-threat-or-menace-d0bce66d4d08)
+ is a great in-indepth explanation of why something that seems as
+ straightforward as laying out the Y-Axis requires thought and
+ care otherwise the visualization could end up being misleading.
+
diff --git a/content/pages/03-data/19-bokeh.markdown b/content/pages/03-data/19-bokeh.markdown
index 668538d90..22f8d55cc 100644
--- a/content/pages/03-data/19-bokeh.markdown
+++ b/content/pages/03-data/19-bokeh.markdown
@@ -61,12 +61,6 @@ basic syntax will change as the library's API is not yet stable.
an appropriate format then explains the code that uses Bokeh to visualize
it.
-* [Data is beautiful: Visualizing Roman imperial dynasties](http://machineloveus.com/data-is-beautiful-visualizing-roman-imperial-dynasties/)
- provides a walkthrough for creating a gorgeous visualization based on
- historical Roman data. The post is about more than just the visual, it also
- goes into the ideation, data wrangling and analysis phases that came
- before using Bokeh to show the results.
-
* [Visualizing with Bokeh](https://programminghistorian.org/en/lessons/visualizing-with-bokeh)
gives a detailed explanation with the code for number Bokeh visuals
you can output while working with a [pandas](/pandas.html) data set.
@@ -105,13 +99,6 @@ basic syntax will change as the library's API is not yet stable.
project has the code to create a simple chart with Bokeh and
[Flask](/flask.html).
-* [Bokeh vs Dash — Which is the Best Dashboard Framework for Python?](https://blog.sicara.com/bokeh-dash-best-dashboard-framework-python-shiny-alternative-c5b576375f7f)
- contains a single project that was written in both Dash and Bokeh. The
- author gives his subjective view on the implementation difficulty
- although the web application only contained a single type of data
- visualization so it is hard to drawn any real conclusions from his
- opinion.
-
* [Realtime Flight Tracking with Pandas and Bokeh](https://www.geodose.com/2019/01/realtime-flight-tracking-pandas-bokeh-python.html)
provides a great example of combining [pandas](/pandas.html) for structuring
data with Bokeh for visualization.
diff --git a/content/pages/03-data/21-matplotlib.markdown b/content/pages/03-data/21-matplotlib.markdown
index 14f4d2f64..9c60d3740 100644
--- a/content/pages/03-data/21-matplotlib.markdown
+++ b/content/pages/03-data/21-matplotlib.markdown
@@ -21,10 +21,6 @@ toolkits.
is an awesome getting started tutorial that breaks through the confusing
beginner steps so you can quick start using the plotting library.
-* [Web Scraping With Python: Scrapy, SQL, Matplotlib To Gain Web Data Insights](http://www.scrapingauthority.com/python-scrapy-mysql-and-matplotlib-to-gain-web-data-insights/)
- is a long and comprehensive tutorial that walks through obtaining,
- cleaning and visualizing data.
-
* [Matplotlib Cheat Sheet: Plotting in Python](https://www.datacamp.com/community/blog/python-matplotlib-cheat-sheet)
contains some handy snippets of code to perform common plotting operations
in Matplotlib.
diff --git a/content/pages/03-data/25-oracle.markdown b/content/pages/03-data/25-oracle.markdown
new file mode 100644
index 000000000..bf6b3cdc0
--- /dev/null
+++ b/content/pages/03-data/25-oracle.markdown
@@ -0,0 +1,165 @@
+title: Oracle
+category: page
+slug: Oracle
+sortorder: 0325
+toc: False
+sidebartitle: Oracle
+meta: Oracle Database is an enterprise relational database management system.
+
+
+[Oracle Database](http://www.oracle.com/) is an enterprise
+[relational database](/databases.html). It can run transaction processing,
+data warehousing, and multi-model database workloads such as machine
+learning, spatial, and graph analysis. Recent versions of Oracle Database
+also added support for JSON and blockchain use cases, and the software
+can be run in on-premise, cloud or hybrid environments.
+
+
+
+
+## How does Oracle fit with Python?
+The Python community and Oracle have a long history. The excellent Python Database API-compliant "cx_Oracle" interface for Oracle Database was first created by the user community in 1998 and is now being enhanced and maintained by Oracle. The [cx_Oracle](https://oracle.github.io/python-cx_Oracle/) module also underpins the [Oracle Machine Learning for Python](https://www.youtube.com/watch?v=P861m__PEMQ) engine. Oracle's high-performance GraalVM framework supports an implementation of Python called [GraalPython](https://github.com/oracle/graalpython).
+
+
+## Why is Oracle Database a great choice?
+Oracle Database is cross-platform, supporting multiple hardware platforms and various operating systems. Developers and companies of all sizes rely on its proven industry-leading performance, scalability, reliability, and security.
+As data volumes rise exponentially, new data types and data models are required to support modern applications. Oracle Database supports the following data types at no extra cost:
+
+* [JSON](https://docs.oracle.com/en/database/oracle/oracle-database/19/adjsn/index.html)
+* [Blockchain](https://docs.oracle.com/en/database/oracle/oracle-database/21/nfcon/details-oracle-blockchain-table-282449857.html)
+* [XML](https://www.oracle.com/database/technologies/appdev/xmldb.html)
+* [Object](https://docs.oracle.com/database/121/ADOBJ/adobjint.htm#ADOBJ00101)
+* [Graph](https://www.oracle.com/database/graph/)
+* [Spatial](https://www.oracle.com/database/spatial/)
+* [Time Series](https://docs.oracle.com/en/database/oracle/oracle-database/19/dmcon/time-series.html)
+* Relational
+
+With support for scale-out database clusters, sharded distributed systems, and disaster recovery with continuous application availability, there is no shortage of features to guarantee the Database continues to run uninterrupted 24/7.
+
+Oracle makes its enterprise-class database readily available to developers with its free on-premises edition Oracle Database XE or on the Oracle public cloud with an Always Free Cloud account. In addition, Oracle Autonomous Database is a popular choice for developers as no database management or tuning is required, leaving developers to do what they do best – writing code for their applications.
+
+
+## Connecting to Oracle Database with Python
+As with any database, applications require a connector or driver to connect to the Oracle Database. The Python DB API-compliant [cx_Oracle](https://github.com/oracle/python-cx_Oracle) interface provides developers access to standard and advanced Oracle Database features, such as SQL execution and document storage APIs. It also gives users access to network traffic encryption capabilities and Oracle's leading high availability features.
+
+[Code examples](https://oracle.github.io/python-cx_Oracle/samples/tutorial/Python-and-Oracle-Database-Scripting-for-the-Future.html) and free workshops such as the introductory [Python and Oracle for Developers Workshop](https://apexapps.oracle.com/pls/apex/dbpm/r/livelabs/view-workshop?wid=766) and a full-stack development workshop using [Python with SQLAlchemy to Oracle Database](https://apexapps.oracle.com/pls/apex/dbpm/r/livelabs/view-workshop?wid=911&clear=180&session=16650643444916) are available.
+
+
+
+You can use many Python frameworks and [object-relational mappers (ORMs)](/object-relational-mappers-orms.html) with Oracle Database. ORMs abstract the tables and objects in a relational database to objects that Python developers can manipulate and operate on. [SQLAlchemy](/sqlalchemy.html) and Django are popular ORMs. SQLAlchemy is used by Pandas, which is very popular with Oracle users.
+The table below shows the relationship between web framework, ORM, driver, and the Oracle Database.
+
+
+
+Learn more about
+[Python ORMs on that dedicated topic page](/object-relational-mappers-orms.html).
+
+ORMs provide a familiar programming model for Python developers, but sometimes you want that extra performance and operate closer to SQL objects. Oracle cx_Oracle offers several [functions](https://oracle.github.io/python-cx_Oracle/samples/tutorial/Python-and-Oracle-Database-Scripting-for-the-Future.html#binding) to deliver that performance. These functions include fetching data, binding data, executing PL/SQL, operating on LOBs, JSON documents, message passing with Oracle Advanced Queuing, and more.
+
+
+## Oracle and Data Safety
+According to Gartner, Oracle has one of the [highest data safety ratings](https://www.gartner.com/reviews/market/cloud-database-management-systems/vendor/oracle/product/oracle-database) in the industry, with a wide range of features for data protection and high availability. These features include:
+
+* [Database encryption](https://www.oracle.com/database/technologies/security/advanced-security.html)
+
+* [Access control to rows](https://www.oracle.com/database/technologies/security/label-security.html) in a table
+
+* [Database vault](https://www.oracle.com/database/technologies/security/db-vault.html) to restrict privileges and access
+
+* [Data redaction, subsetting, and masking](https://www.oracle.com/database/technologies/security/data-masking-subsetting.html)
+
+* All in one data security service in the Oracle Cloud with [Data Safe](https://www.oracle.com/database/technologies/security/data-safe.html)
+
+* Oracle also provides free tools such as the [Database Assessment Tool (DBSAT)](https://www.oracle.com/database/technologies/security/dbsat.html) to help you identify and remedy potential vulnerabilities.
+
+Oracle also provides numerous data recovery features, including:
+
+* Backup capabilities with [RMAN](https://www.oracle.com/database/technologies/high-availability/rman.html)
+
+* Restore point features with [Database Flashback](https://www.oracle.com/database/technologies/high-availability/flashback.html)
+
+* [Application continuity](https://www.oracle.com/database/technologies/high-availability/app-continuity.html) in the event of database failover to a standby
+
+For an overview of Oracle’s security and high availability architecture, see the following white papers:
+
+* [Maximum Availability Architecture](https://www.oracle.com/a/tech/docs/maa-onpremises-overview.pdf) (MAA)
+
+* [Maximum Security Architecture](https://blogs.oracle.com/cloudsecurity/post/oracles-maximum-security-architecture-for-database-security) (MSA)
+
+
+## Python Specific Oracle Database resources
+Many quick starts, tutorials, and workshops exist specifically for Python developers using Oracle Database. Below are some of the best ones to start with.
+
+
+###Getting Started
+If you are looking for a fast way to get started with Python and Oracle Database, check out these two quick start tutorials. These tutorials walk you through installing and setting up the environment you need to connect Python to Oracle Database.
+
+* [Quick Start: Developing Python Applications for Oracle Database](https://www.oracle.com/database/technologies/appdev/python/quickstartpythononprem.html)
+
+* [Quick Start: Developing Python Applications for Oracle Autonomous Database](https://www.oracle.com/database/technologies/appdev/python/quickstartpythononprem.html)
+
+Once you have done one of these, then continue with the popular [Python and Oracle Database Tutorial: Scripting for the Future](https://oracle.github.io/python-cx_Oracle/samples/tutorial/Python-and-Oracle-Database-Scripting-for-the-Future.html) to dive deeper to master the Python cx_Oracle interface and see how to build great Oracle Database applications.
+
+
+###Using Different Frameworks with Oracle
+* [How to Run SQL Queries with Pandas](https://www.oracle.com/news/connect/run-sql-data-queries-with-pandas.html) is a good blog using Pandas for quick and easy data manipulation in Python.
+
+* [Using Oracle with Pandas in OCI Data Science Notebooks](https://docs.oracle.com/en-us/iaas/tools/ads-sdk/latest/user_guide/loading_data/efficient_use_of_oracle_rdbms_with_ads.html) dives deeper into using Pandas with large datasets in data science applications.
+
+* [Using SQLAlchemy with Oracle Database](https://docs.sqlalchemy.org/en/14/dialects/oracle.html) provides an excellent toolkit for Python developers using SQLAlchemy as their ORM.
+
+* [Using Django with Python and Oracle Database](https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/python_django/python_django.htm) is a tutorial from Oracle and shows the Django Framework with Python to an Oracle Database.
+
+* [Connecting Pony ORM to the Database](https://docs.ponyorm.org/database.html) is a friendly guide on using Pony with databases.
+
+* [How to use Python Flask with Oracle Database](https://blogs.oracle.com/opal/post/how-to-use-python-flask-with-oracle-database).
+
+* [Part 1: Docker for Oracle Database Applications in Node.js and Python](https://blogs.oracle.com/opal/post/part-1-docker-for-oracle-database-applications-in-nodejs-and-python).
+
+* [Part 2: Docker for Oracle Database Applications in Node.js and Python](https://blogs.oracle.com/opal/post/part-2-docker-for-oracle-database-applications-in-nodejs-and-python).
+
+* [Faster JSON with Python cx_Oracle and Oracle Database 21’s new OSON storage format](https://blogs.oracle.com/opal/post/faster-json-with-python-cx_oracle-81-and-oracle-database-21s-new-oson-storage-format).
+
+
+###Workshops
+The following hands-on, free workshops provide step-by-step instructions and walkthroughs in a live environment.
+
+* [Use Python with Oracle Database 19c](https://apexapps.oracle.com/pls/apex/dbpm/r/livelabs/view-workshop?wid=635&clear=180&session=3484600041895) is an Oracle LiveLabs workshop that shows how to write Python code to connect to and read data from an Oracle Database, including JSON data.
+
+* [Python and Oracle for Developers](https://apexapps.oracle.com/pls/apex/dbpm/r/livelabs/workshop-attendee-2?p210_workshop_id=766&p210_type=2&session=3484600041895) is an Oracle LiveLabs workshop that explores the features of the Python cx_Oracle interface for Oracle Database, including efficient techniques for connection management and statement handling.
+
+* [Full Stack Development using Python and deployment via OKE](https://apexapps.oracle.com/pls/apex/dbpm/r/livelabs/view-workshop?wid=911&clear=180&session=3484600041895) is an Oracle LiveLabs workshop that explores how to build and deploy a simple cloud-native application using the most common frameworks and the Oracle Cloud Infrastructure services.
+
+
+## Cloud Development with Oracle Database
+The following resources are good starting points for those looking to build applications in the Oracle Cloud and deploy applications in Docker containers and Kubernetes.
+
+* [The Complete Guide To Getting Up And Running With Docker And Kubernetes On The Oracle Cloud](https://blogs.oracle.com/developers/post/the-complete-guide-to-getting-up-and-running-with-docker-and-kubernetes-on-the-oracle-cloud).
+
+* [Oracle Cloud Blog](https://www.oc-blog.com/) has lots of interesting information on different aspects of Oracle Cloud.
+
+For developers looking to focus on application development in the Oracle Cloud and not have to worry about managing the Oracle Database, the Autonomous Database is a good choice. All management, including patching and upgrades, scalability, and security, are entirely autonomous. The following resources offer you a glimpse of its capabilities.
+
+* [Julien Dontcheff’s Database Blog](https://juliandontcheff.wordpress.com/category/autonomous/) is a good collection of technical posts with the Autonomous Database.
+
+* [SQL Maria](https://sqlmaria.com/category/autonomous-database/) also has some excellent posts on all things Oracle Database including Autonomous.
+
+* [An Introduction to Autonomous Database](https://questoraclecommunity.org/learn/blogs/oracles-autonomous-database-an-introduction/) gives you a good overview.
+
+* [Autonomous Database for researchers](https://blogs.oracle.com/research/post/a-roadmap-of-oracle-autonomous-database-benefits-for-research) is a good blog with details on some autonomous features.
+
+
+##General Oracle Database Resources
+Here are some Oracle tutorials and resources not specific to Python that can help you take advantage of the Oracle Database features.
+
+* [Oracle Technical Architecture](https://www.oracle.com/webfolder/technetwork/tutorials/architecture-diagrams/18/technical-architecture/database-technical-architecture.html) is from Oracle and has nice visuals and short paragraphs on the architecture of the Oracle Database.
+
+* [Oracle Database Internals](https://databaseinternalmechanism.com/oracle-database-internals/) is an excellent post explaining the architecture of the Oracle Database.
+
+* This [Oracle Performance Tuning](https://blog.quest.com/oracle-performance-tuning-a-5-step-approach-to-optimized-performance/) blog has a 5-step approach to tuning Oracle.
+
+* [Oracle RAC](https://databaseinternalmechanism.com/oracle-rac/) is another good post on the concepts of RAC, Oracle’s Real Application Cluster software for database high availability.
+
+* The [Oracle Database Security](https://www.oracle.com/database/technologies/security.html) web page has lots of information on Oracle’s solutions for security called “defense in depth.”
+
+* This is a good post on the [Top 5 Reasons to choose Oracle](https://www.dbta.com/Editorial/News-Flashes/Top-5-Reasons-to-Use-an-Oracle-Database-144191.aspx) for a production database.
diff --git a/content/pages/04-web-development/00-web-development.markdown b/content/pages/04-web-development/00-web-development.markdown
index 2c5bc5e60..919cc6c94 100644
--- a/content/pages/04-web-development/00-web-development.markdown
+++ b/content/pages/04-web-development/00-web-development.markdown
@@ -21,6 +21,7 @@ in [1989](http://home.cern/topics/birth-web). Web development is the concept
that encompasses all the activities involved with websites and web
applications.
+
## How does Python fit into web development?
Python can be used to build server-side web applications. While a
[web framework](/web-frameworks.html) is not required to build web apps,
@@ -70,29 +71,20 @@ world.
[data bases](/databases.html), [task queues](/task-queues.html),
[caching](/caching.html) and several other critical concepts.
-* The [Evolution of the Web](http://www.evolutionoftheweb.com/) visualizes
- how web browsers and related technologies have changed over time as well as
- the overall growth of the Internet in the amount of data transferred. Note
- that the visualization unfortunately stops around the beginning of 2013 but
- it's a good way to explore what happened in the first 24 years.
+* [What happens when?](https://github.com/alex/what-happens-when) is an
+ incredibly detailed answer to the questions "What happens when you
+ type google.com into your browser's address box and press enter?" that
+ seems straightforward on the surface until you really dig in.
* [How browsers work](https://www.html5rocks.com/en/tutorials/internals/howbrowserswork/)
provides an overview with solid detail on how browsers take the HTML,
CSS, JavaScript, images and other files as input and render webpages as
output. It is well worth your time to know this stuff as a web developer.
-* [Web app checklist](http://dhilipsiva.com/webapp-checklist/) presents
- good practices that developers building and [deploying](/deployment.html)
- web applications should follow. Don't worry about having every single
- one of these recommendations implemented before getting your site
- live, but it is worthwhile to review the list to make sure there is not
- something obvious you can handle in a few minutes that will improve
- your site's security, performance or usability.
-
-* [Web application development is different and better](http://radar.oreilly.com/2014/01/web-application-development-is-different-and-better.html)
- provides some context for how web development has evolved from writing
- static HTML files into the complex JavaScript client-side applications
- produced today.
+* [The history of the URL](https://blog.cloudflare.com/the-history-of-the-url/)
+ explains how the growth of ARPANET to hundreds of nodes eventually led to
+ the creation of the URL. This is a great read that provides historical
+ context for why things are the way they are with the web.
* [The Browser Hacker's Guide to Instantly Loading Everything](https://www.youtube.com/watch?v=7vUs5yOuv-o)
is a spectacular technical talk given by Addy Osmani at JSConf EU 2017
diff --git a/content/pages/04-web-development/01-web-frameworks.markdown b/content/pages/04-web-development/01-web-frameworks.markdown
index 6b52e3680..2adde9c83 100644
--- a/content/pages/04-web-development/01-web-frameworks.markdown
+++ b/content/pages/04-web-development/01-web-frameworks.markdown
@@ -90,15 +90,15 @@ but it'll make most developers' lives easier in many cases.
### Comparing web frameworks
+[Talk Python to Me had a podcast episode](https://talkpython.fm/episodes/show/149/4-python-web-frameworks-compared)
+with a detailed comparison of the [Django](/django.html),
+[Flask](/flask.html), Tornado and [Pyramid](/pyramid.html) frameworks.
+
Are you curious about how the code in a Django project is structured compared
with Flask? Check out
[this Django web application tutorial](https://www.twilio.com/docs/sms/tutorials/appointment-reminders-python-django)
and then view [the same application built with Flask](https://www.twilio.com/docs/sms/tutorials/appointment-reminders-python-flask).
-[Talk Python to Me had a podcast episode](https://talkpython.fm/episodes/show/149/4-python-web-frameworks-compared)
-with a detailed comparison of the [Django](/django.html),
-[Flask](/flask.html), Tornado and [Pyramid](/pyramid.html) frameworks.
-
There is also a repository called
[compare-python-web-frameworks](https://github.com/mattmakai/compare-python-web-frameworks)
where the same web application is being coded with varying Python web
@@ -112,6 +112,18 @@ frameworks, templating engines and
and the many other pieces that combine to make web frameworks useful
to web developers.
+* [12 requests per second](https://suade.org/dev/12-requests-per-second-with-python/)
+ examines how the traditionally synchronous web framework
+ [Flask](/flask.html) compares to an async framework like
+ [Sanic](/sanic.html) in an artificial, simple benchmark. The
+ results make it look like Sanic is far faster than Flask, but
+ once you add even a basic amount of functionality to a
+ project, including [databasel](/databases.html) queries
+ and templating, the results even out. Miguel Grinberg
+ also has a great read with broader results in this
+ article asking readers to
+ [Ignore All Web Performance Benchmarks, Including This One](https://blog.miguelgrinberg.com/post/ignore-all-web-performance-benchmarks-including-this-one).
+
* When you are learning how to use one or more web frameworks it's helpful
to have an idea of what the code under the covers is doing. This post on
building a
@@ -150,10 +162,6 @@ frameworks, templating engines and
I agree although I've found sessions and database ORMs to be a helpful
part of a framework when done well.
-* "[What is a web framework?](http://www.jeffknupp.com/blog/2014/03/03/what-is-a-web-framework/)"
- is an in-depth explanation of what web frameworks are and their relation
- to web servers.
-
* [Django vs Flask vs Pyramid: Choosing a Python Web Framework](https://www.airpair.com/python/posts/django-flask-pyramid)
contains background information and code comparisons for similar
web applications built in these three big Python frameworks.
diff --git a/content/pages/04-web-development/02-django.markdown b/content/pages/04-web-development/02-django.markdown
index 263a6daef..3061d2eee 100644
--- a/content/pages/04-web-development/02-django.markdown
+++ b/content/pages/04-web-development/02-django.markdown
@@ -21,7 +21,7 @@ For example,
[template engine](/django-templates.html),
an [object-relational mapper](/object-relational-mappers-orms.html) (ORM),
and [database schema migrations](https://docs.djangoproject.com/en/dev/topics/migrations/)
-(as of version 1.7) are all included with the [Django framework](https://pypi.org/project/Django/).
+are all included with the [Django framework](https://pypi.org/project/Django/).
Compare that included functionality to the Flask framework which requires a
separate library such as
[Flask-Login](https://flask-login.readthedocs.org/en/latest/)
@@ -50,11 +50,6 @@ groups such as [Django District](http://www.meetup.com/django-district/),
[San Francisco Django](http://www.meetup.com/The-San-Francisco-Django-Meetup-Group/)
so new developers can get help when they are stuck.
-There's some debate on whether
-[learning Python by using Django is a bad idea](http://www.jeffknupp.com/blog/2012/12/11/learning-python-via-django-considered-harmful/).
-However, that criticism is invalid if you take the time to learn the Python
-syntax and language semantics first before diving into web development.
-
## Django books and tutorials
There are a slew of free or low cost resources out there for Django. Make
@@ -126,7 +121,7 @@ These books and tutorials assume that you know the basics of building
Django and want to go further to become much more knowledgeable about
the framework.
-* [2 Scoops of Django](https://www.twoscoopspress.com/collections/django/products/two-scoops-of-django-1-11)
+* [2 Scoops of Django](https://www.feldroy.com/books/two-scoops-of-django-3-x)
by Daniel Greenfeld and Audrey Roy is well worth the price of admission if
you're serious about learning how to correctly develop Django websites.
@@ -135,22 +130,35 @@ the framework.
order to build and deploy a RESTful API powered by Python, Django,
and Django REST Framework.
+* [User Interaction With Forms](https://www.mattlayman.com/understand-django/user-interaction-forms/)
+ explains general web form input, how Django handles forms via POST requests,
+ different types of input such as CharFields, DateFields and EmailFields,
+ and validating that input.
+
* This 3-part Django project optimization guide covers a wide range of
advanced topics such as
[Profiling and Django settings](https://dizballanze.com/django-project-optimization-part-1/),
[working with databases](https://dizballanze.com/django-project-optimization-part-2/)
and [caching](https://dizballanze.com/django-project-optimization-part-3/).
-* This tutorial shows how to create
- [a Django web app that can make phone calls and send text messages for automated surveys](https://www.twilio.com/docs/tutorials/walkthrough/automated-survey/python/django).
- The code is a really good example of a full Django project and is also
- available
- [open source on GitHub](https://github.com/TwilioDevEd/automated-survey-django).
+* [Caching in Django](https://testdriven.io/blog/django-caching/) is a detailed
+ look at the configuration required for caching and how to measure the
+ performance improvements once you have it in place.
+
+* [Mental Models for Class Based Views](https://djangodeconstructed.com/2020/01/03/mental-models-for-class-based-views/)
+ provides some comparison points between class based views (CBVs) and
+ function based views and the author's opinions for how you can better
+ understand CBVs.
* Working with time zones is necessary for every web application. This
[blog post on pytz and Django](http://tommikaikkonen.github.io/timezones/) is a
great start for figuring out what you need to know.
+* [A Guide to ASGI in Django 3.0 and its Performance](https://arunrocks.com/a-guide-to-asgi-in-django-30-and-its-performance/)
+ covers the new Asynchronous Server Gateway Interface (ASGI) that was
+ introduced in Django 3.0 and explains some of the nuances and gotchas
+ that you should consider if you decide to use it for your web apps.
+
* [REST APIs with Django: Build powerful web APIs with Python and Django](https://www.amazon.com/dp/198302998X)
by [William S. Vincent](https://wsvincent.com/) is the book for you
if you are just moving beyond the basics of Django and looking to get
@@ -226,6 +234,7 @@ the framework.
address can be used as the primary user identifier instead of a
username for authentication.
+
## Django Channels
Channels are a new mechanism in Django 1.9 provided as a standalone app.
They may be incorporated into the core framework in 2.0+. Channels provide
@@ -252,6 +261,11 @@ based on [WebSockets](/websockets.html).
is a good post to read for figuring out how to use [Gulp](http://gulpjs.com/)
for handling front end tools in development and production Django sites.
+* [Django Testing Cheat Sheet](https://www.valentinog.com/blog/testing-django/)
+ covers many common scenarios for Django applications such as testing
+ POST requests, request headers, authentication, and large numbers of
+ model fields in the [Django ORM](/django-orm.html).
+
* [Getting Started with Django Testing](http://howchoo.com/g/mjkwmtu5zdl/getting-started-with-django-testing)
will help you stop procrastinating on testing your Django projects if you're
uncertain where to begin.
@@ -296,33 +310,6 @@ to handle these files properly.
shows how to configure Django to load and serve up static and media files, public and private, via an Amazon S3 bucket.
-## Open source Django example projects
-* [Browser calls with Django and Twilio](https://www.twilio.com/docs/howto/walkthrough/browser-calls/python/django)
- shows how to build a web app with Django and
- [Twilio Client](https://www.twilio.com/client) to turn a user's web
- browser into a full-fledged phone. Pretty awesome!
-
-* [Txt 2 React](https://github.com/mattmakai/txt2react) is a full Django web
- app that allows audiences to text in during a presentation with feedback
- or questions.
-
-* [Openduty](https://github.com/ustream/openduty) is a website status checking
- and alert system similar to PagerDuty.
-
-* [Courtside](https://github.com/myusuf3/courtside) is a pick up sports web
- application written and maintained by the author of PyCoder's Weekly.
-
-* These two Django Interactive Voice Response (IVR) system web application
- repositories [part 1](https://github.com/phalt/twilio-django-part-1) and
- [part 2](https://github.com/phalt/twilio-django-part-2) show you how to
- build a really cool Django application. There's also an accompanying
- [blog post](https://www.twilio.com/blog/2014/07/build-an-ivr-system-with-twilio-and-django.html)
- with detailed explanations of each step.
-
-* [Taiga](https://github.com/taigaio/taiga-back) is a project management
- tool built with Django as the backend and AngularJS as the front end.
-
-
## Django project templates
Project templates, not to be confused with a
[template engine](/template-engines.html), generate boilerplate code for
@@ -330,7 +317,7 @@ a base Django project plus optional libraries that are often used when
developing web applications.
* [Caktus Group's Django project template](https://github.com/caktus/django-project-template)
- is Django 1.6+ ready.
+ is Django 2.2+ ready.
* [Cookiecutter Django](https://github.com/pydanny/cookiecutter-django) is a
project template from Daniel Greenfeld, for use with Audrey Roy's
@@ -345,25 +332,35 @@ developing web applications.
template from Mozilla that is compatible with cookiecutter.
-## Django learning checklist
-1. [Install Django](https://docs.djangoproject.com/en/dev/topics/install/) on
- your local development machine.
+## Open source Django example projects
+Reading open source code can be useful when you are trying to figure
+out how to build your own projects. This is a short list of some
+real-world example applications, and many more can be found on the
+[Django example projects and code](/django-code-examples.html) page.
+
+* [Openduty](https://github.com/ustream/openduty) is a website status checking
+ and alert system similar to PagerDuty.
+
+* [Courtside](https://github.com/myusuf3/courtside) is a pick up sports web
+ application written and maintained by the author of PyCoder's Weekly.
-1. Work through the initial
- ["polls" tutorial](https://docs.djangoproject.com/en/dev/intro/tutorial01/).
-
-1. Build a few more simple applications using the tutorial resources found
- in the "Django resources" section.
+* These two Django Interactive Voice Response (IVR) system web application
+ repositories [part 1](https://github.com/phalt/twilio-django-part-1) and
+ [part 2](https://github.com/phalt/twilio-django-part-2) show you how to
+ build a really cool Django application. There's also an accompanying
+ [blog post](https://www.twilio.com/blog/2014/07/build-an-ivr-system-with-twilio-and-django.html)
+ with detailed explanations of each step.
-1. Start coding your own Django project with help from the
- [official documentation](https://docs.djangoproject.com/en/dev/) and
- resource links below. You'll make plenty of mistakes which is critical
- on your path to learning the right way to build applications.
+* [Taiga](https://github.com/taigaio/taiga-back) is a project management
+ tool built with Django as the backend and AngularJS as the front end.
-1. Read [2 Scoops of Django](http://www.amazon.com/Two-Scoops-Django-Best-Practices/dp/0981467342)
- to understand Django good practices and learn better ways of building
- Django web applications.
+* [Chowist](https://github.com/huangsam/chowist) is a web application
+ that replicates core features of Yelp and adds a couple more bells
+ and whistles.
-1. Move on to the [deployment section](/deployment.html) to get your Django
- project on the web.
+## Open source code to learn Django
+There are many open source projects that rely on Django.
+One of the best ways to learn how to use this framework is to read
+how other projects use it in real-world code. This section lists
+these code examples by class and method in Django's code base.
diff --git a/content/pages/04-web-development/03-flask.markdown b/content/pages/04-web-development/03-flask.markdown
index 29bc0212a..fd7625f0e 100644
--- a/content/pages/04-web-development/03-flask.markdown
+++ b/content/pages/04-web-development/03-flask.markdown
@@ -181,6 +181,11 @@ combined with the example real-world projects listed in the next section.
[Docker](/docker.html). It shows how to [deploy](/deployment.html) it to an
Amazon EC2 instance then scale the services on Amazon EC2 Container Service (ECS).
+* [Build a Video Chat Application with Python, JavaScript and Twilio Programmable Video](https://www.twilio.com/blog/build-video-chat-application-python-javascript-twilio-programmable-video)
+ shows how to use [Twilio](/twilio.html)
+ [Programmable Video](https://www.twilio.com/video) to build cross-platform
+ (web, iOS and Android) video into Flask applications.
+
* [Why and how to handle exceptions in Python Flask](https://opensource.com/article/17/3/python-flask-exceptions)
has some great example code and reasons why you should code defensively
by anticipating and handling the unhappy path exceptions in your Flask
@@ -200,9 +205,6 @@ combined with the example real-world projects listed in the next section.
is a killer Flask tutorial with all the code needed to create a
web app that can dial phones and receive inbound calls.
-* Jeff Knupp provides some solid advice on how to
- [productionize a Flask app](http://www.jeffknupp.com/blog/2014/01/29/productionizing-a-flask-application/).
-
* If you're looking for a fun tutorial with Flask and WebSockets, check out
my blog post on creating
[Choose Your Own Adventure Presentations with Reveal.js, Python and WebSockets](https://www.twilio.com/blog/2014/11/choose-your-own-adventure-presentations-with-reveal-js-python-and-websockets.html).
@@ -293,7 +295,7 @@ about how to working on your codebase.
* [Bean Counter](https://github.com/BouncyNudibranch/bean-counter) is an
open source Flask app for tracking coffee.
-* [FlaskBB](http://flaskbb.org/) is a Flask app for a discussion forum.
+* [FlaskBB](https://flaskbb.org/) is a Flask app for a discussion forum.
* [psdash](https://github.com/Jahaja/psdash) is an app built with Flask and
psutils to display information about the computer it is running on.
@@ -332,20 +334,8 @@ ways to structure your code.
in the JavaScript world.
-### Flask framework learning checklist
-1. [Install Flask](http://flask.pocoo.org/docs/installation/) on
- your local development machine.
-
-1. Work through the 18-part Flask tutorial listed first under "Flask
- resources" above.
-
-1. Read through
- [Flask Extensions Registry](http://flask.pocoo.org/extensions/) to find
- out what extensions you'll need to build your project.
-
-1. Start coding your Flask app based on what you learned from the 18 part
- Flask tutorial plus open source example applications found below.
-
-1. Move on to the [deployment section](/deployment.html) to get your initial
- Flask project on the web.
-
+## Open source code for learning Flask
+There are many open source projects that rely on Flask to operate.
+One of the best ways to learn how to use this framework is to read
+how other projects use it in real-world code. This section lists
+these code examples by class and method in Flask.
diff --git a/content/pages/04-web-development/04-bottle.markdown b/content/pages/04-web-development/04-bottle.markdown
index 1f40db51c..01e543da4 100644
--- a/content/pages/04-web-development/04-bottle.markdown
+++ b/content/pages/04-web-development/04-bottle.markdown
@@ -29,7 +29,7 @@ Bottle is awesome for a few web development situations:
Prototyping simple ideas is often easier with Bottle than a more
opinionated web framework like [Django](/django.html) because Django
projects start with a significant amount of boilerplate code. The
-[Model-View-Template](https://docs.djangoproject.com/en/1.9/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names)
+[Model-View-Template](https://docs.djangoproject.com/en/stable/intro/tutorial03/)
structure for Django apps within projects makes maintaining projects
easier, but it can be cumbersome on starter projects where you're
just playing with random ideas so you aren't worried about your
diff --git a/content/pages/04-web-development/11-template-engines.markdown b/content/pages/04-web-development/11-template-engines.markdown
index f8b7bac06..551a70bfb 100644
--- a/content/pages/04-web-development/11-template-engines.markdown
+++ b/content/pages/04-web-development/11-template-engines.markdown
@@ -161,6 +161,10 @@ know how they work to aid your debugging. The following resources examine
existing template engine design as well as how to build your own engine
when that's necessary for your projects.
+* [Writing a Jinja-inspired template library in Python](https://notes.eatonphil.com/writing-a-template-library-in-python.html)
+ walks through how to create your own a simplified version of the
+ [Jinja](/jinja2.html) template engine as a learning exercise.
+
* [How a template engine works](https://fengsp.github.io/blog/2016/8/how-a-template-engine-works/)
uses the template module in Tornado as an example to step through how
a template engine produces output, from parsing the incoming string to
diff --git a/content/pages/04-web-development/12-jinja2.markdown b/content/pages/04-web-development/12-jinja2.markdown
index 23137b27d..75feb4fdc 100644
--- a/content/pages/04-web-development/12-jinja2.markdown
+++ b/content/pages/04-web-development/12-jinja2.markdown
@@ -8,17 +8,17 @@ meta: Jinja2 is a template engine written in Python for outputting formats such
Jinja, also commonly referred to as
-"[Jinja2](http://jinja.pocoo.org/docs/dev/)" to specify the newest
+"[Jinja2](https://jinja.palletsprojects.com/en/3.0.x/templates/)" to specify the newest
release version, is a Python [template engine](/template-engines.html)
used to create HTML, XML or other markup formats that are returned to the
user via an HTTP response.
-
+
## Why is Jinja2 useful?
Jinja2 is useful because it has consistent template tag syntax and the
project is cleanly extracted as
-[an independent open source project](https://github.com/mitsuhiko/jinja2) so
+[an independent open source project](https://github.com/pallets/jinja) so
it can be used as a dependency by other code libraries.
@@ -31,9 +31,8 @@ end a developer can code whatever she wants.
## Jinja2 origin and development
The first recorded public released of Jinja2 was in
-[2008 with 2.0rc1](http://jinja.pocoo.org/docs/dev/changelog/#version-2-0rc1).
-Since then the engine has seen numerous updates and remains in active
-development.
+2008 with 2.0rc1. Since then the engine has seen numerous updates and
+remains under active development.
Jinja2 engine certainly wasn't the first template engine. In fact, Jinja2's
syntax is inspired by Django's built-in template engine, which was released
@@ -72,7 +71,7 @@ open source project author from having to reinvent a new templating style.
the template engine.
* The official
- [Jinja2 template designer documentation](http://jinja.pocoo.org/docs/dev/templates/)
+ [Jinja2 templates documentation](https://flask.palletsprojects.com/en/2.0.x/tutorial/templates/)
is exceptionally useful both as a reference as well as a full read-through
to understand how to properly work with template tags.
diff --git a/content/pages/04-web-development/15-web-design.markdown b/content/pages/04-web-development/15-web-design.markdown
index b02e8b4ab..58e9cc31f 100644
--- a/content/pages/04-web-development/15-web-design.markdown
+++ b/content/pages/04-web-development/15-web-design.markdown
@@ -21,6 +21,27 @@ Creating web pages with their own style and interactivity so users can easily
accomplish their tasks is a major part of building modern web applications.
+## Getting started if you have no "eye" for design
+Design can feel like something "creative" people understand intuitively,
+but like all skills design is something that can be learned. Some people
+are faster learners in design just like some folks are quicker in
+picking up programming. But anyone can learn how to be a better designer
+by learning the basic principles and practicing them.
+
+One of the best mental models for basic design is C.R.A.P., which helped me
+grasp why some designs look good while others do not. CRAP is an acronym
+for:
+
+ * Contrast: noticeable differences from one element to another
+ * Repetition: elements' consistency
+ * Alignment: order among all elements
+ * Proximity: placement between elements and how they are organized
+
+These basic principles all you to start breaking down the problem into
+digestible pieces that you can work on rather than feeling like you
+"just don't have an eye for design".
+
+
## Designing for various screen sizes
Separating the content from the rules for how to display the content allows
devices to render the output differently based on factors such as screen size
@@ -50,7 +71,7 @@ this short list as my absolute favorites that help developers become
page elements.
* [Resilient web design](https://resilientwebdesign.com/) is an incredible
- online book that teaches how to create websitess that are accessible to
+ online book that teaches how to create websites that are accessible to
every reader and look great while doing it.
* [Design 101 for Developers](https://academy.realm.io/posts/christopher-downer-design-101-for-developers/)
@@ -62,14 +83,19 @@ this short list as my absolute favorites that help developers become
design principles for building user experiences. Highly recommended even
if just to see how the information is presented.
+* [Building your color palette](https://refactoringui.com/previews/building-your-color-palette/)
+ explains why color pickers are not useful for most user interfaces
+ and how you should actually go about selecting your color palette
+ for a real world application.
+
* [How I Work with Color](https://medium.com/@JustinMezzell/how-i-work-with-color-8439c98ae5ed)
is a fantastic article from a professional designer on how he thinks
about color and uses it for certain effects in his designs.
-* The [Bootstrapping Design](http://bootstrappingdesign.com/) book is one of
- the clearest and concise resources for learning design that I've ever read.
- Highly recommended especially if you feel you have no design skills but
- need to learn them.
+* [Building dark mode on Stack Overflow](https://stackoverflow.blog/2020/03/31/building-dark-mode-on-stack-overflow/)
+ explains the thought process and work that the team at Stack Overflow
+ had to do with colors, styling and the code implementation so they
+ could offer a dark mode design to their users.
* [A short history of body copy sizes on the Web](https://fvsch.com/body-copy-sizes/)
provides a useful examination of how originally the web's typography
@@ -92,6 +118,14 @@ this short list as my absolute favorites that help developers become
examples and further details for the four principles, which are contrast,
consistency, Occam's Razor and space.
+* [Gallery of web design history](https://www.webdesignmuseum.org/gallery) is
+ a collection of websites from between 1991 and 2006 that show the evolution
+ of what the web looked like before the modern
+ [CSS](/cascading-style-sheets.html) era. This is a great resource to see
+ how websites evolved, such as
+ [Microsoft in 1996](https://www.webdesignmuseum.org/gallery/microsoft-1996)
+ and [YouTube in 2005](https://www.webdesignmuseum.org/gallery/youtube-2005).
+
* [The Average Web Page (Data from Analyzing 8 Million Websites)](https://css-tricks.com/average-web-page-data-analyzing-8-million-websites/)
shows the most frequently used HTML elements, metadata, text
content and other statistics from a large scale analysis of the web.
@@ -99,6 +133,11 @@ this short list as my absolute favorites that help developers become
* [How to design delightful dark themes](https://blog.superhuman.com/how-to-design-delightful-dark-themes-7b3da644ff1f)
explains some subtle tactics to make dark themes work well for users.
+* [Setting height and width on images is important again](https://www.smashingmagazine.com/2020/03/setting-height-width-images-important-again/)
+ describes how browser layout and resizing settings could affect your
+ images so manually setting the size is more useful than it has
+ been in the past few years.
+
* [Kuler](https://kuler.adobe.com/create/color-wheel/) is a complementary
color picker by Adobe that helps choose colors for your designs.
@@ -108,6 +147,10 @@ this short list as my absolute favorites that help developers become
[blog post series on building a browser engine](http://limpet.net/mbrubeck/2014/08/08/toy-layout-engine-1.html)
that will show you how to build a simple rendering engine.
+* [How to Use C.R.A.P. Design Principles for Better UX](https://vwo.com/blog/crap-design-principles/)
+ has a good summary of what contrast, repetition, alignment and
+ proximity means for designing user interfaces.
+
* [Defining Colors in CSS](https://pineco.de/defining-colors-in-css/)
presents how to define color in your
[Cascading Style Sheets (CSS)](/cascading-style-sheets.html) and breaks
diff --git a/content/pages/04-web-development/16-html.markdown b/content/pages/04-web-development/16-html.markdown
index a1ae84404..6db9c57b0 100644
--- a/content/pages/04-web-development/16-html.markdown
+++ b/content/pages/04-web-development/16-html.markdown
@@ -46,3 +46,8 @@ and is used to display website and web application content.
extensively covers what might be thought of as a simple topic: the
`a` and `button` elements in HTML, along with their many attributes
and quirks.
+
+* [HTML: the inaccessible parts](https://daverupert.com/2020/02/html-the-inaccessible-parts/)
+ explains how even basic HTML elements can cause accessibility issues
+ for screen readers and other devices that help people with impairments
+ to use the web.
diff --git a/content/pages/04-web-development/17-css.markdown b/content/pages/04-web-development/17-css.markdown
index f7d3658b2..d50968d09 100644
--- a/content/pages/04-web-development/17-css.markdown
+++ b/content/pages/04-web-development/17-css.markdown
@@ -119,6 +119,14 @@ web application's design.
provides advice on how to write simpler, easier-to-maintain CSS code
to reduce your need to rely on CSS preprocessors and build pipelines.
+* [How to Detect Unused CSS or JavaScript](https://javascript.plainenglish.io/detect-unused-css-or-javascript-in-your-code-8d200ef07e50)
+ explains how to use [Chrome DevTools](https://developer.chrome.com/docs/devtools/)
+ to analyze a page's CSS and identify parts that are not used. Note that
+ even though a specific page does not use that CSS (or JS), there might
+ be another page that uses the same CSS files and *does* use that "unused"
+ code, so test your pages before and after making the changes to ensure
+ you did not inadvertently break something else!
+
* [CSS refresher notes](https://github.com/vasanthk/css-refresher-notes) is
incredibly helpful if you've learned CSS in bits and pieces along the way
and you now want to fill in the gaps in your knowledge.
@@ -149,10 +157,6 @@ web application's design.
* [Google's Web Fundamentals class](https://developers.google.com/web/fundamentals/)
shows how to create responsive designs and performant websites.
-* [Tailoring CSS for performance](http://programming.oreilly.com/2014/04/tailoring-css-for-performance.html)
- is an interesting read since many developers do not consider the
- implications of CSS complexity in browser rendering time.
-
* [Can I Use...](http://caniuse.com/) is a compatibility table that shows
which versions of browsers implement specific CSS features.
@@ -186,16 +190,6 @@ web application's design.
talk about tables, because that was the only way to position anything
back in the day.
-* [30 seconds of CSS](https://30-seconds.github.io/30-seconds-of-css/)
- provides short useful code snippets for you to learn from and use for
- building your own web applications.
-
-* [CSS: The bad bits](https://www.joeforshaw.com/blog/css-the-bad-bits-and-how-to-avoid-them)
- examines global scope, implicit percentage styling rules and the z-index
- which can be difficult to use and require some restraint to ensure they
- do not cause issues for the rest of your stylesheet rules as you create
- and maintain your frontend.
-
* [Improving Your CSS with Parker](https://csswizardry.com/2016/06/improving-your-css-with-parker/)
shows how to use the static CSS analysis tool
[Parker](https://github.com/katiefenn/parker/) to improve your stylesheets.
diff --git a/content/pages/04-web-development/20-css-frameworks.markdown b/content/pages/04-web-development/20-css-frameworks.markdown
index 01f12984b..de0dbe3b2 100644
--- a/content/pages/04-web-development/20-css-frameworks.markdown
+++ b/content/pages/04-web-development/20-css-frameworks.markdown
@@ -12,7 +12,7 @@ a code library that abstracts common web designs and makes the designs
easier for a developer to implement in their web apps.
-### CSS frameworks
+### CSS framework implementations
* [Bootstrap](/bootstrap-css.html)
* [Foundation](/foundation-css.html)
diff --git a/content/pages/04-web-development/21-bootstrap.markdown b/content/pages/04-web-development/21-bootstrap.markdown
index 8ca1c917f..97c4db1fa 100644
--- a/content/pages/04-web-development/21-bootstrap.markdown
+++ b/content/pages/04-web-development/21-bootstrap.markdown
@@ -47,6 +47,9 @@ specific situation.
is an awesome tutorial that goes through many of the main Bootstrap
elements such as icons, navigation bars and "jumbotron"-style webpages.
+* [Bootstrap 5 tutorial](https://www.youtube.com/watch?v=c9B4TPnak1A) covers
+ the alpha version of the fifth major revision for Bootstrap.
+
* [Bootstrap 4: What's New?](https://medium.com/wdstack/bootstrap-4-whats-new-visual-guide-c84dd81d8387)
shows a slew of comparisons for how user interface elements have changed
design over the past several Bootstrap major version iterations.
diff --git a/content/pages/04-web-development/23-javascript.markdown b/content/pages/04-web-development/23-javascript.markdown
index d23bc1692..8440dcc4f 100644
--- a/content/pages/04-web-development/23-javascript.markdown
+++ b/content/pages/04-web-development/23-javascript.markdown
@@ -46,6 +46,10 @@ JavaScript is an implementation of
[the ECMAScript specification](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/JavaScript_Overview)
which is defined by the
[Ecma International Standards Body](http://www.ecma-international.org/default.htm).
+Read this paper on
+[the first 20 years of JavaScript](https://zenodo.org/record/3710954)
+by Brendan Eich, the original creator of the programming language, and
+Allen Wirfs-Brock for more context on the language's evolution.
### JavaScript resources
@@ -61,6 +65,12 @@ which is defined by the
* [A re-introduction to JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
by Mozilla walks through the basic syntax and operators.
+* [The Cost of Javascript Frameworks](https://timkadlec.com/remembers/2020-04-21-the-cost-of-javascript-frameworks/)
+ presents a balanced view of how JavaScript libraries and frameworks impact
+ web performance on both desktop and mobile. The author acknowledges that
+ these libraries are useful for developers but in extreme cases there are
+ significant downsides to including so much JavaScript in pages.
+
* [The State of JavaScript report](https://stateofjs.com/) contains a wealth
of data on what libraries developers are using in the JavaScript
ecosystem. There are also reports from previous years which show how the
@@ -86,7 +96,13 @@ which is defined by the
tools are that you can use and some comparisons when deciding between
Angular and React.
-* [Developing a Single Page App with Flask and Vue.js](https://testdriven.io/developing-a-single-page-app-with-flask-and-vuejs) step-by-step walkthrough of how to set up a basic CRUD app with Vue and Flask.
+* [Developing a Single Page App with Flask and Vue.js](https://testdriven.io/developing-a-single-page-app-with-flask-and-vuejs)
+ is a step-by-step walkthrough of how to set up a basic CRUD app with
+ [Vue.js](/vuejs.html) and [Flask](/flask.html).
+
+* [A Guide to Console Commands](https://css-tricks.com/a-guide-to-console-commands/)
+ shows off what JavaScript commands you can use in your browser's console,
+ which is a typical debugging pattern for JavaScript development.
* [is-website-vulnerable](https://github.com/lirantal/is-website-vulnerable)
is an open source tool that identifies security vulnerabilities based on
@@ -114,6 +130,15 @@ which is defined by the
* [Learn JS data](http://learnjsdata.com/) teaches how to manipulate data
using JavaScript in a browser or on the server using Node.js.
+* [A Beginner's Guide to JavaScript's Prototype](https://ui.dev/beginners-guide-to-javascript-prototype/)
+ explains the fundamentals of JavaScript's object system, which is
+ a prototype-based model and different from many other common
+ programming languages' object models.
+
+* [Understanding Data Types in JavaScript](https://www.digitalocean.com/community/tutorials/understanding-data-types-in-javascript)
+ examines JavaScript's dynamic data type model and how it manifests
+ in the way numbers, string, Booleans and arrays are used.
+
### JavaScript learning checklist
1. Create a simple HTML file with basic elements in it. Use the
diff --git a/content/pages/04-web-development/24-react.markdown b/content/pages/04-web-development/24-react.markdown
index 18ca6555b..518d5e929 100644
--- a/content/pages/04-web-development/24-react.markdown
+++ b/content/pages/04-web-development/24-react.markdown
@@ -35,6 +35,27 @@ tack on React to build your client-side user interfaces.
quickly fall out of date while this one tends to stick to the basics
that are relevant to beginners.
+* [9 things every React.js beginner should know](https://camjackson.net/post/9-things-every-reactjs-beginner-should-know)
+ is not a tutorial but instead the author gives some strong opinions for
+ what beginners should know as they start learning React.
+
+* [React Bootstrap](https://react-bootstrap.github.io/)
+ ([source code](https://github.com/react-bootstrap/react-bootstrap) replaces
+ the existing Bootstrap JavaScript with React components that do not
+ rely on jQuery.
+
+
+## Python+React tutorials
+* [How to set up Django with React](https://mattsegal.dev/django-react.html)
+ presents one developer's opinionated way of combining a
+ [Django](/django.html)-powered back end with React on the front end,
+ including how to serve up static assets.
+
+* [Django REST with React (Django 2.0 and a sprinkle of testing)](https://www.valentinog.com/blog/tutorial-api-django-rest-react/)
+ combines a [Django](/django.html) plus
+ [Django REST Framework (DRF)](/django-rest-framework-drf.html) backend
+ with React on the front end and shows how to stich it all together.
+
* This Modern Django 4-part tutorial series is well-done, has
[freely available source code](https://github.com/v1k45/ponynote)
and includes:
@@ -44,10 +65,6 @@ tack on React to build your client-side user interfaces.
3. [Creating an API and integrating with React](http://v1k45.com/blog/modern-django-part-3-creating-an-api-and-integrating-with-react/)
4. [Adding authentication to React SPA using DRF](http://v1k45.com/blog/modern-django-part-4-adding-authentication-to-react-spa-using-drf/)
-* [Django REST with React (Django 2.0 and a sprinkle of testing)](https://www.valentinog.com/blog/tutorial-api-django-rest-react/)
- combines a [Django](/django.html) plus
- [Django REST Framework (DRF)](/django-rest-framework-drf.html) backend
- with React on the front end and shows how to stich it all together.
* [Build a Simple CRUD App with Python, Flask, and React](https://developer.okta.com/blog/2018/12/20/crud-app-with-python-flask-react)
shows how to combine a [Flask](/flask.html) backend with React.
@@ -56,15 +73,6 @@ tack on React to build your client-side user interfaces.
is a Git repository with a code tutorial and instructions for how to
follow along, as well as exercises to ensure you are tested as you go.
-* [9 things every React.js beginner should know](https://camjackson.net/post/9-things-every-reactjs-beginner-should-know)
- is not a tutorial but instead the author gives some strong opinions for
- what beginners should know as they start learning React.
-
-* [React Bootstrap](https://react-bootstrap.github.io/)
- ([source code](https://github.com/react-bootstrap/react-bootstrap) replaces
- the existing Bootstrap JavaScript with React components that do not
- rely on jQuery.
-
### Other React resources
* [React interview questions](https://tylermcginnis.com/react-interview-questions/)
diff --git a/content/pages/04-web-development/25-vuejs.markdown b/content/pages/04-web-development/25-vuejs.markdown
index 73c979499..934f720b0 100644
--- a/content/pages/04-web-development/25-vuejs.markdown
+++ b/content/pages/04-web-development/25-vuejs.markdown
@@ -34,6 +34,11 @@ rich apps that run in web browsers.
### Vue.js resources
+* [Building web apps with Vue and Django](https://dafoster.net/articles/2021/02/16/building-web-apps-with-vue-and-django-the-ultimate-guide/)
+ covers architectural decisions such as whether to use one or
+ two servers and then explains how to go down the one server
+ route with a [Django](/django.html) back end.
+
* [A friendly introduction to Vue.js](https://appendto.com/2016/11/a-friendly-introduction-to-vue-js/)
contains the code and brief explanations of what it's doing so you can
learn to create your first Vue app.
@@ -43,6 +48,11 @@ rich apps that run in web browsers.
coding you need to do to build a legitimate Vue.js application that
uses a [Flask](/flask.html) API on the backend.
+* [Building a SaaS using Django and Vue.js](https://www.youtube.com/playlist?list=PLpyspNLjzwBnGesxJOt_0r4xTWR80j7Y3)
+ shows how to create a software-as-a-service application with a
+ [Django](/django.html) back end and Vue on the front end via a
+ series of videos that are each between 10-15 minutes in length.
+
* [Learn Vue by Building and Deploying a CRUD App](https://testdriven.io/courses/learn-vue/?utm_source=fsp)
is an awesome course that walks through the Vue basics and building
a whole application and deploying it to
diff --git a/content/pages/04-web-development/27-task-queues.markdown b/content/pages/04-web-development/27-task-queues.markdown
index 073759f6e..70ec128c2 100644
--- a/content/pages/04-web-development/27-task-queues.markdown
+++ b/content/pages/04-web-development/27-task-queues.markdown
@@ -100,13 +100,6 @@ when scaling out a large deployment of distributed task queues.
## Open source examples that use task queues
-* Take a look at the code in this open source
- [Flask application](https://www.twilio.com/docs/howto/walkthrough/appointment-reminders/python/flask)
- and
- [this Django application](https://www.twilio.com/docs/howto/walkthrough/appointment-reminders/python/django)
- for examples of how to use and deploy Celery with a Redis broker to
- send text messages with these frameworks.
-
* [flask-celery-example](https://github.com/thrisp/flask-celery-example) is
a simple Flask application with Celery as a task queue and Redis as
the broker.
@@ -128,10 +121,6 @@ when scaling out a large deployment of distributed task queues.
is a detailed comparison of Amazon SQS, MongoDB, RabbitMQ, HornetQ and
Kafka's designs and performance.
-* [Queues.io](http://queues.io/) is a collection of task queue systems with
- short summaries for each one. The task queues are not all compatible with
- Python but ones that work with it are tagged with the "Python" keyword.
-
* [Why Task Queues](http://www.slideshare.net/bryanhelmig/task-queues-comorichweb-12962619)
is a presentation for what task queues are and why they are needed.
@@ -187,10 +176,6 @@ when scaling out a large deployment of distributed task queues.
is a straightforward tutorial for setting up the Celery task queue for
Django web applications using the Redis broker on the back end.
-* [Three quick tips from two years with Celery](https://library.launchkit.io/three-quick-tips-from-two-years-with-celery-c05ff9d7f9eb)
- provides some solid advice on retry delays, the -Ofair flag and global
- task timeouts for Celery.
-
* [Asynchronous Tasks with Flask and Redis Queue](https://testdriven.io/asynchronous-tasks-with-flask-and-redis-queue)
looks at how to configure Redis Queue to handle long-running tasks in a Flask app.
diff --git a/content/pages/04-web-development/28-celery.markdown b/content/pages/04-web-development/28-celery.markdown
index d0c263a83..6746997d1 100644
--- a/content/pages/04-web-development/28-celery.markdown
+++ b/content/pages/04-web-development/28-celery.markdown
@@ -7,11 +7,11 @@ sidebartitle: Celery
meta: Celery is a task queue for executing work outside a Python web application HTTP request-response cycle.
-[Celery](http://www.celeryproject.org/) is a [task queue](/task-queues.html)
+[Celery](https://docs.celeryproject.org/) is a [task queue](/task-queues.html)
implementation for [Python web applications](/web-development.html) used
to asynchronously execute work outside the HTTP request-response cycle.
-
+
@@ -163,10 +163,6 @@ web framework of your choice.
is a detailed walkthrough for setting up Celery with Django (although
Celery can also be used without a problem with other frameworks).
-* [Introducing Celery for Python+Django](http://www.linuxforu.com/2013/12/introducing-celery-pythondjango/)
- provides an introduction to the Celery task queue with Django as the
- intended framework for building a web application.
-
* [Asynchronous Tasks with Falcon and Celery](https://testdriven.io/asynchronous-tasks-with-falcon-and-celery)
configures Celery with the [Falcon](/falcon.html) framework, which is
less commonly-used in web tutorials.
@@ -176,6 +172,10 @@ web framework of your choice.
for transient states in your application that are not covered by the
default Celery configuration.
+* [Asynchronous Tasks with Django and Celery](https://testdriven.io/blog/django-and-celery/)
+ looks at how to configure Celery to handle long-running tasks in a
+ Django app.
+
### Celery deployment resources
Celery and its broker run separately from your web and WSGI servers so it
@@ -183,19 +183,7 @@ adds some additional complexity to your [deployments](/deployment.html). The
following resources walk you through how to handle deployments and get the
right configuration settings in place.
-* The "Django in Production" series by
- [Rob Golding](https://twitter.com/robgolding63) contains a post
- specifically on [Background Tasks](http://www.robgolding.com/blog/2011/11/27/django-in-production-part-2---background-tasks/).
-
* [How to run celery as a daemon?](https://pythad.github.io/articles/2016-12/how-to-run-celery-as-a-daemon-in-production)
is a short post with the minimal code for running the Celery daemon and
Celerybeat as system services on Linux.
-* [Celery in Production](http://www.caktusgroup.com/blog/2014/09/29/celery-production/)
- on the Caktus Group blog contains good practices from their experience
- using Celery with RabbitMQ, monitoring tools and other aspects not often
- discussed in existing documentation.
-
-* [Three quick tips from two years with Celery](https://library.launchkit.io/three-quick-tips-from-two-years-with-celery-c05ff9d7f9eb)
- provides some solid advice on retry delays, the `-Ofair` flag and global
- task timeouts for Celery.
diff --git a/content/pages/04-web-development/29-rq-redis-queue.markdown b/content/pages/04-web-development/29-rq-redis-queue.markdown
index 55979bf3c..b9fe01a39 100644
--- a/content/pages/04-web-development/29-rq-redis-queue.markdown
+++ b/content/pages/04-web-development/29-rq-redis-queue.markdown
@@ -19,7 +19,7 @@ track of tasks in the queue that need to be executed.
### RQ resources
* [Asynchronous Tasks in Python with Redis Queue](https://www.twilio.com/blog/asynchronous-tasks-in-python-with-redis-queue)
is a quickstart-style tutorial that shows how to use RQ to fetch data
- from the
+ from the
[Mars Rover web API](https://data.nasa.gov/Space-Science/Mars-Rover-Photos-API/929k-jizu)
and process URLs for each of the photos taken by NASA's Mars rover.
There is also a follow-up post on
@@ -30,6 +30,10 @@ track of tasks in the queue that need to be executed.
* The [RQ intro post](http://nvie.com/posts/introducing-rq/) contains
information on design decisions and how to use RQ in your projects.
+* [Build a Ghostwriting App for Scary Halloween Stories with OpenAI's GPT-3 Engine and Task Queues in Python](https://www.twilio.com/blog/ghost-writer-spooky-task-queues-python-openai-gpt3)
+ is a fun tutorial that uses RQ with OpenAI's [GPT-3](/gpt-3.html) API
+ randomly write original stories inspired by creepy Halloween tales.
+
* [International Space Station notifications with Python and Redis Queue (RQ)](https://www.twilio.com/blog/2015/11/international-space-station-notifications-with-python-redis-queue-and-twilio-copilot.html)
shows how to combine the RQ task queue library with Flask to send
text message notifications every time a condition is met - in this blog
diff --git a/content/pages/04-web-development/31-static-site-generator.markdown b/content/pages/04-web-development/31-static-site-generator.markdown
index 7b07bc047..d7f3c0983 100644
--- a/content/pages/04-web-development/31-static-site-generator.markdown
+++ b/content/pages/04-web-development/31-static-site-generator.markdown
@@ -206,12 +206,6 @@ point a domain name to your site as well as provide HTTPS support.
These guides walk through various ways of handling the static site
deployment.
-* [Static site hosting with S3 and Cloudflare](https://wsvincent.com/static-site-hosting-with-s3-and-cloudflare/)
- shows how to set up an S3 bucket with Cloudflare in front as a CDN that
- serves the content with HTTPS. You should be able to accomplish roughly
- the same situation with Amazon Cloudfront, but as a Cloudflare user I
- like their service for these static site configurations.
-
* Google Cloud provides a tutorial on how to use them to
[host your static site](https://cloud.google.com/storage/docs/hosting-static-website).
Note that you cannot currently use HTTPS on Google Storage servers, which is a
@@ -221,7 +215,7 @@ deployment.
making static site deployments and redeployments to Amazon Web Services
easier.
-* [Deploying a Static Blog with Continuous Integration](https://www.loxodrome.io/post/hugo-on-ci/)
+* [Deploying a Static Blog with Continuous Integration](https://www.jameslmilner.com/post/hugo-on-ci/)
uses a Hugo (a Golang-based static site generator) generated site
as an example but the instructions can easily be used to deploy
a Python-based static site generator output as well.
diff --git a/content/pages/04-web-development/32-pelican.markdown b/content/pages/04-web-development/32-pelican.markdown
index 94f9e36f6..c1b0c0810 100644
--- a/content/pages/04-web-development/32-pelican.markdown
+++ b/content/pages/04-web-development/32-pelican.markdown
@@ -18,7 +18,7 @@ Pelican's
under the
[AGPL 3 license](https://www.gnu.org/licenses/why-affero-gpl.html).
-
+
@@ -76,10 +76,6 @@ to hosting services such as Amazon S3 and GitHub Pages.
take in new input markup formats, modify the generator process and
add handy features such as a custom table of contents.
-* [Pelican Sitemap and Pagination](http://www.vcheng.org/2014/02/22/pelican-sitemap-pagination/)
- explains how to generate a `sitemap.xml` file for your static site that
- includes all pages instead of just auto-included top-level pages.
-
* [Getting started with Pelican and GitHub pages](http://www.mattmakai.com/introduction-to-pelican.html)
is a tutorial I wrote to use the Full Stack Python source code to create
and deploy your first static site.
diff --git a/content/pages/04-web-development/35-testing.markdown b/content/pages/04-web-development/35-testing.markdown
index a69c8f126..7594935d2 100644
--- a/content/pages/04-web-development/35-testing.markdown
+++ b/content/pages/04-web-development/35-testing.markdown
@@ -101,6 +101,12 @@ test runners, coverage reports and related libraries.
* [A beginner's guide to Python testing](https://miguelgfierro.com/blog/2018/a-beginners-guide-to-python-testing/)
covers test-driven development for unit, integration and smoke tests.
+* [Testing a Twilio Interactive Voice Response (IVR) System With Python and pytest](https://www.twilio.com/blog/testing-twilio-ivr-system-python-pytest)
+ is an incredibly in-depth tutorial on testing a Python-powered IVR
+ using pytest. There is also a tutorial on
+ [how to build the IVR before this testing tutorial](https://www.twilio.com/blog/building-interactive-voice-response-ivr-system-python-django-twilio),
+ although you can just clone it to jump into the testing walkthrough.
+
* Still confused about the difference between unit, functional and
integration tests? Check out this
[top answer on Stack Overflow](http://stackoverflow.com/questions/4904096/whats-the-difference-between-unit-functional-acceptance-and-integration-test)
@@ -170,10 +176,10 @@ use mocks in your test cases.
provides a whole code example based on a blog project that shows
how to use `mock` when testing.
-* [Python Mocking 101: Fake It Before You Make It](https://blog.fugue.co/2016-02-11-python-mocking-101.html)
+* [Python Mocking 101: Fake It Before You Make It](https://www.fugue.co/blog/2016-02-11-python-mocking-101)
explains what mocking is and is not, and shows how to use the `patch`
function to accomplish it in your project.
- [Revisiting Unit Testing and Mocking in Python](https://blog.fugue.co/2017-07-18-revisiting-unit-testing-and-mocking-in-python.html)
+ [Revisiting Unit Testing and Mocking in Python](https://www.fugue.co/blog/2017-07-18-revisiting-unit-testing-and-mocking-in-python.html)
is a follow-up post that expands upon using the `patch` function
along with dependency injection.
@@ -185,12 +191,6 @@ use mocks in your test cases.
examines when mocks are necessary and when they are not as useful
so you can avoid them in your test cases.
-* [Mocking Redis & Expiration in Python](http://malexandre.fr/2017/10/08/mocking-redis--expiration-in-python/)
- is a specific scenario where you would want to test your
- [Redis](/redis.html)-dependent code but prefer to mock it rather than
- ensure an installation and connection are present whenever you run
- your tests.
-
* [Better tests for Redis integrations with redislite](https://www.obeythetestinggoat.com/better-tests-for-redis-integrations-with-redislite.html)
is a great example of how using the right mocking library can clean
up existing hacky testing code and make it more straightforward for
diff --git a/content/pages/04-web-development/36-unit-testing.markdown b/content/pages/04-web-development/36-unit-testing.markdown
index 86bebc128..9002c90d6 100644
--- a/content/pages/04-web-development/36-unit-testing.markdown
+++ b/content/pages/04-web-development/36-unit-testing.markdown
@@ -70,10 +70,6 @@ Python-specific applications.
is a detailed tutorial for using the nose test runner for ensuring a
Flask application is working properly.
-* [Understanding unit testing](https://jeffknupp.com/blog/2013/12/09/improve-your-python-understanding-unit-testing/)
- explains why testing is important and shows how to do it effectively in
- your applications.
-
* [Unit testing with Python](http://www.drdobbs.com/testing/unit-testing-with-python/240165163)
provides a high-level overview of testing and has diagrams to demonstrate
what's going on in the testing cycle.
diff --git a/content/pages/04-web-development/37-integration-testing.markdown b/content/pages/04-web-development/37-integration-testing.markdown
index 72d844b82..8594b4492 100644
--- a/content/pages/04-web-development/37-integration-testing.markdown
+++ b/content/pages/04-web-development/37-integration-testing.markdown
@@ -36,9 +36,6 @@ during development so they can be addressed immediately.
gives an example of a system that needs integration tests and shows how
context managers can be used to address the problem.
-* Pytest has a page on [integration good practices](http://doc.pytest.org/en/latest/goodpractices.html)
- that you'll likely want to follow when testing your application.
-
* [Integration testing, or how to sleep well at night](http://enterprisecraftsmanship.com/2015/07/13/integration-testing-or-how-to-sleep-well-at-nights/)
explains what integration tests are and gives an example. The example is
coded in Java but still relevant when you're learning about integration
diff --git a/content/pages/04-web-development/38-debugging.markdown b/content/pages/04-web-development/38-debugging.markdown
index 88f4999e1..8cf576d27 100644
--- a/content/pages/04-web-development/38-debugging.markdown
+++ b/content/pages/04-web-development/38-debugging.markdown
@@ -19,6 +19,31 @@ There are bugs in every modest sized or larger application. Every
developer has to learn how to debug code in order to write programs that
work as correctly as time and budget allow.
+In addition to fixing bugs, debugging is an important skill for improving
+the efficiency of an application by optimizing performance and improving
+the logic. Debugging is a complex skill that takes time and practice
+for a developer to gain as a capability.
+
+
+## What are some common debugging techniques?
+Some common debugging techniques include:
+
+* Printing out or displaying values of variables and state
+ at certain times during the execution of an application
+
+* Changing the state of a program to make it do different
+ things. This is called altering the "path" of the program
+
+* Stepping through the execution of a program line by line
+
+* Breakpoints
+
+* Trace Points
+
+* Stopping the program at certain events
+
+* Viewing the output of a program in a debugger window
+
### Debugging tools
There are many debugging tools, some of which are built into
@@ -112,10 +137,6 @@ give solid programming language-agnostic debugging advice.
* [The art of debugging](https://remysharp.com/2015/10/14/the-art-of-debugging)
provides a whirlwind overview for how to fix issues in your code.
-* [How to debug JavaScript errors](https://rollbar.com/guides/how-to-debug-javascript/)
- introduces some key debugging tools such as source maps that make
- identifying errors on the client side much easier during development.
-
* [Linux debugging tools you'll love](https://jvns.ca/debugging-zine.pdf)
is an awesome comic that covers the Linux ecosystem for debugging.
diff --git a/content/pages/04-web-development/39-code-metrics.markdown b/content/pages/04-web-development/39-code-metrics.markdown
index 27761947e..1ce96176f 100644
--- a/content/pages/04-web-development/39-code-metrics.markdown
+++ b/content/pages/04-web-development/39-code-metrics.markdown
@@ -48,7 +48,7 @@ system are occurring.
* [Black](https://github.com/ambv/black) is a Python code formatter with
strong, uncompromising assumptions about how your code must be formatted.
-* [dlint](https://github.com/duo-labs/dlint) is a linter for secure coding
+* [dlint](https://github.com/dlint-py) is a linter for secure coding
practices.
* [pylintdb](https://github.com/nedbat/pylintdb) puts pylint results into
@@ -151,10 +151,6 @@ diminishing returns.
covers Pylint, PyFlakes and mypy with a short description of the
advantages and disadvantage for each one.
-* [Dynamic tracing a Pony + Python program with DTrace](https://blog.wallaroolabs.com/2017/12/dynamic-tracing-a-pony---python-program-with-dtrace/)
- shows how to use dynamic tracing programs to analyze code
- at runtime.
-
* This [Stack Overflow question on Python static code analysis tools](http://stackoverflow.com/questions/1428872/pylint-pychecker-or-pyflakes)
contains comparison discussions of PyLint, PyChecker and PyFlakes.
diff --git a/content/pages/04-web-development/40-networking.markdown b/content/pages/04-web-development/40-networking.markdown
index 15ab8fce9..7e690cdba 100644
--- a/content/pages/04-web-development/40-networking.markdown
+++ b/content/pages/04-web-development/40-networking.markdown
@@ -13,6 +13,19 @@ web applications.
### Resources about networking
+The "Let's code a TCP/IP stack" series along with its
+[open source code](https://github.com/saminiir/level-ip) gives a ton of
+context on how TCP/IP works while providing the code for implementing the
+foundational pieces. You will likely need to pair this with a more theoretical
+reference tutorial such as [RFC 1180](https://tools.ietf.org/html/rfc1180)
+to have a more complete understanding of the protocol:
+
+1. [Ethernet & ARP](http://www.saminiir.com/lets-code-tcp-ip-stack-1-ethernet-arp/)
+1. [IPv4 & ICMPv4](http://www.saminiir.com/lets-code-tcp-ip-stack-2-ipv4-icmpv4/)
+1. [TCP Basics & Handshake](http://www.saminiir.com/lets-code-tcp-ip-stack-3-tcp-handshake/)
+1. [TCP Data Flow & Socket API](http://www.saminiir.com/lets-code-tcp-ip-stack-4-tcp-data-flow-socket-api/)
+1. [TCP Retransmission](http://www.saminiir.com/lets-code-tcp-ip-stack-5-tcp-retransmission/)
+
* [Monitoring and Tuning the Linux Networking Stack: Receiving Data](https://blog.packagecloud.io/eng/2016/06/22/monitoring-tuning-linux-networking-stack-receiving-data/)
along with
[Monitoring and Tuning the Linux Networking Stack: Sending Data](https://blog.packagecloud.io/eng/2017/02/06/monitoring-tuning-linux-networking-stack-sending-data/)
@@ -38,3 +51,7 @@ web applications.
explains how Dropbox uses edge-of-the-network resources closer to the
end user to optimize performance of their service.
+* [On the shoulders of giants: recent changes in Internet traffic](https://blog.cloudflare.com/on-the-shoulders-of-giants-recent-changes-in-internet-traffic/)
+ examines how the COVID-19 pandemic of 2020 changed the times during the
+ day and locations of how the majority of internet traffic was routed
+ and consumed.
diff --git a/content/pages/04-web-development/41-https.markdown b/content/pages/04-web-development/41-https.markdown
index ffe55e7c9..3bbb8239a 100644
--- a/content/pages/04-web-development/41-https.markdown
+++ b/content/pages/04-web-development/41-https.markdown
@@ -64,7 +64,3 @@ client web browser.
cover the high-level information on the latest approved version of
Transport Security Layer (TLS) 1.3.
-* [How https works](https://www.sudhakar.online/programming/2015/08/09/https.html)
- is a fun cartoon illustration that demonstrates the basic concepts of
- a secure HTTP connection.
-
diff --git a/content/pages/04-web-development/42-websockets.markdown b/content/pages/04-web-development/42-websockets.markdown
index fb8293c46..2a902f79c 100644
--- a/content/pages/04-web-development/42-websockets.markdown
+++ b/content/pages/04-web-development/42-websockets.markdown
@@ -7,10 +7,10 @@ sidebartitle: WebSockets
meta: WebSockets are a protocol for full-duplex web communications. Learn about WebSockets on Full Stack Python.
-A WebSocket is a [standard protocol](http://tools.ietf.org/html/rfc6455) for
-two-way data transfer between a client and server. The WebSockets protocol
-does not run over HTTP, instead it is a separate implementation on top of
-[TCP](http://en.wikipedia.org/wiki/Transmission_Control_Protocol).
+A WebSocket is a [standard protocol](https://datatracker.ietf.org/doc/html/rfc6455)
+for two-way data transfer between a client and server. The WebSockets
+protocol does not run over HTTP, instead it is a separate implementation
+on top of [TCP](http://en.wikipedia.org/wiki/Transmission_Control_Protocol).
## Why use WebSockets?
@@ -173,12 +173,6 @@ own project.
that demos sending server generated events as well as input from users
via a text box input on a form.
-* The [realtime codenames game](https://github.com/joshporter1/codenames)
- source code is a full-featured example for using WebSockets via
- Flask-SocketIO. There is also a
- [multi-part tutorial](https://secdevops.ai/weekend-project-part-1-creating-a-real-time-web-based-application-using-flask-vue-and-socket-b71c73f37df7)
- that walks through the code.
-
* The
[python-websockets-example](https://github.com/mattmakai/python-websockets-example)
contains code to create a simple web application that provides WebSockets
@@ -192,13 +186,6 @@ own project.
Flask web app implementation that allows the audience to interact with
WebSockets as I built out the application.
-* [Creating a Real-time Web-based Application using Flask, Vue, and Socket.io: part 1](https://secdevops.ai/weekend-project-part-1-creating-a-real-time-web-based-application-using-flask-vue-and-socket-b71c73f37df7),
- [part 2](https://secdevops.ai/weekend-project-part-2-turning-flask-into-a-real-time-websocket-server-using-flask-socketio-ab6b45f1d896)
- and
- [part 3](https://secdevops.ai/weekend-project-part-3-centralizing-state-management-with-vuex-5f4387ebc144)
- are a complete front-to-backend WebSockets, Python and JavaScript front
- end framework example with open source code.
-
* [Real-time in Python](http://mrjoes.github.io/2013/06/21/python-realtime.html)
provides Python-specific context for how the server push updates were
implemented in the past and how Python's tools have evolved to perform
@@ -206,9 +193,7 @@ own project.
* [websockets](https://github.com/aaugustin/websockets) is a WebSockets
implementation for Python 3.3+ written with the
- [asyncio](https://docs.python.org/3.4/library/asyncio.html) module (or with
- [Tulip](https://code.google.com/p/tulip/) if you're working with
- Python 3.3).
+ [asyncio](https://docs.python.org/3.9/library/asyncio.html) module.
* [Speeding up Websockets 60X](https://www.willmcgugan.com/blog/tech/post/speeding-up-websockets-60x/)
is a cool experiment in coding loops different ways to eek out more
@@ -217,10 +202,6 @@ own project.
of how tweaking and tuning can produce outsized returns in some
applications.
-* The [Choose Your Own Adventure Presentations](https://www.twilio.com/blog/2014/11/choose-your-own-adventure-presentations-with-reveal-js-python-and-websockets.html)
- tutorial uses WebSockets via gevent on the server and socketio.js for
- pushing vote count updates from the server to the client.
-
* [Adding Real Time to Django Applications](http://crossbar.io/docs/Adding-Real-Time-to-Django-Applications/)
shows how to use Django and Crossbar.io to implement a publish/subscribe
feature in the application.
diff --git a/content/pages/04-web-development/43-webrtc.markdown b/content/pages/04-web-development/43-webrtc.markdown
index 46fe12109..e34ac044a 100644
--- a/content/pages/04-web-development/43-webrtc.markdown
+++ b/content/pages/04-web-development/43-webrtc.markdown
@@ -18,10 +18,13 @@ browser) and server (usually a [web server](/web-servers.html)).
explains what you do and do not need to know as prerequisites for
building with WebRTC along with some sources for learning.
-* [A real world guide to WebRTC](https://deepstreamhub.com/tutorials/protocols/webrtc-intro/)
- goes through WebRTC fundamentals such as data channels, audio and video,
- screen sharing and file transfers with the JavaScript code provided
- for each concept.
+* This post titled
+ [WebRTC: a working example](http://pfertyk.me/2020/03/webrtc-a-working-example/)
+ and the
+ [companion open source repository](https://github.com/pfertyk/webrtc-working-example)
+ provides a simple working example of WebRTC technology, without any 3rd party
+ dependencies. It allows 2 web browsers to exchange audio and video streams by
+ using the `aiohttp` and `python-socketio` modules.
* The
[Introduction to WebRTC video series](https://www.youtube.com/watch?v=ujpIAWmK2Vo)
@@ -30,10 +33,6 @@ browser) and server (usually a [web server](/web-servers.html)).
at points but overall has a ton of good information that gives a solid
overview of the technology.
-* [Building a Snapchat-like app with WebRTC in the browser](https://tokbox.com/blog/building-a-snapchat-like-app-with-webrtc-in-the-browser/)
- walks through the front end JavaScript for building a photo filter
- application using the WebRTC browser APIs.
-
* [WebRTC issues and how to debug them](https://blog.codeship.com/webrtc-issues-and-how-to-debug-them/)
explains the various ways that implementations can go wrong and where
to start looking when you run into errors.
diff --git a/content/pages/04-web-development/44-web-apis.markdown b/content/pages/04-web-development/44-web-apis.markdown
index a0e1eea19..77ce68888 100644
--- a/content/pages/04-web-development/44-web-apis.markdown
+++ b/content/pages/04-web-development/44-web-apis.markdown
@@ -43,7 +43,7 @@ Webhooks are important because they enable two-way communication initiation
for APIs. Webhook flexibility comes in from their definition by the API user
instead of the API itself.
-For example, in the [Twilio API](https://www.twilio.com/api) when a text
+For example, in the [Twilio API](https://www.twilio.com/docs/api) when a text
message is sent to a Twilio phone number Twilio sends an HTTP POST request
webhook to the URL specified by the user. The URL is defined in a text box
on the number's page on Twilio as shown below.
@@ -91,11 +91,6 @@ on the number's page on Twilio as shown below.
provides context for why JSON-based web services are more common today than
SOAP which was popular in the early 2000s.
-* [API tools for every occasion](https://medium.com/@orliesaurus/api-tools-for-every-occasion-10-api-tools-released-in-2015-i-can-t-live-without-d5947d9ca9c3)
- provides a list of 10 tools that are really helpful when working with APIs
- that are new in 2015.
-
-
## APIs learning checklist
1. Learn the API concepts of machine-to-machine communication with JSON and
diff --git a/content/pages/04-web-development/45-microservices.markdown b/content/pages/04-web-development/45-microservices.markdown
index 684dfc20d..0b06889e6 100644
--- a/content/pages/04-web-development/45-microservices.markdown
+++ b/content/pages/04-web-development/45-microservices.markdown
@@ -40,22 +40,6 @@ ease further development and deployment. This approach is called the
article is one of the best in-depth explanations for what microservices are
and why to consider them as an architectural pattern.
-* [Why microservices?](http://dev.otto.de/2016/03/20/why-microservices/)
- presents some of the advantages, such as the dramatically increased number
- of deployments per day, that a well-done microservices architecture can
- provide in the right situation. Many organizational environments won't
- allow this level of flexibility but if yours is one that will, it's worth
- considering these points.
-
-* [On monoliths and microservices](http://dev.otto.de/2015/09/30/on-monoliths-and-microservices/)
- provides some advice on using microservices in a fairly early stage of
- a software project's lifecycle.
-
-* [Why Microservices?](https://dev.otto.de/2016/03/20/why-microservices/)
- presents advantages microservices can bring to an existing monolithic
- application where it is clear what needs to be broken down into smaller
- components to make it easier to iterate and maintain.
-
* [Developing a RESTful microservice in Python](http://www.skybert.net/python/developing-a-restful-micro-service-in-python/)
is a good story of how an aging Java project was replaced with a
microservice built with Python and Flask.
@@ -128,3 +112,7 @@ ease further development and deployment. This approach is called the
* [The Hardest Part About Microservices: Your Data](http://blog.christianposta.com/microservices/the-hardest-part-about-microservices-data/)
presents a data-centric view on how to structure and transport data
in a microservices architecture.
+
+* [Deleting data distributed throughout your microservices architecture](https://blog.twitter.com/engineering/en_us/topics/infrastructure/2020/deleting-data-distributed-throughout-your-microservices-architecture.html)
+ examines how Twitter handles issues with discoverability, access and erasure
+ in their microservices-heavy production environment.
diff --git a/content/pages/04-web-development/46-webhooks.markdown b/content/pages/04-web-development/46-webhooks.markdown
index a50c9f558..6583c526d 100644
--- a/content/pages/04-web-development/46-webhooks.markdown
+++ b/content/pages/04-web-development/46-webhooks.markdown
@@ -23,9 +23,21 @@ otherwise independent web applications.
### Webhook resources
-* [Should you build a webhooks API?](https://brandur.org/webhooks)
+* [Building Webhooks Into Your Application: Guidelines and Best Practices](https://workos.com/blog/building-webhooks-into-your-application-guidelines-and-best-practices)
+ is an extensive high-level guide that defines what webhooks are, why you
+ will want to build them if you need to proactively notify other applications
+ of events, and what security considerations you need to have when
+ creating them.
+
+* [What's a webhook?](https://sendgrid.com/blog/whats-webhook/) is a high-level
+ explanation of this concept that also contains some basic security
+ considerations when using them.
-* [Webhooks do’s and dont’s: what we learned after integrating +100 APIs](https://restful.io/webhooks-dos-and-dont-s-what-we-learned-after-integrating-100-apis-d567405a3671)
+* [Webhooks for Beginners - Full Course](https://www.youtube.com/watch?v=41NOoEz3Tzc)
+ is an entire free video course that shows both how to use and implement
+ webhooks into applications.
+
+* [Should you build a webhooks API?](https://brandur.org/webhooks)
* [Why Are Webhooks Better Than Serverless Extensibility?](https://developer.okta.com/blog/2017/10/11/why-are-webhooks-better-than-serverless-extensibility)
diff --git a/content/pages/04-web-development/47-bots.markdown b/content/pages/04-web-development/47-bots.markdown
index 82ba58758..46d487720 100644
--- a/content/pages/04-web-development/47-bots.markdown
+++ b/content/pages/04-web-development/47-bots.markdown
@@ -53,7 +53,7 @@ often also called "chatbots", "assistants" or "agents."
* [Making a Reddit + Facebook Messenger Bot](https://pythontips.com/2017/04/13/making-a-reddit-facebook-messenger-bot/)
builds a bot for two platforms and shows how to deploy it to Heroku.
-* [Build a Slack Bot that Mimics Your Colleagues with Python](http://hirelofty.com/blog/how-build-slack-bot-mimics-your-colleague/)
+* [Build a Slack Bot that Mimics Your Colleagues with Python](https://hirelofty.com/blog/software-development/how-build-slack-bot-mimics-your-colleague/)
is a humorous post that uses the
[markovify](https://github.com/jsvine/markovify) Markov Chains library to
generate responses that are similar to ones other Slack users have said.
diff --git a/content/pages/04-web-development/48-api-creation.markdown b/content/pages/04-web-development/48-api-creation.markdown
index 9125e39fa..aa49c5aa1 100644
--- a/content/pages/04-web-development/48-api-creation.markdown
+++ b/content/pages/04-web-development/48-api-creation.markdown
@@ -26,9 +26,6 @@ applications through machine-to-machine communication.
[own GitHub organization](https://github.com/flask-restful/flask-restful)
so engineers from outside the company could be core contributors.
-* [Flask API](http://www.flaskapi.org/) is another common library for
- exposing APIs from Flask web applications.
-
* [Sandman](http://www.github.com/jeffknupp/sandman) is a widely used tool to
automatically generate a RESTful API service from a legacy database without
writing a line of code (though it's easily extensible through code).
@@ -133,12 +130,8 @@ equivalent of browser testing in the web application world.
compatibility and a whole slew of other great advice for developers
and API designers.
-* [Self-descriptive, isn't. Don't assume anything.](http://www.bizcoder.com/self-descriptive-isn-t-don-t-assume-anything)
- is an appeal that metadata makes a difference in whether APIs are descriptive
- or not.
-
* [Designing the Artsy API](http://artsy.github.io/blog/2014/09/12/designing-the-public-artsy-api/)
- has their recommendations list for building an API based on their recent
+ has their recommendations list for building an API based on their
experiences.
* Hacker News had a discussion on
@@ -149,9 +142,10 @@ equivalent of browser testing in the web application world.
is free and contains a wealth of practical advice for what design
decisions to make for your web API.
-* [1-to-1 Relationships and Subresources in REST APIs](http://developers.lyst.com/2015/02/20/1-to-1-relationships-and-subresources-in-rest-apis/)
- tells the story of design decisions that were made during an API's creation
- and why those choices were made.
+* [Documenting APIs: A guide for technical writers and engineers](https://idratherbewriting.com/learnapidoc/)
+ is a guide that covers good practices for thinking like a developer who
+ will use your API, as well as what the documentation for endpoints
+ and other important pieces should look like.
* [How many status codes does your API need?](https://blogs.dropbox.com/developers/2015/04/how-many-http-status-codes-should-your-api-use/)
gives an answer from a Dropbox API developer as to their decision making
@@ -201,16 +195,19 @@ equivalent of browser testing in the web application world.
second as well as the number of concurrent open connections can help
keep your API alive during times of heavy traffic.
+* [Writing HTTP files to test HTTP APIs](https://renato.athaydes.com/posts/writing-http-files-for-testing.html)
+ shows how to perform automated testing of APIs using HTTP file formats
+ provided by
+ [VS Code REST Client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client)
+ and the
+ [JetBrains HTTP Client Editor](https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html).
+
* Serialization is common for transforming objects into web API JSON
results. One company found the serialization performance of Django REST
framework was lacking so they created
[Serpy](https://github.com/clarkduvall/serpy) and
[wrote a blog post with the results of its performance](https://engineering.betterworks.com/2015/09/04/ditching-django-rest-framework-serializers-for-serpy/).
-* [Designing a Web API](http://restlet.com/company/blog/2015/03/16/api-design-designing-a-web-api/)
- gives a detailed walkthrough of concepts and design decisions you need
- to make when building an API.
-
* Microsoft's
[REST API Guidelines](https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md)
are a detailed set of considerations for when you are building your own
@@ -223,6 +220,11 @@ equivalent of browser testing in the web application world.
* [Building better API docs](https://engineering.gosquared.com/building-better-api-docs)
shows how Square used Swagger with React to create more helpful docs.
+* [Best Practices For Creating Useful API Documentation](https://nordicapis.com/best-practices-for-creating-useful-api-documentation/)
+ covers standard but important topics such as knowing your audience,
+ ensuring your documentation covers the error codes, and providing
+ a changelog as well as terms of service.
+
## Django REST Framework resources
* This multi-part series on
diff --git a/content/pages/04-web-development/50-django-rest-framework-drf.markdown b/content/pages/04-web-development/50-django-rest-framework-drf.markdown
index 5824966bc..8e37986e0 100644
--- a/content/pages/04-web-development/50-django-rest-framework-drf.markdown
+++ b/content/pages/04-web-development/50-django-rest-framework-drf.markdown
@@ -60,10 +60,6 @@ typically abbreviated "DRF", is a Python library for building web
* [Optimizing slow Django REST Framework performance](https://ses4j.github.io/2015/11/23/optimizing-slow-django-rest-framework-performance/)
-* [TLT: Serializing Authenticated User Data With Django REST Framework](http://gregblogs.com/tlt-serializing-authenticated-user-data-with-django-rest-framework/)
-
-* [Building an API with Django REST Framework and Class-Based Views](https://codeburst.io/building-an-api-with-django-rest-framework-and-class-based-views-75b369b30396)
-
* [Simple Nested API Using Django REST Framework](https://blog.apptension.com/2017/09/13/rest-api-using-django-rest-framework/)
* [Building APIs with Django and Django Rest Framework](https://books.agiliq.com/projects/django-api-polls-tutorial/en/latest/)
diff --git a/content/pages/04-web-development/51-api-integration.markdown b/content/pages/04-web-development/51-api-integration.markdown
index ecf006c20..ee646bfa9 100644
--- a/content/pages/04-web-development/51-api-integration.markdown
+++ b/content/pages/04-web-development/51-api-integration.markdown
@@ -26,7 +26,7 @@ across many implementation areas.
add them to applications with a bunch of useful code examples.
* Some developers prefer to use
- [Requests](http://docs.python-requests.org/en/latest/) instead of an API's
+ [Requests](https://requests.readthedocs.io/en/master/) instead of an API's
helper library. In that case check out this
[tutorial on using requests to access web APIs](http://engineering.hackerearth.com/2014/08/21/python-requests-module/).
@@ -43,6 +43,12 @@ across many implementation areas.
[18F's API-All-the-X list](http://18f.github.io/API-All-the-X/). The list
is updated whenever a new API comes online.
+* [The Only Type of API Services I'll Use](https://www.rdegges.com/2020/the-only-type-of-api-services-ill-use/)
+ explains how alignment between usage and pricing is crucial to a solid,
+ long-lasting API experience. Other models such as tiered usage and
+ enterprise upsells typically lead to terrible developer experiences and
+ should generally be avoided when building applications with APIs.
+
* John Sheehan's
"[Zen and the Art of API Maintenance](https://speakerdeck.com/johnsheehan/zen-and-the-art-of-api-maintenance)"
slides are relevant for API integration.
@@ -57,9 +63,6 @@ across many implementation areas.
is a nice tutorial for easily re-executing failed HTTP requests with the
Requests library.
-* My DjangoCon 2013 talk dove into
- "[Making Django Play Nice With Third Party Services](http://www.youtube.com/watch?v=iGP8DQIqxXs)."
-
* If you're looking for a fun project that uses two web APIs within a
Django application, try out this tutorial to
[Build your own Pokédex with Django, MMS and PokéAPI](https://www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html).
@@ -92,7 +95,7 @@ across many implementation areas.
requests and responses.
1. Evaluate whether to use a helper library or work with
- [Requests](http://docs.python-requests.org/en/latest/). Helper libraries
+ [Requests](https://requests.readthedocs.io/en/master/). Helper libraries
are usually easier to get started with while Requests gives you more
control over the HTTP calls.
diff --git a/content/pages/04-web-development/52-twilio.markdown b/content/pages/04-web-development/52-twilio.markdown
index 8322563b1..e038e3907 100644
--- a/content/pages/04-web-development/52-twilio.markdown
+++ b/content/pages/04-web-development/52-twilio.markdown
@@ -77,10 +77,6 @@ for fellow developers.
[Twilio SMS API via some Python code](https://www.twilio.com/docs/quickstart/python/sms)
to send a text message with the results.
-* IBM's Bluemix blog contains a nice tutorial on building an
- [IoT Python app with a Raspberry Pi and Bluemix](https://developer.ibm.com/bluemix/2015/04/02/tutorial-using-a-raspberry-pi-python-iot-twilio-bluemix/)
- that uses Twilio to interact with the Raspberry Pi.
-
* The [Python tag on the Twilio blog](https://www.twilio.com/blog/tag/python)
provides walkthroughs for
[Django](https://www.twilio.com/blog/2015/12/city-chat-with-python-django-and-twilio-ip-messaging.html),
@@ -88,6 +84,14 @@ for fellow developers.
and [Bottle](https://www.twilio.com/blog/2016/08/getting-started-python-bottle-twilio-sms-mms.html)
apps to learn from while building your own projects.
+* [Serverless Phone Number Validation with AWS Lambda, Python and Twilio](https://www.twilio.com/blog/serverless-phone-number-validation-aws-lambda-python-twilio)
+ walks through using the Python runtime on [AWS Lambda](/aws-lambda.html)
+ and Twilio's APIs for validating phone number information.
+
+* [Receive Flask Error Alerts by Email with Twilio SendGrid](https://www.twilio.com/blog/receive-flask-error-alerts-email-twilio-sendgrid)
+ shows how to use Twilio to add email error reports to [Flask](/flask.html)
+ web applications.
+
* [Google Cloud recommends developers use Twilio](https://cloud.google.com/appengine/docs/standard/python/sms/twilio)
for communications in their apps and provides a short walkthrough for
Python developers.
@@ -95,7 +99,13 @@ for fellow developers.
* [SMS Tracking Notifications](https://www.easypost.com/sms-tracking-tutorial)
is a fun tutorial that combines two APIs together - Twilio and
[Easypost](https://www.easypost.com) to track packages sent through the
- Easypost service.
+ Easypost service. There is also another tutorial on
+ [shipment notifications specifically for Flask](https://www.twilio.com/blog/build-shipment-notification-service-python-flask-twilio-easypost).
+
+* [Build a Video Chat Application with Python, JavaScript and Twilio Programmable Video](https://www.twilio.com/blog/build-video-chat-application-python-javascript-twilio-programmable-video)
+ shows how to use [Flask](/flask.html) and the
+ [Twilio Programmable Video API](https://www.twilio.com/docs/video) to
+ build cross-platform video into new and existing applications.
* This video titled
"[We're No Strangers to VoIP: Building the National Rick Astley Hotline)](https://www.youtube.com/watch?v=LgKshQoGh64)"
diff --git a/content/pages/04-web-development/53-stripe.markdown b/content/pages/04-web-development/53-stripe.markdown
index 03b01c986..82aba71f0 100644
--- a/content/pages/04-web-development/53-stripe.markdown
+++ b/content/pages/04-web-development/53-stripe.markdown
@@ -18,8 +18,10 @@ for processing payments.
### Stripe tutorials
-* [Switching from Braintree to Stripe](https://www.deekit.com/braintree-to-stripe/)
- covers one development team's experience with moving payment providers.
+* [How to Create a Subscription SaaS Application with Django and Stripe](https://www.saaspegasus.com/guides/django-stripe-integrate/)
+ shows how to build a [Django](/django.html) application with models for
+ the subscription data in the [Django ORM](/django-orm.html) and create
+ a pricing page.
* [Dirt Cheap Recurring Payments with Stripe and AWS Lambda](http://normal-extensions.com/2017/05/05/simple-recurring/)
explains how to use the Stripe API with [AWS Lambda](/aws-lambda.html)
@@ -38,6 +40,11 @@ for processing payments.
### Resources about Stripe
+* [Stripe’s payments APIs: the first ten years](https://stripe.com/blog/payment-api-design)
+ has a ton of great context about how Stripe's payments API has evolved,
+ its architecture, how they expanded it over time, and generally a bunch
+ of solid storytelling behind how it has been built.
+
* [How Stripe Designs Beautiful Websites](https://leerob.io/blog/how-stripe-designs-beautiful-websites)
explains the process for how Stripe creates their gorgeous design that
makes people want to use the service and explore what else they can
diff --git a/content/pages/04-web-development/54-slack.markdown b/content/pages/04-web-development/54-slack.markdown
index 15931fae1..e9e251609 100644
--- a/content/pages/04-web-development/54-slack.markdown
+++ b/content/pages/04-web-development/54-slack.markdown
@@ -70,6 +70,10 @@ for programmatically interacting with its messaging service.
Slack-provided library for working with the Slack API and
[WebSockets](/websockets.html) connection.
+* [slackify](https://github.com/Ambro17/slackify) is a lightweight
+ framework for building bots and the quickstart walks you through
+ how to create a simple example bot.
+
* [slack-starterbot](https://github.com/mattmakai/slack-starterbot)
* [slack-api-python-examples](https://github.com/mattmakai/slack-api-python-examples)
diff --git a/content/pages/04-web-development/56-web-app-security.markdown b/content/pages/04-web-development/56-web-app-security.markdown
index 7cf2fd0c0..992091dbf 100644
--- a/content/pages/04-web-development/56-web-app-security.markdown
+++ b/content/pages/04-web-development/56-web-app-security.markdown
@@ -14,8 +14,6 @@ request forgery and usage of public-private keypairs.
### Security tools
-* [Bro](http://www.bro.org/) is a network security and traffic monitor.
-
* [lynis](https://cisofy.com/lynis/)
([source code](https://github.com/CISOfy/lynis)) is a security
audit tool that can run as a shell script on a Linux system to find out
@@ -107,7 +105,12 @@ resources can also give you a good overview of how HTTPS works.
[cheat sheets for security](https://www.owasp.org/index.php/Cheat_Sheets)
topics.
-* [The SaaS CTO Security Checklist](https://cto-security-checklist.sqreen.io/)
+* [Stanford's CS253 class](https://web.stanford.edu/class/cs253/) is available
+ for free online, including lecture slides, videos and course materials to
+ learn about web browser internals, session attacks, fingerprinting, HTTPS
+ and many other fundamental topics.
+
+* [The SaaS CTO Security Checklist Redux](https://www.goldfiglabs.com/guide/saas-cto-security-checklist/)
is an awesome list of steps for securing your infrastructure and employees
as well as what stage and size company it is recommended that you put those
procedures in place.
@@ -127,11 +130,6 @@ resources can also give you a good overview of how HTTPS works.
* The [/r/netsec](http://www.reddit.com/r/netsec/) subreddit is one place to
go to learn more about network and application security.
-* [Hacking Tools Repository](http://gexos.github.io/Hacking-Tools-Repository/)
- is a great list of password cracking, scanning, sniffing and other security
- penetration testing tools.
-
-
* The EFF has a well written overview on
[what makes a good security audit](https://www.eff.org/deeplinks/2014/11/what-makes-good-security-audit). It's broad but contains some of their behind the
scenes thinking on important considerations with security audits.
@@ -155,10 +153,6 @@ resources can also give you a good overview of how HTTPS works.
important technique to use to keep your database passwords and other
secrets more secure if the hashed strings are leaked.
-* [An in-depth analysis of SSH attacks on Amazon EC2](http://getprismatic.com/story/1409447605839)
- shows how important it is to secure your web servers, especially when they are
- hosted in IP address ranges that are commonly scanned by malicious actors.
-
* [Cloud Security Auditing: Challenges and Emerging Approaches](http://www.infoq.com/articles/cloud-security-auditing-challenges-and-emerging-approaches)
is a high-level overview of some of security auditing problems that come
with cloud deployments.
diff --git a/content/pages/04-web-development/57-sql-injection.markdown b/content/pages/04-web-development/57-sql-injection.markdown
index 8536fdb73..084f699e8 100644
--- a/content/pages/04-web-development/57-sql-injection.markdown
+++ b/content/pages/04-web-development/57-sql-injection.markdown
@@ -24,3 +24,8 @@ can affect both [relational databases](/databases.html) and
* [Securing your site like it's 1999](https://24ways.org/2018/securing-your-site-like-its-1999/)
covers a bunch of common web application vulnerabilities including
SQL injection.
+
+* [Automating Blind Sql Injection](https://bad-jubies.github.io/Blind-SQLi-1/)
+ shows how to use Python to execute SQL injection on the example
+ [Damn Vulnerable Web Application](https://github.com/digininja/DVWA)
+ project.
diff --git a/content/pages/05-deployment/00-deployment.markdown b/content/pages/05-deployment/00-deployment.markdown
index b5ecc4025..48ad4e8c1 100644
--- a/content/pages/05-deployment/00-deployment.markdown
+++ b/content/pages/05-deployment/00-deployment.markdown
@@ -51,7 +51,7 @@ guide as they are considered advanced deployment techniques.
* [teletraan](https://github.com/pinterest/teletraan) is the deploy system
used by the development teams at Pinterest, a huge Python shop!
-* [pants](https://www.pantsbuild.org/index.html) is a build system originally
+* [pants](https://www.pantsbuild.org/) is a build system originally
created at Twitter and now split out as its own sustainable open source
project.
@@ -112,14 +112,6 @@ guide as they are considered advanced deployment techniques.
defines delivery versus deployment and walks through a continuous deployment
workflow.
-* [Automation for the People](https://gist.github.com/classam/9e07a36aa63624ca2dda75a1367a53c6)
- provides an awesome overview of one developer's story for how he gradually
- learned about deployment automation. The story follows my own journey in
- many ways, from starting with brittle [Bash](/bourne-again-shell-bash.html)
- scripts, moving to Fabric and then on to [Ansible](/ansible.html) for
- higher-level [configuration management](/configuration-management.html)
- and continuous deployment pipelines.
-
* [5 ways to deploy your Python application in 2017](https://www.youtube.com/watch?v=vGphzPLemZE)
is a talk from
[PyCon US 2017](https://www.youtube.com/channel/UCrJhliKNQ8g0qoE_zvL8eVg/videos)
@@ -140,14 +132,6 @@ guide as they are considered advanced deployment techniques.
is an awesome in-depth read covering topics ranging from git branching
to database migrations.
-* In [this free video by Neal Ford](http://player.oreilly.com/videos/9781491908181?toc_id=210188),
- he talks about engineering practices for continuous delivery. He explains
- the difference between
- [continuous integration](/continuous-integration.html),
- continuous deployment and continuous delivery. Highly recommended for an
- overview of deployment concepts and as an introduction to the other videos
- on those subjects in that series.
-
* [TestDriven.io](https://testdriven.io/) shows how to deploy a
[microservices](/microservices.html) architecture that uses
[Docker](/docker.html), [Flask](/flask.html), and React with
diff --git a/content/pages/05-deployment/02-servers.markdown b/content/pages/05-deployment/02-servers.markdown
index 90bb07951..f66f68977 100644
--- a/content/pages/05-deployment/02-servers.markdown
+++ b/content/pages/05-deployment/02-servers.markdown
@@ -128,8 +128,8 @@ provides a unified API for many cloud service providers.
* [Amazon Web Services has official documentation](http://aws.amazon.com/python/) for running Python web applications.
-* [boto](https://github.com/boto/boto) is an extensive and well-tested
-Python library for working with Amazon Web Services.
+* [boto3](https://github.com/boto/boto3) is an extensive and well-tested
+ Python library for working with Amazon Web Services.
* [Poseidon](https://github.com/changhiskhan/poseidon) is a Python commandline
interface for managing Digital Ocean droplets (servers).
diff --git a/content/pages/05-deployment/03-static-content.markdown b/content/pages/05-deployment/03-static-content.markdown
index 8c6e88262..f5a118f54 100644
--- a/content/pages/05-deployment/03-static-content.markdown
+++ b/content/pages/05-deployment/03-static-content.markdown
@@ -21,9 +21,9 @@ Django framework calls these two categories *assets* and *media*.
## Content delivery networks
A content delivery network (CDN) is a third party that stores and serves
static files. [Amazon CloudFront](http://aws.amazon.com/cloudfront/),
-[Akamai](http://www.akamai.com/), and
-[Rackspace Cloud Files](http://www.rackspace.com/cloud/public/files/)
-are examples of CDNs. The purpose of a CDN is to remove the load of static
+[CloudFlare](https://www.cloudflare.com/) and [Fastly](https://www.fastly.com/),
+are examples of CDN services. The purpose of
+a CDN is to remove the load of static
file requests from web servers that are handling dynamic web content. For
example, if you have an nginx server that handles both static files and
acts as a front for a Green Unicorn WSGI server on a 512 megabyte
diff --git a/content/pages/05-deployment/04-cdns.markdown b/content/pages/05-deployment/04-cdns.markdown
index 027f064d6..bd4e516fe 100644
--- a/content/pages/05-deployment/04-cdns.markdown
+++ b/content/pages/05-deployment/04-cdns.markdown
@@ -12,9 +12,9 @@ servers to improve web app loading speed.
### CDN resources
-* [Mastering HTTP Caching](https://blog.fortrabbit.com/mastering-http-caching)
- is a fantastic post that goes into great technical detail on how CDNs and
- caching work.
+* [The 5 hour CDN](https://fly.io/blog/the-5-hour-content-delivery-network/)
+ explains the basics of what CDNs are and how they are a combination of
+ many standard web server components, but used globally and at scale.
* [MaxCDN vs CloudFlare vs Amazon CloudFront vs Akamai Edge vs Fastly](https://www.codeinwp.com/blog/maxcdn-vs-cloudflare-vs-cloudfront-vs-akamai-edge-vs-fastly/)
compares and contrasts the most popular CDN services based on features,
@@ -29,10 +29,6 @@ servers to improve web app loading speed.
[django-storages](https://django-storages.readthedocs.io/en/latest/) library
to deploy static assets for a [Django](/django.html) application to a CDN.
-* [Building your own CDN for Fun and Profit](https://pasztor.at/blog/building-your-own-cdn)
- is a great high-level overview of how CDNs work and shows you how to
- create your own, albeit simplified CDN.
-
* [Do not let your CDN betray you: Use Subresource Integrity](https://hacks.mozilla.org/2015/09/subresource-integrity-in-firefox-43/)
describes the security implications for CDNs with unexpectedly modified
content and how Subresource Integrity in modern web browsers can mitigate
diff --git a/content/pages/05-deployment/09-paas.markdown b/content/pages/05-deployment/09-paas.markdown
index 9416e971f..c89a37439 100644
--- a/content/pages/05-deployment/09-paas.markdown
+++ b/content/pages/05-deployment/09-paas.markdown
@@ -69,11 +69,6 @@ of controlling and modifying the project for your own applications,
but prevents you from offloading the responsibility of keeping servers
running to someone else.
-* [Kel](http://www.kelproject.com/) uses Kubernetes as a foundation
- for a custom self-hosted PaaS. Note that it was created by Eldarion,
- which had one of the first Python-specific PaaS offerings on the
- market around the time that Heroku was launched.
-
* [Dokku](http://dokku.viewdocs.io/dokku/) builds on Docker and has
hooks for plugins to extend the small core of the project and customize
deployments for your applications.
@@ -82,7 +77,6 @@ running to someone else.
designed to run on top of AWS services.
-
## Platform-as-a-service resources
* [The differences between IaaS, PaaS and SaaS](https://www.engineyard.com/blog/the-differences-between-iaas-paas-and-saas-and-when-to-use-each)
explains the abstract layer differences among "X-as-a-service" offering
@@ -156,11 +150,6 @@ running to someone else.
on Google Cloud and posits what they may be paying to run their
service.
-* [PaaS (false) economics](https://blog.drie.co/paas-false-economics-13f72d87b485)
- gives some quick back-of-the-envelope calculations on why running your
- applications on a PaaS is obviously going to appear more expensive if you
- do not take the cost of your own software engineers into the equation.
-
* Two blog posts on using AWS Autoscaling in [Automatic replacement of Autoscaling nodes with equivalent spot instances](https://mcristi.wordpress.com/2016/04/21/my-approach-at-making-aws-ec2-affordable-automatic-replacement-of-autoscaling-nodes-with-equivalent-spot-instances/)
and
[Autoscaling nodes: seeing it in action](https://mcristi.wordpress.com/2016/04/27/automatic-replacement-of-autoscaling-nodes-with-equivalent-spot-instances-seeing-it-in-action/)
diff --git a/content/pages/05-deployment/10-heroku.markdown b/content/pages/05-deployment/10-heroku.markdown
index 63aad7441..06c1d7bcb 100644
--- a/content/pages/05-deployment/10-heroku.markdown
+++ b/content/pages/05-deployment/10-heroku.markdown
@@ -21,7 +21,14 @@ easily [deploy](/deployment.html) Python applications.
[MySQL](/mysql.html) to [PostgreSQL](/postgresql.html) if necessary
as well as how to properly handle your settings files.
-* Heroku's
+* [How to deploy Django project to Heroku using Docker](https://www.accordbox.com/blog/deploy-django-project-heroku-using-docker/)
+ explains that although [Buildpacks](https://devcenter.heroku.com/articles/buildpacks)
+ are the most common way to deploy to Heroku, packaing your app in a
+ [Docker](/docker.html) container is also a viable approach. It walks through
+ the steps needed to deploy a [Django](/django.html) app in the remainder
+ of the article.
+
+* Heroku's
[official Python documentation](https://devcenter.heroku.com/articles/getting-started-with-python)
is fantastic and walks through deploying WSGI applications in short order.
diff --git a/content/pages/05-deployment/19-nginx.markdown b/content/pages/05-deployment/19-nginx.markdown
index d7f393ab6..3f17e6524 100644
--- a/content/pages/05-deployment/19-nginx.markdown
+++ b/content/pages/05-deployment/19-nginx.markdown
@@ -63,21 +63,18 @@ to make sure you are avoiding the most common security errors that plague
HTTP(S) configurations.
* [HTTPS with Let's Encrypt and nginx](https://botleg.com/stories/https-with-lets-encrypt-and-nginx/)
- walks throough installing a free SSL certificate from Let's Encrypt
+ walks through installing a free SSL certificate from Let's Encrypt
to secure HTTP connects to your nginx server via HTTPS.
-* The tools [Nginx Config](https://nginxconfig.io/) and
- [Cipherlist](https://cipherli.st/) generate strong encryption configurations
- and ciphers for Nginx.
+* The [Nginx Config](https://nginxconfig.io/) tool can generate strong
+ encryption configurations and ciphers for Nginx.
* [Gixy](https://github.com/yandex/gixy) is a static analyzer for your
Nginx configuration and can tell you issues with how you are setup.
* [Strong SSL Security on Nginx](https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html)
shows how to mitigate high profile SSL attacks like
- [Logjam](https://weakdh.org/),
- [Heartbleed](http://heartbleed.com/)
- and [FREAK](https://freakattack.com/).
+ [Logjam](https://weakdh.org/) and [Heartbleed](http://heartbleed.com/).
## Specific Nginx resources
@@ -104,7 +101,7 @@ several years.
* [Test-driving web server configuration](https://gdstechnology.blog.gov.uk/2015/03/25/test-driving-web-server-configuration/)
tells a good story for how to iteratively apply configuration changes, such
- as routing traffic to [Piwik](http://piwik.org/) for
+ as routing traffic to [Matoma](https://matomo.org/) for
[web analytics](/web-analytics.html), reverse proxying to backend
application servers and terminately TLS connections appropriately.
It is impressive to read a well-written softare development article like
@@ -119,10 +116,6 @@ several years.
as well as the Pagespeed module that Google released for both Nginx
and the [Apache HTTP Server](/apache-http-server.html).
-* [Nginx for Developers: An Introduction](http://carrot.is/coding/nginx_introduction)
- provides the first steps to getting an initial Nginx configuration up and
- running.
-
* [A faster Web server: ripping out Apache for Nginx](http://arstechnica.com/business/2011/11/a-faster-web-server-ripping-out-apache-for-nginx/)
explains how Nginx can be used instead of Apache in some cases for
better performance.
@@ -155,7 +148,3 @@ several years.
from your traffic logs when using them for web traffic analytics.
-### Nginx release summaries
-* [nginx-1.13.0](http://mailman.nginx.org/pipermail/nginx-announce/2017/000195.html)
-
-
diff --git a/content/pages/05-deployment/20-caddy.markdown b/content/pages/05-deployment/20-caddy.markdown
index 1b27e6d11..56ca43167 100644
--- a/content/pages/05-deployment/20-caddy.markdown
+++ b/content/pages/05-deployment/20-caddy.markdown
@@ -15,7 +15,8 @@ and design emphasize HTTPS-everywhere along with the HTTP/2 protocol.
## How can Caddy be used with Python deployments?
Caddy can be used both for testing during local development or as part
of a production deployment as an HTTP server and a reverse proxy with
-the [proxy directive](https://caddyserver.com/docs/proxy).
+the
+[reverse_proxy directive](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy).
Caddy is an implementation of the web server concept. Learn how these pieces fit together in the deployment chapter or view the table of contents for all topics.
diff --git a/content/pages/05-deployment/22-wsgi-servers.markdown b/content/pages/05-deployment/22-wsgi-servers.markdown
index 7a92941c8..036bdbe54 100644
--- a/content/pages/05-deployment/22-wsgi-servers.markdown
+++ b/content/pages/05-deployment/22-wsgi-servers.markdown
@@ -146,7 +146,7 @@ configuration. For real SSL and non-SSL templates, take a look at the
[Underwear web server templates](https://github.com/mattmakai/underwear/tree/master/underwear/roles/web/templates) on GitHub.
-## WSGI servers
+## WSGI server implementations
There is a comprehensive list of WSGI servers on the
[WSGI Read the Docs](http://wsgi.readthedocs.org/en/latest/servers.html) page.
The following are WSGI servers based on community recommendations.
@@ -177,9 +177,9 @@ The following are WSGI servers based on community recommendations.
is a good read to understand basic information about various WSGI server
implementations.
-* A thorough and informative post for LAMP-stack hosting choices is
- presented in the
- "[complete single server Django stack tutorial](http://www.apreche.net/complete-single-server-django-stack-tutorial/)."
+* [What is WSGI and Why Do You Need Gunicorn and Nginx in Django](https://apirobot.me/posts/what-is-wsgi-and-why-do-you-need-gunicorn-and-nginx-in-django)
+ explains the breakdown between a [web server](/web-servers.html)
+ and a WSGI server in an application deployment environment.
* The Python community made a long effort to
[transition from mod\_python](http://blog.dscpl.com.au/2010/05/modpython-project-soon-to-be-officially.html)
diff --git a/content/pages/05-deployment/23-gunicorn.markdown b/content/pages/05-deployment/23-gunicorn.markdown
index f7098a1b4..1973ab6ff 100644
--- a/content/pages/05-deployment/23-gunicorn.markdown
+++ b/content/pages/05-deployment/23-gunicorn.markdown
@@ -49,7 +49,7 @@ file with the following contents:
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
- https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
+ https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""
import os
@@ -115,11 +115,15 @@ perform the request handling. Each worker is independent of the controller.
screenshots along the way with what to expect while you are configuring
the deployment server.
-* The [Django](https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/gunicorn/)
+* The [Django](https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/gunicorn/)
and [Flask](http://flask.pocoo.org/docs/latest/deploying/wsgi-standalone/)
documentation each contain instructions for deploying the respective
frameworks with Gunicorn.
+* [Dockerizing Django with Postgres, Gunicorn, and Traefik](https://testdriven.io/blog/django-docker-traefik/)
+ is a more advanced tutorial with a complete project deployment
+ that uses Gunicorn within [Docker](/docker.html).
+
* [Set up Django, Nginx and Gunicorn in a Virtualenv controled by Supervisor](https://gist.github.com/Atem18/4696071)
is a GitHub Gist with some great explanations for why we're setting up
virtualenv and what to watch out for while you're doing the deployment.
diff --git a/content/pages/05-deployment/24-uwsgi.markdown b/content/pages/05-deployment/24-uwsgi.markdown
index 985d326fe..7561de3da 100644
--- a/content/pages/05-deployment/24-uwsgi.markdown
+++ b/content/pages/05-deployment/24-uwsgi.markdown
@@ -42,7 +42,3 @@ implementation that is typically used to run Python web applications.
is awesome because it shows you how to code a quick WSGI application without
using a framework then builds up an example with deploying a traditional
Django web app.
-
-* [Deployment Notes for Pylons, Nginx, and uWSGI](http://tonylandis.com/python/deployment-howt-pylons-nginx-and-uwsgi/)
- gives the code and instructions for setting up a Pylons application
- with uWSGI.
diff --git a/content/pages/05-deployment/27-continuous-integration.markdown b/content/pages/05-deployment/27-continuous-integration.markdown
index 835f5589a..ea620e337 100644
--- a/content/pages/05-deployment/27-continuous-integration.markdown
+++ b/content/pages/05-deployment/27-continuous-integration.markdown
@@ -127,11 +127,6 @@ programming language agnostic. Learn more via the following resources or on
uses well done drawings to show how continuous integration and delivery
works for testing and managing data.
-* [The real difference between CI and CD](https://fire.ci/blog/the-difference-between-ci-and-cd/)
- explains what advantages CI provides, what constraints it operates under
- (such as total build time) to work well, and how that is different from
- the related but distinct concept of continuous delivery.
-
* [6 top continuous integration tools](https://opensource.com/business/15/7/six-continuous-integration-tools)
gives a high level overview of six CI tools from a programming language
agnostic perspective.
diff --git a/content/pages/05-deployment/28-jenkins.markdown b/content/pages/05-deployment/28-jenkins.markdown
index 46a711870..63c64d57c 100644
--- a/content/pages/05-deployment/28-jenkins.markdown
+++ b/content/pages/05-deployment/28-jenkins.markdown
@@ -35,20 +35,11 @@ used to automate building, [testing](/testing.html) and
is another solid tutorial that also shows how to send email notifications
as part of the build process.
-* If you're running into difficulty adding an SSH key to your Jenkins system
- account so you can connect to another server or Git repository
- [this blog post on connecting Jenkins with Git](http://dcycleproject.org/blog/51/connecting-jenkins-and-git)
- to get the steps to solve that problem.
-
* [Running Jenkins in Docker Containers](http://www.catosplace.net/blog/2015/02/11/running-jenkins-in-docker-containers/)
is a short tutorial showing how to use the official
[Jenkins container](https://registry.hub.docker.com/_/jenkins/) on the
Docker hub.
-* [Securing Jenkins](https://wiki.jenkins.io/display/JENKINS/Securing+Jenkins)
- is the landing page for Jenkins security. If you're deploying your own
- instance, you'll need to lock it down against unauthorized users.
-
* [Updating the GOV.UK Continuous Integration environment](https://gdstechnology.blog.gov.uk/2017/02/10/updating-the-gov-uk-continuous-integration-environment/)
describes the configuration improvements one infrastructure team made
to Jenkins, where they enabled
@@ -77,7 +68,3 @@ used to automate building, [testing](/testing.html) and
* [Automated API testing with Jenkins](https://assertible.com/blog/automated-api-testing-with-jenkins)
walks through how to use Jenkins to tests your
[API](/application-programming-interfaces.html) upon each deployment.
-
-* [Continuous Delivery with Jenkins and Rollbar](https://rollbar.com/blog/continuous-delivery-with-jenkins/)
- is a tutorial on using Jenkins for continuous integration paired with
- [Rollbar](/rollbar.html) for tracking deployments and errors.
diff --git a/content/pages/05-deployment/29-gocd.markdown b/content/pages/05-deployment/29-gocd.markdown
index 3a4f48e21..3bffa4e41 100644
--- a/content/pages/05-deployment/29-gocd.markdown
+++ b/content/pages/05-deployment/29-gocd.markdown
@@ -8,7 +8,7 @@ meta: GoCD is a continuous integration (CI) server often used to automatically b
[GoCD](https://www.gocd.org/) is a
-[continuous integration (CI)](/continuous-integation.html)
+[continuous integration (CI)](/continuous-integration.html)
server often used to automatically build, [test](/testing.html) and
[deploy](/deployment.html) Python applications.
diff --git a/content/pages/05-deployment/32-configuration-management.markdown b/content/pages/05-deployment/32-configuration-management.markdown
index 252df484e..23f4b07ba 100644
--- a/content/pages/05-deployment/32-configuration-management.markdown
+++ b/content/pages/05-deployment/32-configuration-management.markdown
@@ -32,10 +32,6 @@ operations, such as querying the database from the Django manage.py shell.
is an openly biased but detailed post on why to choose SaltStack over
Ansible in certain situations.
-* [Ansible vs. Shell Scripts](https://valdhaus.co/writings/ansible-vs-shell-scripts/)
- provides some perspective on why using a configuration management tool is a
- better choice than venerable but brittle shell scripts.
-
* [Ansible vs. Chef](http://tjheeta.github.io/2015/04/15/ansible-vs-chef/)
is a comparsion of Ansible with the Chef configuration management tool.
@@ -59,17 +55,6 @@ management and application deployment tool built in Python.
is a fantastically detailed introduction on using Ansible to set up
servers.
-* [Ansible Text Message Notifications with Twilio SMS](https://www.twilio.com/blog/2014/05/ansible-text-messages-notifications-with-twilio-sms.html)
- is my blog post with a detailed example for using the Twilio module in
- core Ansible 1.6+.
-
-* [Python for Configuration Management with Ansible slides](http://www.insom.me.uk/post/pycon-talk.html)
-from PyCon UK 2013
-
-* [First Steps with Ansible](http://labs.qandidate.com/blog/2013/11/15/first-steps-with-ansible/)
-
-* [Red Badger on Ansible](http://red-badger.com/blog/2013/06/29/ansible/)
-
* [Getting Started with Ansible](http://lowendbox.com/blog/getting-started-with-ansible/)
* [An introduction to Ansible](https://davidwinter.me/introduction-to-ansible/)
@@ -89,8 +74,6 @@ from PyCon UK 2013
* [Idempotence, convergence, and other silly fancy words we often use](https://groups.google.com/forum/#!msg/Ansible-project/WpRblldA2PQ/lYDpFjBXDlsJ)
-* [Testing with Jenkins, Docker and Ansible](http://blog.mist.io/post/82383668190/move-fast-and-dont-break-things-testing-with)
-
## Application dependencies learning checklist
1. Learn about configuration management in the context of deployment
diff --git a/content/pages/05-deployment/33-ansible.markdown b/content/pages/05-deployment/33-ansible.markdown
index a64409f26..bbdff142a 100644
--- a/content/pages/05-deployment/33-ansible.markdown
+++ b/content/pages/05-deployment/33-ansible.markdown
@@ -38,9 +38,6 @@ be able to structure your playbooks:
configures macOS with various applications and developer tools such as
[Docker](/docker.html), Homebrew and [Sublime Text](/sublime-text.html).
-* [ansible-nginx-haproxy-elasticsearch](https://github.com/gp187/ansible-nginx-haproxy-elasticsearch)
- sets up a server with [Nginx](/nginx.html), HAProxy and ElasticSearch.
-
### Specific Ansible topics
* [An Ansible2 Tutorial](https://serversforhackers.com/c/an-ansible2-tutorial)
@@ -63,10 +60,6 @@ be able to structure your playbooks:
* [DevOps from Scratch, Part 1: Vagrant & Ansible](https://www.kevinlondon.com/2016/09/19/devops-from-scratch-pt-1.html)
-* [Ansible: Post-Install Setup](https://valdhaus.co/writings/ansible-post-install/)
-
-* [How To Use Vault to Protect Sensitive Ansible Data on Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-use-vault-to-protect-sensitive-ansible-data-on-ubuntu-16-04)
-
* [How to use Ansible Variables and Vaults](https://www.expressvpn.com/blog/ansible-variables-vaults/)
* [CI for Ansible playbooks which require Ansible Vault protected variables](https://www.jeffgeerling.com/blog/2017/ci-ansible-playbooks-which-require-ansible-vault-protected-variables)
diff --git a/content/pages/05-deployment/35-containers.markdown b/content/pages/05-deployment/35-containers.markdown
index 76fb8b62c..5ec0a850e 100644
--- a/content/pages/05-deployment/35-containers.markdown
+++ b/content/pages/05-deployment/35-containers.markdown
@@ -55,6 +55,11 @@ useful.
on building jails and other process isolation abstractions into
operating systems.
+* [Linux containers in a few lines of code](https://zserge.com/posts/containers/)
+ shows how containers work by providing some code to run a busybox
+ Docker image but without using docker. It then explains what's
+ happening under the hood as you run basic commands such as `/bin/sh`.
+
* [A Practical Introduction to Container Terminology](https://developers.redhat.com/blog/2018/02/22/container-terminology-practical-introduction/)
has both some solid introductory information on containers as well as
a good description of terms such as container host, registry server,
@@ -65,6 +70,12 @@ useful.
explains how Linux features such as `cgroups`, `chroot` and namespaces
are used by container implementations.
+* [Container networking is simple](https://iximiuz.com/en/posts/container-networking-is-simple/)
+ shows that container networking is nothing more than a simple combination
+ of the well-known Linux facilities such as network namespaces, virtual
+ Ethernet devices (veth), virtual network switches (bridge) and
+ IP routing and network address translation (NAT).
+
* [Running containers without Docker](https://jvns.ca/blog/2016/10/26/running-container-without-docker/)
reviews a migration path for an organization that already has a bunch of
infrastructure but sees advantages in using containers. However, the
@@ -72,6 +83,11 @@ useful.
eventually plan to use Docker, Kubernetes or other container tools and
orchestration layer.
+* [Datadog's 2020 Container Report](https://www.datadoghq.com/container-report/)
+ contains some interesting statistics about container usage across
+ their customer base, such as [Kubernetes](/kubernetes.html) adoption
+ and container deployments by cloud platform.
+
* [mocker](https://github.com/tonybaloney/mocker) is a Docker imitation
open source project written in all Python which is intended for learning
purposes.
@@ -110,6 +126,13 @@ Container security is a hot topic because there are so many ways of screwing
it up, just like any infrastructure that runs your applications. These
resources explain security considerations specific to containers.
+* [A Practical Introduction to Container Security](https://cloudberry.engineering/article/practical-introduction-container-security/)
+ examines security at build time for projects and how to
+ minimize the risk of supply chain attack. It then goes into
+ infrastructure and runtime security where you need to understand
+ different attack vectors and minimize malicious attempts against
+ your containers during these phases..
+
* [Building Container Images Securely on Kubernetes](https://blog.jessfraz.com/post/building-container-images-securely-on-kubernetes/)
discusses some of the issues with building containers and why the
author created [img](https://github.com/genuinetools/img) as a tool
diff --git a/content/pages/05-deployment/36-docker.markdown b/content/pages/05-deployment/36-docker.markdown
index ed7052dee..f133b70f0 100644
--- a/content/pages/05-deployment/36-docker.markdown
+++ b/content/pages/05-deployment/36-docker.markdown
@@ -36,6 +36,10 @@ on Amazon Web Services, Google Compute Engine, Linode, Rackspace or elsewhere.
## Docker resources
+* [Beginners guide to Docker](https://www.learncloudnative.com/blog/2020-04-29-beginners-guide-to-docker/)
+ explains what it is, the difference between containers and virtual machines,
+ and then provides a hands-on walkthrough command-driven tutorial.
+
* [What is Docker and When to Use It](https://www.ctl.io/developers/blog/post/what-is-docker-and-when-to-use-it/)
clearly delineates what Docker is and what it isn't. This is a good article
for when you're first wrapping your head around Docker conceptually.
@@ -44,20 +48,10 @@ on Amazon Web Services, Google Compute Engine, Linode, Rackspace or elsewhere.
repository and tutorial that shows you how to recreate a simplified version
of Docker to better understand what it's doing under the hood.
-* [Andrew Baker](https://github.com/atbaker) presented a fantastic tutorial
- at [PyOhio](http://andrewtorkbaker.com/pyohio-docker-101-tutorial) on
- [beginner and advanced Docker usage](https://github.com/atbaker/docker-tutorial).
- Andrew also wrote the article
- [what containers can do for you](http://radar.oreilly.com/2015/01/what-containers-can-do-for-you.html).
-
* [Docker curriculum](http://prakhar.me/docker-curriculum/) is a detailed
tutorial created by a developer to show the exact steps for deploying an
application that relies on [Elasticsearch](https://www.elastic.co/).
-* [How To Install and Use Docker on Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04)
- provides a walkthrough for Ubuntu 16.04 for installing and beginning to
- use Docker for development.
-
* [It Really is the Future](http://blog.circleci.com/it-really-is-the-future/)
discusses Docker and containers in the context of whether it's all just a
bunch of hype or if this is a real trend for infrastructure automation.
@@ -83,10 +77,6 @@ on Amazon Web Services, Google Compute Engine, Linode, Rackspace or elsewhere.
is a short tutorial for creating a Docker container with a specific
configuration.
-* [10 things to avoid in Docker containers](http://developerblog.redhat.com/2016/02/24/10-things-to-avoid-in-docker-containers/)
- provides a lot of "don'ts" that you'll want to consider before bumping
- up against the limitations of how containers should be used.
-
* [Docker Internals](http://docker-saigon.github.io/post/Docker-Internals/) presents
Linux containers and how Docker uses them as its base for how the project works.
This article is a great way to bridge what you know about Docker with a more
diff --git a/content/pages/05-deployment/37-kubernetes.markdown b/content/pages/05-deployment/37-kubernetes.markdown
index 20f8578f8..96e69ef62 100644
--- a/content/pages/05-deployment/37-kubernetes.markdown
+++ b/content/pages/05-deployment/37-kubernetes.markdown
@@ -30,11 +30,11 @@ applications.
* [Kompose](http://kompose.io/index)
([source code](https://github.com/kubernetes/kompose))
- translate Docker Compose files into Kubernetes configuration resources.
+ translates Docker Compose files into Kubernetes configuration resources.
-* [skaffold](https://skaffold.dev/). [Using Kubernetes for local development](https://nemethgergely.com/using-kubernetes-for-local-development/index.html)
- is a good starting place for more information on getting started with
- Skaffold.
+* [skaffold](https://skaffold.dev/)
+ ([source code](https://github.com/GoogleContainerTools/skaffold)) makes
+ it easier to develop locally with Kubernetes.
* [kubethanos](https://github.com/berkay-dincer/kubethanos) is a tool to kill
half of your Kubernetes pods at random, to test the resilience of your
@@ -80,6 +80,18 @@ applications.
is stuffed full of great design advice that is now available as people
having been using Kubernetes for a couple of years.
+* ["Let’s use Kubernetes!" Now you have 8 problems](https://pythonspeed.com/articles/dont-need-kubernetes/)
+ is a counter-argument for why you should be cautious about introducing
+ the significant complexity overhead of Kubernetes (or any related tools)
+ into your environment unless you really need the advantages that they can
+ provide. Each developer, team and organization should perform an explicit
+ cost-benefit analysis to make sure the tool's scability, reliability
+ and related functionality will outweigh the downsides.
+
+* [How Zalando manages 140+ Kubernetes clusters](https://srcco.de/posts/how-zalando-manages-140-kubernetes-clusters.html)
+ covers the architecture, monitoring and workflow of a team that has
+ to run a decent number of clusters for their development teams.
+
### Kubernetes tutorials
* [Kubernetes The Hard Way](https://github.com/kelseyhightower/kubernetes-the-hard-way)
@@ -96,6 +108,10 @@ applications.
is a Git README tutorial with clear steps for how to get started running
a Kubernetes cluster.
+* [Anatomy of my Kubernetes Cluster](https://ttt.io/anatomy-of-my-kubernetes-cluster)
+ shows how one developer created their own Raspberry Pi cluster that could
+ run Kubernetes to learn more about how it works.
+
* [The cult of Kubernetes](https://christine.website/blog/the-cult-of-kubernetes-2019-09-07)
is a hilarious rant that also manages to teach the reader a lot about how to
avoid some big issues the author ran into while working with Kubernetes for
@@ -122,6 +138,6 @@ applications.
uses Helm to make it easier to deploy the example [Django](/django.html)
web app with a [PostgreSQL](/postgresql.html) backend.
-* [How Zolando manages 140+ Kubernetes clusters](https://srcco.de/posts/how-zalando-manages-140-kubernetes-clusters.html)
- covers the architecture, monitoring and workflow of a team that has
- to run a decent number of clusters for their development teams.
+* [K8s YAML Alternative: Python](https://www.phillipsj.net/posts/k8s-yaml-alternative-python/)
+ shows how you can use Python scripts instead of YAML to configure
+ your Kubernetes clusters.
diff --git a/content/pages/05-deployment/38-serverless.markdown b/content/pages/05-deployment/38-serverless.markdown
index 306caa320..a5236c280 100644
--- a/content/pages/05-deployment/38-serverless.markdown
+++ b/content/pages/05-deployment/38-serverless.markdown
@@ -45,9 +45,8 @@ These implementations are under significant active development
and not all of them have Python support.
* [AWS Lambda](/aws-lambda.html) is the current leader among serverless
- compute implementations. It has support for both
- [Python 2.7](/blog/aws-lambda-python-2-7.html) and
- [Python 3.6/3.7](/blog/aws-lambda-python-3-6.html).
+ compute implementations. It has support for
+ [Python 3.x](/blog/aws-lambda-python-3-6.html).
* Azure Functions has second-class citizen support for Python. It's
supposed to be possible but
@@ -59,14 +58,11 @@ and not all of them have Python support.
[Apache OpenWhisk](https://github.com/openwhisk/openwhisk)
open source project.
-* [Google Cloud Functions](/google-cloud-functions.html) currently
- only supports JavaScript code execution.
+* [Google Cloud Functions](/google-cloud-functions.html) has
+ [native Python 3.x runtimes](https://cloud.google.com/functions/docs/concepts/python-runtime).
-* Webtask.io also only supports JavaScript but there is a cool
- *prototype* project named [webtask-pytask](https://github.com/tehsis/webtask-pytask)
- to run Python code in the browser via webtask. This demo is definitely not
- for production code use but awesome to see what the programming community
- can put together using existing code and services.
+* [Webtask.io](https://webtask.io/) started as a JavaScript service but
+ now also has a Python runtime as well.
### Serverless frameworks
@@ -83,8 +79,7 @@ include:
which is a useful but generically-named library that focuses on deployment
and operations for serverless applications.
-* [Zappa](https://www.zappa.io/)
- ([source code](https://github.com/Miserlou/Zappa))
+* [Zappa](https://github.com/Miserlou/Zappa)
provides code and tools to make it much easier to build on AWS Lambda
and AWS API Gateway than rolling your own on the bare services.
@@ -92,8 +87,6 @@ include:
([source code](https://github.com/aws/chalice)) is built by the AWS team
specifically for Python applications.
-* [Apex](http://apex.run/) ([source code](https://github.com/apex/apex))
-
### General serverless resources
Serverless concepts and implementations are still in their early
@@ -101,6 +94,12 @@ iterations so there are many ideas and good practices yet to be
discovered. These resources are the first attempts at figuring
out how to structure and operate serverless applications.
+* [What's Serverless?](https://technically.substack.com/p/whats-serverless)
+ is an accessible "first read" for both developers and non-technical
+ audiences alike. It breaks down the differences between what most
+ developers consider serverless and infrastructure-as-a-service (IaaS)
+ offerings.
+
* [Serverless software](https://talkpython.fm/episodes/show/118/serverless-software)
covers a range of topics under serverless and how deployments have
changed as new options such as [PaaS](/platform-as-a-service.html)
@@ -183,37 +182,18 @@ have varying degrees of support for Python. AWS Lambda has
production-ready support for Python 2 and 3.7, while Azure and Google Cloud
have "beta" support with unclear production-worthiness. The following
resources are some comparison articles to help you in your decision-making
-process for which platform to learn.
-
-* [Serverless at scale](https://blog.binaris.com/serverless-at-scale/)
- compares the "Big 3" AWS, Azure and Google Cloud in serverless performance.
- The author provides some nice data around average response times and
- outliers.
-
-* [Serverless hosting comparison](https://headmelted.com/serverless-showdown-4a771ca561d2)
- is a broad overview of documentation, community, pricing and other
- notes for the major platforms as well as IBM OpenWhisk and
- the [Fission.io](https://fission.io/) project.
-
-* [Microsoft Azure Functions vs. Google Cloud Functions vs. AWS Lambda](https://cloudacademy.com/blog/microsoft-azure-functions-vs-google-cloud-functions-fight-for-serverless-cloud-domination-continues/)
- presents an overview of Azure Functions and how they compare to
- Google Cloud Functions and AWS Lambda.
+process for which platform to learn.
+[Microsoft Azure Functions vs. Google Cloud Functions vs. AWS Lambda](https://cloudacademy.com/blog/microsoft-azure-functions-vs-google-cloud-functions-fight-for-serverless-cloud-domination-continues/)
+presents an overview of Azure Functions and how they compare to
+Google Cloud Functions and AWS Lambda.
### Serverless vendor lock-in?
There is some concern by organizations and developers about vendor lock-in
on serverless platforms. It is unclear if portability is worse for
serverless than other infrastructure-as-a-service pieces, but still worth
-thinking about ahead of time. These resources provide additional
-perspectives on lock-in and using multiple cloud providers.
-
-* [On Serverless, Multi-Cloud, and Vendor Lock In](https://blog.symphonia.io/on-serverless-multi-cloud-and-vendor-lock-in-da930b3993f)
- is an opinion piece that for *most* cases the additional work of
- going multi-cloud is not worth the tradeoffs, therefore at this time
- it's better to go for a single vendor such as AWS or Azure and optimize
- on that platform.
-
-* [Why vendor lock-in with serverless isn’t what you think it is](https://medium.com/@PaulDJohnston/why-vendor-lock-in-with-serverless-isnt-what-you-think-it-is-d6be40fa9ca9)
- recommends using a single vendor for now and stop worrying about
- hedging your bets because it typically makes your infrastructure
- significantly more complex.
+thinking about ahead of time.
+[Why vendor lock-in with serverless isn’t what you think it is](https://medium.com/@PaulDJohnston/why-vendor-lock-in-with-serverless-isnt-what-you-think-it-is-d6be40fa9ca9)
+is a piece on this topic that recommends using a single vendor for
+now and for organizations to stop worrying about hedging their bets
+because it typically makes infrastructure significantly more complex.
diff --git a/content/pages/05-deployment/39-aws-lambda.markdown b/content/pages/05-deployment/39-aws-lambda.markdown
index ab177e024..80efa5ef7 100644
--- a/content/pages/05-deployment/39-aws-lambda.markdown
+++ b/content/pages/05-deployment/39-aws-lambda.markdown
@@ -38,8 +38,7 @@ has support for both Python 2.7, 3.6 and 3.7.
* [Zappa](https://github.com/Miserlou/Zappa) is a serverless framework
for deploying Python web applications. It's a really slick project
and used even by internal AWS developers for their own application
- deployments. Be sure to [read the Zappa blog](https://blog.zappa.io/)
- as well for walkthroughs and new feature announcements.
+ deployments.
* [How to Setup a Serverless URL Shortener With API Gateway Lambda and DynamoDB on AWS](https://blog.ruanbekker.com/blog/2018/11/30/how-to-setup-a-serverless-url-shortener-with-api-gateway-lambda-and-dynamodb-on-aws/)
builds a non-trivial URL shortener application as an example Python
@@ -53,10 +52,6 @@ has support for both Python 2.7, 3.6 and 3.7.
provides a screen capture of one developer deploying their
application to Lambda.
-* [Automated SQL Injection Testing of Serverless Functions On a Shoestring Budget (and Some Good Music)](https://www.puresec.io/blog/automated-sql-injection-testing-of-serverless-functions-on-a-shoestring-budget-and-some-good-music)
- is an awesome operational security post that uses Python to test
- for SQL injection vulnerabilities in serverless functions on AWS Lambda.
-
* [Building Scikit-Learn For AWS Lambda](https://serverlesscode.com/post/scikitlearn-with-amazon-linux-container/)
follows up on the
[Using Scikit-Learn In AWS Lambda](https://serverlesscode.com/post/deploy-scikitlearn-on-lamba/)
@@ -70,20 +65,12 @@ has support for both Python 2.7, 3.6 and 3.7.
* [Code Evaluation With AWS Lambda and API Gateway](https://realpython.com/blog/python/code-evaluation-with-aws-lambda-and-api-gateway/)
shows how to develop a code evaluation API, to execute arbitrary code, with AWS Lambda and API Gateway.
-* [Crawling thousands of products using AWS Lambda](https://engineering.21buttons.com/crawling-thousands-of-products-using-aws-lambda-80332e259de1)
- gives a real-world example of where using Python, Selenium and
- [headless Chrome](https://developers.google.com/web/updates/2017/04/headless-chrome)
- on AWS Lambda could crawl thousands of pages to collect data
- with each crawler running within its own Lambda Function.
-
-* [Going Serverless with AWS Lambda and API Gateway](http://blog.ryankelly.us/2016/08/07/going-serverless-with-aws-lambda-and-api-gateway.html)
-
### General AWS Lambda resources
-* [AWS Lambda Serverless Reference Architectures](http://www.allthingsdistributed.com/2016/06/aws-lambda-serverless-reference-architectures.html)
- provides blueprints with diagrams of common architecture patterns that
- developers use for their mobile backend, file processing, stream
- processing and web application projects.
+* [Getting started with serverless on AWS](https://emshea.com/post/serverless-getting-started)
+ is a wonderful tutorials, example projects and additional resources
+ guide created by a developer who used all of these bits to learn
+ AWS services herself.
* [Security Overview of AWS Lambda](https://d1.awsstatic.com/whitepapers/Overview-AWS-Lambda-Security.pdf)
(PDF file) covers their "Shared Responsibility Model" for security and
diff --git a/content/pages/05-deployment/40-azure-functions.markdown b/content/pages/05-deployment/40-azure-functions.markdown
index 4cb8243e1..7f718f64a 100644
--- a/content/pages/05-deployment/40-azure-functions.markdown
+++ b/content/pages/05-deployment/40-azure-functions.markdown
@@ -44,6 +44,3 @@ in other Azure services.
what is confusing to newcomers that hopefully will be addressed
as Microsoft continues to work on their Azure platform.
-* [Azure in Plain English](https://www.expeditedssl.com/azure-in-plain-english)
- covers all of the Azure services and explains them because their
- default names are often too vague to understand their purpose.
diff --git a/content/pages/06-devops/00-devops.markdown b/content/pages/06-devops/00-devops.markdown
index 47ec79be1..f6053c2e4 100644
--- a/content/pages/06-devops/00-devops.markdown
+++ b/content/pages/06-devops/00-devops.markdown
@@ -35,8 +35,8 @@ tools and services for DevOps environments.
which when used properly can enable continuous software delivery.
* For an Atlassian-centric perspective on tooling, take a look at
- this post on how to
- [choose the right DevOps tools](http://blogs.atlassian.com/2016/03/how-to-choose-devops-tools/)
+ this guide on how to
+ [choose the right DevOps tools](https://www.atlassian.com/devops/devops-tools)
which is biased towards their tools but still has some good insight
such as using automated testing to provide immediate awareness of
defects that require fixing.
@@ -47,10 +47,10 @@ The following resources give advice and approaches for building the right
teams, culture, processes and tools into software development organizations.
* [DevOps vs. Platform Engineering](https://alexgaynor.net/2015/mar/06/devops-vs-platform-engineering/)
- considers DevOps an ad hoc approach to developing software while building
- a platform is a strict contract. I see this as "DevOps is a process",
- while a "platform is code". Running code is better than any organizational
- process.
+ considers DevOps to be an ad hoc approach to developing software while
+ building a platform is a strict contract. I see this as "DevOps is a
+ process", while a "platform is code". Running code is better than any
+ organizational process.
* The open source
[PagerDuty Incident Response guide](https://response.pagerduty.com/) is the
@@ -58,6 +58,12 @@ teams, culture, processes and tools into software development organizations.
their services running and putting them out for other developers to consume.
Highly recommended.
+* [Introduction to DevOps and Software Delivery Performance](https://www.stridenyc.com/blog/devops-and-software-delivery-performance)
+ explains the four key delivery metrics of Delivery Lead Time,
+ Deployment Frequency, Time to Restore Service, and Change Fail Rate,
+ and then gives a high-level overview of technical, process and
+ cultural capabilities that impact these metrics.
+
* [Operations for software developers for beginners](https://jvns.ca/blog/2016/10/15/operations-for-software-developers-for-beginners/)
gives advice to developers who have never done operations work and
been on call for outages before in their career. The advantage of DevOps
@@ -82,10 +88,6 @@ teams, culture, processes and tools into software development organizations.
your pager goes off, ownership and how startups can be different from
large companies with their incident responses.
-* [Bing: Continuous Delivery](http://stories.visualstudio.com/bing-continuous-delivery/)
- is an impressive visual story that explains the practices for how their
- team delivers updates to the search engine.
-
* [Why are we racing to DevOps?](http://www.cio.com/article/3015237/application-development/why-are-we-racing-to-devops.html)
is a very high level summary of the benefits of DevOps to IT organizations.
It's not specific to Python and doesn't dive into the details, but it's
diff --git a/content/pages/06-devops/01-monitoring.markdown b/content/pages/06-devops/01-monitoring.markdown
index 95efe2cf4..50021eecf 100644
--- a/content/pages/06-devops/01-monitoring.markdown
+++ b/content/pages/06-devops/01-monitoring.markdown
@@ -112,7 +112,7 @@ Application Performance Monitoring (APM)
* [New Relic](http://newrelic.com/) provides application and database
monitoring as well as plug ins for capturing and analyzing data about
- other devleoper tools in your stack, such as [Twilio](/twilio.html).
+ other developer tools in your stack, such as [Twilio](/twilio.html).
* [Opbeat](https://opbeat.com) Built for django. Opbeat combines performance
metrics, release tracking, and error logging into a single simple service.
* [Scout](https://scoutapp.com/python-monitoring) monitors the performance of Django and Flask apps, auto-instrumenting views, SQL queries, templates, and more.
@@ -146,11 +146,6 @@ Incident Management
* [The Virtues of Monitoring](http://www.paperplanes.de/2011/1/5/the_virtues_of_monitoring.html)
-* [Effortless Monitoring with collectd, Graphite, and Docker](http://blog.docker.io/2013/07/effortless-monitoring-with-collectd-graphite-and-docker/)
-
-* [Practical Guide to StatsD/Graphite Monitoring](http://matt.aimonetti.net/posts/2013/06/26/practical-guide-to-graphite-monitoring/)
- is a detailed guide with code examples for monitoring infrastructure.
-
* Bit.ly describes the
"[10 Things They Forgot to Monitor](http://word.bitly.com/post/74839060954/ten-things-to-monitor)"
beyond the standard metrics such as disk & memory usage.
diff --git a/content/pages/06-devops/03-prometheus.markdown b/content/pages/06-devops/03-prometheus.markdown
index 54eeddc20..7974550b9 100644
--- a/content/pages/06-devops/03-prometheus.markdown
+++ b/content/pages/06-devops/03-prometheus.markdown
@@ -16,6 +16,11 @@ and report on Python [web applications](/web-development.html).
### Prometheus resources
+* [Prometheus-Basics](https://github.com/yolossn/Prometheus-Basics)
+ is a newbie's introduction to this tool. It covers what Prometheus
+ is, the tool's architecture, types of metrics and contains a
+ walkthrough of how to get it configured.
+
* This primer on [Prometheus](https://www.kartar.net/2017/10/prometheus/)
walks through installation, configuration and metrics collection.
diff --git a/content/pages/06-devops/06-web-app-performance.markdown b/content/pages/06-devops/06-web-app-performance.markdown
index 7a3a2c8e6..8a954546f 100644
--- a/content/pages/06-devops/06-web-app-performance.markdown
+++ b/content/pages/06-devops/06-web-app-performance.markdown
@@ -49,10 +49,6 @@ database queries, page size and many other factors.
is a 20 minute code-first demo that shows how to get a realistic estimate
for how many requests per second your web application will be able to handle.
-* [How to Interpret Site Performance Tests](https://fly.io/articles/how-to-understand-performance-tests/)
- covers the difference between client, page and connection speed tests
- as well as a bit on caching performance.
-
* [Practical scaling techniques for websites](https://hackernoon.com/practical-scaling-techniques-for-web-sites-554a38dbd492)
examines how to improve your website performance with asynchronous
[task queues](/task-queues.html), [database](/databases.html) optimization
@@ -67,6 +63,10 @@ database queries, page size and many other factors.
provides a list with a crazy number of scaling and performance optimization
resources and tools by category.
+* [Every Web Performance Test Tool](https://www.swyx.io/writing/webperf-tests/)
+ provides a nice list of tools and provides short summaries of what each
+ one can help with in identifying performance problems.
+
* [The Infrastructure Behind Twitter: Scale](https://blog.twitter.com/engineering/en_us/topics/infrastructure/2017/the-infrastructure-behind-twitter-scale.html)
examines the evolution from having to buy your own hardware from vendors
to run a service to the current days of being able to rely on cloud
@@ -76,3 +76,8 @@ database queries, page size and many other factors.
covers the architecture scaling techniques commonly used to move up
in serving users by orders of magnitude, for example from 100 to 1000.
+* [Web Performance Recipes with Puppeteer](https://addyosmani.com/blog/puppeteer-recipes/)
+ digs into tracing through page rendering to measure performance and
+ how to extract performance metrics from the
+ [Lighthouse](https://developers.google.com/web/tools/lighthouse/) tool
+ for further analysis.
diff --git a/content/pages/06-devops/11-caching.markdown b/content/pages/06-devops/11-caching.markdown
index 5a8e94abf..c8aebbbf1 100644
--- a/content/pages/06-devops/11-caching.markdown
+++ b/content/pages/06-devops/11-caching.markdown
@@ -45,19 +45,10 @@ A cache can be created for multiple layers of the stack.
reading even though the author is describing his Microsoft code as the
impetus for writing the content.
-* While caching is a useful technique in many situations, it's important
- to also note that there are
- [downsides to caching](https://msol.io/blog/tech/2015/09/05/youre-probably-wrong-about-caching/)
- that many developers fail to take into consideration.
-
* [Caching at Reddit](https://redditblog.com/2017/1/17/caching-at-reddit/)
covers monitoring, tuning and scaling for the very high scale
[Reddit.com](https://www.reddit.com/) website.
-* [Mastering HTTP caching](https://blog.fortrabbit.com/mastering-http-caching)
- provides more advanced advice on caching dynamic as well as static
- content via CDNs and other configurations.
-
## Caching learning checklist
diff --git a/content/pages/06-devops/14-logging.markdown b/content/pages/06-devops/14-logging.markdown
index 3fa5b0e30..9738d98fc 100644
--- a/content/pages/06-devops/14-logging.markdown
+++ b/content/pages/06-devops/14-logging.markdown
@@ -55,8 +55,9 @@ certain threshold.
There are libraries for most major languages, including python. Saves data
in Elasticache.
-* [Logstash](http://logstash.net/) Similar to Graylog2, logstash offers
- features to programmatically configure log data workflows.
+* [Logstash](https://www.elastic.co/guide/en/logstash/current/index.html).
+ Similar to Graylog2, logstash offers features to programmatically
+ configure log data workflows.
* [Scribe](https://github.com/facebook/scribe) A project written by Facebook
to aggregate logs. It's designed to run on multiple servers and scale with
@@ -112,7 +113,7 @@ certain threshold.
* [Good logging practice in Python](http://victorlin.me/posts/2012/08/26/good-logging-practice-in-python)
shows how to use the standard library to log data from your application.
Definitely worth a read as most applications do not log nearly enough
- output to help debuggin when things go wrong, or to determine if something
+ output to help debugging when things go wrong, or to determine if something
is going wrong.
* [Structured Logging: The Best Friend You’ll Want When Things Go Wrong](https://engineering.grab.com/structured-logging)
diff --git a/content/pages/06-devops/18-web-analytics.markdown b/content/pages/06-devops/18-web-analytics.markdown
index c51330f04..6430b1057 100644
--- a/content/pages/06-devops/18-web-analytics.markdown
+++ b/content/pages/06-devops/18-web-analytics.markdown
@@ -38,9 +38,13 @@ application before taking some action, such as purchasing your service.
## Open source web analytics projects
-* [Piwik](http://piwik.org/) is a web analytics platform you can host yourself.
- Piwik is a solid choice if you cannot use Google Analytics or want to
- customize your own web analytics platform.
+* [Matoma](https://matomo.org/) (formerly Piwik), is a web analytics
+ platform that you can host yourself. Matoma is a solid choice if you
+ cannot use Google Analytics or want to customize your own web analytics
+ software.
+
+* [Shynet](https://github.com/milesmcc/shynet) is a lightweight, privacy-friendly
+ cookie-free web analytics application written in Python.
* [Open Web Analytics](http://www.openwebanalytics.com/) is another
self-hosted platform that integrates through a JavaScript snippet that
@@ -59,10 +63,6 @@ application before taking some action, such as purchasing your service.
collected into the server side or client side code. MixPanel captures that
data and provides metrics and visualizations based on the data.
-* [KISSmetrics](https://www.kissmetrics.com/)' analytics provides context
- for who is visiting a website and what actions they are taking while on
- the site.
-
* [Heap](https://heapanalytics.com/) is a recently founded analytics service
with a free introductory tier to get started.
@@ -110,10 +110,6 @@ application before taking some action, such as purchasing your service.
is not for everyone but it is really useful if you want to avoid the Google
data trap.
-* This beginner's guide to
- [math and stats behind web analytics](http://www.seotakeaways.com/beginners-guide-maths-stats-web-analytics/)
- provides some context for understanding and reasoning about web traffic.
-
* [An Analytics Primer for Developers](https://hacks.mozilla.org/2015/03/an-analytics-primer-for-developers/)
by Mozilla explains what to track, choosing an analytics platform and how
to serve up the analytics JavaScript asynchronously.
@@ -143,8 +139,8 @@ application before taking some action, such as purchasing your service.
## Web analytics learning checklist
-1. Add Google Analytics or Piwik to your application. Both are free and while
- Piwik is not as powerful as Google Analytics you can self-host the
+1. Add Google Analytics or Matoma to your application. Both are free and while
+ Matoma is not as powerful as Google Analytics you can self-host the
application which is the only option in many environments.
1. Think critically about the factors that will make your application
diff --git a/content/pages/10-working/00-gpt-3.markdown b/content/pages/10-working/00-gpt-3.markdown
new file mode 100644
index 000000000..04e1726a5
--- /dev/null
+++ b/content/pages/10-working/00-gpt-3.markdown
@@ -0,0 +1,171 @@
+title: GPT-3
+category: page
+slug: gpt-3
+sortorder: 1001
+toc: False
+sidebartitle: GPT-3
+meta: GPT-3 is a trained neural network with 175 billion parameters that allows it to be significantly better at text generation than previous models.
+
+
+[GPT-3](https://arxiv.org/abs/2005.14165) is a neural network
+trained by the [OpenAI](https://openai.com/) organization with
+significantly more parameters than previous generation models.
+
+There are several variations of GPT-3, which range from 125 to 175 billion
+parameters. The different variations allow the model to better respond to
+different types of input, such as a question & answer format, long-form
+writing, human language translations (e.g. English to French). The large
+numbers of parameters make GPT-3 significantly better at natural
+language processing and text generation than the prior model,
+[GPT-2](https://openai.com/blog/gpt-2-1-5b-release/), which only had
+1.5 billion parameters.
+
+
+
+GPT-3 can only currently be access by an
+[API provided by OpenAI](https://openai.com/blog/openai-api/), which is
+in private beta.
+
+
+## What's so special about GPT-3?
+The GPT-3 model can generate texts of up to 50,000 characters, with no
+supervision. It can even generate creative Shakespearean-style fiction
+stories in addition to fact-based writing. This is the first time that a
+neural network model has been able to generate texts at an acceptable
+quality that makes it difficult, if not impossible, for a typical
+person to whether the output was written by a human or GPT-3.
+
+
+## How does GPT-3 work?
+To generate output, GPT-3 has a very large vocabulary, which it can
+combine to generate sentences. These words are sorted
+into different categories (nouns, verbs, adjectives, etc.), and for each
+category, there is a “production rule”, which can be used to generate a
+sentence. The production rules can be modified with different parameters.
+
+A few examples:
+
+* noun + verb = subject + verb
+* noun + verb + adjective = subject + verb + adjective
+* verb + noun = subject + verb
+* noun + verb + noun = subject + verb + noun
+* noun + noun = subject + noun
+* noun + verb + noun + noun = subject + verb + noun + noun
+
+In addition, GPT-3 is able to understand negations, as well as the use
+of tenses, which allows the model to generate sentences in the past,
+present and future.
+
+
+## Does GPT-3 matter to Python developers?
+GPT-3 is not that useful right now for programmers other than as an
+experiment. If you get access to [OpenAI's API](https://openai.com/blog/openai-api/)
+then Python is an easy language to use for interacting with it and
+you could use its text generation as inputs into your applications.
+Although there have been some initial impressive experiments in
+generating code for
+[the layout of the Google homepage](https://twitter.com/sharifshameem/status/1283322990625607681),
+[JSX output](https://twitter.com/sharifshameem/status/1282676454690451457),
+and [other technical demos](https://twitter.com/__MLT__/status/1287357881675853825),
+the
+[model will otherwise not (yet) put any developers out of a job](https://www.youtube.com/watch?v=Yg3C38P5EkA)
+who are coding real-world applications.
+
+
+## How was GPT-3 trained?
+At a high level, training the GPT-3 neural network consists of two steps.
+
+The first step requires creating the vocabulary, the different
+categories and the production rules. This is done by feeding
+GPT-3 with books. For each word, the model must predict the category
+to which the word belongs, and then, a production rule must be created.
+
+The second step consists of creating a vocabulary and production rules
+for each category. This is done by feeding the model with sentences.
+For each sentence, the model must predict the category to which each
+word belongs, and then, a production rule must be created.
+
+The result of the training is a vocabulary, and production rules for each
+category.
+
+The model also has a few tricks that allow it to improve its ability to
+generate texts. For example, it is able to guess the beginning of a word
+by observing the context of the word. It can also predict the next word
+by looking at the last word of a sentence. It is also able to predict the
+length of a sentence.
+
+While those two steps and the related tricks may sound simple in theory,
+in practice they require massive amounts of computation. Training
+175 billion parameters in mid-2020 cost in the ballpark of
+[$4.6 million dollars](https://lambdalabs.com/blog/demystifying-gpt-3/#:~:text=But%20to%20put%20things%20into,for%20a%20single%20training%20run.),
+although some other estimates calculated it could take up
+to $12 million depending on how the hardware was provisioned.
+
+
+## GPT-3 resources
+These resources range from broad philosophy of what GPT-3 means
+for machine learning to specific technical details for how the model
+is trained.
+
+* [The Ultimate Guide to OpenAI's GPT-3 Language Model](https://www.twilio.com/blog/ultimate-guide-openai-gpt-3-language-model)
+ is a detailed tutorial on how to use OpenAI's playground user interface,
+ what the parameters do, and how to convert what you have done in
+ the playground into a Python script that calls their API.
+
+* [OpenAI's GPT-3 Language Model: A Technical Overview](https://lambdalabs.com/blog/demystifying-gpt-3/)
+ and
+ [GPT-3: A Hitchhiker's Guide](https://lambdalabs.com/blog/gpt-3/)
+ are two long-format guides that analyze how GPT-3's technical
+ specifications fit in the larger machine learning ecosystem, quotes by
+ researchers on its usage, and some initial resources to get a
+ better understanding of what this model is capable of performing.
+
+* [What Is GPT-3: How It Works and Why You Should Care](https://www.twilio.com/blog/what-is-gpt-3)
+ presents a high-level accessible overview of GPT-3, how it compares
+ to other language models, and resources to learn more.
+
+* [How GPT3 Works - Visualizations and Animations](https://jalammar.github.io/how-gpt3-works-visualizations-animations/)
+ contains some wonderful animated visuals to show how the model is
+ trained and what happens in various scenarios such as text output
+ and code generation.
+
+* [GPT 3 Demo and Explanation](https://www.youtube.com/watch?v=8psgEDhT1MM)
+ is a video that gives a brief overview of GPT-3 and shows a bunch
+ of live demos for what has so far been created with this technology.
+
+* [Tempering expectations for GPT-3](https://minimaxir.com/2020/07/gpt3-expectations/)
+ points out that many of the good examples on social media have been
+ cherry picked to impress readers.
+
+* [Why GPT-3 matters](https://leogao.dev/2020/05/29/GPT-3-A-Brief-Summary/)
+ compares and contrasts this model with similar models that have been
+ developed and tries to give an overview of where each one stands
+ with its strengths and weaknesses.
+
+
+## GPT-3 tutorials
+* [Building a Chatbot with OpenAI's GPT-3 engine, Twilio SMS and Python](https://www.twilio.com/blog/openai-gpt-3-chatbot-python-twilio-sms)
+ is a step-by-step tutorial for using GPT-3 as a smart backend
+ for an SMS-based chatbot powered by the [Twilio API](/twilio.html).
+
+* [Automating my job by using GPT-3 to generate database-ready SQL to answer business questions](https://blog.seekwell.io/gpt3)
+ walks through how the author created a bridge to translate between
+ plain English-language questions and
+ [relational database](/databases.html) SQL. The post provides both
+ a story for why someone would want to use GPT-3 for this purpose and
+ incremental steps for how the author started and figured out how
+ to make it better. In the end it does not quite work in all scenarios
+ but the proof of concept is impressive and the story is a fun read.
+
+* [gpt-3-experiments](https://github.com/minimaxir/gpt-3-experiments)
+ contains Python code open sourced under the MIT license that
+ shows how to interact with the API.
+
+* [Twilio](/twilio.html) put out a series of fun GPT-3 tutorials that show
+ the range of creative outputs the model can generate:
+
+ * [Control a Spooky Ghost Writer for Halloween with OpenAI's GPT-3 Engine, Python, and Twilio WhatsApp API](https://www.twilio.com/blog/ghost-writer-spooky-openai-gpt3-python-whatsapp)
+ * [Generating Lyrics in the Style of your Favorite Artist with Python, OpenAI's GPT-3 and Twilio SMS](https://www.twilio.com/blog/generating-lyrics-in-the-style-of-your-favorite-artist-with-python-openai-s-gpt-3-and-twilio-sms)
+ * [Automated Yugioh Deckbuilding in Python with OpenAI's GPT-3 and Twilio SMS](https://www.twilio.com/blog/building-computer-generated-yugioh-decks-in-python-with-openai-s-gpt-3-and-twilio-sms)
+ * [Build a Telephone Chatbot with GPT-3 and Twilio Autopilot](https://www.twilio.com/blog/build-telephone-chatbot-gpt3-twilio-autopilot)
+
diff --git a/content/pages/10-working/01-event-streams.markdown b/content/pages/10-working/01-event-streams.markdown
new file mode 100644
index 000000000..0daa6e45f
--- /dev/null
+++ b/content/pages/10-working/01-event-streams.markdown
@@ -0,0 +1,58 @@
+title: Event Streams
+category: page
+slug: event-streams
+sortorder: 1002
+toc: False
+sidebartitle: Event Streams
+meta: An event stream is a log of one or more events.
+
+
+Event streams are a log of one or more "things that happen", which are
+usually referred to as events. Event streams are
+conceptually focused around events than objects or tables, which are
+the typical storage unit of [relational databases](/databases.html).
+
+Apache Kafka and Gazette are a popular open source implementations of event
+streams. Amazon Web Services' Kinesis and
+[Azure Event-Hubs](https://azure.microsoft.com/en-us/services/event-hubs/)
+are proprietary hosted implementations.
+
+
+## Why do event streams matter to developers?
+The way that data is stored affects how you can work with it. Constraints
+and guarantees like consistency make it easier to code certain applications
+but harder to build other types of applications that need performance in
+different ways. Event streams make it easier to build applications that
+analyze large amounts of constantly-updated data because the events are
+not stored relationally.
+
+
+## How are event streams typically stored?
+Some applications, such as aggregating millions of sensors, or thousands
+of streaming cameras, constantly output large amounts of data with no breaks.
+It is difficult to process such large volumes of data in traditional data
+stores, so event streams are built off of a simpler data structure: logs.
+
+Logs are ordered sequences of events and they typically have less constraints
+than a database. In event streams, logs are also immutable. They do not change
+once they are written. Instead, newer events are written in the sequence as
+state changes. The reduced constraints compared to a database and the
+immutability mean that logs can handle the high throughput writes needed to
+keep up with the constant flood of data from the source of an event stream.
+
+
+## Event stream resources
+* [What is Apache Kafka?](https://www.youtube.com/watch?v=FKgi3n-FyNU) sounds
+ like it just focuses on Kafka but it actually covers the fundamental
+ concepts behind event streams and how they fit into
+ [microservices](/microservices.html) architectures.
+
+* Quora has a solid answer to the question of
+ [what is an event stream?](https://www.quora.com/What-is-an-event-stream).
+
+* [Summary of the Amazon Kinesis Event in the Northern Virginia (US-EAST-1) Region](https://aws.amazon.com/message/11201/)
+ is specific to AWS Kinesis but it explains how Amazon uses event
+ streams at scale to run and coordinate a significant number of their
+ services. When their event streams service went down... it took a
+ whole lot of other stuff down at the same time. There is also some
+ [additional analysis in this post by an independent developer](https://ryanfrantz.com/posts/aws-kinesis-outage-analysis.html).
diff --git a/content/pages/10-working/18-developer-demos.md b/content/pages/10-working/18-developer-demos.md
new file mode 100644
index 000000000..a195edfdc
--- /dev/null
+++ b/content/pages/10-working/18-developer-demos.md
@@ -0,0 +1,29 @@
+title: Demoing to Software Developers
+category: page
+slug: demo-software-developers
+sortorder: 1018
+toc: False
+sidebartitle: Developer Demos
+meta: How to give technical demos to software developers.
+
+
+Creating and executing an appealing technical demo to an audience of software
+developers is a ton of work, but there is no better way to get people
+legitimately interested in your product if you land a great demo with
+an appropriate audience. The inherent difficulty involved in exceptional
+technical demos also creates a large barrier that prevent others from simply
+copying your work, which can happen with technical blog content.
+
+To achieve a standout technical demo you must:
+
+1. show how to easily solve a difficult technical problem
+1. speak plainly but accurately
+1. do it live, including writing code if required
+1. tell a story with a narrative arc
+1. rehearse constantly, both the happy path and recovering from errors
+
+
+Examples:
+* [Twilio Phone Calls Demo at NY Tech Meetup (2010)](https://www.youtube.com/watch?v=-VuXIgp9S7o)
+* [Concurrency from the Ground Up (2015)](https://www.youtube.com/watch?v=MCs5OvhV9S4)
+* [The Mother of All Demos (1968)](https://www.youtube.com/watch?v=yJDv-zdhzMY)
diff --git a/content/pages/examples/celery/celery-example-projects-code.markdown b/content/pages/examples/celery/celery-example-projects-code.markdown
new file mode 100644
index 000000000..ba474a42f
--- /dev/null
+++ b/content/pages/examples/celery/celery-example-projects-code.markdown
@@ -0,0 +1,17 @@
+title: Celery Example Projects and Code
+category: page
+slug: celery-code-examples
+sortorder: 500040001
+toc: False
+sidebartitle: Celery Example Code
+meta: Python example projects and code for using the Celery asynchronous task queue.
+
+
+## Example Projects with Great Example Code
+The following active projects use the [Celery](/celery.html) task queue
+in various ways. The code within the projects can show you how to build
+your own applications.
+
+
+### flask-celery-example
+
diff --git a/content/pages/examples/django/django-conf-urls-url.markdown b/content/pages/examples/django/django-conf-urls-url.markdown
index 6540e8fe8..23f28e83c 100644
--- a/content/pages/examples/django/django-conf-urls-url.markdown
+++ b/content/pages/examples/django/django-conf-urls-url.markdown
@@ -128,7 +128,7 @@ urlpatterns = [
## Example 3 from register
[register](https://github.com/ORGAN-IZE/register) is a [Django](/django.html),
-[Bootstrap](/bootstrap.html), [PostgreSQL](/postgresql.html) project that is
+[Bootstrap](/bootstrap-css.html), [PostgreSQL](/postgresql.html) project that is
open source under the
[GNU General Public License v3.0](https://github.com/ORGAN-IZE/register/blob/master/LICENSE).
This web application makes it easier for people to register as organ donors.
diff --git a/content/pages/examples/django/django-contrib-admin-helpers-actionform.markdown b/content/pages/examples/django/django-contrib-admin-helpers-actionform.markdown
new file mode 100644
index 000000000..322d1d28a
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-helpers-actionform.markdown
@@ -0,0 +1,102 @@
+title: django.contrib.admin.helpers ActionForm Example Code
+category: page
+slug: django-contrib-admin-helpers-actionform-examples
+sortorder: 500011020
+toc: False
+sidebartitle: django.contrib.admin.helpers ActionForm
+meta: Python example code for the ActionForm class from the django.contrib.admin.helpers module of the Django project.
+
+
+[ActionForm](https://github.com/django/django/blob/master/django/contrib/admin/helpers.py)
+is a class within the django.contrib.admin.helpers module of the
+[Django](/django.html) project. It is not typically used when creating
+applications but is sometimes used by libraries that want to extend the
+[actions available](https://docs.djangoproject.com/en/stable/ref/contrib/admin/actions/)
+within the
+[Django Admin](https://docs.djangoproject.com/en/stable/ref/contrib/admin/).
+
+
+## Example 1 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / forms.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./forms.py)
+
+```python
+# forms.py
+import os.path
+
+from django import forms
+~~from django.contrib.admin.helpers import ActionForm
+from django.utils.translation import gettext_lazy as _
+
+
+class ImportForm(forms.Form):
+ import_file = forms.FileField(
+ label=_('File to import')
+ )
+ input_format = forms.ChoiceField(
+ label=_('Format'),
+ choices=(),
+ )
+
+ def __init__(self, import_formats, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ choices = []
+ for i, f in enumerate(import_formats):
+ choices.append((str(i), f().get_title(),))
+ if len(import_formats) > 1:
+ choices.insert(0, ('', '---'))
+
+ self.fields['input_format'].choices = choices
+
+
+class ConfirmImportForm(forms.Form):
+
+
+## ... source file abbreviated to get to ActionForm examples ...
+
+
+
+ def clean_import_file_name(self):
+ data = self.cleaned_data['import_file_name']
+ data = os.path.basename(data)
+ return data
+
+
+class ExportForm(forms.Form):
+ file_format = forms.ChoiceField(
+ label=_('Format'),
+ choices=(),
+ )
+
+ def __init__(self, formats, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ choices = []
+ for i, f in enumerate(formats):
+ choices.append((str(i), f().get_title(),))
+ if len(formats) > 1:
+ choices.insert(0, ('', '---'))
+
+ self.fields['file_format'].choices = choices
+
+
+def export_action_form_factory(formats):
+~~ class _ExportActionForm(ActionForm):
+ file_format = forms.ChoiceField(
+ label=_('Format'), choices=formats, required=False)
+ _ExportActionForm.__name__ = str('ExportActionForm')
+
+ return _ExportActionForm
+
+
+
+## ... source file continues with no further ActionForm examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-helpers-adminform.markdown b/content/pages/examples/django/django-contrib-admin-helpers-adminform.markdown
new file mode 100644
index 000000000..27b6fe19d
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-helpers-adminform.markdown
@@ -0,0 +1,120 @@
+title: django.contrib.admin.helpers AdminForm Example Code
+category: page
+slug: django-contrib-admin-helpers-adminform-examples
+sortorder: 500011021
+toc: False
+sidebartitle: django.contrib.admin.helpers AdminForm
+meta: Python example code for the AdminForm class from the django.contrib.admin.helpers module of the Django project.
+
+
+[AdminForm](https://github.com/django/django/blob/master/django/contrib/admin/helpers.py)
+is a class within the django.contrib.admin.helpers module of the
+[Django](/django.html) project. AdminForm is not usually used directly by
+developers but can be used by libraries that want to extend the
+[forms](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/admindocs/)
+within the
+[Django Admin](https://docs.djangoproject.com/en/stable/ref/contrib/admin/).
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / admin / placeholderadmin.py**](https://github.com/divio/django-cms/blob/develop/cms/admin/placeholderadmin.py)
+
+```python
+# placeholderadmin.py
+import uuid
+import warnings
+
+from django.conf.urls import url
+~~from django.contrib.admin.helpers import AdminForm
+from django.contrib.admin.utils import get_deleted_objects
+from django.core.exceptions import PermissionDenied
+from django.db import router, transaction
+from django.http import (
+ HttpResponse,
+ HttpResponseBadRequest,
+ HttpResponseForbidden,
+ HttpResponseNotFound,
+ HttpResponseRedirect,
+)
+from django.shortcuts import get_list_or_404, get_object_or_404, render
+from django.template.response import TemplateResponse
+from django.utils.decorators import method_decorator
+from django.utils.encoding import force_text
+from django.utils import translation
+from django.utils.translation import ugettext as _
+from django.views.decorators.clickjacking import xframe_options_sameorigin
+from django.views.decorators.http import require_POST
+
+from six.moves.urllib.parse import parse_qsl, urlparse
+
+from six import get_unbound_function, get_method_function
+
+from cms import operations
+
+
+## ... source file abbreviated to get to AdminForm examples ...
+
+
+ saved_successfully = False
+ cancel_clicked = request.POST.get("_cancel", False)
+ raw_fields = request.GET.get("edit_fields")
+ fields = [field for field in raw_fields.split(",") if field in self.frontend_editable_fields]
+ if not fields:
+ context = {
+ 'opts': opts,
+ 'message': force_text(_("Field %s not found")) % raw_fields
+ }
+ return render(request, 'admin/cms/page/plugin/error_form.html', context)
+ if not request.user.has_perm("{0}.change_{1}".format(self.model._meta.app_label,
+ self.model._meta.model_name)):
+ context = {
+ 'opts': opts,
+ 'message': force_text(_("You do not have permission to edit this item"))
+ }
+ return render(request, 'admin/cms/page/plugin/error_form.html', context)
+ form_class = self.get_form(request, obj, fields=fields)
+ if not cancel_clicked and request.method == 'POST':
+ form = form_class(instance=obj, data=request.POST)
+ if form.is_valid():
+ form.save()
+ saved_successfully = True
+ else:
+ form = form_class(instance=obj)
+~~ admin_form = AdminForm(form, fieldsets=[(None, {'fields': fields})], prepopulated_fields={},
+ model_admin=self)
+ media = self.media + admin_form.media
+ context = {
+ 'CMS_MEDIA_URL': get_cms_setting('MEDIA_URL'),
+ 'title': opts.verbose_name,
+ 'plugin': None,
+ 'plugin_id': None,
+ 'adminform': admin_form,
+ 'add': False,
+ 'is_popup': True,
+ 'media': media,
+ 'opts': opts,
+ 'change': True,
+ 'save_as': False,
+ 'has_add_permission': False,
+ 'window_close_timeout': 10,
+ }
+ if cancel_clicked:
+ context.update({
+ 'cancel': True,
+ })
+ return render(request, 'admin/cms/page/plugin/confirm_form.html', context)
+ if not cancel_clicked and request.method == 'POST' and saved_successfully:
+ return render(request, 'admin/cms/page/plugin/confirm_form.html', context)
+
+
+## ... source file continues with no further AdminForm examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-helpers.markdown b/content/pages/examples/django/django-contrib-admin-helpers.markdown
new file mode 100644
index 000000000..0e32e48f9
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-helpers.markdown
@@ -0,0 +1,382 @@
+title: django.contrib.admin helpers Example Code
+category: page
+slug: django-contrib-admin-helpers-examples
+sortorder: 500011017
+toc: False
+sidebartitle: django.contrib.admin helpers
+meta: Python example code for the helpers callable from the django.contrib.admin module of the Django project.
+
+
+[helpers](https://github.com/django/django/blob/master/django/contrib/admin/helpers.py)
+is a module within the [Django](/django.html) project code base. It
+contains classes related to extending the functionality of the
+[Django Admin](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/actions/),
+such as [ActionForm](/django-contrib-admin-helpers-actionform-examples.html)
+and [AdminForm](/django-contrib-admin-helpers-adminform-examples.html).
+
+
+## Example 1 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / admin / folderadmin.py**](https://github.com/divio/django-filer/blob/develop/filer/admin/folderadmin.py)
+
+```python
+# folderadmin.py
+from __future__ import absolute_import, division, unicode_literals
+
+import itertools
+import os
+import re
+from collections import OrderedDict
+
+from django import forms
+from django.conf import settings as django_settings
+from django.conf.urls import url
+from django.contrib import messages
+~~from django.contrib.admin import helpers
+from django.contrib.admin.utils import capfirst, quote, unquote
+from django.core.exceptions import PermissionDenied, ValidationError
+from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
+from django.db import models, router
+from django.http import HttpResponse, HttpResponseRedirect
+from django.shortcuts import get_object_or_404, render
+from django.urls import reverse
+from django.utils.encoding import force_text
+from django.utils.html import escape
+from django.utils.http import urlquote, urlunquote
+from django.utils.safestring import mark_safe
+from django.utils.translation import ugettext as _
+from django.utils.translation import ugettext_lazy, ungettext
+
+from .. import settings
+from ..models import (
+ File, Folder, FolderPermission, FolderRoot, ImagesWithMissingData,
+ UnsortedImages, tools,
+)
+from ..settings import FILER_IMAGE_MODEL, FILER_PAGINATE_BY
+from ..thumbnail_processors import normalize_subject_location
+from ..utils.compatibility import get_delete_permission
+from ..utils.filer_easy_thumbnails import FilerActionThumbnailer
+from ..utils.loader import load_model
+
+
+## ... source file abbreviated to get to helpers examples ...
+
+
+ if "move-to-clipboard-%d" % (f.id,) in request.POST:
+ clipboard = tools.get_user_clipboard(request.user)
+ if f.has_edit_permission(request):
+ tools.move_file_to_clipboard([f], clipboard)
+ return HttpResponseRedirect(request.get_full_path())
+ else:
+ raise PermissionDenied
+
+ selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
+ if (
+ actions and request.method == 'POST'
+ and 'index' in request.POST
+ and '_save' not in request.POST
+ ):
+ if selected:
+ response = self.response_action(request, files_queryset=file_qs, folders_queryset=folder_qs)
+ if response:
+ return response
+ else:
+ msg = _("Items must be selected in order to perform "
+ "actions on them. No items have been changed.")
+ self.message_user(request, msg)
+
+ if (
+ actions and request.method == 'POST'
+~~ and helpers.ACTION_CHECKBOX_NAME in request.POST
+ and 'index' not in request.POST
+ and '_save' not in request.POST
+ ):
+ if selected:
+ response = self.response_action(request, files_queryset=file_qs, folders_queryset=folder_qs)
+ if response:
+ return response
+
+ if actions:
+ action_form = self.action_form(auto_id=None)
+ action_form.fields['action'].choices = self.get_action_choices(request)
+ else:
+ action_form = None
+
+ selection_note_all = ungettext('%(total_count)s selected',
+ 'All %(total_count)s selected', paginator.count)
+
+ try:
+ paginated_items = paginator.page(request.GET.get('page', 1))
+ except PageNotAnInteger:
+ paginated_items = paginator.page(1)
+ except EmptyPage:
+ paginated_items = paginator.page(paginator.num_pages)
+
+
+
+## ... source file abbreviated to get to helpers examples ...
+
+
+ self.log_deletion(request, f, force_text(f))
+ f.delete()
+ self.message_user(request, _("Successfully deleted %(count)d files and/or folders.") % {"count": n, })
+ return None
+
+ if all_perms_needed or all_protected:
+ title = _("Cannot delete files and/or folders")
+ else:
+ title = _("Are you sure?")
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": title,
+ "instance": current_folder,
+ "breadcrumbs_action": _("Delete files and/or folders"),
+ "deletable_objects": all_deletable_objects,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": all_perms_needed,
+ "protected": all_protected,
+ "opts": opts,
+ 'is_popup': popup_status(request),
+ 'filer_admin_context': AdminContext(request),
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+~~ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+ return render(
+ request,
+ "admin/filer/delete_selected_files_confirmation.html",
+ context
+ )
+
+ delete_files_or_folders.short_description = ugettext_lazy(
+ "Delete selected files and/or folders")
+
+ def _format_callback(self, obj, user, admin_site, perms_needed):
+ has_admin = obj.__class__ in admin_site._registry
+ opts = obj._meta
+ if has_admin:
+ admin_url = reverse('%s:%s_%s_change'
+ % (admin_site.name,
+ opts.app_label,
+ opts.object_name.lower()),
+ None, (quote(obj._get_pk_val()),))
+ p = get_delete_permission(opts)
+ if not user.has_perm(p):
+ perms_needed.add(opts.verbose_name)
+ return mark_safe('%s: %s' %
+
+
+## ... source file abbreviated to get to helpers examples ...
+
+
+ conflicting_names = [folder.name for folder in self.get_queryset(request).filter(parent=destination, name__in=folders_queryset.values('name'))]
+ if conflicting_names:
+ messages.error(request, _("Folders with names %s already exist at the selected "
+ "destination") % ", ".join(conflicting_names))
+ elif n:
+ self._move_files_and_folders_impl(files_queryset, folders_queryset, destination)
+ self.message_user(request, _("Successfully moved %(count)d files and/or folders to folder '%(destination)s'.") % {
+ "count": n,
+ "destination": destination,
+ })
+ return None
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Move files and/or folders"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Move files and/or folders"),
+ "to_move": to_move,
+ "destination_folders": folders,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+~~ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+ return render(request, "admin/filer/folder/choose_move_destination.html", context)
+
+ move_files_and_folders.short_description = ugettext_lazy("Move selected files and/or folders")
+
+ def _rename_file(self, file_obj, form_data, counter, global_counter):
+ original_basename, original_extension = os.path.splitext(file_obj.original_filename)
+ if file_obj.name:
+ current_basename, current_extension = os.path.splitext(file_obj.name)
+ else:
+ current_basename = ""
+ current_extension = ""
+ file_obj.name = form_data['rename_format'] % {
+ 'original_filename': file_obj.original_filename,
+ 'original_basename': original_basename,
+ 'original_extension': original_extension,
+ 'current_filename': file_obj.name or "",
+ 'current_basename': current_basename,
+ 'current_extension': current_extension,
+ 'current_folder': getattr(file_obj.folder, 'name', ''),
+ 'counter': counter + 1, # 1-based
+ 'global_counter': global_counter + 1, # 1-based
+ }
+
+
+## ... source file abbreviated to get to helpers examples ...
+
+
+ raise PermissionDenied
+ form = RenameFilesForm(request.POST)
+ if form.is_valid():
+ if files_queryset.count() + folders_queryset.count():
+ n = self._rename_files_impl(files_queryset, folders_queryset, form.cleaned_data, 0)
+ self.message_user(request, _("Successfully renamed %(count)d files.") % {
+ "count": n,
+ })
+ return None
+ else:
+ form = RenameFilesForm()
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Rename files"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Rename files"),
+ "to_rename": to_rename,
+ "rename_form": form,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+~~ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+ return render(request, "admin/filer/folder/choose_rename_format.html", context)
+
+ rename_files.short_description = ugettext_lazy("Rename files")
+
+ def _generate_new_filename(self, filename, suffix):
+ basename, extension = os.path.splitext(filename)
+ return basename + suffix + extension
+
+ def _copy_file(self, file_obj, destination, suffix, overwrite):
+ if overwrite:
+ raise NotImplementedError
+
+
+ filename = self._generate_new_filename(file_obj.file.name, suffix)
+
+ file_obj.pk = None
+ file_obj.id = None
+ file_obj.save()
+ file_obj.folder = destination
+ file_obj._file_data_changed_hint = False # no need to update size, sha1, etc.
+ file_obj.file = file_obj._copy_file(filename)
+ file_obj.original_filename = self._generate_new_filename(file_obj.original_filename, suffix)
+
+
+## ... source file abbreviated to get to helpers examples ...
+
+
+ form = CopyFilesAndFoldersForm()
+
+ try:
+ selected_destination_folder = int(request.POST.get('destination', 0))
+ except ValueError:
+ if current_folder:
+ selected_destination_folder = current_folder.pk
+ else:
+ selected_destination_folder = 0
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Copy files and/or folders"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Copy files and/or folders"),
+ "to_copy": to_copy,
+ "destination_folders": folders,
+ "selected_destination_folder": selected_destination_folder,
+ "copy_form": form,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+~~ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+ return render(request, "admin/filer/folder/choose_copy_destination.html", context)
+
+ copy_files_and_folders.short_description = ugettext_lazy("Copy selected files and/or folders")
+
+ def _check_resize_perms(self, request, files_queryset, folders_queryset):
+ try:
+ check_files_read_permissions(request, files_queryset)
+ check_folder_read_permissions(request, folders_queryset)
+ check_files_edit_permissions(request, files_queryset)
+ except PermissionDenied:
+ return True
+ return False
+
+ def _list_folders_to_resize(self, request, folders):
+ for fo in folders:
+ children = list(self._list_folders_to_resize(request, fo.children.all()))
+ children.extend([self._format_callback(f, request.user, self.admin_site, set()) for f in sorted(fo.files) if isinstance(f, Image)])
+ if children:
+ yield self._format_callback(fo, request.user, self.admin_site, set())
+ yield children
+
+ def _list_all_to_resize(self, request, files_queryset, folders_queryset):
+
+
+## ... source file abbreviated to get to helpers examples ...
+
+
+ form.cleaned_data['width'] = form.cleaned_data['thumbnail_option'].width
+ form.cleaned_data['height'] = form.cleaned_data['thumbnail_option'].height
+ form.cleaned_data['crop'] = form.cleaned_data['thumbnail_option'].crop
+ form.cleaned_data['upscale'] = form.cleaned_data['thumbnail_option'].upscale
+ if files_queryset.count() + folders_queryset.count():
+ n = self._resize_images_impl(files_queryset, folders_queryset, form.cleaned_data)
+ self.message_user(request, _("Successfully resized %(count)d images.") % {"count": n, })
+ return None
+ else:
+ form = ResizeImagesForm()
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Resize images"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Resize images"),
+ "to_resize": to_resize,
+ "resize_form": form,
+ "cmsplugin_enabled": 'cmsplugin_filer_image' in django_settings.INSTALLED_APPS,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+~~ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+ return render(request, "admin/filer/folder/choose_images_resize_options.html", context)
+
+ resize_images.short_description = ugettext_lazy("Resize selected images")
+
+
+
+## ... source file continues with no further helpers examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-options-csrf-protect-m.markdown b/content/pages/examples/django/django-contrib-admin-options-csrf-protect-m.markdown
new file mode 100644
index 000000000..334ae0b80
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-options-csrf-protect-m.markdown
@@ -0,0 +1,116 @@
+title: django.contrib.admin.options csrf_protect_m code examples
+category: page
+slug: django-contrib-admin-options-csrf-protect-m-examples
+sortorder: 500011025
+toc: False
+sidebartitle: django.contrib.admin.options csrf_protect_m
+meta: Python example code for the csrf_protect_m function from the django.contrib.admin.options module of the Django project.
+
+
+csrf_protect_m is a function within the django.contrib.admin.options module of the Django project.
+
+
+## Example 1 from django-haystack
+[django-haystack](https://github.com/django-haystack/django-haystack)
+([project website](http://haystacksearch.org/) and
+[PyPI page](https://pypi.org/project/django-haystack/))
+is a search abstraction layer that separates the Python search code
+in a [Django](/django.html) web application from the search engine
+implementation that it runs on, such as
+[Apache Solr](http://lucene.apache.org/solr/),
+[Elasticsearch](https://www.elastic.co/)
+or [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html).
+
+The django-haystack project is open source under the
+[BSD license](https://github.com/django-haystack/django-haystack/blob/master/LICENSE).
+
+[**django-haystack / haystack / admin.py**](https://github.com/django-haystack/django-haystack/blob/master/haystack/./admin.py)
+
+```python
+# admin.py
+~~from django.contrib.admin.options import ModelAdmin, csrf_protect_m
+from django.contrib.admin.views.main import SEARCH_VAR, ChangeList
+from django.core.exceptions import PermissionDenied
+from django.core.paginator import InvalidPage, Paginator
+from django.shortcuts import render
+from django.utils.encoding import force_str
+from django.utils.translation import ungettext
+
+from haystack import connections
+from haystack.constants import DEFAULT_ALIAS
+from haystack.query import SearchQuerySet
+from haystack.utils import get_model_ct_tuple
+
+
+class SearchChangeList(ChangeList):
+ def __init__(self, **kwargs):
+ self.haystack_connection = kwargs.pop("haystack_connection", DEFAULT_ALIAS)
+ super(SearchChangeList, self).__init__(**kwargs)
+
+ def get_results(self, request):
+ if SEARCH_VAR not in request.GET:
+ return super(SearchChangeList, self).get_results(request)
+
+ sqs = (
+ SearchQuerySet(self.haystack_connection)
+
+
+## ... source file abbreviated to get to csrf_protect_m examples ...
+
+
+ result_count = paginator.count
+ full_result_count = (
+ SearchQuerySet(self.haystack_connection).models(self.model).all().count()
+ )
+
+ can_show_all = result_count <= self.list_max_show_all
+ multi_page = result_count > self.list_per_page
+
+ try:
+ result_list = paginator.page(self.page_num + 1).object_list
+ result_list = [result.object for result in result_list]
+ except InvalidPage:
+ result_list = ()
+
+ self.result_count = result_count
+ self.full_result_count = full_result_count
+ self.result_list = result_list
+ self.can_show_all = can_show_all
+ self.multi_page = multi_page
+ self.paginator = paginator
+
+
+class SearchModelAdminMixin(object):
+ haystack_connection = DEFAULT_ALIAS
+
+~~ @csrf_protect_m
+ def changelist_view(self, request, extra_context=None):
+ if not self.has_change_permission(request, None):
+ raise PermissionDenied
+
+ if SEARCH_VAR not in request.GET:
+ return super(SearchModelAdminMixin, self).changelist_view(
+ request, extra_context
+ )
+
+ indexed_models = (
+ connections[self.haystack_connection]
+ .get_unified_index()
+ .get_indexed_models()
+ )
+
+ if self.model not in indexed_models:
+ return super(SearchModelAdminMixin, self).changelist_view(
+ request, extra_context
+ )
+
+ list_display = list(self.list_display)
+
+ kwargs = {
+ "haystack_connection": self.haystack_connection,
+
+
+## ... source file continues with no further csrf_protect_m examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-options-incorrectlookupparameters.markdown b/content/pages/examples/django/django-contrib-admin-options-incorrectlookupparameters.markdown
new file mode 100644
index 000000000..56d6cf44e
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-options-incorrectlookupparameters.markdown
@@ -0,0 +1,137 @@
+title: django.contrib.admin.options IncorrectLookupParameters code examples
+category: page
+slug: django-contrib-admin-options-incorrectlookupparameters-examples
+sortorder: 500011023
+toc: False
+sidebartitle: django.contrib.admin.options IncorrectLookupParameters
+meta: Python example code for the IncorrectLookupParameters class from the django.contrib.admin.options module of the Django project.
+
+
+IncorrectLookupParameters is a class within the django.contrib.admin.options module of the Django project.
+
+
+## Example 1 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / utils.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/./utils.py)
+
+```python
+# utils.py
+from django.template import Context
+from django.utils import translation
+from jet import settings
+from jet.models import PinnedApplication
+
+try:
+ from django.apps.registry import apps
+except ImportError:
+ try:
+ from django.apps import apps # Fix Django 1.7 import issue
+ except ImportError:
+ pass
+from django.core.serializers.json import DjangoJSONEncoder
+from django.http import HttpResponse
+try:
+ from django.core.urlresolvers import reverse, resolve, NoReverseMatch
+except ImportError: # Django 1.11
+ from django.urls import reverse, resolve, NoReverseMatch
+
+from django.contrib.admin import AdminSite
+from django.utils.encoding import smart_text
+from django.utils.text import capfirst
+from django.contrib import messages
+from django.utils.encoding import force_text
+from django.utils.functional import Promise
+~~from django.contrib.admin.options import IncorrectLookupParameters
+from django.contrib import admin
+from django.utils.translation import ugettext_lazy as _
+from django.utils.text import slugify
+
+try:
+ from collections import OrderedDict
+except ImportError:
+ from ordereddict import OrderedDict # Python 2.6
+
+
+class JsonResponse(HttpResponse):
+
+ def __init__(self, data, encoder=DjangoJSONEncoder, safe=True, **kwargs):
+ if safe and not isinstance(data, dict):
+ raise TypeError('In order to allow non-dict objects to be '
+ 'serialized set the safe parameter to False')
+ kwargs.setdefault('content_type', 'application/json')
+ data = json.dumps(data, cls=encoder)
+ super(JsonResponse, self).__init__(content=data, **kwargs)
+
+
+def get_app_list(context, order=True):
+ admin_site = get_admin_site(context)
+ request = context['request']
+
+
+## ... source file abbreviated to get to IncorrectLookupParameters examples ...
+
+
+ if hasattr(model_admin, 'get_search_fields') else model_admin.search_fields
+ list_select_related = model_admin.get_list_select_related(request) \
+ if hasattr(model_admin, 'get_list_select_related') else model_admin.list_select_related
+
+ actions = model_admin.get_actions(request)
+ if actions:
+ list_display = ['action_checkbox'] + list(list_display)
+
+ ChangeList = model_admin.get_changelist(request)
+
+ change_list_args = [
+ request, model, list_display, list_display_links, list_filter,
+ model_admin.date_hierarchy, search_fields, list_select_related,
+ model_admin.list_per_page, model_admin.list_max_show_all,
+ model_admin.list_editable, model_admin]
+
+ try:
+ sortable_by = model_admin.get_sortable_by(request)
+ change_list_args.append(sortable_by)
+ except AttributeError:
+ pass
+
+ try:
+ cl = ChangeList(*change_list_args)
+ queryset = cl.get_queryset(request)
+~~ except IncorrectLookupParameters:
+ pass
+
+ return queryset
+
+
+def get_possible_language_codes():
+ language_code = translation.get_language()
+
+ language_code = language_code.replace('_', '-').lower()
+ language_codes = []
+
+ split = language_code.split('-', 2)
+ if len(split) == 2:
+ language_code = '%s-%s' % (split[0].lower(), split[1].upper()) if split[0] != split[1] else split[0]
+
+ language_codes.append(language_code)
+
+ if len(split) == 2:
+ language_codes.append(split[0].lower())
+
+ return language_codes
+
+
+def get_original_menu_items(context):
+
+
+## ... source file continues with no further IncorrectLookupParameters examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-options-is-popup-var.markdown b/content/pages/examples/django/django-contrib-admin-options-is-popup-var.markdown
new file mode 100644
index 000000000..d19f241c2
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-options-is-popup-var.markdown
@@ -0,0 +1,154 @@
+title: django.contrib.admin.options IS_POPUP_VAR code examples
+category: page
+slug: django-contrib-admin-options-is-popup-var-examples
+sortorder: 500011022
+toc: False
+sidebartitle: django.contrib.admin.options IS_POPUP_VAR
+meta: Python example code for the IS_POPUP_VAR class from the django.contrib.admin.options module of the Django project.
+
+
+IS_POPUP_VAR is a class within the django.contrib.admin.options module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / admin / pageadmin.py**](https://github.com/divio/django-cms/blob/develop/cms/admin/pageadmin.py)
+
+```python
+# pageadmin.py
+from collections import namedtuple
+import copy
+import json
+import sys
+import uuid
+
+
+import django
+from django.contrib.admin.helpers import AdminForm
+from django.conf import settings
+from django.conf.urls import url
+from django.contrib import admin, messages
+from django.contrib.admin.models import LogEntry, CHANGE
+~~from django.contrib.admin.options import IS_POPUP_VAR
+from django.contrib.admin.utils import get_deleted_objects
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.sites.models import Site
+from django.core.exceptions import (ObjectDoesNotExist,
+ PermissionDenied, ValidationError)
+from django.db import router, transaction
+from django.db.models import Q, Prefetch
+from django.http import (
+ HttpResponseRedirect,
+ HttpResponse,
+ Http404,
+ HttpResponseBadRequest,
+ HttpResponseForbidden,
+)
+from django.shortcuts import render, get_object_or_404
+from django.template.defaultfilters import escape
+from django.template.loader import get_template
+from django.template.response import SimpleTemplateResponse, TemplateResponse
+from django.utils.encoding import force_text
+from django.utils.translation import ugettext, ugettext_lazy as _, get_language
+from django.utils.decorators import method_decorator
+from django.views.decorators.http import require_POST
+from django.http import QueryDict
+
+
+
+## ... source file continues with no further IS_POPUP_VAR examples...
+
+```
+
+
+## Example 2 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / admin / tools.py**](https://github.com/divio/django-filer/blob/develop/filer/admin/tools.py)
+
+```python
+# tools.py
+from __future__ import absolute_import, unicode_literals
+
+~~from django.contrib.admin.options import IS_POPUP_VAR
+from django.core.exceptions import PermissionDenied
+from django.utils.http import urlencode
+
+
+ALLOWED_PICK_TYPES = ('folder', 'file')
+
+
+def check_files_edit_permissions(request, files):
+ for f in files:
+ if not f.has_edit_permission(request):
+ raise PermissionDenied
+
+
+def check_folder_edit_permissions(request, folders):
+ for f in folders:
+ if not f.has_edit_permission(request):
+ raise PermissionDenied
+ check_files_edit_permissions(request, f.files)
+ check_folder_edit_permissions(request, f.children.all())
+
+
+def check_files_read_permissions(request, files):
+ for f in files:
+ if not f.has_read_permission(request):
+
+
+## ... source file abbreviated to get to IS_POPUP_VAR examples ...
+
+
+ params = params or {}
+ if popup_status(request):
+ params[IS_POPUP_VAR] = '1'
+ pick_type = popup_pick_type(request)
+ if pick_type:
+ params['_pick'] = pick_type
+ return params
+
+
+def admin_url_params_encoded(request, first_separator='?', params=None):
+ params = urlencode(
+ sorted(admin_url_params(request, params=params).items())
+ )
+ if not params:
+ return ''
+ return '{0}{1}'.format(first_separator, params)
+
+
+class AdminContext(dict):
+ def __init__(self, request):
+ super(AdminContext, self).__init__()
+ self.update(admin_url_params(request))
+
+ def __missing__(self, key):
+ if key == 'popup':
+~~ return self.get(IS_POPUP_VAR, False) == '1'
+ elif key == 'pick':
+ return self.get('_pick', '')
+ elif key.startswith('pick_'):
+ return self.get('_pick', '') == key.split('pick_')[1]
+
+ def __getattr__(self, name):
+ if name in ('popup', 'pick') or name.startswith('pick_'):
+ return self.get(name)
+ raise AttributeError
+
+
+
+## ... source file continues with no further IS_POPUP_VAR examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-options-modeladmin.markdown b/content/pages/examples/django/django-contrib-admin-options-modeladmin.markdown
new file mode 100644
index 000000000..93bd414bf
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-options-modeladmin.markdown
@@ -0,0 +1,94 @@
+title: django.contrib.admin.options ModelAdmin code examples
+category: page
+slug: django-contrib-admin-options-modeladmin-examples
+sortorder: 500011024
+toc: False
+sidebartitle: django.contrib.admin.options ModelAdmin
+meta: Python example code for the ModelAdmin class from the django.contrib.admin.options module of the Django project.
+
+
+ModelAdmin is a class within the django.contrib.admin.options module of the Django project.
+
+
+## Example 1 from django-haystack
+[django-haystack](https://github.com/django-haystack/django-haystack)
+([project website](http://haystacksearch.org/) and
+[PyPI page](https://pypi.org/project/django-haystack/))
+is a search abstraction layer that separates the Python search code
+in a [Django](/django.html) web application from the search engine
+implementation that it runs on, such as
+[Apache Solr](http://lucene.apache.org/solr/),
+[Elasticsearch](https://www.elastic.co/)
+or [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html).
+
+The django-haystack project is open source under the
+[BSD license](https://github.com/django-haystack/django-haystack/blob/master/LICENSE).
+
+[**django-haystack / haystack / admin.py**](https://github.com/django-haystack/django-haystack/blob/master/haystack/./admin.py)
+
+```python
+# admin.py
+~~from django.contrib.admin.options import ModelAdmin, csrf_protect_m
+from django.contrib.admin.views.main import SEARCH_VAR, ChangeList
+from django.core.exceptions import PermissionDenied
+from django.core.paginator import InvalidPage, Paginator
+from django.shortcuts import render
+from django.utils.encoding import force_str
+from django.utils.translation import ungettext
+
+from haystack import connections
+from haystack.constants import DEFAULT_ALIAS
+from haystack.query import SearchQuerySet
+from haystack.utils import get_model_ct_tuple
+
+
+class SearchChangeList(ChangeList):
+ def __init__(self, **kwargs):
+ self.haystack_connection = kwargs.pop("haystack_connection", DEFAULT_ALIAS)
+ super(SearchChangeList, self).__init__(**kwargs)
+
+ def get_results(self, request):
+ if SEARCH_VAR not in request.GET:
+ return super(SearchChangeList, self).get_results(request)
+
+ sqs = (
+ SearchQuerySet(self.haystack_connection)
+
+
+## ... source file abbreviated to get to ModelAdmin examples ...
+
+
+ "cl": changelist,
+ "media": media,
+ "has_add_permission": self.has_add_permission(request),
+ "opts": changelist.opts,
+ "app_label": self.model._meta.app_label,
+ "action_form": action_form,
+ "actions_on_top": self.actions_on_top,
+ "actions_on_bottom": self.actions_on_bottom,
+ "actions_selection_counter": getattr(self, "actions_selection_counter", 0),
+ }
+ context.update(extra_context or {})
+ request.current_app = self.admin_site.name
+ app_name, model_name = get_model_ct_tuple(self.model)
+ return render(
+ request,
+ self.change_list_template
+ or [
+ "admin/%s/%s/change_list.html" % (app_name, model_name),
+ "admin/%s/change_list.html" % app_name,
+ "admin/change_list.html",
+ ],
+ context,
+ )
+
+
+~~class SearchModelAdmin(SearchModelAdminMixin, ModelAdmin):
+ pass
+
+
+
+## ... source file continues with no further ModelAdmin examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-sites-notregistered.markdown b/content/pages/examples/django/django-contrib-admin-sites-notregistered.markdown
new file mode 100644
index 000000000..731cd623b
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-sites-notregistered.markdown
@@ -0,0 +1,66 @@
+title: django.contrib.admin.sites NotRegistered code examples
+category: page
+slug: django-contrib-admin-sites-notregistered-examples
+sortorder: 500011026
+toc: False
+sidebartitle: django.contrib.admin.sites NotRegistered
+meta: Python example code for the NotRegistered class from the django.contrib.admin.sites module of the Django project.
+
+
+NotRegistered is a class within the django.contrib.admin.sites module of the Django project.
+
+
+## Example 1 from django-sitetree
+[django-sitetree](https://github.com/idlesign/django-sitetree)
+([project documentation](https://django-sitetree.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-sitetree/))
+is a [Django](/django.html) extension that makes it easier for
+developers to add site trees, menus and breadcrumb navigation elements
+to their web applications.
+
+The django-sitetree project is provided as open source under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/idlesign/django-sitetree/blob/master/LICENSE).
+
+[**django-sitetree / sitetree / admin.py**](https://github.com/idlesign/django-sitetree/blob/master/sitetree/./admin.py)
+
+```python
+# admin.py
+from typing import Tuple, Type, Optional
+
+from django import forms
+from django.conf import settings as django_settings
+from django.conf.urls import url
+from django.contrib import admin
+from django.contrib import messages
+~~from django.contrib.admin.sites import NotRegistered
+from django.http import HttpResponseRedirect, HttpRequest, HttpResponse
+from django.urls import get_urlconf, get_resolver
+from django.utils.translation import gettext_lazy as _
+
+from .fields import TreeItemChoiceField
+from .settings import MODEL_TREE, MODEL_TREE_ITEM
+from .utils import get_tree_model, get_tree_item_model, get_app_n_model
+
+if False: # pragma: nocover
+ from .models import TreeItemBase, TreeBase # noqa
+
+
+SMUGGLER_INSTALLED = 'smuggler' in django_settings.INSTALLED_APPS
+
+MODEL_TREE_CLASS = get_tree_model()
+MODEL_TREE_ITEM_CLASS = get_tree_item_model()
+
+_TREE_ADMIN = lambda: TreeAdmin
+_ITEM_ADMIN = lambda: TreeItemAdmin
+
+
+def get_model_url_name(model_nfo: Tuple[str, str], page: str, with_namespace: bool = False) -> str:
+ prefix = ''
+ if with_namespace:
+
+
+## ... source file continues with no further NotRegistered examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-admin-sites-site.markdown b/content/pages/examples/django/django-contrib-admin-sites-site.markdown
new file mode 100644
index 000000000..2aabe9df0
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-admin-sites-site.markdown
@@ -0,0 +1,290 @@
+title: django.contrib.admin.sites site code examples
+category: page
+slug: django-contrib-admin-sites-site-examples
+sortorder: 500011027
+toc: False
+sidebartitle: django.contrib.admin.sites site
+meta: Python example code for the site function from the django.contrib.admin.sites module of the Django project.
+
+
+site is a function within the django.contrib.admin.sites module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / tests / test_admin.py**](https://github.com/divio/django-cms/blob/develop/cms/tests/test_admin.py)
+
+```python
+# test_admin.py
+import json
+import datetime
+
+from djangocms_text_ckeditor.cms_plugins import TextPlugin
+from djangocms_text_ckeditor.models import Text
+from django.contrib import admin
+from django.contrib.admin.models import LogEntry
+~~from django.contrib.admin.sites import site
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import Permission
+from django.contrib.sites.models import Site
+from django.urls import reverse
+from django.http import (Http404, HttpResponseBadRequest,
+ HttpResponseNotFound)
+from django.utils.encoding import force_text, smart_str
+from django.utils import timezone
+
+from cms import api
+from cms.api import create_page, create_title, add_plugin, publish_page
+from cms.admin.pageadmin import PageAdmin
+from cms.constants import TEMPLATE_INHERITANCE_MAGIC
+from cms.models import StaticPlaceholder
+from cms.models.pagemodel import Page, PageType
+from cms.models.permissionmodels import GlobalPagePermission, PagePermission
+from cms.models.placeholdermodel import Placeholder
+from cms.models.pluginmodel import CMSPlugin
+from cms.models.titlemodels import Title
+from cms.test_utils import testcases as base
+from cms.test_utils.testcases import (
+ CMSTestCase, URL_CMS_PAGE_DELETE, URL_CMS_PAGE,URL_CMS_TRANSLATION_DELETE,
+ URL_CMS_PAGE_CHANGE_LANGUAGE, URL_CMS_PAGE_CHANGE,
+ URL_CMS_PAGE_PUBLISHED,
+)
+from cms.utils.conf import get_cms_setting
+from cms.utils.urlutils import admin_reverse
+
+
+class AdminTestsBase(CMSTestCase):
+ @property
+ def admin_class(self):
+~~ return site._registry[Page]
+
+ def _get_guys(self, admin_only=False, use_global_permissions=True):
+ admin_user = self.get_superuser()
+
+ if admin_only:
+ return admin_user
+ staff_user = self._get_staff_user(use_global_permissions)
+ return admin_user, staff_user
+
+ def _get_staff_user(self, use_global_permissions=True):
+ USERNAME = 'test'
+
+ if get_user_model().USERNAME_FIELD == 'email':
+ normal_guy = get_user_model().objects.create_user(USERNAME, 'test@test.com', 'test@test.com')
+ else:
+ normal_guy = get_user_model().objects.create_user(USERNAME, 'test@test.com', USERNAME)
+
+ normal_guy.is_staff = True
+ normal_guy.is_active = True
+ perms = Permission.objects.filter(
+ codename__in=['change_page', 'change_title', 'add_page', 'add_title', 'delete_page', 'delete_title']
+ )
+ normal_guy.save()
+ normal_guy.user_permissions.set(perms)
+ if use_global_permissions:
+ gpp = GlobalPagePermission.objects.create(
+ user=normal_guy,
+ can_change=True,
+ can_delete=True,
+ can_change_advanced_settings=False,
+ can_publish=True,
+ can_change_permissions=False,
+ can_move_page=True,
+ )
+ gpp.sites.set(Site.objects.all())
+ return normal_guy
+
+
+class AdminTestCase(AdminTestsBase):
+
+ def test_extension_not_in_admin(self):
+ admin_user, staff = self._get_guys()
+ with self.login_user_context(admin_user):
+ request = self.get_request(URL_CMS_PAGE_CHANGE % 1, 'en',)
+~~ response = site.index(request)
+ self.assertNotContains(response, '/mytitleextension/')
+ self.assertNotContains(response, '/mypageextension/')
+
+ def test_2apphooks_with_same_namespace(self):
+ PAGE1 = 'Test Page'
+ PAGE2 = 'Test page 2'
+ APPLICATION_URLS = 'project.sampleapp.urls'
+
+ admin_user, normal_guy = self._get_guys()
+
+ current_site = Site.objects.get(pk=1)
+
+ page = create_page(PAGE1, "nav_playground.html", "en",
+ site=current_site, created_by=admin_user)
+ page2 = create_page(PAGE2, "nav_playground.html", "en",
+ site=current_site, created_by=admin_user)
+
+ page.application_urls = APPLICATION_URLS
+ page.application_namespace = "space1"
+ page.save()
+ page2.application_urls = APPLICATION_URLS
+ page2.save()
+
+ page_data = {
+
+
+## ... source file abbreviated to get to site examples ...
+
+
+ admin_user = self.get_superuser()
+ create_page("home", "nav_playground.html", "en",
+ created_by=admin_user, published=True)
+ page = create_page("delete-page", "nav_playground.html", "en",
+ created_by=admin_user, published=True)
+ create_page('child-page', "nav_playground.html", "de",
+ created_by=admin_user, published=True, parent=page)
+ body = page.placeholders.get(slot='body')
+ add_plugin(body, 'TextPlugin', 'en', body='text')
+ page.publish('en')
+ with self.login_user_context(admin_user):
+ data = {'post': 'yes'}
+ response = self.client.post(URL_CMS_PAGE_DELETE % page.pk, data)
+ self.assertRedirects(response, URL_CMS_PAGE)
+
+ def test_search_fields(self):
+ superuser = self.get_superuser()
+ from django.contrib.admin import site
+
+ with self.login_user_context(superuser):
+~~ for model, admin_instance in site._registry.items():
+ if model._meta.app_label != 'cms':
+ continue
+ if not admin_instance.search_fields:
+ continue
+ url = admin_reverse('cms_%s_changelist' % model._meta.model_name)
+ response = self.client.get('%s?q=1' % url)
+ errmsg = response.content
+ self.assertEqual(response.status_code, 200, errmsg)
+
+ def test_pagetree_filtered(self):
+ superuser = self.get_superuser()
+ create_page("root-page", "nav_playground.html", "en",
+ created_by=superuser, published=True)
+ with self.login_user_context(superuser):
+ url = admin_reverse('cms_page_changelist')
+ response = self.client.get('%s?template__exact=nav_playground.html' % url)
+ errmsg = response.content
+ self.assertEqual(response.status_code, 200, errmsg)
+
+ def test_delete_translation(self):
+ admin_user = self.get_superuser()
+ page = create_page("delete-page-translation", "nav_playground.html", "en",
+ created_by=admin_user, published=True)
+ create_title("de", "delete-page-translation-2", page, slug="delete-page-translation-2")
+
+
+## ... source file abbreviated to get to site examples ...
+
+
+ page_admin = PageAdmin(Page, None)
+ page_admin._current_page = page
+ page.publish("en")
+ draft_page = page.get_draft_object()
+ admin_url = reverse("admin:cms_page_edit_title_fields", args=(
+ draft_page.pk, language
+ ))
+
+ post_data = {
+ 'title': "A Title"
+ }
+ with self.login_user_context(admin_user):
+ self.client.post(admin_url, post_data)
+ draft_page = Page.objects.get(pk=page.pk).get_draft_object()
+ self.assertTrue(draft_page.is_dirty('en'))
+
+
+class NoDBAdminTests(CMSTestCase):
+ @property
+ def admin_class(self):
+~~ return site._registry[Page]
+
+ def test_lookup_allowed_site__exact(self):
+ self.assertTrue(self.admin_class.lookup_allowed('site__exact', '1'))
+
+ def test_lookup_allowed_published(self):
+ self.assertTrue(self.admin_class.lookup_allowed('published', value='1'))
+
+
+class PluginPermissionTests(AdminTestsBase):
+ def setUp(self):
+ self._page = create_page('test page', 'nav_playground.html', 'en')
+ self._placeholder = self._page.placeholders.all()[0]
+
+ def _get_admin(self):
+ User = get_user_model()
+
+ fields = dict(email="admin@django-cms.org", is_staff=True, is_active=True)
+
+ if (User.USERNAME_FIELD != 'email'):
+ fields[User.USERNAME_FIELD] = "admin"
+
+ admin_user = User(**fields)
+
+ admin_user.set_password('admin')
+
+
+## ... source file abbreviated to get to site examples ...
+
+
+ page2_data = {
+ 'reverse_id': 'p1',
+ 'template': 'col_two.html',
+ }
+
+ with self.login_user_context(superuser):
+ response = self.client.post(page2_endpoint, page2_data)
+ expected_error = (
+ '
'
+ '
A page with this reverse URL id exists already.
'
+ )
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response, expected_error.format(page2.pk), html=True)
+
+ def test_advanced_settings_endpoint(self):
+ admin_user = self.get_superuser()
+ site = Site.objects.get_current()
+ page = create_page('Page 1', 'nav_playground.html', 'en')
+ page_data = {
+ 'language': 'en',
+~~ 'site': site.pk,
+ 'template': 'col_two.html',
+ }
+ path = admin_reverse('cms_page_advanced', args=(page.pk,))
+
+ with self.login_user_context(admin_user):
+ en_path = path + u"?language=en"
+ redirect_path = admin_reverse('cms_page_changelist') + '?language=en'
+ response = self.client.post(en_path, page_data)
+ self.assertRedirects(response, redirect_path)
+ self.assertEqual(Page.objects.get(pk=page.pk).template, 'col_two.html')
+
+ page_data['language'] = 'de'
+ page_data['template'] = 'nav_playground.html'
+
+ with self.login_user_context(admin_user):
+ de_path = path + u"?language=de"
+ redirect_path = admin_reverse('cms_page_change', args=(page.pk,)) + '?language=de'
+ response = self.client.post(de_path, page_data)
+ self.assertRedirects(response, redirect_path)
+ self.assertEqual(Page.objects.get(pk=page.pk).template, 'col_two.html')
+
+ de_translation = create_title('de', title='Page 1', page=page.reload())
+ de_translation.slug = ''
+ de_translation.save()
+
+
+## ... source file continues with no further site examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-finders-basefinder.markdown b/content/pages/examples/django/django-contrib-staticfiles-finders-basefinder.markdown
new file mode 100644
index 000000000..92766a9a8
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-finders-basefinder.markdown
@@ -0,0 +1,100 @@
+title: django.contrib.staticfiles.finders BaseFinder Example Code
+category: page
+slug: django-contrib-staticfiles-finders-basefinder-examples
+sortorder: 500011067
+toc: False
+sidebartitle: django.contrib.staticfiles.finders BaseFinder
+meta: Python example code for the BaseFinder class from the django.contrib.staticfiles.finders module of the Django project.
+
+
+BaseFinder is a class within the django.contrib.staticfiles.finders module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / finders.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./finders.py)
+
+```python
+# finders.py
+from itertools import chain
+
+from django.contrib.staticfiles.storage import staticfiles_storage
+~~from django.contrib.staticfiles.finders import BaseFinder, BaseStorageFinder, find, \
+ AppDirectoriesFinder as DjangoAppDirectoriesFinder, FileSystemFinder as DjangoFileSystemFinder
+from django.utils._os import safe_join
+from os.path import normpath
+
+from pipeline.conf import settings
+
+
+class PipelineFinder(BaseStorageFinder):
+ storage = staticfiles_storage
+
+ def find(self, path, all=False):
+ if not settings.PIPELINE_ENABLED:
+ return super(PipelineFinder, self).find(path, all)
+ else:
+ return []
+
+ def list(self, ignore_patterns):
+ return []
+
+
+~~class ManifestFinder(BaseFinder):
+ def find(self, path, all=False):
+ matches = []
+ for elem in chain(settings.STYLESHEETS.values(), settings.JAVASCRIPT.values()):
+ if normpath(elem['output_filename']) == normpath(path):
+ match = safe_join(settings.PIPELINE_ROOT, path)
+ if not all:
+ return match
+ matches.append(match)
+ return matches
+
+ def list(self, *args):
+ return []
+
+
+~~class CachedFileFinder(BaseFinder):
+ def find(self, path, all=False):
+ try:
+ start, _, extn = path.rsplit('.', 2)
+ except ValueError:
+ return []
+ path = '.'.join((start, extn))
+ return find(path, all=all) or []
+
+ def list(self, *args):
+ return []
+
+
+class PatternFilterMixin(object):
+ ignore_patterns = []
+
+ def get_ignored_patterns(self):
+ return list(set(self.ignore_patterns))
+
+ def list(self, ignore_patterns):
+ if ignore_patterns:
+ ignore_patterns = ignore_patterns + self.get_ignored_patterns()
+ return super(PatternFilterMixin, self).list(ignore_patterns)
+
+
+
+
+## ... source file continues with no further BaseFinder examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-finders-basestoragefinder.markdown b/content/pages/examples/django/django-contrib-staticfiles-finders-basestoragefinder.markdown
new file mode 100644
index 000000000..799b108ed
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-finders-basestoragefinder.markdown
@@ -0,0 +1,72 @@
+title: django.contrib.staticfiles.finders BaseStorageFinder Example Code
+category: page
+slug: django-contrib-staticfiles-finders-basestoragefinder-examples
+sortorder: 500011068
+toc: False
+sidebartitle: django.contrib.staticfiles.finders BaseStorageFinder
+meta: Python example code for the BaseStorageFinder class from the django.contrib.staticfiles.finders module of the Django project.
+
+
+BaseStorageFinder is a class within the django.contrib.staticfiles.finders module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / finders.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./finders.py)
+
+```python
+# finders.py
+from itertools import chain
+
+from django.contrib.staticfiles.storage import staticfiles_storage
+~~from django.contrib.staticfiles.finders import BaseFinder, BaseStorageFinder, find, \
+ AppDirectoriesFinder as DjangoAppDirectoriesFinder, FileSystemFinder as DjangoFileSystemFinder
+from django.utils._os import safe_join
+from os.path import normpath
+
+from pipeline.conf import settings
+
+
+~~class PipelineFinder(BaseStorageFinder):
+ storage = staticfiles_storage
+
+ def find(self, path, all=False):
+ if not settings.PIPELINE_ENABLED:
+ return super(PipelineFinder, self).find(path, all)
+ else:
+ return []
+
+ def list(self, ignore_patterns):
+ return []
+
+
+class ManifestFinder(BaseFinder):
+ def find(self, path, all=False):
+ matches = []
+ for elem in chain(settings.STYLESHEETS.values(), settings.JAVASCRIPT.values()):
+ if normpath(elem['output_filename']) == normpath(path):
+ match = safe_join(settings.PIPELINE_ROOT, path)
+ if not all:
+ return match
+ matches.append(match)
+ return matches
+
+ def list(self, *args):
+
+
+## ... source file continues with no further BaseStorageFinder examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-finders-find.markdown b/content/pages/examples/django/django-contrib-staticfiles-finders-find.markdown
new file mode 100644
index 000000000..c30f933a5
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-finders-find.markdown
@@ -0,0 +1,107 @@
+title: django.contrib.staticfiles.finders find Example Code
+category: page
+slug: django-contrib-staticfiles-finders-find-examples
+sortorder: 500011069
+toc: False
+sidebartitle: django.contrib.staticfiles.finders find
+meta: Python example code for the find callable from the django.contrib.staticfiles.finders module of the Django project.
+
+
+find is a callable within the django.contrib.staticfiles.finders module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / finders.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./finders.py)
+
+```python
+# finders.py
+from itertools import chain
+
+from django.contrib.staticfiles.storage import staticfiles_storage
+~~from django.contrib.staticfiles.finders import BaseFinder, BaseStorageFinder, find, \
+ AppDirectoriesFinder as DjangoAppDirectoriesFinder, FileSystemFinder as DjangoFileSystemFinder
+from django.utils._os import safe_join
+from os.path import normpath
+
+from pipeline.conf import settings
+
+
+class PipelineFinder(BaseStorageFinder):
+ storage = staticfiles_storage
+
+~~ def find(self, path, all=False):
+ if not settings.PIPELINE_ENABLED:
+ return super(PipelineFinder, self).find(path, all)
+ else:
+ return []
+
+ def list(self, ignore_patterns):
+ return []
+
+
+class ManifestFinder(BaseFinder):
+~~ def find(self, path, all=False):
+ matches = []
+ for elem in chain(settings.STYLESHEETS.values(), settings.JAVASCRIPT.values()):
+ if normpath(elem['output_filename']) == normpath(path):
+ match = safe_join(settings.PIPELINE_ROOT, path)
+ if not all:
+ return match
+ matches.append(match)
+ return matches
+
+ def list(self, *args):
+ return []
+
+
+class CachedFileFinder(BaseFinder):
+~~ def find(self, path, all=False):
+ try:
+ start, _, extn = path.rsplit('.', 2)
+ except ValueError:
+ return []
+ path = '.'.join((start, extn))
+~~ return find(path, all=all) or []
+
+ def list(self, *args):
+ return []
+
+
+class PatternFilterMixin(object):
+ ignore_patterns = []
+
+ def get_ignored_patterns(self):
+ return list(set(self.ignore_patterns))
+
+ def list(self, ignore_patterns):
+ if ignore_patterns:
+ ignore_patterns = ignore_patterns + self.get_ignored_patterns()
+ return super(PatternFilterMixin, self).list(ignore_patterns)
+
+
+class AppDirectoriesFinder(PatternFilterMixin, DjangoAppDirectoriesFinder):
+ ignore_patterns = [
+ '*.js',
+ '*.css',
+ '*.less',
+ '*.scss',
+ '*.styl',
+
+
+## ... source file continues with no further find examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-finders-get-finders.markdown b/content/pages/examples/django/django-contrib-staticfiles-finders-get-finders.markdown
new file mode 100644
index 000000000..fbc7b0457
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-finders-get-finders.markdown
@@ -0,0 +1,78 @@
+title: django.contrib.staticfiles.finders get_finders Example Code
+category: page
+slug: django-contrib-staticfiles-finders-get-finders-examples
+sortorder: 500011070
+toc: False
+sidebartitle: django.contrib.staticfiles.finders get_finders
+meta: Python example code for the get_finders callable from the django.contrib.staticfiles.finders module of the Django project.
+
+
+get_finders is a callable within the django.contrib.staticfiles.finders module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / manifest.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./manifest.py)
+
+```python
+# manifest.py
+import os
+
+from django.conf.settings import settings as django_settings
+~~from django.contrib.staticfiles.finders import get_finders
+from django.contrib.staticfiles.storage import staticfiles_storage
+
+from pipeline.conf import settings
+
+from manifesto import Manifest
+
+from pipeline.packager import Packager
+
+
+class PipelineManifest(Manifest):
+ def __init__(self):
+ self.packager = Packager()
+ self.packages = self.collect_packages()
+~~ self.finders = get_finders()
+ self.package_files = []
+
+ def collect_packages(self):
+ packages = []
+ for package_name in self.packager.packages['css']:
+ package = self.packager.package_for('css', package_name)
+ if package.manifest:
+ packages.append(package)
+ for package_name in self.packager.packages['js']:
+ package = self.packager.package_for('js', package_name)
+ if package.manifest:
+ packages.append(package)
+ return packages
+
+ def cache(self):
+
+ if settings.PIPELINE_ENABLED:
+ for package in self.packages:
+ path = package.output_filename
+ self.package_files.append(path)
+ yield staticfiles_storage.url(path)
+ else:
+ for package in self.packages:
+ for path in self.packager.compile(package.paths):
+
+
+## ... source file continues with no further get_finders examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-finders.markdown b/content/pages/examples/django/django-contrib-staticfiles-finders.markdown
new file mode 100644
index 000000000..f86f42dcd
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-finders.markdown
@@ -0,0 +1,232 @@
+title: django.contrib.staticfiles finders Example Code
+category: page
+slug: django-contrib-staticfiles-finders-examples
+sortorder: 500011065
+toc: False
+sidebartitle: django.contrib.staticfiles finders
+meta: Python example code for the finders callable from the django.contrib.staticfiles module of the Django project.
+
+
+finders is a callable within the django.contrib.staticfiles module of the Django project.
+
+
+## Example 1 from django-debug-toolbar
+[django-debug-toolbar](https://github.com/jazzband/django-debug-toolbar)
+([project documentation](https://github.com/jazzband/django-debug-toolbar)
+and [PyPI page](https://pypi.org/project/django-debug-toolbar/))
+grants a developer detailed request-response cycle information while
+developing a [Django](/django.html) web application.
+The code for django-debug-toolbar is
+[open source](https://github.com/jazzband/django-debug-toolbar/blob/master/LICENSE)
+and maintained by the developer community group known as
+[Jazzband](https://jazzband.co/).
+
+[**django-debug-toolbar / debug_toolbar / panels / staticfiles.py**](https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/panels/staticfiles.py)
+
+```python
+# staticfiles.py
+from collections import OrderedDict
+from os.path import join, normpath
+
+from django.conf import settings
+~~from django.contrib.staticfiles import finders, storage
+from django.core.files.storage import get_storage_class
+from django.utils.functional import LazyObject
+from django.utils.translation import gettext_lazy as _, ngettext as __
+
+from debug_toolbar import panels
+from debug_toolbar.utils import ThreadCollector
+
+try:
+ import threading
+except ImportError:
+ threading = None
+
+
+class StaticFile:
+
+ def __init__(self, path):
+ self.path = path
+
+ def __str__(self):
+ return self.path
+
+ def real_path(self):
+~~ return finders.find(self.path)
+
+ def url(self):
+ return storage.staticfiles_storage.url(self.path)
+
+
+class FileCollector(ThreadCollector):
+ def collect(self, path, thread=None):
+ if path.endswith("/"):
+ return
+ super().collect(StaticFile(path), thread)
+
+
+collector = FileCollector()
+
+
+class DebugConfiguredStorage(LazyObject):
+
+ def _setup(self):
+
+ configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE)
+
+ class DebugStaticFilesStorage(configured_storage_cls):
+ def __init__(self, collector, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+
+## ... source file abbreviated to get to finders examples ...
+
+
+ return __("%(num_used)s file used", "%(num_used)s files used", num_used) % {
+ "num_used": num_used
+ }
+
+ def process_request(self, request):
+ collector.clear_collection()
+ return super().process_request(request)
+
+ def generate_stats(self, request, response):
+ used_paths = collector.get_collection()
+ self._paths[threading.currentThread()] = used_paths
+
+ self.record_stats(
+ {
+ "num_found": self.num_found,
+ "num_used": self.num_used,
+ "staticfiles": used_paths,
+ "staticfiles_apps": self.get_staticfiles_apps(),
+ "staticfiles_dirs": self.get_staticfiles_dirs(),
+ "staticfiles_finders": self.get_staticfiles_finders(),
+ }
+ )
+
+ def get_staticfiles_finders(self):
+ finders_mapping = OrderedDict()
+~~ for finder in finders.get_finders():
+ for path, finder_storage in finder.list([]):
+ if getattr(finder_storage, "prefix", None):
+ prefixed_path = join(finder_storage.prefix, path)
+ else:
+ prefixed_path = path
+ finder_cls = finder.__class__
+ finder_path = ".".join([finder_cls.__module__, finder_cls.__name__])
+ real_path = finder_storage.path(path)
+ payload = (prefixed_path, real_path)
+ finders_mapping.setdefault(finder_path, []).append(payload)
+ self.num_found += 1
+ return finders_mapping
+
+ def get_staticfiles_dirs(self):
+ dirs = []
+~~ for finder in finders.get_finders():
+~~ if isinstance(finder, finders.FileSystemFinder):
+ dirs.extend(finder.locations)
+ return [(prefix, normpath(dir)) for prefix, dir in dirs]
+
+ def get_staticfiles_apps(self):
+ apps = []
+~~ for finder in finders.get_finders():
+~~ if isinstance(finder, finders.AppDirectoriesFinder):
+ for app in finder.apps:
+ if app not in apps:
+ apps.append(app)
+ return apps
+
+
+
+## ... source file continues with no further finders examples...
+
+```
+
+
+## Example 2 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / collector.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./collector.py)
+
+```python
+# collector.py
+import os
+
+from collections import OrderedDict
+
+import django
+~~from django.contrib.staticfiles import finders
+from django.contrib.staticfiles.storage import staticfiles_storage
+
+from pipeline.finders import PipelineFinder
+
+
+class Collector(object):
+ request = None
+
+ def __init__(self, storage=None):
+ if storage is None:
+ storage = staticfiles_storage
+ self.storage = storage
+
+ def _get_modified_time(self, storage, prefixed_path):
+ if django.VERSION[:2] >= (1, 10):
+ return storage.get_modified_time(prefixed_path)
+ return storage.modified_time(prefixed_path)
+
+ def clear(self, path=""):
+ dirs, files = self.storage.listdir(path)
+ for f in files:
+ fpath = os.path.join(path, f)
+ self.storage.delete(fpath)
+ for d in dirs:
+ self.clear(os.path.join(path, d))
+
+ def collect(self, request=None, files=[]):
+ if self.request and self.request is request:
+ return
+ self.request = request
+ found_files = OrderedDict()
+~~ for finder in finders.get_finders():
+ if isinstance(finder, PipelineFinder):
+ continue
+ for path, storage in finder.list(['CVS', '.*', '*-']):
+ if getattr(storage, 'prefix', None):
+ prefixed_path = os.path.join(storage.prefix, path)
+ else:
+ prefixed_path = path
+
+ if (prefixed_path not in found_files and
+ (not files or prefixed_path in files)):
+ found_files[prefixed_path] = (storage, path)
+ self.copy_file(path, prefixed_path, storage)
+
+ if files and len(files) == len(found_files):
+ break
+
+ return found_files.keys()
+
+ def copy_file(self, path, prefixed_path, source_storage):
+ if not self.delete_file(path, prefixed_path, source_storage):
+ return
+ with source_storage.open(path) as source_file:
+ self.storage.save(prefixed_path, source_file)
+
+
+
+## ... source file continues with no further finders examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-handlers-staticfileshandler.markdown b/content/pages/examples/django/django-contrib-staticfiles-handlers-staticfileshandler.markdown
new file mode 100644
index 000000000..b9aaa6a9e
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-handlers-staticfileshandler.markdown
@@ -0,0 +1,107 @@
+title: django.contrib.staticfiles.handlers StaticFilesHandler Example Code
+category: page
+slug: django-contrib-staticfiles-handlers-staticfileshandler-examples
+sortorder: 500011071
+toc: False
+sidebartitle: django.contrib.staticfiles.handlers StaticFilesHandler
+meta: Python example code for the StaticFilesHandler class from the django.contrib.staticfiles.handlers module of the Django project.
+
+
+StaticFilesHandler is a class within the django.contrib.staticfiles.handlers module of the Django project.
+
+
+## Example 1 from django-webtest
+[django-webtest](https://github.com/django-webtest/django-webtest)
+([PyPI package information](https://pypi.org/project/django-webtest/))
+is a [Django](/django.html) extension that makes it easier to use
+[WebTest](http://docs.pylonsproject.org/projects/webtest/) with
+your projects.
+
+The project is open sourced under the
+[MIT license](https://github.com/django-webtest/django-webtest/blob/master/LICENSE.txt).
+
+[**django-webtest / django_webtest / __init__.py**](https://github.com/django-webtest/django-webtest/blob/master/django_webtest/./__init__.py)
+
+```python
+# __init__.py
+import copy
+
+from django.conf import settings
+from django.test.signals import template_rendered
+from django.core.handlers.wsgi import WSGIHandler
+from django.test import TestCase, TransactionTestCase
+from django.test.client import store_rendered_templates
+
+from functools import partial
+
+try:
+ from importlib import import_module
+except ImportError:
+ from django.utils.importlib import import_module
+
+from django.core import signals
+try:
+ from django.db import close_old_connections
+except ImportError:
+ from django.db import close_connection
+ close_old_connections = None
+try:
+ from django.core.servers.basehttp import (
+ AdminMediaHandler as StaticFilesHandler)
+except ImportError:
+~~ from django.contrib.staticfiles.handlers import StaticFilesHandler
+
+from webtest import TestApp
+try:
+ from webtest.utils import NoDefault
+except ImportError:
+ NoDefault = ''
+
+from django_webtest.response import DjangoWebtestResponse
+from django_webtest.compat import to_string, to_wsgi_safe_string
+
+
+_notgiven = object()
+
+
+class DjangoTestApp(TestApp):
+ response_class = DjangoWebtestResponse
+
+ def __init__(self, *args, **kwargs):
+ extra_environ = (kwargs.get('extra_environ') or {}).copy()
+ extra_environ.setdefault('HTTP_HOST', 'testserver')
+ kwargs['extra_environ'] = extra_environ
+ super(DjangoTestApp, self).__init__(self.get_wsgi_handler(), *args, **kwargs)
+
+ def get_wsgi_handler(self):
+~~ return StaticFilesHandler(WSGIHandler())
+
+ def set_user(self, user):
+ if user is None and 'WEBTEST_USER' in self.extra_environ:
+ del self.extra_environ['WEBTEST_USER']
+ if user is not None:
+ self.extra_environ = self._update_environ(self.extra_environ, user)
+
+ def _update_environ(self, environ, user=_notgiven):
+ environ = environ or {}
+
+ if user is not _notgiven:
+ if user is None:
+ environ['WEBTEST_USER'] = ''
+ else:
+ username = _get_username(user)
+ environ['WEBTEST_USER'] = to_wsgi_safe_string(username)
+
+ return environ
+
+ def do_request(self, req, status, expect_errors):
+ if close_old_connections is not None: # Django 1.6+
+ signals.request_started.disconnect(close_old_connections)
+ signals.request_finished.disconnect(close_old_connections)
+ else: # Django < 1.6
+
+
+## ... source file continues with no further StaticFilesHandler examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-storage-cachedstaticfilesstorage.markdown b/content/pages/examples/django/django-contrib-staticfiles-storage-cachedstaticfilesstorage.markdown
new file mode 100644
index 000000000..62d79de7f
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-storage-cachedstaticfilesstorage.markdown
@@ -0,0 +1,110 @@
+title: django.contrib.staticfiles.storage CachedStaticFilesStorage Example Code
+category: page
+slug: django-contrib-staticfiles-storage-cachedstaticfilesstorage-examples
+sortorder: 500011072
+toc: False
+sidebartitle: django.contrib.staticfiles.storage CachedStaticFilesStorage
+meta: Python example code for the CachedStaticFilesStorage class from the django.contrib.staticfiles.storage module of the Django project.
+
+
+CachedStaticFilesStorage is a class within the django.contrib.staticfiles.storage module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / storage.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./storage.py)
+
+```python
+# storage.py
+import gzip
+
+from io import BytesIO
+
+~~from django.contrib.staticfiles.storage import CachedStaticFilesStorage, ManifestStaticFilesStorage, StaticFilesStorage
+from django.contrib.staticfiles.utils import matches_patterns
+
+from django.core.files.base import File
+
+
+class PipelineMixin(object):
+ packing = True
+
+ def post_process(self, paths, dry_run=False, **options):
+ if dry_run:
+ return
+
+ from pipeline.packager import Packager
+ packager = Packager(storage=self)
+ for package_name in packager.packages['css']:
+ package = packager.package_for('css', package_name)
+ output_file = package.output_filename
+ if self.packing:
+ packager.pack_stylesheets(package)
+ paths[output_file] = (self, output_file)
+ yield output_file, output_file, True
+ for package_name in packager.packages['js']:
+ package = packager.package_for('js', package_name)
+ output_file = package.output_filename
+
+
+## ... source file abbreviated to get to CachedStaticFilesStorage examples ...
+
+
+ for path in paths:
+ if path:
+ if not matches_patterns(path, self.gzip_patterns):
+ continue
+ original_file = self.open(path)
+ gzipped_path = f"{path}.gz"
+ if self.exists(gzipped_path):
+ self.delete(gzipped_path)
+ gzipped_file = self._compress(original_file)
+ gzipped_path = self.save(gzipped_path, gzipped_file)
+ yield gzipped_path, gzipped_path, True
+
+
+class NonPackagingMixin(object):
+ packing = False
+
+
+class PipelineStorage(PipelineMixin, StaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineStorage(NonPackagingMixin, PipelineStorage):
+ pass
+
+
+~~class PipelineCachedStorage(PipelineMixin, CachedStaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineCachedStorage(NonPackagingMixin, PipelineCachedStorage):
+ pass
+
+
+class PipelineManifestStorage(PipelineMixin, ManifestStaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineManifestStorage(NonPackagingMixin, ManifestStaticFilesStorage):
+ pass
+
+
+
+## ... source file continues with no further CachedStaticFilesStorage examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-storage-hashedfilesmixin.markdown b/content/pages/examples/django/django-contrib-staticfiles-storage-hashedfilesmixin.markdown
new file mode 100644
index 000000000..ca526d574
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-storage-hashedfilesmixin.markdown
@@ -0,0 +1,57 @@
+title: django.contrib.staticfiles.storage HashedFilesMixin Example Code
+category: page
+slug: django-contrib-staticfiles-storage-hashedfilesmixin-examples
+sortorder: 500011073
+toc: False
+sidebartitle: django.contrib.staticfiles.storage HashedFilesMixin
+meta: Python example code for the HashedFilesMixin class from the django.contrib.staticfiles.storage module of the Django project.
+
+
+HashedFilesMixin is a class within the django.contrib.staticfiles.storage module of the Django project.
+
+
+## Example 1 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / admin / staticfiles.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/admin/staticfiles.py)
+
+```python
+# staticfiles.py
+import hashlib
+
+from django.conf import settings
+~~from django.contrib.staticfiles.storage import HashedFilesMixin
+from django.core.files.storage import get_storage_class
+from django.templatetags.static import static
+
+from wagtail import __version__
+
+
+try:
+ use_version_strings = settings.WAGTAILADMIN_STATIC_FILE_VERSION_STRINGS
+except AttributeError:
+
+ if settings.DEBUG:
+ use_version_strings = True
+ else:
+ storage = get_storage_class(settings.STATICFILES_STORAGE)
+ use_version_strings = not issubclass(storage, HashedFilesMixin)
+
+
+if use_version_strings:
+ VERSION_HASH = hashlib.sha1(
+ (__version__ + settings.SECRET_KEY).encode('utf-8')
+ ).hexdigest()[:8]
+else:
+ VERSION_HASH = None
+
+
+
+## ... source file continues with no further HashedFilesMixin examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-storage-manifeststaticfilesstorage.markdown b/content/pages/examples/django/django-contrib-staticfiles-storage-manifeststaticfilesstorage.markdown
new file mode 100644
index 000000000..d920d3c9f
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-storage-manifeststaticfilesstorage.markdown
@@ -0,0 +1,102 @@
+title: django.contrib.staticfiles.storage ManifestStaticFilesStorage Example Code
+category: page
+slug: django-contrib-staticfiles-storage-manifeststaticfilesstorage-examples
+sortorder: 500011074
+toc: False
+sidebartitle: django.contrib.staticfiles.storage ManifestStaticFilesStorage
+meta: Python example code for the ManifestStaticFilesStorage class from the django.contrib.staticfiles.storage module of the Django project.
+
+
+ManifestStaticFilesStorage is a class within the django.contrib.staticfiles.storage module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / storage.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./storage.py)
+
+```python
+# storage.py
+import gzip
+
+from io import BytesIO
+
+~~from django.contrib.staticfiles.storage import CachedStaticFilesStorage, ManifestStaticFilesStorage, StaticFilesStorage
+from django.contrib.staticfiles.utils import matches_patterns
+
+from django.core.files.base import File
+
+
+class PipelineMixin(object):
+ packing = True
+
+ def post_process(self, paths, dry_run=False, **options):
+ if dry_run:
+ return
+
+ from pipeline.packager import Packager
+ packager = Packager(storage=self)
+ for package_name in packager.packages['css']:
+ package = packager.package_for('css', package_name)
+ output_file = package.output_filename
+ if self.packing:
+ packager.pack_stylesheets(package)
+ paths[output_file] = (self, output_file)
+ yield output_file, output_file, True
+ for package_name in packager.packages['js']:
+ package = packager.package_for('js', package_name)
+ output_file = package.output_filename
+
+
+## ... source file abbreviated to get to ManifestStaticFilesStorage examples ...
+
+
+ gzipped_file = self._compress(original_file)
+ gzipped_path = self.save(gzipped_path, gzipped_file)
+ yield gzipped_path, gzipped_path, True
+
+
+class NonPackagingMixin(object):
+ packing = False
+
+
+class PipelineStorage(PipelineMixin, StaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineStorage(NonPackagingMixin, PipelineStorage):
+ pass
+
+
+class PipelineCachedStorage(PipelineMixin, CachedStaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineCachedStorage(NonPackagingMixin, PipelineCachedStorage):
+ pass
+
+
+~~class PipelineManifestStorage(PipelineMixin, ManifestStaticFilesStorage):
+ pass
+
+
+~~class NonPackagingPipelineManifestStorage(NonPackagingMixin, ManifestStaticFilesStorage):
+ pass
+
+
+
+## ... source file continues with no further ManifestStaticFilesStorage examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-storage-staticfiles-storage.markdown b/content/pages/examples/django/django-contrib-staticfiles-storage-staticfiles-storage.markdown
new file mode 100644
index 000000000..ae20ee1a5
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-storage-staticfiles-storage.markdown
@@ -0,0 +1,197 @@
+title: django.contrib.staticfiles.storage staticfiles_storage Example Code
+category: page
+slug: django-contrib-staticfiles-storage-staticfiles-storage-examples
+sortorder: 500011076
+toc: False
+sidebartitle: django.contrib.staticfiles.storage staticfiles_storage
+meta: Python example code for the staticfiles_storage callable from the django.contrib.staticfiles.storage module of the Django project.
+
+
+staticfiles_storage is a callable within the django.contrib.staticfiles.storage module of the Django project.
+
+
+## Example 1 from django-angular
+[django-angular](https://github.com/jrief/django-angular)
+([project examples website](https://django-angular.awesto.com/classic_form/))
+is a library with helper code to make it easier to use
+[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
+The code for django-angular is
+[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
+
+[**django-angular / djng / forms / fields.py**](https://github.com/jrief/django-angular/blob/master/djng/forms/fields.py)
+
+```python
+# fields.py
+import re
+import mimetypes
+
+from django.conf import settings
+~~from django.contrib.staticfiles.storage import staticfiles_storage
+from django.core import signing
+from django.core.exceptions import ImproperlyConfigured, ValidationError
+from django.core.files.storage import default_storage
+from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
+from django.urls import reverse_lazy
+from django.forms import fields, models as model_fields, widgets
+from django.utils.html import format_html
+from django.utils.module_loading import import_string
+from django.utils.safestring import mark_safe
+from django.utils.translation import ugettext_lazy as _, ungettext_lazy
+
+from djng import app_settings
+from .widgets import DropFileWidget, DropImageWidget
+
+
+class DefaultFieldMixin(object):
+ render_label = True
+
+ def has_subwidgets(self):
+ return False
+
+ def get_potential_errors(self):
+ return self.get_input_required_errors()
+
+
+
+## ... source file abbreviated to get to staticfiles_storage examples ...
+
+
+
+class FileField(FileFieldMixin, fields.FileField):
+ storage = app_settings.upload_storage
+ signer = signing.Signer()
+
+ def __init__(self, *args, **kwargs):
+ accept = kwargs.pop('accept', '*/*')
+ fileupload_url = kwargs.pop('fileupload_url', reverse_lazy('fileupload'))
+ area_label = kwargs.pop('area_label', _("Drop file here or click to upload"))
+ attrs = {
+ 'accept': accept,
+ 'ngf-pattern': accept,
+ }
+ kwargs.update(widget=DropFileWidget(area_label, fileupload_url, attrs=attrs))
+ super(FileField, self).__init__(*args, **kwargs)
+
+ @classmethod
+ def preview(cls, file_obj):
+ available_name = cls.storage.get_available_name(file_obj.name)
+ temp_name = cls.storage.save(available_name, file_obj)
+ extension = mimetypes.guess_extension(file_obj.content_type)
+ if extension:
+ extension = extension[1:]
+ else:
+ extension = '_blank'
+~~ icon_url = staticfiles_storage.url('djng/icons/{}.png'.format(extension))
+ return {
+ 'url': 'url({})'.format(icon_url),
+ 'temp_name': cls.signer.sign(temp_name),
+ 'file_name': file_obj.name,
+ 'file_size': file_obj.size,
+ 'charset': file_obj.charset,
+ 'content_type': file_obj.content_type,
+ 'content_type_extra': file_obj.content_type_extra,
+ }
+
+
+class ImageField(FileFieldMixin, fields.ImageField):
+ storage = app_settings.upload_storage
+ signer = signing.Signer()
+
+ def __init__(self, *args, **kwargs):
+ if 'easy_thumbnails' not in settings.INSTALLED_APPS:
+ raise ImproperlyConfigured("'djng.forms.fields.ImageField' requires 'easy-thubnails' to be installed")
+ accept = kwargs.pop('accept', 'image/*')
+ fileupload_url = kwargs.pop('fileupload_url', reverse_lazy('fileupload'))
+ area_label = kwargs.pop('area_label', _("Drop image here or click to upload"))
+ attrs = {
+ 'accept': accept,
+ 'ngf-pattern': accept,
+
+
+## ... source file continues with no further staticfiles_storage examples...
+
+```
+
+
+## Example 2 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / manifest.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./manifest.py)
+
+```python
+# manifest.py
+import os
+
+from django.conf.settings import settings as django_settings
+from django.contrib.staticfiles.finders import get_finders
+~~from django.contrib.staticfiles.storage import staticfiles_storage
+
+from pipeline.conf import settings
+
+from manifesto import Manifest
+
+from pipeline.packager import Packager
+
+
+class PipelineManifest(Manifest):
+ def __init__(self):
+ self.packager = Packager()
+ self.packages = self.collect_packages()
+ self.finders = get_finders()
+ self.package_files = []
+
+ def collect_packages(self):
+ packages = []
+ for package_name in self.packager.packages['css']:
+ package = self.packager.package_for('css', package_name)
+ if package.manifest:
+ packages.append(package)
+ for package_name in self.packager.packages['js']:
+ package = self.packager.package_for('js', package_name)
+ if package.manifest:
+ packages.append(package)
+ return packages
+
+ def cache(self):
+
+ if settings.PIPELINE_ENABLED:
+ for package in self.packages:
+ path = package.output_filename
+ self.package_files.append(path)
+~~ yield staticfiles_storage.url(path)
+ else:
+ for package in self.packages:
+ for path in self.packager.compile(package.paths):
+ self.package_files.append(path)
+~~ yield staticfiles_storage.url(path)
+
+ ignore_patterns = getattr(django_settings, "STATICFILES_IGNORE_PATTERNS", None)
+ for finder in self.finders:
+ for path, storage in finder.list(ignore_patterns):
+ if getattr(storage, 'prefix', None):
+ prefixed_path = os.path.join(storage.prefix, path)
+ else:
+ prefixed_path = path
+
+ if prefixed_path not in self.package_files:
+
+ self.package_files.append(prefixed_path)
+~~ yield staticfiles_storage.url(prefixed_path)
+
+
+
+## ... source file continues with no further staticfiles_storage examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-storage-staticfilesstorage.markdown b/content/pages/examples/django/django-contrib-staticfiles-storage-staticfilesstorage.markdown
new file mode 100644
index 000000000..0562d9564
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-storage-staticfilesstorage.markdown
@@ -0,0 +1,118 @@
+title: django.contrib.staticfiles.storage StaticFilesStorage Example Code
+category: page
+slug: django-contrib-staticfiles-storage-staticfilesstorage-examples
+sortorder: 500011075
+toc: False
+sidebartitle: django.contrib.staticfiles.storage StaticFilesStorage
+meta: Python example code for the StaticFilesStorage class from the django.contrib.staticfiles.storage module of the Django project.
+
+
+StaticFilesStorage is a class within the django.contrib.staticfiles.storage module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / storage.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./storage.py)
+
+```python
+# storage.py
+import gzip
+
+from io import BytesIO
+
+~~from django.contrib.staticfiles.storage import CachedStaticFilesStorage, ManifestStaticFilesStorage, StaticFilesStorage
+from django.contrib.staticfiles.utils import matches_patterns
+
+from django.core.files.base import File
+
+
+class PipelineMixin(object):
+ packing = True
+
+ def post_process(self, paths, dry_run=False, **options):
+ if dry_run:
+ return
+
+ from pipeline.packager import Packager
+ packager = Packager(storage=self)
+ for package_name in packager.packages['css']:
+ package = packager.package_for('css', package_name)
+ output_file = package.output_filename
+ if self.packing:
+ packager.pack_stylesheets(package)
+ paths[output_file] = (self, output_file)
+ yield output_file, output_file, True
+ for package_name in packager.packages['js']:
+ package = packager.package_for('js', package_name)
+ output_file = package.output_filename
+
+
+## ... source file abbreviated to get to StaticFilesStorage examples ...
+
+
+ for name, hashed_name, processed in super_class.post_process(paths.copy(), dry_run, **options):
+ if hashed_name != name:
+ paths[hashed_name] = (self, hashed_name)
+ yield name, hashed_name, processed
+
+ if dry_run:
+ return
+
+ for path in paths:
+ if path:
+ if not matches_patterns(path, self.gzip_patterns):
+ continue
+ original_file = self.open(path)
+ gzipped_path = f"{path}.gz"
+ if self.exists(gzipped_path):
+ self.delete(gzipped_path)
+ gzipped_file = self._compress(original_file)
+ gzipped_path = self.save(gzipped_path, gzipped_file)
+ yield gzipped_path, gzipped_path, True
+
+
+class NonPackagingMixin(object):
+ packing = False
+
+
+~~class PipelineStorage(PipelineMixin, StaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineStorage(NonPackagingMixin, PipelineStorage):
+ pass
+
+
+class PipelineCachedStorage(PipelineMixin, CachedStaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineCachedStorage(NonPackagingMixin, PipelineCachedStorage):
+ pass
+
+
+class PipelineManifestStorage(PipelineMixin, ManifestStaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineManifestStorage(NonPackagingMixin, ManifestStaticFilesStorage):
+ pass
+
+
+
+## ... source file continues with no further StaticFilesStorage examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-storage.markdown b/content/pages/examples/django/django-contrib-staticfiles-storage.markdown
new file mode 100644
index 000000000..9ea16df48
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-storage.markdown
@@ -0,0 +1,143 @@
+title: django.contrib.staticfiles storage Example Code
+category: page
+slug: django-contrib-staticfiles-storage-examples
+sortorder: 500011066
+toc: False
+sidebartitle: django.contrib.staticfiles storage
+meta: Python example code for the storage callable from the django.contrib.staticfiles module of the Django project.
+
+
+storage is a callable within the django.contrib.staticfiles module of the Django project.
+
+
+## Example 1 from django-debug-toolbar
+[django-debug-toolbar](https://github.com/jazzband/django-debug-toolbar)
+([project documentation](https://github.com/jazzband/django-debug-toolbar)
+and [PyPI page](https://pypi.org/project/django-debug-toolbar/))
+grants a developer detailed request-response cycle information while
+developing a [Django](/django.html) web application.
+The code for django-debug-toolbar is
+[open source](https://github.com/jazzband/django-debug-toolbar/blob/master/LICENSE)
+and maintained by the developer community group known as
+[Jazzband](https://jazzband.co/).
+
+[**django-debug-toolbar / debug_toolbar / panels / staticfiles.py**](https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/panels/staticfiles.py)
+
+```python
+# staticfiles.py
+from collections import OrderedDict
+from os.path import join, normpath
+
+from django.conf import settings
+~~from django.contrib.staticfiles import finders, storage
+from django.core.files.storage import get_storage_class
+from django.utils.functional import LazyObject
+from django.utils.translation import gettext_lazy as _, ngettext as __
+
+from debug_toolbar import panels
+from debug_toolbar.utils import ThreadCollector
+
+try:
+ import threading
+except ImportError:
+ threading = None
+
+
+class StaticFile:
+
+ def __init__(self, path):
+ self.path = path
+
+ def __str__(self):
+ return self.path
+
+ def real_path(self):
+ return finders.find(self.path)
+
+ def url(self):
+~~ return storage.staticfiles_storage.url(self.path)
+
+
+class FileCollector(ThreadCollector):
+ def collect(self, path, thread=None):
+ if path.endswith("/"):
+ return
+ super().collect(StaticFile(path), thread)
+
+
+collector = FileCollector()
+
+
+class DebugConfiguredStorage(LazyObject):
+
+ def _setup(self):
+
+ configured_storage_cls = get_storage_class(settings.STATICFILES_STORAGE)
+
+ class DebugStaticFilesStorage(configured_storage_cls):
+ def __init__(self, collector, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.collector = collector
+
+ def url(self, path):
+ self.collector.collect(path)
+ return super().url(path)
+
+ self._wrapped = DebugStaticFilesStorage(collector)
+
+
+~~_original_storage = storage.staticfiles_storage
+
+
+class StaticFilesPanel(panels.Panel):
+
+ name = "Static files"
+ template = "debug_toolbar/panels/staticfiles.html"
+
+ @property
+ def title(self):
+ return _("Static files (%(num_found)s found, %(num_used)s used)") % {
+ "num_found": self.num_found,
+ "num_used": self.num_used,
+ }
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.num_found = 0
+ self._paths = {}
+
+ def enable_instrumentation(self):
+~~ storage.staticfiles_storage = DebugConfiguredStorage()
+
+ def disable_instrumentation(self):
+~~ storage.staticfiles_storage = _original_storage
+
+ @property
+ def num_used(self):
+ return len(self._paths[threading.currentThread()])
+
+ nav_title = _("Static files")
+
+ @property
+ def nav_subtitle(self):
+ num_used = self.num_used
+ return __("%(num_used)s file used", "%(num_used)s files used", num_used) % {
+ "num_used": num_used
+ }
+
+ def process_request(self, request):
+ collector.clear_collection()
+ return super().process_request(request)
+
+ def generate_stats(self, request, response):
+ used_paths = collector.get_collection()
+ self._paths[threading.currentThread()] = used_paths
+
+ self.record_stats(
+ {
+
+
+## ... source file continues with no further storage examples...
+
+```
+
diff --git a/content/pages/examples/django/django-contrib-staticfiles-utils-matches-patterns.markdown b/content/pages/examples/django/django-contrib-staticfiles-utils-matches-patterns.markdown
new file mode 100644
index 000000000..47c78f231
--- /dev/null
+++ b/content/pages/examples/django/django-contrib-staticfiles-utils-matches-patterns.markdown
@@ -0,0 +1,121 @@
+title: django.contrib.staticfiles.utils matches_patterns Example Code
+category: page
+slug: django-contrib-staticfiles-utils-matches-patterns-examples
+sortorder: 500011077
+toc: False
+sidebartitle: django.contrib.staticfiles.utils matches_patterns
+meta: Python example code for the matches_patterns callable from the django.contrib.staticfiles.utils module of the Django project.
+
+
+matches_patterns is a callable within the django.contrib.staticfiles.utils module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / storage.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./storage.py)
+
+```python
+# storage.py
+import gzip
+
+from io import BytesIO
+
+from django.contrib.staticfiles.storage import CachedStaticFilesStorage, ManifestStaticFilesStorage, StaticFilesStorage
+~~from django.contrib.staticfiles.utils import matches_patterns
+
+from django.core.files.base import File
+
+
+class PipelineMixin(object):
+ packing = True
+
+ def post_process(self, paths, dry_run=False, **options):
+ if dry_run:
+ return
+
+ from pipeline.packager import Packager
+ packager = Packager(storage=self)
+ for package_name in packager.packages['css']:
+ package = packager.package_for('css', package_name)
+ output_file = package.output_filename
+ if self.packing:
+ packager.pack_stylesheets(package)
+ paths[output_file] = (self, output_file)
+ yield output_file, output_file, True
+ for package_name in packager.packages['js']:
+ package = packager.package_for('js', package_name)
+ output_file = package.output_filename
+ if self.packing:
+
+
+## ... source file abbreviated to get to matches_patterns examples ...
+
+
+
+class GZIPMixin(object):
+ gzip_patterns = ("*.css", "*.js")
+
+ def _compress(self, original_file):
+ content = BytesIO()
+ gzip_file = gzip.GzipFile(mode='wb', fileobj=content)
+ gzip_file.write(original_file.read())
+ gzip_file.close()
+ content.seek(0)
+ return File(content)
+
+ def post_process(self, paths, dry_run=False, **options):
+ super_class = super(GZIPMixin, self)
+ if hasattr(super_class, 'post_process'):
+ for name, hashed_name, processed in super_class.post_process(paths.copy(), dry_run, **options):
+ if hashed_name != name:
+ paths[hashed_name] = (self, hashed_name)
+ yield name, hashed_name, processed
+
+ if dry_run:
+ return
+
+ for path in paths:
+ if path:
+~~ if not matches_patterns(path, self.gzip_patterns):
+ continue
+ original_file = self.open(path)
+ gzipped_path = f"{path}.gz"
+ if self.exists(gzipped_path):
+ self.delete(gzipped_path)
+ gzipped_file = self._compress(original_file)
+ gzipped_path = self.save(gzipped_path, gzipped_file)
+ yield gzipped_path, gzipped_path, True
+
+
+class NonPackagingMixin(object):
+ packing = False
+
+
+class PipelineStorage(PipelineMixin, StaticFilesStorage):
+ pass
+
+
+class NonPackagingPipelineStorage(NonPackagingMixin, PipelineStorage):
+ pass
+
+
+class PipelineCachedStorage(PipelineMixin, CachedStaticFilesStorage):
+ pass
+
+
+## ... source file continues with no further matches_patterns examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-cache.markdown b/content/pages/examples/django/django-core-cache.markdown
new file mode 100644
index 000000000..f8bb4baf2
--- /dev/null
+++ b/content/pages/examples/django/django-core-cache.markdown
@@ -0,0 +1,115 @@
+title: django.core cache code examples
+category: page
+slug: django-core-cache-examples
+sortorder: 500011078
+toc: False
+sidebartitle: django.core cache
+meta: Python example code for the cache function from the django.core module of the Django project.
+
+
+cache is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-debug-toolbar
+[django-debug-toolbar](https://github.com/jazzband/django-debug-toolbar)
+([project documentation](https://github.com/jazzband/django-debug-toolbar)
+and [PyPI page](https://pypi.org/project/django-debug-toolbar/))
+grants a developer detailed request-response cycle information while
+developing a [Django](/django.html) web application.
+The code for django-debug-toolbar is
+[open source](https://github.com/jazzband/django-debug-toolbar/blob/master/LICENSE)
+and maintained by the developer community group known as
+[Jazzband](https://jazzband.co/).
+
+[**django-debug-toolbar / debug_toolbar / panels / cache.py**](https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/panels/cache.py)
+
+```python
+# cache.py
+import inspect
+import sys
+import time
+from collections import OrderedDict
+
+from django.conf import settings
+~~from django.core import cache
+from django.core.cache import CacheHandler, caches as original_caches
+from django.core.cache.backends.base import BaseCache
+from django.dispatch import Signal
+from django.middleware import cache as middleware_cache
+from django.utils.translation import gettext_lazy as _, ngettext as __
+
+from debug_toolbar import settings as dt_settings
+from debug_toolbar.panels import Panel
+from debug_toolbar.utils import (
+ get_stack,
+ get_template_info,
+ render_stacktrace,
+ tidy_stacktrace,
+)
+
+cache_called = Signal()
+
+
+def send_signal(method):
+ def wrapped(self, *args, **kwargs):
+ t = time.time()
+ value = method(self, *args, **kwargs)
+ t = time.time() - t
+
+
+
+## ... source file abbreviated to get to cache examples ...
+
+
+ @property
+ def nav_subtitle(self):
+ cache_calls = len(self.calls)
+ return __(
+ "%(cache_calls)d call in %(time).2fms",
+ "%(cache_calls)d calls in %(time).2fms",
+ cache_calls,
+ ) % {"cache_calls": cache_calls, "time": self.total_time}
+
+ @property
+ def title(self):
+ count = len(getattr(settings, "CACHES", ["default"]))
+ return __(
+ "Cache calls from %(count)d backend",
+ "Cache calls from %(count)d backends",
+ count,
+ ) % {"count": count}
+
+ def enable_instrumentation(self):
+ if isinstance(middleware_cache.caches, CacheHandlerPatch):
+~~ cache.caches = middleware_cache.caches
+ else:
+~~ cache.caches = CacheHandlerPatch()
+
+ def disable_instrumentation(self):
+~~ cache.caches = original_caches
+ middleware_cache.caches = original_caches
+
+ def generate_stats(self, request, response):
+ self.record_stats(
+ {
+ "total_calls": len(self.calls),
+ "calls": self.calls,
+ "total_time": self.total_time,
+ "hits": self.hits,
+ "misses": self.misses,
+ "counts": self.counts,
+ }
+ )
+
+ def generate_server_timing(self, request, response):
+ stats = self.get_stats()
+ value = stats.get("total_time", 0)
+ title = "Cache {} Calls".format(stats.get("total_calls", 0))
+ self.record_server_timing("total_time", title, value)
+
+
+
+## ... source file continues with no further cache examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-checks.markdown b/content/pages/examples/django/django-core-checks.markdown
new file mode 100644
index 000000000..c4bceb4db
--- /dev/null
+++ b/content/pages/examples/django/django-core-checks.markdown
@@ -0,0 +1,388 @@
+title: django.core checks code examples
+category: page
+slug: django-core-checks-examples
+sortorder: 500011079
+toc: False
+sidebartitle: django.core checks
+meta: Python example code for the checks function from the django.core module of the Django project.
+
+
+checks is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / tests / test_apphooks.py**](https://github.com/divio/django-cms/blob/develop/cms/tests/test_apphooks.py)
+
+```python
+# test_apphooks.py
+import sys
+import mock
+
+from django.contrib.admin.models import CHANGE, LogEntry
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import Permission
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.sites.models import Site
+~~from django.core import checks
+from django.core.cache import cache
+from django.core.checks.urls import check_url_config
+from django.test.utils import override_settings
+from django.urls import NoReverseMatch, clear_url_caches, resolve, reverse
+from django.utils.timezone import now
+from django.utils.translation import override as force_language
+
+from six import string_types
+
+from cms.admin.forms import AdvancedSettingsForm
+from cms.api import create_page, create_title
+from cms.app_base import CMSApp
+from cms.apphook_pool import apphook_pool
+from cms.appresolver import applications_page_check, clear_app_resolvers, get_app_patterns
+from cms.constants import PUBLISHER_STATE_DIRTY
+from cms.models import Title, Page
+from cms.middleware.page import get_page
+from cms.test_utils.project.placeholderapp.models import Example1
+from cms.test_utils.testcases import CMSTestCase
+from cms.tests.test_menu_utils import DumbPageLanguageUrl
+from cms.toolbar.toolbar import CMSToolbar
+from cms.utils.conf import get_cms_setting
+from cms.utils.urlutils import admin_reverse
+from menus.menu_pool import menu_pool
+
+
+## ... source file abbreviated to get to checks examples ...
+
+
+ create_title("de", "aphooked-page-de", page)
+ self.assertTrue(page.publish('en'))
+ self.assertTrue(page.publish('de'))
+ self.assertTrue(blank_page.publish('en'))
+ with force_language("en"):
+ response = self.client.get(self.get_pages_root())
+ self.assertTemplateUsed(response, 'sampleapp/home.html')
+ self.assertContains(response, '<--noplaceholder-->')
+ response = self.client.get('/en/blankapp/')
+ self.assertTemplateUsed(response, 'nav_playground.html')
+
+ self.apphook_clear()
+
+ @override_settings(ROOT_URLCONF='cms.test_utils.project.urls_for_apphook_tests')
+ def test_apphook_does_not_crash_django_checks(self):
+ self.apphook_clear()
+ superuser = get_user_model().objects.create_superuser('admin', 'admin@admin.com', 'admin')
+ create_page("apphooked-page", "nav_playground.html", "en",
+ created_by=superuser, published=True, apphook="SampleApp")
+ self.reload_urls()
+~~ checks.run_checks()
+ self.apphook_clear()
+
+ @override_settings(ROOT_URLCONF='cms.test_utils.project.urls_for_apphook_tests')
+ def test_apphook_on_root_reverse(self):
+ self.apphook_clear()
+ superuser = get_user_model().objects.create_superuser('admin', 'admin@admin.com', 'admin')
+ page = create_page("apphooked-page", "nav_playground.html", "en",
+ created_by=superuser, published=True, apphook="SampleApp")
+ create_title("de", "aphooked-page-de", page)
+ self.assertTrue(page.publish('de'))
+ self.assertTrue(page.publish('en'))
+
+ self.reload_urls()
+
+ self.assertFalse(reverse('sample-settings').startswith('//'))
+ self.apphook_clear()
+
+ @override_settings(ROOT_URLCONF='cms.test_utils.project.urls_for_apphook_tests')
+ def test_multisite_apphooks(self):
+ self.apphook_clear()
+ site1, _ = Site.objects.get_or_create(pk=1)
+ site2, _ = Site.objects.get_or_create(pk=2)
+ superuser = get_user_model().objects.create_superuser('admin', 'admin@admin.com', 'admin')
+ home_site_1 = create_page(
+
+
+## ... source file continues with no further checks examples...
+
+```
+
+
+## Example 2 from django-cors-headers
+[django-cors-headers](https://github.com/ottoyiu/django-cors-headers)
+is an
+[open source](https://github.com/ottoyiu/django-cors-headers/blob/master/LICENSE)
+library for enabling
+[Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
+handling in your [Django](/django.html) web applications and appropriately
+dealing with HTTP headers for CORS requests.
+
+[**django-cors-headers / src/corsheaders / checks.py**](https://github.com/ottoyiu/django-cors-headers/blob/master/src/corsheaders/./checks.py)
+
+```python
+# checks.py
+import re
+from collections.abc import Sequence
+from numbers import Integral
+from urllib.parse import urlparse
+
+from django.conf import settings
+~~from django.core import checks
+
+from corsheaders.conf import conf
+
+re_type = type(re.compile(""))
+
+
+@checks.register
+def check_settings(app_configs, **kwargs):
+ errors = []
+
+ if not is_sequence(conf.CORS_ALLOW_HEADERS, str):
+ errors.append(
+~~ checks.Error(
+ "CORS_ALLOW_HEADERS should be a sequence of strings.",
+ id="corsheaders.E001",
+ )
+ )
+
+ if not is_sequence(conf.CORS_ALLOW_METHODS, str):
+ errors.append(
+~~ checks.Error(
+ "CORS_ALLOW_METHODS should be a sequence of strings.",
+ id="corsheaders.E002",
+ )
+ )
+
+ if not isinstance(conf.CORS_ALLOW_CREDENTIALS, bool):
+ errors.append(
+~~ checks.Error(
+ "CORS_ALLOW_CREDENTIALS should be a bool.", id="corsheaders.E003"
+ )
+ )
+
+ if (
+ not isinstance(conf.CORS_PREFLIGHT_MAX_AGE, Integral)
+ or conf.CORS_PREFLIGHT_MAX_AGE < 0
+ ):
+ errors.append(
+~~ checks.Error(
+ (
+ "CORS_PREFLIGHT_MAX_AGE should be an integer greater than "
+ + "or equal to zero."
+ ),
+ id="corsheaders.E004",
+ )
+ )
+
+ if not isinstance(conf.CORS_ORIGIN_ALLOW_ALL, bool):
+ errors.append(
+~~ checks.Error(
+ "CORS_ORIGIN_ALLOW_ALL should be a bool.", id="corsheaders.E005"
+ )
+ )
+
+ if not is_sequence(conf.CORS_ORIGIN_WHITELIST, str):
+ errors.append(
+~~ checks.Error(
+ "CORS_ORIGIN_WHITELIST should be a sequence of strings.",
+ id="corsheaders.E006",
+ )
+ )
+ else:
+ special_origin_values = (
+ "null",
+ "file://",
+ )
+ for origin in conf.CORS_ORIGIN_WHITELIST:
+ if origin in special_origin_values:
+ continue
+ parsed = urlparse(origin)
+ if parsed.scheme == "" or parsed.netloc == "":
+ errors.append(
+~~ checks.Error(
+ (
+ "Origin {} in CORS_ORIGIN_WHITELIST is missing "
+ + " scheme or netloc"
+ ).format(repr(origin)),
+ id="corsheaders.E013",
+ hint=(
+ "Add a scheme (e.g. https://) or netloc (e.g. "
+ + "example.com)."
+ ),
+ )
+ )
+ else:
+ for part in ("path", "params", "query", "fragment"):
+ if getattr(parsed, part) != "":
+ errors.append(
+~~ checks.Error(
+ (
+ "Origin {} in CORS_ORIGIN_WHITELIST should "
+ + "not have {}"
+ ).format(repr(origin), part),
+ id="corsheaders.E014",
+ )
+ )
+
+ if not is_sequence(conf.CORS_ORIGIN_REGEX_WHITELIST, (str, re_type)):
+ errors.append(
+~~ checks.Error(
+ (
+ "CORS_ORIGIN_REGEX_WHITELIST should be a sequence of "
+ + "strings and/or compiled regexes."
+ ),
+ id="corsheaders.E007",
+ )
+ )
+
+ if not is_sequence(conf.CORS_EXPOSE_HEADERS, str):
+ errors.append(
+~~ checks.Error(
+ "CORS_EXPOSE_HEADERS should be a sequence.", id="corsheaders.E008"
+ )
+ )
+
+ if not isinstance(conf.CORS_URLS_REGEX, (str, re_type)):
+ errors.append(
+~~ checks.Error(
+ "CORS_URLS_REGEX should be a string or regex.", id="corsheaders.E009"
+ )
+ )
+
+ if not isinstance(conf.CORS_REPLACE_HTTPS_REFERER, bool):
+ errors.append(
+~~ checks.Error(
+ "CORS_REPLACE_HTTPS_REFERER should be a bool.", id="corsheaders.E011"
+ )
+ )
+
+ if hasattr(settings, "CORS_MODEL"):
+ errors.append(
+~~ checks.Error(
+ (
+ "The CORS_MODEL setting has been removed - see "
+ + "django-cors-headers' HISTORY."
+ ),
+ id="corsheaders.E012",
+ )
+ )
+
+ return errors
+
+
+def is_sequence(thing, type_or_types):
+ return isinstance(thing, Sequence) and all(
+ isinstance(x, type_or_types) for x in thing
+ )
+
+
+
+## ... source file continues with no further checks examples...
+
+```
+
+
+## Example 3 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / snippets / tests.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/snippets/tests.py)
+
+```python
+# tests.py
+import json
+
+from django.contrib.admin.utils import quote
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import AnonymousUser, Permission
+~~from django.core import checks
+from django.core.exceptions import ValidationError
+from django.core.files.base import ContentFile
+from django.core.files.uploadedfile import SimpleUploadedFile
+from django.http import HttpRequest, HttpResponse
+from django.test import RequestFactory, TestCase
+from django.test.utils import override_settings
+from django.urls import reverse
+from taggit.models import Tag
+
+from wagtail.admin.edit_handlers import FieldPanel
+from wagtail.admin.forms import WagtailAdminModelForm
+from wagtail.core.models import Page
+from wagtail.snippets.blocks import SnippetChooserBlock
+from wagtail.snippets.edit_handlers import SnippetChooserPanel
+from wagtail.snippets.models import SNIPPET_MODELS, register_snippet
+from wagtail.snippets.views.snippets import get_snippet_edit_handler
+from wagtail.tests.snippets.forms import FancySnippetForm
+from wagtail.tests.snippets.models import (
+ AlphaSnippet, FancySnippet, FileUploadSnippet, RegisterDecorator, RegisterFunction,
+ SearchableSnippet, StandardSnippet, StandardSnippetWithCustomPrimaryKey, ZuluSnippet)
+from wagtail.tests.testapp.models import (
+ Advert, AdvertWithCustomPrimaryKey, AdvertWithCustomUUIDPrimaryKey, AdvertWithTabbedInterface,
+ SnippetChooserModel, SnippetChooserModelWithCustomPrimaryKey)
+from wagtail.tests.utils import WagtailTestUtils
+
+
+## ... source file abbreviated to get to checks examples ...
+
+
+ def setUp(self):
+ self.login()
+
+ def get(self, pk, params=None):
+ return self.client.get(reverse('wagtailsnippets:chosen',
+ args=('tests', 'advertwithcustomuuidprimarykey', quote(pk))),
+ params or {})
+
+ def test_choose_a_page(self):
+ response = self.get(pk=AdvertWithCustomUUIDPrimaryKey.objects.all()[0].pk)
+ response_json = json.loads(response.content.decode())
+ self.assertEqual(response_json['step'], 'chosen')
+
+
+class TestPanelConfigurationChecks(TestCase, WagtailTestUtils):
+
+ def setUp(self):
+ self.warning_id = 'wagtailadmin.W002'
+
+ def get_checks_result():
+~~ checks_result = checks.run_checks(tags=['panels'])
+ return [
+ warning for warning in
+ checks_result if warning.id == self.warning_id]
+
+ self.get_checks_result = get_checks_result
+
+ def test_model_with_single_tabbed_panel_only(self):
+
+ StandardSnippet.content_panels = [FieldPanel('text')]
+
+~~ warning = checks.Warning(
+ "StandardSnippet.content_panels will have no effect on snippets editing",
+ hint="""Ensure that StandardSnippet uses `panels` instead of `content_panels`\
+or set up an `edit_handler` if you want a tabbed editing interface.
+There are no default tabs on non-Page models so there will be no\
+ Content tab for the content_panels to render in.""",
+ obj=StandardSnippet,
+ id='wagtailadmin.W002',
+ )
+
+ checks_results = self.get_checks_result()
+
+ self.assertEqual([warning], checks_results)
+
+ delattr(StandardSnippet, 'content_panels')
+
+
+
+## ... source file continues with no further checks examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-disallowedredirect.markdown b/content/pages/examples/django/django-core-exceptions-disallowedredirect.markdown
new file mode 100644
index 000000000..6ad7cc979
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-disallowedredirect.markdown
@@ -0,0 +1,64 @@
+title: django.core.exceptions DisallowedRedirect Example Code
+category: page
+slug: django-core-exceptions-disallowedredirect-examples
+sortorder: 500011098
+toc: False
+sidebartitle: django.core.exceptions DisallowedRedirect
+meta: Python example code for the DisallowedRedirect class from the django.core.exceptions module of the Django project.
+
+
+DisallowedRedirect is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from django-oauth-toolkit
+[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit)
+([project website](http://dot.evonove.it/) and
+[PyPI package information](https://pypi.org/project/django-oauth-toolkit/1.2.0/))
+is a code library for adding and handling [OAuth2](https://oauth.net/)
+flows within your [Django](/django.html) web application and
+[API](/application-programming-interfaces.html).
+
+The django-oauth-toolkit project is open sourced under the
+[FreeBSD license](https://github.com/jazzband/django-oauth-toolkit/blob/master/LICENSE)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-oauth-toolkit / oauth2_provider / http.py**](https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/./http.py)
+
+```python
+# http.py
+from urllib.parse import urlparse
+
+~~from django.core.exceptions import DisallowedRedirect
+from django.http import HttpResponse
+from django.utils.encoding import iri_to_uri
+
+
+class OAuth2ResponseRedirect(HttpResponse):
+ status_code = 302
+
+ def __init__(self, redirect_to, allowed_schemes, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self["Location"] = iri_to_uri(redirect_to)
+ self.allowed_schemes = allowed_schemes
+ self.validate_redirect(redirect_to)
+
+ @property
+ def url(self):
+ return self["Location"]
+
+ def validate_redirect(self, redirect_to):
+ parsed = urlparse(str(redirect_to))
+ if not parsed.scheme:
+~~ raise DisallowedRedirect("OAuth2 redirects require a URI scheme.")
+ if parsed.scheme not in self.allowed_schemes:
+~~ raise DisallowedRedirect(
+ "Redirect to scheme {!r} is not permitted".format(parsed.scheme)
+ )
+
+
+
+## ... source file continues with no further DisallowedRedirect examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-fielddoesnotexist.markdown b/content/pages/examples/django/django-core-exceptions-fielddoesnotexist.markdown
new file mode 100644
index 000000000..fda98d83f
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-fielddoesnotexist.markdown
@@ -0,0 +1,972 @@
+title: django.core.exceptions FieldDoesNotExist Example Code
+category: page
+slug: django-core-exceptions-fielddoesnotexist-examples
+sortorder: 500011099
+toc: False
+sidebartitle: django.core.exceptions FieldDoesNotExist
+meta: Python example code for the FieldDoesNotExist class from the django.core.exceptions module of the Django project.
+
+
+FieldDoesNotExist is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from AuditLog
+[Auditlog](https://github.com/jjkester/django-auditlog)
+([project documentation](https://django-auditlog.readthedocs.io/en/latest/))
+is a [Django](/django.html) app that logs changes to Python objects,
+similar to the Django admin's logs but with more details and
+output formats. Auditlog's source code is provided as open source under the
+[MIT license](https://github.com/jjkester/django-auditlog/blob/master/LICENSE).
+
+[**AuditLog / src / auditlog / models.py**](https://github.com/jjkester/django-auditlog/blob/master/src/auditlog/models.py)
+
+```python
+# models.py
+from __future__ import unicode_literals
+
+import json
+import ast
+
+from django.conf import settings
+from django.contrib.contenttypes.fields import GenericRelation
+from django.contrib.contenttypes.models import ContentType
+~~from django.core.exceptions import FieldDoesNotExist
+from django.db import models, DEFAULT_DB_ALIAS
+from django.db.models import QuerySet, Q
+from django.utils import formats, timezone
+from django.utils.encoding import python_2_unicode_compatible, smart_text
+from django.utils.six import iteritems, integer_types
+from django.utils.translation import ugettext_lazy as _
+
+from jsonfield.fields import JSONField
+from dateutil import parser
+from dateutil.tz import gettz
+
+
+class LogEntryManager(models.Manager):
+
+ def log_create(self, instance, **kwargs):
+ changes = kwargs.get('changes', None)
+ pk = self._get_pk_value(instance)
+
+ if changes is not None:
+ kwargs.setdefault('content_type', ContentType.objects.get_for_model(instance))
+ kwargs.setdefault('object_pk', pk)
+ kwargs.setdefault('object_repr', smart_text(instance))
+
+ if isinstance(pk, integer_types):
+
+
+## ... source file abbreviated to get to FieldDoesNotExist examples ...
+
+
+ @property
+ def changes_str(self, colon=': ', arrow=smart_text(' \u2192 '), separator='; '):
+ substrings = []
+
+ for field, values in iteritems(self.changes_dict):
+ substring = smart_text('{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}').format(
+ field_name=field,
+ colon=colon,
+ old=values[0],
+ arrow=arrow,
+ new=values[1],
+ )
+ substrings.append(substring)
+
+ return separator.join(substrings)
+
+ @property
+ def changes_display_dict(self):
+ from auditlog.registry import auditlog
+ model = self.content_type.model_class()
+ model_fields = auditlog.get_model_fields(model._meta.model)
+ changes_display_dict = {}
+ for field_name, values in iteritems(self.changes_dict):
+ try:
+ field = model._meta.get_field(field_name)
+~~ except FieldDoesNotExist:
+ changes_display_dict[field_name] = values
+ continue
+ values_display = []
+ choices_dict = None
+ if hasattr(field, 'choices') and len(field.choices) > 0:
+ choices_dict = dict(field.choices)
+ if hasattr(field, 'base_field') and getattr(field.base_field, 'choices', False):
+ choices_dict = dict(field.base_field.choices)
+
+ if choices_dict:
+ for value in values:
+ try:
+ value = ast.literal_eval(value)
+ if type(value) is [].__class__:
+ values_display.append(', '.join([choices_dict.get(val, 'None') for val in value]))
+ else:
+ values_display.append(choices_dict.get(value, 'None'))
+ except ValueError:
+ values_display.append(choices_dict.get(value, 'None'))
+ except:
+ values_display.append(choices_dict.get(value, 'None'))
+ else:
+ try:
+ field_type = field.get_internal_type()
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 2 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / utils.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/./utils.py)
+
+```python
+# utils.py
+import base64
+import importlib
+import json
+import random
+import re
+import string
+import unicodedata
+from collections import OrderedDict
+from urllib.parse import urlsplit
+
+import django
+from django.contrib.auth import get_user_model
+from django.contrib.sites.models import Site
+~~from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
+from django.core.serializers.json import DjangoJSONEncoder
+from django.core.validators import ValidationError, validate_email
+from django.db.models import FileField
+from django.db.models.fields import (
+ BinaryField,
+ DateField,
+ DateTimeField,
+ EmailField,
+ TimeField,
+)
+from django.utils import dateparse
+from django.utils.encoding import force_bytes, force_str
+
+
+MAX_USERNAME_SUFFIX_LENGTH = 7
+USERNAME_SUFFIX_CHARS = (
+ [string.digits] * 4 +
+ [string.ascii_letters] * (MAX_USERNAME_SUFFIX_LENGTH - 4))
+
+
+def _generate_unique_username_base(txts, regex=None):
+ from .account.adapter import get_adapter
+ adapter = get_adapter()
+ username = None
+
+
+## ... source file abbreviated to get to FieldDoesNotExist examples ...
+
+
+ else:
+ ret = path_or_callable
+ return ret
+
+
+SERIALIZED_DB_FIELD_PREFIX = '_db_'
+
+
+def serialize_instance(instance):
+ data = {}
+ for k, v in instance.__dict__.items():
+ if k.startswith('_') or callable(v):
+ continue
+ try:
+ field = instance._meta.get_field(k)
+ if isinstance(field, BinaryField):
+ v = force_str(base64.b64encode(v))
+ elif isinstance(field, FileField):
+ if v and not isinstance(v, str):
+ v = v.name
+ try:
+ json.dumps(v, cls=DjangoJSONEncoder)
+ except TypeError:
+ v = field.get_prep_value(v)
+ k = SERIALIZED_DB_FIELD_PREFIX + k
+~~ except FieldDoesNotExist:
+ pass
+ data[k] = v
+ return json.loads(json.dumps(data, cls=DjangoJSONEncoder))
+
+
+def deserialize_instance(model, data):
+ ret = model()
+ for k, v in data.items():
+ is_db_value = False
+ if k.startswith(SERIALIZED_DB_FIELD_PREFIX):
+ k = k[len(SERIALIZED_DB_FIELD_PREFIX):]
+ is_db_value = True
+ if v is not None:
+ try:
+ f = model._meta.get_field(k)
+ if isinstance(f, DateTimeField):
+ v = dateparse.parse_datetime(v)
+ elif isinstance(f, TimeField):
+ v = dateparse.parse_time(v)
+ elif isinstance(f, DateField):
+ v = dateparse.parse_date(v)
+ elif isinstance(f, BinaryField):
+ v = force_bytes(
+ base64.b64decode(
+ force_bytes(v)))
+ elif is_db_value:
+ try:
+ if django.VERSION < (3, 0):
+ v = f.from_db_value(v, None, None, None)
+ else:
+ v = f.from_db_value(v, None, None)
+ except Exception:
+ raise ImproperlyConfigured(
+ "Unable to auto serialize field '{}', custom"
+ " serialization override required".format(k)
+ )
+~~ except FieldDoesNotExist:
+ pass
+ setattr(ret, k, v)
+ return ret
+
+
+def set_form_field_order(form, field_order):
+ if field_order is None:
+ return
+ fields = OrderedDict()
+ for key in field_order:
+ try:
+ fields[key] = form.fields.pop(key)
+ except KeyError: # ignore unknown fields
+ pass
+ fields.update(form.fields) # add remaining fields in original order
+ form.fields = fields
+
+
+def build_absolute_uri(request, location, protocol=None):
+ from .account import app_settings as account_settings
+
+ if request is None:
+ site = Site.objects.get_current()
+ bits = urlsplit(location)
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 3 from django-filter
+[django-filter](https://github.com/carltongibson/django-filter)
+([project documentation](https://django-filter.readthedocs.io/en/master/)
+and
+[PyPI page](https://pypi.org/project/django-filter/2.2.0/))
+makes it easier to filter down querysets from the
+[Django ORM](/django-orm.html) by providing common bits of boilerplate
+code. django-filter is provided as
+[open source](https://github.com/carltongibson/django-filter/blob/master/LICENSE).
+
+[**django-filter / django_filters / utils.py**](https://github.com/carltongibson/django-filter/blob/master/django_filters/./utils.py)
+
+```python
+# utils.py
+import warnings
+from collections import OrderedDict
+
+from django.conf import settings
+~~from django.core.exceptions import FieldDoesNotExist, FieldError
+from django.db import models
+from django.db.models.constants import LOOKUP_SEP
+from django.db.models.expressions import Expression
+from django.db.models.fields.related import ForeignObjectRel, RelatedField
+from django.utils import timezone
+from django.utils.encoding import force_str
+from django.utils.text import capfirst
+from django.utils.translation import gettext as _
+
+from .exceptions import FieldLookupError
+
+
+def deprecate(msg, level_modifier=0):
+ warnings.warn(msg, MigrationNotice, stacklevel=3 + level_modifier)
+
+
+class MigrationNotice(DeprecationWarning):
+ url = 'https://django-filter.readthedocs.io/en/master/guide/migration.html'
+
+ def __init__(self, message):
+ super().__init__('%s See: %s' % (message, self.url))
+
+
+class RenameAttributesBase(type):
+
+
+## ... source file abbreviated to get to FieldDoesNotExist examples ...
+
+
+
+
+def get_all_model_fields(model):
+ opts = model._meta
+
+ return [
+ f.name for f in sorted(opts.fields + opts.many_to_many)
+ if not isinstance(f, models.AutoField) and
+ not (getattr(f.remote_field, 'parent_link', False))
+ ]
+
+
+def get_model_field(model, field_name):
+ fields = get_field_parts(model, field_name)
+ return fields[-1] if fields else None
+
+
+def get_field_parts(model, field_name):
+ parts = field_name.split(LOOKUP_SEP)
+ opts = model._meta
+ fields = []
+
+ for name in parts:
+ try:
+ field = opts.get_field(name)
+~~ except FieldDoesNotExist:
+ return None
+
+ fields.append(field)
+ if isinstance(field, RelatedField):
+ opts = field.remote_field.model._meta
+ elif isinstance(field, ForeignObjectRel):
+ opts = field.related_model._meta
+
+ return fields
+
+
+def resolve_field(model_field, lookup_expr):
+ query = model_field.model._default_manager.all().query
+ lhs = Expression(model_field)
+ lookups = lookup_expr.split(LOOKUP_SEP)
+
+ assert len(lookups) > 0
+
+ try:
+ while lookups:
+ name = lookups[0]
+ args = (lhs, name)
+ if len(lookups) == 1:
+ final_lookup = lhs.get_lookup(name)
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 4 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / managers.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./managers.py)
+
+```python
+# managers.py
+~~from django.core.exceptions import FieldDoesNotExist
+from django.db import models
+from django.db.models import Q
+from guardian.core import ObjectPermissionChecker
+from guardian.ctypes import get_content_type
+from guardian.exceptions import ObjectNotPersisted
+from django.contrib.auth.models import Permission
+
+import warnings
+
+
+class BaseObjectPermissionManager(models.Manager):
+
+ @property
+ def user_or_group_field(self):
+ try:
+ self.model._meta.get_field('user')
+ return 'user'
+~~ except FieldDoesNotExist:
+ return 'group'
+
+ def is_generic(self):
+ try:
+ self.model._meta.get_field('object_pk')
+ return True
+~~ except FieldDoesNotExist:
+ return False
+
+ def assign_perm(self, perm, user_or_group, obj):
+ if getattr(obj, 'pk', None) is None:
+ raise ObjectNotPersisted("Object %s needs to be persisted first"
+ % obj)
+ ctype = get_content_type(obj)
+ if not isinstance(perm, Permission):
+ permission = Permission.objects.get(content_type=ctype, codename=perm)
+ else:
+ permission = perm
+
+ kwargs = {'permission': permission, self.user_or_group_field: user_or_group}
+ if self.is_generic():
+ kwargs['content_type'] = ctype
+ kwargs['object_pk'] = obj.pk
+ else:
+ kwargs['content_object'] = obj
+ obj_perm, _ = self.get_or_create(**kwargs)
+ return obj_perm
+
+ def bulk_assign_perm(self, perm, user_or_group, queryset):
+ if isinstance(queryset, list):
+ ctype = get_content_type(queryset[0])
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 5 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / resources.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./resources.py)
+
+```python
+# resources.py
+import django
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured, ValidationError
+from django.core.management.color import no_style
+from django.core.paginator import Paginator
+from django.db import DEFAULT_DB_ALIAS, connections
+from django.db.models.fields.related import ForeignObjectRel
+from django.db.models.query import QuerySet
+from django.db.transaction import (
+ TransactionManagementError,
+ atomic,
+ savepoint,
+ savepoint_commit,
+ savepoint_rollback
+)
+from django.utils.encoding import force_str
+from django.utils.safestring import mark_safe
+
+from . import widgets
+from .fields import Field
+from .instance_loaders import ModelInstanceLoader
+from .results import Error, Result, RowResult
+from .utils import atomic_if_using_transaction
+
+if django.VERSION[0] >= 3:
+~~ from django.core.exceptions import FieldDoesNotExist
+else:
+ from django.db.models.fields import FieldDoesNotExist
+
+
+logger = logging.getLogger(__name__)
+logger.addHandler(logging.NullHandler())
+
+USE_TRANSACTIONS = getattr(settings, 'IMPORT_EXPORT_USE_TRANSACTIONS', True)
+CHUNK_SIZE = getattr(settings, 'IMPORT_EXPORT_CHUNK_SIZE', 1)
+
+
+def get_related_model(field):
+ if hasattr(field, 'related_model'):
+ return field.related_model
+ if field.rel:
+ return field.rel.to
+
+
+class ResourceOptions:
+
+ model = None
+ fields = None
+
+ exclude = None
+
+
+## ... source file abbreviated to get to FieldDoesNotExist examples ...
+
+
+ continue
+
+ field = new_class.field_from_django_field(f.name, f,
+ readonly=False)
+ field_list.append((f.name, field, ))
+
+ new_class.fields.update(OrderedDict(field_list))
+
+ if opts.fields is not None:
+ field_list = []
+ for field_name in opts.fields:
+ if field_name in declared_fields:
+ continue
+ if field_name.find('__') == -1:
+ continue
+
+ model = opts.model
+ attrs = field_name.split('__')
+ for i, attr in enumerate(attrs):
+ verbose_path = ".".join([opts.model.__name__] + attrs[0:i+1])
+
+ try:
+ f = model._meta.get_field(attr)
+~~ except FieldDoesNotExist as e:
+ logger.debug(e, exc_info=e)
+~~ raise FieldDoesNotExist(
+ "%s: %s has no field named '%s'" %
+ (verbose_path, model.__name__, attr))
+
+ if i < len(attrs) - 1:
+ if isinstance(f, ForeignObjectRel):
+ model = get_related_model(f)
+ else:
+ if get_related_model(f) is None:
+ raise KeyError(
+ '%s is not a relation' % verbose_path)
+ model = get_related_model(f)
+
+ if isinstance(f, ForeignObjectRel):
+ f = f.field
+
+ field = new_class.field_from_django_field(field_name, f,
+ readonly=True)
+ field_list.append((field_name, field))
+
+ new_class.fields.update(OrderedDict(field_list))
+
+ return new_class
+
+
+ continue
+ if f.name in declared_fields:
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 6 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / serializers.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./serializers.py)
+
+```python
+# serializers.py
+import copy
+import inspect
+import traceback
+from collections import OrderedDict, defaultdict
+from collections.abc import Mapping
+
+~~from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
+from django.core.exceptions import ValidationError as DjangoValidationError
+from django.db import models
+from django.db.models.fields import Field as DjangoModelField
+from django.utils import timezone
+from django.utils.functional import cached_property
+from django.utils.translation import gettext_lazy as _
+
+from rest_framework.compat import postgres_fields
+from rest_framework.exceptions import ErrorDetail, ValidationError
+from rest_framework.fields import get_error_detail, set_value
+from rest_framework.settings import api_settings
+from rest_framework.utils import html, model_meta, representation
+from rest_framework.utils.field_mapping import (
+ ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs,
+ get_relation_kwargs, get_url_kwargs
+)
+from rest_framework.utils.serializer_helpers import (
+ BindingDict, BoundField, JSONBoundField, NestedBoundField, ReturnDict,
+ ReturnList
+)
+from rest_framework.validators import (
+ UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator,
+ UniqueTogetherValidator
+)
+
+
+## ... source file abbreviated to get to FieldDoesNotExist examples ...
+
+
+ extra_kwargs[key] = value
+
+ return extra_kwargs, hidden_fields
+
+ def _get_model_fields(self, field_names, declared_fields, extra_kwargs):
+ model = getattr(self.Meta, 'model')
+ model_fields = {}
+
+ for field_name in field_names:
+ if field_name in declared_fields:
+ field = declared_fields[field_name]
+ source = field.source or field_name
+ else:
+ try:
+ source = extra_kwargs[field_name]['source']
+ except KeyError:
+ source = field_name
+
+ if '.' in source or source == '*':
+ continue
+
+ try:
+ field = model._meta.get_field(source)
+ if isinstance(field, DjangoModelField):
+ model_fields[source] = field
+~~ except FieldDoesNotExist:
+ pass
+
+ return model_fields
+
+
+ def get_validators(self):
+ validators = getattr(getattr(self, 'Meta', None), 'validators', None)
+ if validators is not None:
+ return list(validators)
+
+ return (
+ self.get_unique_together_validators() +
+ self.get_unique_for_date_validators()
+ )
+
+ def get_unique_together_validators(self):
+ model_class_inheritance_tree = (
+ [self.Meta.model] +
+ list(self.Meta.model._meta.parents)
+ )
+
+ field_sources = OrderedDict(
+ (field.field_name, field.source) for field in self._writable_fields
+ if (field.source != '*') and ('.' not in field.source)
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 7 from django-tables2
+[django-tables2](https://github.com/jieter/django-tables2)
+([projection documentation](https://django-tables2.readthedocs.io/en/latest/)
+and
+[PyPI page](https://pypi.org/project/django-tables2/))
+is a code library for [Django](/django.html) that simplifies creating and
+displaying tables in [Django templates](/django-templates.html),
+especially with more advanced features such as pagination and sorting.
+The project and its code are
+[available as open source](https://github.com/jieter/django-tables2/blob/master/LICENSE).
+
+[**django-tables2 / django_tables2 / utils.py**](https://github.com/jieter/django-tables2/blob/master/django_tables2/./utils.py)
+
+```python
+# utils.py
+import inspect
+import warnings
+from collections import OrderedDict
+from functools import total_ordering
+from itertools import chain
+
+~~from django.core.exceptions import FieldDoesNotExist
+from django.db import models
+from django.utils.html import format_html_join
+
+
+class Sequence(list):
+
+ def expand(self, columns):
+ ellipses = self.count("...")
+ if ellipses > 1:
+ raise ValueError("'...' must be used at most once in a sequence.")
+ elif ellipses == 0:
+ self.append("...")
+
+ columns = list(columns) # take a copy and exhaust the generator
+ head = []
+ tail = []
+ target = head # start by adding things to the head
+ for name in self:
+ if name == "...":
+ target = tail
+ continue
+ target.append(name)
+ if name in columns:
+ columns.pop(columns.index(name))
+
+
+## ... source file abbreviated to get to FieldDoesNotExist examples ...
+
+
+ if safe and getattr(current, "alters_data", False):
+ raise ValueError(self.ALTERS_DATA_ERROR_FMT.format(method=repr(current)))
+ if not getattr(current, "do_not_call_in_templates", False):
+ current = current()
+ if current is None:
+ break
+ return current
+ except Exception:
+ if not quiet:
+ raise
+
+ @property
+ def bits(self):
+ if self == "":
+ return ()
+ return self.split(self.SEPARATOR)
+
+ def get_field(self, model):
+ if not hasattr(model, "_meta"):
+ return
+
+ field = None
+ for bit in self.bits:
+ try:
+ field = model._meta.get_field(bit)
+~~ except FieldDoesNotExist:
+ break
+
+ if hasattr(field, "remote_field"):
+ rel = getattr(field, "remote_field", None)
+ model = getattr(rel, "model", model)
+
+ return field
+
+ def penultimate(self, context, quiet=True):
+ path, _, remainder = self.rpartition(self.SEPARATOR)
+ return A(path).resolve(context, quiet=quiet), remainder
+
+
+A = Accessor # alias
+
+
+class AttributeDict(OrderedDict):
+
+ blacklist = ("th", "td", "_ordering", "thead", "tbody", "tfoot")
+
+ def _iteritems(self):
+ for key, v in self.items():
+ value = v() if callable(v) else v
+ if key not in self.blacklist and value is not None:
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 8 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / forms_account_handling.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./forms_account_handling.py)
+
+```python
+# forms_account_handling.py
+import random
+import string
+
+import django.contrib.auth.models
+from django import forms
+from django.contrib.auth import get_user_model
+from django.contrib.auth.forms import UserCreationForm
+~~from django.core.exceptions import FieldDoesNotExist
+from django.db.models.fields import CharField
+from django.db.models.fields import EmailField
+from django.utils.translation import gettext_lazy as _
+from wiki.conf import settings
+
+
+def _get_field(model, field):
+ try:
+ return model._meta.get_field(field)
+~~ except FieldDoesNotExist:
+ return
+
+
+User = get_user_model()
+
+
+def check_user_field(user_model):
+ return isinstance(_get_field(user_model, user_model.USERNAME_FIELD), CharField)
+
+
+def check_email_field(user_model):
+ return isinstance(
+ _get_field(user_model, user_model.get_email_field_name()), EmailField
+ )
+
+
+CustomUser = (
+ User
+ if (
+ settings.ACCOUNT_HANDLING and check_user_field(User) and check_email_field(User)
+ )
+ else django.contrib.auth.models.User
+)
+
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
+
+## Example 9 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / admin / edit_handlers.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/admin/edit_handlers.py)
+
+```python
+# edit_handlers.py
+import functools
+import re
+
+from django import forms
+~~from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
+from django.db.models.fields import CharField, TextField
+from django.forms.formsets import DELETION_FIELD_NAME, ORDERING_FIELD_NAME
+from django.forms.models import fields_for_model
+from django.template.loader import render_to_string
+from django.utils.functional import cached_property
+from django.utils.safestring import mark_safe
+from django.utils.translation import gettext_lazy
+from taggit.managers import TaggableManager
+
+from wagtail.admin import compare, widgets
+from wagtail.core.fields import RichTextField
+from wagtail.core.models import Page
+from wagtail.core.utils import camelcase_to_underscore, resolve_model_string
+from wagtail.utils.decorators import cached_classmethod
+
+from .forms.models import ( # NOQA
+ DIRECT_FORM_FIELD_OVERRIDES, FORM_FIELD_OVERRIDES, WagtailAdminModelForm, formfield_for_dbfield)
+from .forms.pages import WagtailAdminPageForm
+
+
+def widget_with_script(widget, script):
+ return mark_safe('{0}'.format(widget, script))
+
+
+
+
+## ... source file abbreviated to get to FieldDoesNotExist examples ...
+
+
+ def get_comparison_class(self):
+ widget_override = self.widget_overrides().get(self.field_name, None)
+ if widget_override and widget_override.is_hidden:
+ return
+
+ try:
+ field = self.db_field
+
+ if field.choices:
+ return compare.ChoiceFieldComparison
+
+ if field.is_relation:
+ if isinstance(field, TaggableManager):
+ return compare.TagsFieldComparison
+ elif field.many_to_many:
+ return compare.M2MFieldComparison
+
+ return compare.ForeignObjectComparison
+
+ if isinstance(field, RichTextField):
+ return compare.RichTextFieldComparison
+
+ if isinstance(field, (CharField, TextField)):
+ return compare.TextFieldComparison
+
+~~ except FieldDoesNotExist:
+ pass
+
+ return compare.FieldComparison
+
+ def get_comparison(self):
+ comparator_class = self.get_comparison_class()
+
+ if comparator_class:
+ try:
+ return [functools.partial(comparator_class, self.db_field)]
+~~ except FieldDoesNotExist:
+ return []
+ return []
+
+ @cached_property
+ def db_field(self):
+ try:
+ model = self.model
+ except AttributeError:
+ raise ImproperlyConfigured("%r must be bound to a model before calling db_field" % self)
+
+ return model._meta.get_field(self.field_name)
+
+ def on_form_bound(self):
+ self.bound_field = self.form[self.field_name]
+ self.heading = self.heading or self.bound_field.label
+ self.help_text = self.bound_field.help_text
+
+ def __repr__(self):
+ return "<%s '%s' with model=%s instance=%s request=%s form=%s>" % (
+ self.__class__.__name__, self.field_name,
+ self.model, self.instance, self.request, self.form.__class__.__name__)
+
+
+class RichTextFieldPanel(FieldPanel):
+
+
+## ... source file continues with no further FieldDoesNotExist examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-fielderror.markdown b/content/pages/examples/django/django-core-exceptions-fielderror.markdown
new file mode 100644
index 000000000..e9ea93104
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-fielderror.markdown
@@ -0,0 +1,371 @@
+title: django.core.exceptions FieldError Example Code
+category: page
+slug: django-core-exceptions-fielderror-examples
+sortorder: 500011100
+toc: False
+sidebartitle: django.core.exceptions FieldError
+meta: Python example code for the FieldError class from the django.core.exceptions module of the Django project.
+
+
+FieldError is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / api.py**](https://github.com/divio/django-cms/blob/develop/cms/./api.py)
+
+```python
+# api.py
+import datetime
+
+from django.contrib.auth import get_user_model
+from django.contrib.sites.models import Site
+~~from django.core.exceptions import FieldError
+from django.core.exceptions import PermissionDenied
+from django.core.exceptions import ValidationError
+from django.db import transaction
+from django.template.defaultfilters import slugify
+from django.template.loader import get_template
+from django.utils.translation import activate
+
+from six import string_types
+
+from cms import constants
+from cms.app_base import CMSApp
+from cms.apphook_pool import apphook_pool
+from cms.constants import TEMPLATE_INHERITANCE_MAGIC
+from cms.models.pagemodel import Page
+from cms.models.permissionmodels import (PageUser, PagePermission, GlobalPagePermission,
+ ACCESS_PAGE_AND_DESCENDANTS)
+from cms.models.placeholdermodel import Placeholder
+from cms.models.pluginmodel import CMSPlugin
+from cms.models.titlemodels import Title
+from cms.plugin_base import CMSPluginBase
+from cms.plugin_pool import plugin_pool
+from cms.utils import copy_plugins, get_current_site
+from cms.utils.conf import get_cms_setting
+from cms.utils.i18n import get_language_list
+
+
+## ... source file abbreviated to get to FieldError examples ...
+
+
+
+ if navigation_extenders:
+ raw_menus = menu_pool.get_menus_by_attribute("cms_enabled", True)
+ menus = [menu[0] for menu in raw_menus]
+ assert navigation_extenders in menus
+
+ accepted_limitations = (constants.VISIBILITY_ALL, constants.VISIBILITY_USERS, constants.VISIBILITY_ANONYMOUS)
+ assert limit_visibility_in_menu in accepted_limitations
+
+ assert position in ('last-child', 'first-child', 'left', 'right')
+ target_node = parent.node if parent else None
+
+ if apphook:
+ application_urls = _verify_apphook(apphook, apphook_namespace)
+ else:
+ application_urls = None
+
+ if created_by and isinstance(created_by, get_user_model()):
+ _thread_locals.user = created_by
+ created_by = getattr(created_by, get_user_model().USERNAME_FIELD)
+ else:
+ _thread_locals.user = None
+
+ if reverse_id:
+ if Page.objects.drafts().filter(reverse_id=reverse_id, node__site=site).exists():
+~~ raise FieldError('A page with the reverse_id="%s" already exist.' % reverse_id)
+
+ page = Page(
+ created_by=created_by,
+ changed_by=created_by,
+ publication_date=publication_date,
+ publication_end_date=publication_end_date,
+ in_navigation=in_navigation,
+ soft_root=soft_root,
+ reverse_id=reverse_id,
+ navigation_extenders=navigation_extenders,
+ template=template,
+ application_urls=application_urls,
+ application_namespace=apphook_namespace,
+ login_required=login_required,
+ limit_visibility_in_menu=limit_visibility_in_menu,
+ xframe_options=xframe_options,
+ )
+ page.set_tree_node(site=site, target=target_node, position=position)
+ page.save()
+ page.rescan_placeholders()
+
+ create_title(
+ language=language,
+ title=title,
+
+
+## ... source file continues with no further FieldError examples...
+
+```
+
+
+## Example 2 from django-filter
+[django-filter](https://github.com/carltongibson/django-filter)
+([project documentation](https://django-filter.readthedocs.io/en/master/)
+and
+[PyPI page](https://pypi.org/project/django-filter/2.2.0/))
+makes it easier to filter down querysets from the
+[Django ORM](/django-orm.html) by providing common bits of boilerplate
+code. django-filter is provided as
+[open source](https://github.com/carltongibson/django-filter/blob/master/LICENSE).
+
+[**django-filter / django_filters / utils.py**](https://github.com/carltongibson/django-filter/blob/master/django_filters/./utils.py)
+
+```python
+# utils.py
+import warnings
+from collections import OrderedDict
+
+from django.conf import settings
+~~from django.core.exceptions import FieldDoesNotExist, FieldError
+from django.db import models
+from django.db.models.constants import LOOKUP_SEP
+from django.db.models.expressions import Expression
+from django.db.models.fields.related import ForeignObjectRel, RelatedField
+from django.utils import timezone
+from django.utils.encoding import force_str
+from django.utils.text import capfirst
+from django.utils.translation import gettext as _
+
+from .exceptions import FieldLookupError
+
+
+def deprecate(msg, level_modifier=0):
+ warnings.warn(msg, MigrationNotice, stacklevel=3 + level_modifier)
+
+
+class MigrationNotice(DeprecationWarning):
+ url = 'https://django-filter.readthedocs.io/en/master/guide/migration.html'
+
+ def __init__(self, message):
+ super().__init__('%s See: %s' % (message, self.url))
+
+
+class RenameAttributesBase(type):
+
+
+## ... source file abbreviated to get to FieldError examples ...
+
+
+ elif isinstance(field, ForeignObjectRel):
+ opts = field.related_model._meta
+
+ return fields
+
+
+def resolve_field(model_field, lookup_expr):
+ query = model_field.model._default_manager.all().query
+ lhs = Expression(model_field)
+ lookups = lookup_expr.split(LOOKUP_SEP)
+
+ assert len(lookups) > 0
+
+ try:
+ while lookups:
+ name = lookups[0]
+ args = (lhs, name)
+ if len(lookups) == 1:
+ final_lookup = lhs.get_lookup(name)
+ if not final_lookup:
+ lhs = query.try_transform(*args)
+ final_lookup = lhs.get_lookup('exact')
+ return lhs.output_field, final_lookup.lookup_name
+ lhs = query.try_transform(*args)
+ lookups = lookups[1:]
+~~ except FieldError as e:
+ raise FieldLookupError(model_field, lookup_expr) from e
+
+
+def handle_timezone(value, is_dst=None):
+ if settings.USE_TZ and timezone.is_naive(value):
+ return timezone.make_aware(value, timezone.get_current_timezone(), is_dst)
+ elif not settings.USE_TZ and timezone.is_aware(value):
+ return timezone.make_naive(value, timezone.utc)
+ return value
+
+
+def verbose_field_name(model, field_name):
+ if field_name is None:
+ return '[invalid name]'
+
+ parts = get_field_parts(model, field_name)
+ if not parts:
+ return '[invalid name]'
+
+ names = []
+ for part in parts:
+ if isinstance(part, ForeignObjectRel):
+ if part.related_name:
+ names.append(part.related_name.replace('_', ' '))
+
+
+## ... source file continues with no further FieldError examples...
+
+```
+
+
+## Example 3 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / tracker.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./tracker.py)
+
+```python
+# tracker.py
+from copy import deepcopy
+from functools import wraps
+
+import django
+~~from django.core.exceptions import FieldError
+from django.db import models
+from django.db.models.fields.files import FileDescriptor
+from django.db.models.query_utils import DeferredAttribute
+
+
+class DescriptorMixin:
+ tracker_instance = None
+
+ def __get__(self, instance, owner):
+ if instance is None:
+ return self
+ was_deferred = False
+ field_name = self._get_field_name()
+ if field_name in instance._deferred_fields:
+ instance._deferred_fields.remove(field_name)
+ was_deferred = True
+ value = super().__get__(instance, owner)
+ if was_deferred:
+ self.tracker_instance.saved_data[field_name] = deepcopy(value)
+ return value
+
+ def _get_field_name(self):
+ return self.field_name
+
+
+
+## ... source file abbreviated to get to FieldError examples ...
+
+
+ else:
+ self.saved_data.update(**self.current(fields=fields))
+
+ for field, field_value in self.saved_data.items():
+ self.saved_data[field] = deepcopy(field_value)
+
+ def current(self, fields=None):
+ if fields is None:
+ deferred_fields = self.deferred_fields
+ if deferred_fields:
+ fields = [
+ field for field in self.fields
+ if field not in deferred_fields
+ ]
+ else:
+ fields = self.fields
+
+ return {f: self.get_field_value(f) for f in fields}
+
+ def has_changed(self, field):
+ if field in self.fields:
+ if field in self.deferred_fields and field not in self.instance.__dict__:
+ return False
+ return self.previous(field) != self.get_field_value(field)
+ else:
+~~ raise FieldError('field "%s" not tracked' % field)
+
+ def previous(self, field):
+
+ if self.instance.pk and field in self.deferred_fields and field not in self.saved_data:
+
+ if field not in self.instance.__dict__:
+ self.get_field_value(field)
+
+ else:
+ current_value = self.get_field_value(field)
+ self.instance.refresh_from_db(fields=[field])
+ self.saved_data[field] = deepcopy(self.get_field_value(field))
+ setattr(self.instance, self.field_map[field], current_value)
+
+ return self.saved_data.get(field)
+
+ def changed(self):
+ return {
+ field: self.previous(field)
+ for field in self.fields
+ if self.has_changed(field)
+ }
+
+ def init_deferred_fields(self):
+
+
+## ... source file abbreviated to get to FieldError examples ...
+
+
+ field for field in update_fields if
+ field in self.fields
+ )
+ getattr(instance, self.attname).set_saved_fields(
+ fields=fields
+ )
+ return ret
+
+ setattr(model, method, inner)
+
+ def __get__(self, instance, owner):
+ if instance is None:
+ return self
+ else:
+ return getattr(instance, self.attname)
+
+
+class ModelInstanceTracker(FieldInstanceTracker):
+
+ def has_changed(self, field):
+ if not self.instance.pk:
+ return True
+ elif field in self.saved_data:
+ return self.previous(field) != self.get_field_value(field)
+ else:
+~~ raise FieldError('field "%s" not tracked' % field)
+
+ def changed(self):
+ if not self.instance.pk:
+ return {}
+ saved = self.saved_data.items()
+ current = self.current()
+ return {k: v for k, v in saved if v != current[k]}
+
+
+class ModelTracker(FieldTracker):
+ tracker_class = ModelInstanceTracker
+
+ def get_field_map(self, cls):
+ return {field: field for field in self.fields}
+
+
+
+## ... source file continues with no further FieldError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-improperlyconfigured.markdown b/content/pages/examples/django/django-core-exceptions-improperlyconfigured.markdown
index d38b11d7a..8b4c0bcb0 100644
--- a/content/pages/examples/django/django-core-exceptions-improperlyconfigured.markdown
+++ b/content/pages/examples/django/django-core-exceptions-improperlyconfigured.markdown
@@ -1,6 +1,6 @@
title: django.core.exceptions ImproperlyConfigured Example Code
category: page
-slug: django-core-exceptions-improperlyconfigured
+slug: django-core-exceptions-improperlyconfigured-examples
sortorder: 500012505
toc: False
sidebartitle: django.core.exceptions ImproperlyConfigured
diff --git a/content/pages/examples/django/django-core-exceptions-middlewarenotused.markdown b/content/pages/examples/django/django-core-exceptions-middlewarenotused.markdown
new file mode 100644
index 000000000..d93d2e380
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-middlewarenotused.markdown
@@ -0,0 +1,122 @@
+title: django.core.exceptions MiddlewareNotUsed Example Code
+category: page
+slug: django-core-exceptions-middlewarenotused-examples
+sortorder: 500011102
+toc: False
+sidebartitle: django.core.exceptions MiddlewareNotUsed
+meta: Python example code for the MiddlewareNotUsed class from the django.core.exceptions module of the Django project.
+
+
+MiddlewareNotUsed is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from django-pipeline
+[django-pipeline](https://github.com/jazzband/django-pipeline)
+([project documentation](https://django-pipeline.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-pipeline/))
+is a code library for handling and compressing
+[static content assets](/static-content.html) when handling requests in
+[Django](/django.html) web applications.
+
+The django-pipeline project is open sourced under the
+[MIT License](https://github.com/jazzband/django-pipeline/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-pipeline / pipeline / middleware.py**](https://github.com/jazzband/django-pipeline/blob/master/pipeline/./middleware.py)
+
+```python
+# middleware.py
+~~from django.core.exceptions import MiddlewareNotUsed
+from django.utils.encoding import DjangoUnicodeDecodeError
+from django.utils.html import strip_spaces_between_tags as minify_html
+
+from pipeline.conf import settings
+
+from django.utils.deprecation import MiddlewareMixin
+
+
+class MinifyHTMLMiddleware(MiddlewareMixin):
+ def __init__(self, *args, **kwargs):
+ super(MinifyHTMLMiddleware, self).__init__(*args, **kwargs)
+ if not settings.PIPELINE_ENABLED:
+~~ raise MiddlewareNotUsed
+
+ def process_response(self, request, response):
+ if response.has_header('Content-Type') and 'text/html' in response['Content-Type']:
+ try:
+ response.content = minify_html(response.content.decode('utf-8').strip())
+ response['Content-Length'] = str(len(response.content))
+ except DjangoUnicodeDecodeError:
+ pass
+ return response
+
+
+
+## ... source file continues with no further MiddlewareNotUsed examples...
+
+```
+
+
+## Example 2 from django-user-visit
+[django-user-visit](https://github.com/yunojuno/django-user-visit)
+([PyPI package information](https://pypi.org/project/django-user-visit/))
+is a [Django](/django.html) app and
+[middleware](https://docs.djangoproject.com/en/stable/topics/http/middleware/)
+for tracking daily user visits to your web application. The goal
+is to record per user per day instead of for every request a user
+sends to the application. The project is provided as open source
+under the
+[MIT license](https://github.com/yunojuno/django-user-visit/blob/master/LICENSE).
+
+[**django-user-visit / user_visit / middleware.py**](https://github.com/yunojuno/django-user-visit/blob/master/user_visit/./middleware.py)
+
+```python
+# middleware.py
+import logging
+import typing
+
+import django.db
+~~from django.core.exceptions import MiddlewareNotUsed
+from django.http import HttpRequest, HttpResponse
+from django.utils import timezone
+
+from user_visit.models import UserVisit
+
+from .settings import RECORDING_DISABLED
+
+logger = logging.getLogger(__name__)
+
+SESSION_KEY = "user_visit.hash"
+
+
+class UserVisitMiddleware:
+
+ def __init__(self, get_response: typing.Callable) -> None:
+ if RECORDING_DISABLED:
+~~ raise MiddlewareNotUsed("UserVisit recording has been disabled")
+ self.get_response = get_response
+
+ def __call__(self, request: HttpRequest) -> typing.Optional[HttpResponse]:
+ if request.user.is_anonymous:
+ return self.get_response(request)
+
+ uv = UserVisit.objects.build(request, timezone.now())
+ if request.session.get(SESSION_KEY, "") == uv.hash:
+ return self.get_response(request)
+
+ try:
+ uv.save()
+ except django.db.IntegrityError:
+ logger.warning("Unable to record user visit - duplicate request hash")
+ else:
+ request.session[SESSION_KEY] = uv.hash
+ return self.get_response(request)
+
+
+
+## ... source file continues with no further MiddlewareNotUsed examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-non-field-errors.markdown b/content/pages/examples/django/django-core-exceptions-non-field-errors.markdown
new file mode 100644
index 000000000..0421e6dd7
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-non-field-errors.markdown
@@ -0,0 +1,158 @@
+title: django.core.exceptions NON_FIELD_ERRORS Example Code
+category: page
+slug: django-core-exceptions-non-field-errors-examples
+sortorder: 500011103
+toc: False
+sidebartitle: django.core.exceptions NON_FIELD_ERRORS
+meta: Python example code for the NON_FIELD_ERRORS constant from the django.core.exceptions module of the Django project.
+
+
+NON_FIELD_ERRORS is a constant within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / results.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./results.py)
+
+```python
+# results.py
+from collections import OrderedDict
+from tablib import Dataset
+
+~~from django.core.exceptions import NON_FIELD_ERRORS
+
+
+class Error:
+ def __init__(self, error, traceback=None, row=None):
+ self.error = error
+ self.traceback = traceback
+ self.row = row
+
+
+class RowResult:
+ IMPORT_TYPE_UPDATE = 'update'
+ IMPORT_TYPE_NEW = 'new'
+ IMPORT_TYPE_DELETE = 'delete'
+ IMPORT_TYPE_SKIP = 'skip'
+ IMPORT_TYPE_ERROR = 'error'
+ IMPORT_TYPE_INVALID = 'invalid'
+
+ valid_import_types = frozenset([
+ IMPORT_TYPE_NEW,
+ IMPORT_TYPE_UPDATE,
+ IMPORT_TYPE_DELETE,
+ IMPORT_TYPE_SKIP,
+ ])
+
+
+
+## ... source file abbreviated to get to NON_FIELD_ERRORS examples ...
+
+
+ self.diff = None
+ self.import_type = None
+ self.raw_values = {}
+
+
+class InvalidRow:
+
+ def __init__(self, number, validation_error, values):
+ self.number = number
+ self.error = validation_error
+ self.values = values
+ try:
+ self.error_dict = validation_error.message_dict
+ except AttributeError:
+ self.error_dict = {NON_FIELD_ERRORS: validation_error.messages}
+
+ @property
+ def field_specific_errors(self):
+ return {
+ key: value for key, value in self.error_dict.items()
+ if key != NON_FIELD_ERRORS
+ }
+
+ @property
+ def non_field_specific_errors(self):
+~~ return self.error_dict.get(NON_FIELD_ERRORS, [])
+
+ @property
+ def error_count(self):
+ count = 0
+ for error_list in self.error_dict.values():
+ count += len(error_list)
+ return count
+
+
+class Result:
+ def __init__(self, *args, **kwargs):
+ super().__init__()
+ self.base_errors = []
+ self.diff_headers = []
+ self.rows = [] # RowResults
+ self.invalid_rows = [] # InvalidRow
+ self.failed_dataset = Dataset()
+ self.totals = OrderedDict([(RowResult.IMPORT_TYPE_NEW, 0),
+ (RowResult.IMPORT_TYPE_UPDATE, 0),
+ (RowResult.IMPORT_TYPE_DELETE, 0),
+ (RowResult.IMPORT_TYPE_SKIP, 0),
+ (RowResult.IMPORT_TYPE_ERROR, 0),
+ (RowResult.IMPORT_TYPE_INVALID, 0)])
+ self.total_rows = 0
+
+
+## ... source file continues with no further NON_FIELD_ERRORS examples...
+
+```
+
+
+## Example 2 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / admin / messages.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/admin/messages.py)
+
+```python
+# messages.py
+from django.contrib import messages
+~~from django.core.exceptions import NON_FIELD_ERRORS
+from django.template.loader import render_to_string
+from django.utils.html import format_html, format_html_join
+
+
+def render(message, buttons, detail=''):
+ return render_to_string('wagtailadmin/shared/messages.html', {
+ 'message': message,
+ 'buttons': buttons,
+ 'detail': detail,
+ })
+
+
+def debug(request, message, buttons=None, extra_tags=''):
+ return messages.debug(request, render(message, buttons), extra_tags=extra_tags)
+
+
+def info(request, message, buttons=None, extra_tags=''):
+ return messages.info(request, render(message, buttons), extra_tags=extra_tags)
+
+
+def success(request, message, buttons=None, extra_tags=''):
+ return messages.success(request, render(message, buttons), extra_tags=extra_tags)
+
+
+
+
+## ... source file continues with no further NON_FIELD_ERRORS examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-objectdoesnotexist.markdown b/content/pages/examples/django/django-core-exceptions-objectdoesnotexist.markdown
new file mode 100644
index 000000000..755ef4441
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-objectdoesnotexist.markdown
@@ -0,0 +1,969 @@
+title: django.core.exceptions ObjectDoesNotExist Example Code
+category: page
+slug: django-core-exceptions-objectdoesnotexist-examples
+sortorder: 500011104
+toc: False
+sidebartitle: django.core.exceptions ObjectDoesNotExist
+meta: Python example code for the ObjectDoesNotExist class from the django.core.exceptions module of the Django project.
+
+
+ObjectDoesNotExist is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from AuditLog
+[Auditlog](https://github.com/jjkester/django-auditlog)
+([project documentation](https://django-auditlog.readthedocs.io/en/latest/))
+is a [Django](/django.html) app that logs changes to Python objects,
+similar to the Django admin's logs but with more details and
+output formats. Auditlog's source code is provided as open source under the
+[MIT license](https://github.com/jjkester/django-auditlog/blob/master/LICENSE).
+
+[**AuditLog / src / auditlog / diff.py**](https://github.com/jjkester/django-auditlog/blob/master/src/auditlog/diff.py)
+
+```python
+# diff.py
+from __future__ import unicode_literals
+
+from django.conf import settings
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.db.models import Model, NOT_PROVIDED, DateTimeField
+from django.utils import timezone
+from django.utils.encoding import smart_text
+
+
+def track_field(field):
+ from auditlog.models import LogEntry
+ if field.many_to_many:
+ return False
+
+ if getattr(field, 'remote_field', None) is not None and field.remote_field.model == LogEntry:
+ return False
+
+ elif getattr(field, 'rel', None) is not None and field.rel.to == LogEntry:
+ return False
+
+ return True
+
+
+def get_fields_in_model(instance):
+ assert isinstance(instance, Model)
+
+ use_api = hasattr(instance._meta, 'get_fields') and callable(instance._meta.get_fields)
+
+ if use_api:
+ return [f for f in instance._meta.get_fields() if track_field(f)]
+ return instance._meta.fields
+
+
+def get_field_value(obj, field):
+ if isinstance(field, DateTimeField):
+ try:
+ value = field.to_python(getattr(obj, field.name, None))
+ if value is not None and settings.USE_TZ and not timezone.is_naive(value):
+ value = timezone.make_naive(value, timezone=timezone.utc)
+~~ except ObjectDoesNotExist:
+ value = field.default if field.default is not NOT_PROVIDED else None
+ else:
+ try:
+ value = smart_text(getattr(obj, field.name, None))
+~~ except ObjectDoesNotExist:
+ value = field.default if field.default is not NOT_PROVIDED else None
+
+ return value
+
+
+def model_instance_diff(old, new):
+ from auditlog.registry import auditlog
+
+ if not(old is None or isinstance(old, Model)):
+ raise TypeError("The supplied old instance is not a valid model instance.")
+ if not(new is None or isinstance(new, Model)):
+ raise TypeError("The supplied new instance is not a valid model instance.")
+
+ diff = {}
+
+ if old is not None and new is not None:
+ fields = set(old._meta.fields + new._meta.fields)
+ model_fields = auditlog.get_model_fields(new._meta.model)
+ elif old is not None:
+ fields = set(get_fields_in_model(old))
+ model_fields = auditlog.get_model_fields(old._meta.model)
+ elif new is not None:
+ fields = set(get_fields_in_model(new))
+ model_fields = auditlog.get_model_fields(new._meta.model)
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 2 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / test_utils / testcases.py**](https://github.com/divio/django-cms/blob/develop/cms/test_utils/testcases.py)
+
+```python
+# testcases.py
+import json
+import sys
+import warnings
+
+from django.conf import settings
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import AnonymousUser, Permission
+from django.contrib.sites.models import Site
+from django.core.cache import cache
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.forms.models import model_to_dict
+from django.template import engines
+from django.template.context import Context
+from django.test import testcases
+from django.test.client import RequestFactory
+from django.urls import reverse
+from django.utils.http import urlencode
+from django.utils.timezone import now
+from django.utils.translation import activate
+from menus.menu_pool import menu_pool
+
+from six.moves.urllib.parse import unquote, urljoin
+
+from cms.api import create_page
+from cms.constants import (
+ PUBLISHER_STATE_DEFAULT,
+ PUBLISHER_STATE_DIRTY,
+ PUBLISHER_STATE_PENDING,
+)
+from cms.plugin_rendering import ContentRenderer, StructureRenderer
+from cms.models import Page
+from cms.models.permissionmodels import (
+ GlobalPagePermission,
+ PagePermission,
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ page_data['pagepermission_set-2-TOTAL_FORMS'] = 0
+ page_data['pagepermission_set-2-INITIAL_FORMS'] = 0
+ page_data['pagepermission_set-2-MAX_NUM_FORMS'] = 0
+ return page_data
+
+ def print_page_structure(self, qs):
+ for page in qs.order_by('path'):
+ ident = " " * page.level
+ print(u"%s%s (%s), path: %s, depth: %s, numchild: %s" % (ident, page,
+ page.pk, page.path, page.depth, page.numchild))
+
+ def print_node_structure(self, nodes, *extra):
+ def _rec(nodes, level=0):
+ ident = level * ' '
+ for node in nodes:
+ raw_attrs = [(bit, getattr(node, bit, node.attr.get(bit, "unknown"))) for bit in extra]
+ attrs = ', '.join(['%s: %r' % data for data in raw_attrs])
+ print(u"%s%s: %s" % (ident, node.title, attrs))
+ _rec(node.children, level + 1)
+
+ _rec(nodes)
+
+ def assertObjectExist(self, qs, **filter):
+ try:
+ return qs.get(**filter)
+~~ except ObjectDoesNotExist:
+ pass
+ raise self.failureException("ObjectDoesNotExist raised for filter %s" % filter)
+
+ def assertObjectDoesNotExist(self, qs, **filter):
+ try:
+ qs.get(**filter)
+~~ except ObjectDoesNotExist:
+ return
+ raise self.failureException("ObjectDoesNotExist not raised for filter %s" % filter)
+
+ def copy_page(self, page, target_page, position=0, target_site=None):
+ from cms.utils.page import get_available_slug
+
+ if target_site is None:
+ target_site = target_page.node.site
+
+ data = {
+ 'position': position,
+ 'target': target_page.pk,
+ 'source_site': page.node.site_id,
+ 'copy_permissions': 'on',
+ 'copy_moderation': 'on',
+ }
+ source_translation = page.title_set.all()[0]
+ parent_translation = target_page.title_set.all()[0]
+ language = source_translation.language
+ copied_page_path = source_translation.get_path_for_base(parent_translation.path)
+ new_page_slug = get_available_slug(target_site, copied_page_path, language)
+
+ with self.settings(SITE_ID=target_site.pk):
+ response = self.client.post(URL_CMS_PAGE + "%d/copy-page/" % page.pk, data)
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 3 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / fields / folder.py**](https://github.com/divio/django-filer/blob/develop/filer/fields/folder.py)
+
+```python
+# folder.py
+from __future__ import absolute_import
+
+import warnings
+
+from django import forms
+from django.contrib.admin.sites import site
+from django.contrib.admin.widgets import ForeignKeyRawIdWidget
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.db import models
+from django.template.loader import render_to_string
+from django.urls import reverse
+from django.utils.http import urlencode
+from django.utils.safestring import mark_safe
+
+from ..models import Folder
+from ..utils.compatibility import truncate_words
+from ..utils.model_label import get_model_label
+
+
+class AdminFolderWidget(ForeignKeyRawIdWidget):
+ choices = None
+ input_type = 'hidden'
+ is_hidden = False
+
+ def render(self, name, value, attrs=None, renderer=None):
+ obj = self.obj_for_value(value)
+ css_id = attrs.get('id')
+ css_id_folder = "%s_folder" % css_id
+ css_id_description_txt = "%s_description_txt" % css_id
+ if attrs is None:
+ attrs = {}
+ related_url = None
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ context = {
+ 'hidden_input': hidden_input,
+ 'lookup_url': '%s%s' % (related_url, url),
+ 'lookup_name': name,
+ 'span_id': css_id_description_txt,
+ 'object': obj,
+ 'clear_id': '%s_clear' % css_id,
+ 'descid': css_id_description_txt,
+ 'noimg': 'filer/icons/nofile_32x32.png',
+ 'foldid': css_id_folder,
+ 'id': css_id,
+ }
+ html = render_to_string('admin/filer/widgets/admin_folder.html', context)
+ return mark_safe(html)
+
+ def label_for_value(self, value):
+ obj = self.obj_for_value(value)
+ return ' %s' % truncate_words(obj, 14)
+
+ def obj_for_value(self, value):
+ if not value:
+ return None
+ try:
+ key = self.rel.get_related_field().name
+ obj = self.rel.model._default_manager.get(**{key: value})
+~~ except ObjectDoesNotExist:
+ obj = None
+ return obj
+
+ class Media(object):
+ js = (
+ 'filer/js/addons/popup_handling.js',
+ )
+
+
+class AdminFolderFormField(forms.ModelChoiceField):
+ widget = AdminFolderWidget
+
+ def __init__(self, rel, queryset, to_field_name, *args, **kwargs):
+ self.rel = rel
+ self.queryset = queryset
+ self.limit_choices_to = kwargs.pop('limit_choices_to', None)
+ self.to_field_name = to_field_name
+ self.max_value = None
+ self.min_value = None
+ kwargs.pop('widget', None)
+ forms.Field.__init__(self, widget=self.widget(rel, site), *args, **kwargs)
+
+ def widget_attrs(self, widget):
+ widget.required = self.required
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 4 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / utils.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./utils.py)
+
+```python
+# utils.py
+import logging
+import os
+from itertools import chain
+
+from django.conf import settings
+from django.contrib.auth import REDIRECT_FIELD_NAME, get_user_model
+from django.contrib.auth.models import AnonymousUser, Group
+~~from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
+from django.db.models import Model, QuerySet
+from django.http import HttpResponseForbidden, HttpResponseNotFound
+from django.shortcuts import render
+from guardian.conf import settings as guardian_settings
+from guardian.ctypes import get_content_type
+from guardian.exceptions import NotUserNorGroup
+
+logger = logging.getLogger(__name__)
+abspath = lambda *p: os.path.abspath(os.path.join(*p))
+
+
+def get_anonymous_user():
+ User = get_user_model()
+ lookup = {User.USERNAME_FIELD: guardian_settings.ANONYMOUS_USER_NAME}
+ return User.objects.get(**lookup)
+
+
+def get_identity(identity):
+ if isinstance(identity, AnonymousUser):
+ identity = get_anonymous_user()
+
+ if isinstance(identity, QuerySet):
+ identity_model_type = identity.model
+ if identity_model_type == get_user_model():
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ redirect_field_name = redirect_field_name or REDIRECT_FIELD_NAME
+
+
+ has_permissions = False
+ if accept_global_perms:
+ has_permissions = all(request.user.has_perm(perm) for perm in perms)
+ if not has_permissions:
+ has_permissions = all(request.user.has_perm(perm, obj)
+ for perm in perms)
+
+ if not has_permissions:
+ if return_403:
+ if guardian_settings.RENDER_403:
+ response = render(request, guardian_settings.TEMPLATE_403)
+ response.status_code = 403
+ return response
+ elif guardian_settings.RAISE_403:
+ raise PermissionDenied
+ return HttpResponseForbidden()
+ if return_404:
+ if guardian_settings.RENDER_404:
+ response = render(request, guardian_settings.TEMPLATE_404)
+ response.status_code = 404
+ return response
+ elif guardian_settings.RAISE_404:
+~~ raise ObjectDoesNotExist
+ return HttpResponseNotFound()
+ else:
+ from django.contrib.auth.views import redirect_to_login
+ return redirect_to_login(request.get_full_path(),
+ login_url,
+ redirect_field_name)
+
+
+from django.apps import apps as django_apps
+from django.core.exceptions import ImproperlyConfigured
+
+def get_obj_perm_model_by_conf(setting_name):
+ try:
+ setting_value = getattr(guardian_settings, setting_name)
+ return django_apps.get_model(setting_value, require_ready=False)
+ except ValueError as e:
+ raise ImproperlyConfigured("{} must be of the form 'app_label.model_name'".format(setting_value)) from e
+ except LookupError as e:
+ raise ImproperlyConfigured(
+ "{} refers to model '{}' that has not been installed".format(setting_name, setting_value)
+ ) from e
+
+
+def clean_orphan_obj_perms():
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 5 from django-haystack
+[django-haystack](https://github.com/django-haystack/django-haystack)
+([project website](http://haystacksearch.org/) and
+[PyPI page](https://pypi.org/project/django-haystack/))
+is a search abstraction layer that separates the Python search code
+in a [Django](/django.html) web application from the search engine
+implementation that it runs on, such as
+[Apache Solr](http://lucene.apache.org/solr/),
+[Elasticsearch](https://www.elastic.co/)
+or [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html).
+
+The django-haystack project is open source under the
+[BSD license](https://github.com/django-haystack/django-haystack/blob/master/LICENSE).
+
+[**django-haystack / haystack / models.py**](https://github.com/django-haystack/django-haystack/blob/master/haystack/./models.py)
+
+```python
+# models.py
+
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.utils.encoding import force_str
+from django.utils.text import capfirst
+
+from haystack.constants import DEFAULT_ALIAS
+from haystack.exceptions import NotHandled, SpatialError
+from haystack.utils import log as logging
+from haystack.utils.app_loading import haystack_get_model
+
+try:
+ from geopy import distance as geopy_distance
+except ImportError:
+ geopy_distance = None
+
+
+class SearchResult(object):
+
+ def __init__(self, app_label, model_name, pk, score, **kwargs):
+ self.app_label, self.model_name = app_label, model_name
+ self.pk = pk
+ self.score = score
+ self._object = None
+ self._model = None
+ self._verbose_name = None
+ self._additional_fields = []
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ return self.__dict__.get(attr, None)
+
+ def _get_searchindex(self):
+ from haystack import connections
+
+ return connections[DEFAULT_ALIAS].get_unified_index().get_index(self.model)
+
+ searchindex = property(_get_searchindex)
+
+ def _get_object(self):
+ if self._object is None:
+ if self.model is None:
+ self.log.error("Model could not be found for SearchResult '%s'.", self)
+ return None
+
+ try:
+ try:
+ self._object = self.searchindex.read_queryset().get(pk=self.pk)
+ except NotHandled:
+ self.log.warning(
+ "Model '%s.%s' not handled by the routers.",
+ self.app_label,
+ self.model_name,
+ )
+ self._object = self.model._default_manager.get(pk=self.pk)
+~~ except ObjectDoesNotExist:
+ self.log.error(
+ "Object could not be found in database for SearchResult '%s'.", self
+ )
+ self._object = None
+
+ return self._object
+
+ def _set_object(self, obj):
+ self._object = obj
+
+ object = property(_get_object, _set_object)
+
+ def _get_model(self):
+ if self._model is None:
+ try:
+ self._model = haystack_get_model(self.app_label, self.model_name)
+ except LookupError:
+ pass
+
+ return self._model
+
+ def _set_model(self, obj):
+ self._model = obj
+
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 6 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / fields.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./fields.py)
+
+```python
+# fields.py
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.db.models.fields import NOT_PROVIDED
+from django.db.models.manager import Manager
+
+from . import widgets
+
+
+class Field:
+ empty_values = [None, '']
+
+ def __init__(self, attribute=None, column_name=None, widget=None,
+ default=NOT_PROVIDED, readonly=False, saves_null_values=True):
+ self.attribute = attribute
+ self.default = default
+ self.column_name = column_name
+ if not widget:
+ widget = widgets.Widget()
+ self.widget = widget
+ self.readonly = readonly
+ self.saves_null_values = saves_null_values
+
+ def __repr__(self):
+ path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
+ column_name = getattr(self, 'column_name', None)
+ if column_name is not None:
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ try:
+ value = data[self.column_name]
+ except KeyError:
+ raise KeyError("Column '%s' not found in dataset. Available "
+ "columns are: %s" % (self.column_name, list(data)))
+
+ value = self.widget.clean(value, row=data)
+
+ if value in self.empty_values and self.default != NOT_PROVIDED:
+ if callable(self.default):
+ return self.default()
+ return self.default
+
+ return value
+
+ def get_value(self, obj):
+ if self.attribute is None:
+ return None
+
+ attrs = self.attribute.split('__')
+ value = obj
+
+ for attr in attrs:
+ try:
+ value = getattr(value, attr, None)
+~~ except (ValueError, ObjectDoesNotExist):
+ return None
+ if value is None:
+ return None
+
+ if callable(value) and not isinstance(value, Manager):
+ value = value()
+ return value
+
+ def save(self, obj, data, is_m2m=False):
+ if not self.readonly:
+ attrs = self.attribute.split('__')
+ for attr in attrs[:-1]:
+ obj = getattr(obj, attr, None)
+ cleaned = self.clean(data)
+ if cleaned is not None or self.saves_null_values:
+ if not is_m2m:
+ setattr(obj, attrs[-1], cleaned)
+ else:
+ getattr(obj, attrs[-1]).set(cleaned)
+
+ def export(self, obj):
+ value = self.get_value(obj)
+ if value is None:
+ return ""
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 7 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / managers.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./managers.py)
+
+```python
+# managers.py
+import django
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.db import connection
+from django.db import models
+from django.db.models.constants import LOOKUP_SEP
+from django.db.models.fields.related import OneToOneField, OneToOneRel
+from django.db.models.query import ModelIterable
+from django.db.models.query import QuerySet
+from django.db.models.sql.datastructures import Join
+
+
+class InheritanceIterable(ModelIterable):
+ def __iter__(self):
+ queryset = self.queryset
+ iter = ModelIterable(queryset)
+ if getattr(queryset, 'subclasses', False):
+ extras = tuple(queryset.query.extra.keys())
+ subclasses = sorted(queryset.subclasses, key=len, reverse=True)
+ for obj in iter:
+ sub_obj = None
+ for s in subclasses:
+ sub_obj = queryset._get_sub_obj_recurse(obj, s)
+ if sub_obj:
+ break
+ if not sub_obj:
+ sub_obj = obj
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ def _get_ancestors_path(self, model, levels=None):
+ if not issubclass(model, self.model):
+ raise ValueError(
+ "{!r} is not a subclass of {!r}".format(model, self.model))
+
+ ancestry = []
+ parent_link = model._meta.get_ancestor_link(self.model)
+ if levels:
+ levels -= 1
+ while parent_link is not None:
+ related = parent_link.remote_field
+ ancestry.insert(0, related.get_accessor_name())
+ if levels or levels is None:
+ parent_model = related.model
+ parent_link = parent_model._meta.get_ancestor_link(
+ self.model)
+ else:
+ parent_link = None
+ return LOOKUP_SEP.join(ancestry)
+
+ def _get_sub_obj_recurse(self, obj, s):
+ rel, _, s = s.partition(LOOKUP_SEP)
+
+ try:
+ node = getattr(obj, rel)
+~~ except ObjectDoesNotExist:
+ return None
+ if s:
+ child = self._get_sub_obj_recurse(node, s)
+ return child
+ else:
+ return node
+
+ def get_subclass(self, *args, **kwargs):
+ return self.select_subclasses().get(*args, **kwargs)
+
+
+class InheritanceQuerySet(InheritanceQuerySetMixin, QuerySet):
+ def instance_of(self, *models):
+
+
+
+ where_queries = []
+ for model in models:
+ where_queries.append('(' + ' AND '.join([
+ '"{}"."{}" IS NOT NULL'.format(
+ model._meta.db_table,
+ field.attname, # Should this be something else?
+ ) for field in model._meta.parents.values()
+ ]) + ')')
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 8 from django-oauth-toolkit
+[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit)
+([project website](http://dot.evonove.it/) and
+[PyPI package information](https://pypi.org/project/django-oauth-toolkit/1.2.0/))
+is a code library for adding and handling [OAuth2](https://oauth.net/)
+flows within your [Django](/django.html) web application and
+[API](/application-programming-interfaces.html).
+
+The django-oauth-toolkit project is open sourced under the
+[FreeBSD license](https://github.com/jazzband/django-oauth-toolkit/blob/master/LICENSE)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-oauth-toolkit / oauth2_provider / oauth2_validators.py**](https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/./oauth2_validators.py)
+
+```python
+# oauth2_validators.py
+import base64
+import binascii
+import http.client
+import logging
+from collections import OrderedDict
+from datetime import datetime, timedelta
+from urllib.parse import unquote_plus
+
+import requests
+from django.conf import settings
+from django.contrib.auth import authenticate, get_user_model
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.db import transaction
+from django.db.models import Q
+from django.utils import timezone
+from django.utils.timezone import make_aware
+from django.utils.translation import gettext_lazy as _
+from oauthlib.oauth2 import RequestValidator
+
+from .exceptions import FatalClientError
+from .models import (
+ AbstractApplication, get_access_token_model,
+ get_application_model, get_grant_model, get_refresh_token_model
+)
+from .scopes import get_scopes_backend
+from .settings import oauth2_settings
+
+
+log = logging.getLogger("oauth2_provider")
+
+GRANT_TYPE_MAPPING = {
+ "authorization_code": (AbstractApplication.GRANT_AUTHORIZATION_CODE, ),
+ "password": (AbstractApplication.GRANT_PASSWORD, ),
+ "client_credentials": (AbstractApplication.GRANT_CLIENT_CREDENTIALS, ),
+ "refresh_token": (
+ AbstractApplication.GRANT_AUTHORIZATION_CODE,
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ scope=" ".join(request.scopes),
+ code_challenge=request.code_challenge or "",
+ code_challenge_method=request.code_challenge_method or ""
+ )
+
+ def _create_refresh_token(self, request, refresh_token_code, access_token):
+ return RefreshToken.objects.create(
+ user=request.user,
+ token=refresh_token_code,
+ application=request.client,
+ access_token=access_token
+ )
+
+ def revoke_token(self, token, token_type_hint, request, *args, **kwargs):
+ if token_type_hint not in ["access_token", "refresh_token"]:
+ token_type_hint = None
+
+ token_types = {
+ "access_token": AccessToken,
+ "refresh_token": RefreshToken,
+ }
+
+ token_type = token_types.get(token_type_hint, AccessToken)
+ try:
+ token_type.objects.get(token=token).revoke()
+~~ except ObjectDoesNotExist:
+ for other_type in [_t for _t in token_types.values() if _t != token_type]:
+ list(map(lambda t: t.revoke(), other_type.objects.filter(token=token)))
+
+ def validate_user(self, username, password, client, request, *args, **kwargs):
+ u = authenticate(username=username, password=password)
+ if u is not None and u.is_active:
+ request.user = u
+ return True
+ return False
+
+ def get_original_scopes(self, refresh_token, request, *args, **kwargs):
+ rt = request.refresh_token_instance
+ if not rt.access_token_id:
+ return AccessToken.objects.get(source_refresh_token_id=rt.id).scope
+
+ return rt.access_token.scope
+
+ def validate_refresh_token(self, refresh_token, client, request, *args, **kwargs):
+
+ null_or_recent = Q(revoked__isnull=True) | Q(
+ revoked__gt=timezone.now() - timedelta(
+ seconds=oauth2_settings.REFRESH_TOKEN_GRACE_PERIOD_SECONDS
+ )
+ )
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
+
+## Example 9 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / fields.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./fields.py)
+
+```python
+# fields.py
+import copy
+import datetime
+import decimal
+import functools
+import inspect
+import re
+import uuid
+import warnings
+from collections import OrderedDict
+from collections.abc import Mapping
+
+from django.conf import settings
+~~from django.core.exceptions import ObjectDoesNotExist
+from django.core.exceptions import ValidationError as DjangoValidationError
+from django.core.validators import (
+ EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
+ MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
+ URLValidator, ip_address_validators
+)
+from django.forms import FilePathField as DjangoFilePathField
+from django.forms import ImageField as DjangoImageField
+from django.utils import timezone
+from django.utils.dateparse import (
+ parse_date, parse_datetime, parse_duration, parse_time
+)
+from django.utils.duration import duration_string
+from django.utils.encoding import is_protected_type, smart_str
+from django.utils.formats import localize_input, sanitize_separators
+from django.utils.ipv6 import clean_ipv6_address
+from django.utils.timezone import utc
+from django.utils.translation import gettext_lazy as _
+from pytz.exceptions import InvalidTimeError
+
+from rest_framework import (
+ ISO_8601, RemovedInDRF313Warning, RemovedInDRF314Warning
+)
+from rest_framework.exceptions import ErrorDetail, ValidationError
+
+
+## ... source file abbreviated to get to ObjectDoesNotExist examples ...
+
+
+ if inspect.isbuiltin(obj):
+ raise BuiltinSignatureError(
+ 'Built-in function signatures are not inspectable. '
+ 'Wrap the function call in a simple, pure Python function.')
+
+ if not (inspect.isfunction(obj) or inspect.ismethod(obj) or isinstance(obj, functools.partial)):
+ return False
+
+ sig = inspect.signature(obj)
+ params = sig.parameters.values()
+ return all(
+ param.kind == param.VAR_POSITIONAL or
+ param.kind == param.VAR_KEYWORD or
+ param.default != param.empty
+ for param in params
+ )
+
+
+def get_attribute(instance, attrs):
+ for attr in attrs:
+ try:
+ if isinstance(instance, Mapping):
+ instance = instance[attr]
+ else:
+ instance = getattr(instance, attr)
+~~ except ObjectDoesNotExist:
+ return None
+ if is_simple_callable(instance):
+ try:
+ instance = instance()
+ except (AttributeError, KeyError) as exc:
+ raise ValueError('Exception raised in callable attribute "{}"; original exception was: {}'.format(attr, exc))
+
+ return instance
+
+
+def set_value(dictionary, keys, value):
+ if not keys:
+ dictionary.update(value)
+ return
+
+ for key in keys[:-1]:
+ if key not in dictionary:
+ dictionary[key] = {}
+ dictionary = dictionary[key]
+
+ dictionary[keys[-1]] = value
+
+
+def to_choices_dict(choices):
+
+
+## ... source file continues with no further ObjectDoesNotExist examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-permissiondenied.markdown b/content/pages/examples/django/django-core-exceptions-permissiondenied.markdown
new file mode 100644
index 000000000..e741da531
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-permissiondenied.markdown
@@ -0,0 +1,1120 @@
+title: django.core.exceptions PermissionDenied Example Code
+category: page
+slug: django-core-exceptions-permissiondenied-examples
+sortorder: 500011105
+toc: False
+sidebartitle: django.core.exceptions PermissionDenied
+meta: Python example code for the PermissionDenied class from the django.core.exceptions module of the Django project.
+
+
+PermissionDenied is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / socialaccount / models.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py)
+
+```python
+# models.py
+from __future__ import absolute_import
+
+from django.contrib.auth import authenticate
+from django.contrib.sites.models import Site
+from django.contrib.sites.shortcuts import get_current_site
+~~from django.core.exceptions import PermissionDenied
+from django.db import models
+from django.utils.crypto import get_random_string
+from django.utils.encoding import force_str
+from django.utils.translation import gettext_lazy as _
+
+import allauth.app_settings
+from allauth.account.models import EmailAddress
+from allauth.account.utils import get_next_redirect_url, setup_user_email
+from allauth.utils import get_user_model
+
+from ..utils import get_request_param
+from . import app_settings, providers
+from .adapter import get_adapter
+from .fields import JSONField
+
+
+class SocialAppManager(models.Manager):
+ def get_current(self, provider, request=None):
+ cache = {}
+ if request:
+ cache = getattr(request, '_socialapp_cache', {})
+ request._socialapp_cache = cache
+ app = cache.get(provider)
+ if not app:
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+ def get_redirect_url(self, request):
+ url = self.state.get('next')
+ return url
+
+ @classmethod
+ def state_from_request(cls, request):
+ state = {}
+ next_url = get_next_redirect_url(request)
+ if next_url:
+ state['next'] = next_url
+ state['process'] = get_request_param(request, 'process', 'login')
+ state['scope'] = get_request_param(request, 'scope', '')
+ state['auth_params'] = get_request_param(request, 'auth_params', '')
+ return state
+
+ @classmethod
+ def stash_state(cls, request):
+ state = cls.state_from_request(request)
+ verifier = get_random_string()
+ request.session['socialaccount_state'] = (state, verifier)
+ return verifier
+
+ @classmethod
+ def unstash_state(cls, request):
+ if 'socialaccount_state' not in request.session:
+~~ raise PermissionDenied()
+ state, verifier = request.session.pop('socialaccount_state')
+ return state
+
+ @classmethod
+ def verify_and_unstash_state(cls, request, verifier):
+ if 'socialaccount_state' not in request.session:
+~~ raise PermissionDenied()
+ state, verifier2 = request.session.pop('socialaccount_state')
+ if verifier != verifier2:
+~~ raise PermissionDenied()
+ return state
+
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 2 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / exceptions.py**](https://github.com/jazzband/django-axes/blob/master/axes/./exceptions.py)
+
+```python
+# exceptions.py
+~~from django.core.exceptions import PermissionDenied
+
+
+~~class AxesBackendPermissionDenied(PermissionDenied):
+
+
+class AxesBackendRequestParameterRequired(ValueError):
+
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 3 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / api.py**](https://github.com/divio/django-cms/blob/develop/cms/./api.py)
+
+```python
+# api.py
+import datetime
+
+from django.contrib.auth import get_user_model
+from django.contrib.sites.models import Site
+from django.core.exceptions import FieldError
+~~from django.core.exceptions import PermissionDenied
+from django.core.exceptions import ValidationError
+from django.db import transaction
+from django.template.defaultfilters import slugify
+from django.template.loader import get_template
+from django.utils.translation import activate
+
+from six import string_types
+
+from cms import constants
+from cms.app_base import CMSApp
+from cms.apphook_pool import apphook_pool
+from cms.constants import TEMPLATE_INHERITANCE_MAGIC
+from cms.models.pagemodel import Page
+from cms.models.permissionmodels import (PageUser, PagePermission, GlobalPagePermission,
+ ACCESS_PAGE_AND_DESCENDANTS)
+from cms.models.placeholdermodel import Placeholder
+from cms.models.pluginmodel import CMSPlugin
+from cms.models.titlemodels import Title
+from cms.plugin_base import CMSPluginBase
+from cms.plugin_pool import plugin_pool
+from cms.utils import copy_plugins, get_current_site
+from cms.utils.conf import get_cms_setting
+from cms.utils.i18n import get_language_list
+from cms.utils.page import get_available_slug
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+ 'can_add': can_add or grant_all,
+ 'can_change': can_change or grant_all,
+ 'can_delete': can_delete or grant_all,
+ 'can_change_advanced_settings': can_change_advanced_settings or grant_all,
+ 'can_publish': can_publish or grant_all,
+ 'can_change_permissions': can_change_permissions or grant_all,
+ 'can_move_page': can_move_page or grant_all,
+ 'can_view': can_view or grant_all,
+ }
+
+ page_permission = PagePermission(page=page, user=user,
+ grant_on=grant_on, **data)
+ page_permission.save()
+ if global_permission:
+ page_permission = GlobalPagePermission(
+ user=user, can_recover_page=can_recover_page, **data)
+ page_permission.save()
+ page_permission.sites.add(get_current_site())
+ return page_permission
+
+
+def publish_page(page, user, language):
+ page = page.reload()
+
+ if not page.has_publish_permission(user):
+~~ raise PermissionDenied()
+ with current_user(user.get_username()):
+ page.publish(language)
+ return page.reload()
+
+
+def publish_pages(include_unpublished=False, language=None, site=None):
+ qs = Page.objects.drafts()
+
+ if not include_unpublished:
+ qs = qs.filter(title_set__published=True).distinct()
+
+ if site:
+ qs = qs.filter(node__site=site)
+
+ output_language = None
+ for i, page in enumerate(qs):
+ add = True
+ titles = page.title_set
+ if not include_unpublished:
+ titles = titles.filter(published=True)
+ for lang in titles.values_list("language", flat=True):
+ if language is None or lang == language:
+ if not output_language:
+ output_language = lang
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 4 from django-downloadview
+[django-downloadview](https://github.com/benoitbryon/django-downloadview)
+([project documentation](https://django-downloadview.readthedocs.io/en/1.9/)
+and
+[PyPI package information](https://pypi.org/project/django-downloadview/))
+is a [Django](/django.html) extension for serving downloads through your
+web application. While typically you would use a web server to handle
+[static content](/static-content.html), sometimes you need to control
+file access, such as requiring a user to register before downloading a
+PDF. In that situations, django-downloadview is a handy library to avoid
+boilerplate code for common scenarios.
+
+[**django-downloadview / django_downloadview / decorators.py**](https://github.com/benoitbryon/django-downloadview/blob/master/django_downloadview/./decorators.py)
+
+```python
+# decorators.py
+
+from functools import wraps
+
+from django.conf import settings
+~~from django.core.exceptions import PermissionDenied
+from django.core.signing import BadSignature, SignatureExpired, TimestampSigner
+
+
+class DownloadDecorator(object):
+
+ def __init__(self, middleware_factory):
+ self.middleware_factory = middleware_factory
+
+ def __call__(self, view_func, *middleware_args, **middleware_kwargs):
+
+ def decorated(request, *view_args, **view_kwargs):
+ response = view_func(request, *view_args, **view_kwargs)
+ middleware = self.middleware_factory(*middleware_args, **middleware_kwargs)
+ return middleware.process_response(request, response)
+
+ return decorated
+
+
+def _signature_is_valid(request):
+
+ signer = TimestampSigner()
+ signature = request.GET.get("X-Signature")
+ expiration = getattr(settings, "DOWNLOADVIEW_URL_EXPIRATION", None)
+
+ try:
+ signature_path = signer.unsign(signature, max_age=expiration)
+ except SignatureExpired as e:
+~~ raise PermissionDenied("Signature expired") from e
+ except BadSignature as e:
+~~ raise PermissionDenied("Signature invalid") from e
+ except Exception as e:
+~~ raise PermissionDenied("Signature error") from e
+
+ if request.path != signature_path:
+~~ raise PermissionDenied("Signature mismatch")
+
+
+def signature_required(function):
+
+ @wraps(function)
+ def decorator(request, *args, **kwargs):
+ _signature_is_valid(request)
+ return function(request, *args, **kwargs)
+
+ return decorator
+
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 5 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / admin / tools.py**](https://github.com/divio/django-filer/blob/develop/filer/admin/tools.py)
+
+```python
+# tools.py
+from __future__ import absolute_import, unicode_literals
+
+from django.contrib.admin.options import IS_POPUP_VAR
+~~from django.core.exceptions import PermissionDenied
+from django.utils.http import urlencode
+
+
+ALLOWED_PICK_TYPES = ('folder', 'file')
+
+
+def check_files_edit_permissions(request, files):
+ for f in files:
+ if not f.has_edit_permission(request):
+~~ raise PermissionDenied
+
+
+def check_folder_edit_permissions(request, folders):
+ for f in folders:
+ if not f.has_edit_permission(request):
+~~ raise PermissionDenied
+ check_files_edit_permissions(request, f.files)
+ check_folder_edit_permissions(request, f.children.all())
+
+
+def check_files_read_permissions(request, files):
+ for f in files:
+ if not f.has_read_permission(request):
+~~ raise PermissionDenied
+
+
+def check_folder_read_permissions(request, folders):
+ for f in folders:
+ if not f.has_read_permission(request):
+~~ raise PermissionDenied
+ check_files_read_permissions(request, f.files)
+ check_folder_read_permissions(request, f.children.all())
+
+
+def userperms_for_request(item, request):
+ r = []
+ ps = ['read', 'edit', 'add_children']
+ for p in ps:
+ attr = "has_%s_permission" % p
+ if hasattr(item, attr):
+ x = getattr(item, attr)(request)
+ if x:
+ r.append(p)
+ return r
+
+
+def popup_status(request):
+ return (
+ IS_POPUP_VAR in request.GET
+ or 'pop' in request.GET
+ or IS_POPUP_VAR in request.POST
+ or 'pop' in request.POST
+ )
+
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 6 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / mixins.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./mixins.py)
+
+```python
+# mixins.py
+from collections.abc import Iterable
+
+from django.conf import settings
+from django.contrib.auth.decorators import login_required, REDIRECT_FIELD_NAME
+~~from django.core.exceptions import ImproperlyConfigured, PermissionDenied
+from guardian.utils import get_user_obj_perms_model
+UserObjectPermission = get_user_obj_perms_model()
+from guardian.utils import get_40x_or_None, get_anonymous_user
+from guardian.shortcuts import get_objects_for_user
+
+
+class LoginRequiredMixin:
+ redirect_field_name = REDIRECT_FIELD_NAME
+ login_url = settings.LOGIN_URL
+
+ def dispatch(self, request, *args, **kwargs):
+ return login_required(redirect_field_name=self.redirect_field_name,
+ login_url=self.login_url)(
+ super().dispatch
+ )(request, *args, **kwargs)
+
+
+class PermissionRequiredMixin:
+ login_url = settings.LOGIN_URL
+ permission_required = None
+ redirect_field_name = REDIRECT_FIELD_NAME
+ return_403 = False
+ return_404 = False
+ raise_exception = False
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+ % self.permission_required)
+ return perms
+
+ def get_permission_object(self):
+ if hasattr(self, 'permission_object'):
+ return self.permission_object
+ return (hasattr(self, 'get_object') and self.get_object() or
+ getattr(self, 'object', None))
+
+ def check_permissions(self, request):
+ obj = self.get_permission_object()
+
+ forbidden = get_40x_or_None(request,
+ perms=self.get_required_permissions(
+ request),
+ obj=obj,
+ login_url=self.login_url,
+ redirect_field_name=self.redirect_field_name,
+ return_403=self.return_403,
+ return_404=self.return_404,
+ accept_global_perms=self.accept_global_perms
+ )
+ if forbidden:
+ self.on_permission_check_fail(request, forbidden, obj=obj)
+ if forbidden and self.raise_exception:
+~~ raise PermissionDenied()
+ return forbidden
+
+ def on_permission_check_fail(self, request, response, obj=None):
+
+ def dispatch(self, request, *args, **kwargs):
+ self.request = request
+ self.args = args
+ self.kwargs = kwargs
+ response = self.check_permissions(request)
+ if response:
+ return response
+ return super().dispatch(request, *args, **kwargs)
+
+
+class GuardianUserMixin:
+
+ @staticmethod
+ def get_anonymous():
+ return get_anonymous_user()
+
+ def add_obj_perm(self, perm, obj):
+ return UserObjectPermission.objects.assign_perm(perm, self, obj)
+
+ def del_obj_perm(self, perm, obj):
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 7 from django-haystack
+[django-haystack](https://github.com/django-haystack/django-haystack)
+([project website](http://haystacksearch.org/) and
+[PyPI page](https://pypi.org/project/django-haystack/))
+is a search abstraction layer that separates the Python search code
+in a [Django](/django.html) web application from the search engine
+implementation that it runs on, such as
+[Apache Solr](http://lucene.apache.org/solr/),
+[Elasticsearch](https://www.elastic.co/)
+or [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html).
+
+The django-haystack project is open source under the
+[BSD license](https://github.com/django-haystack/django-haystack/blob/master/LICENSE).
+
+[**django-haystack / haystack / admin.py**](https://github.com/django-haystack/django-haystack/blob/master/haystack/./admin.py)
+
+```python
+# admin.py
+from django.contrib.admin.options import ModelAdmin, csrf_protect_m
+from django.contrib.admin.views.main import SEARCH_VAR, ChangeList
+~~from django.core.exceptions import PermissionDenied
+from django.core.paginator import InvalidPage, Paginator
+from django.shortcuts import render
+from django.utils.encoding import force_str
+from django.utils.translation import ungettext
+
+from haystack import connections
+from haystack.constants import DEFAULT_ALIAS
+from haystack.query import SearchQuerySet
+from haystack.utils import get_model_ct_tuple
+
+
+class SearchChangeList(ChangeList):
+ def __init__(self, **kwargs):
+ self.haystack_connection = kwargs.pop("haystack_connection", DEFAULT_ALIAS)
+ super(SearchChangeList, self).__init__(**kwargs)
+
+ def get_results(self, request):
+ if SEARCH_VAR not in request.GET:
+ return super(SearchChangeList, self).get_results(request)
+
+ sqs = (
+ SearchQuerySet(self.haystack_connection)
+ .models(self.model)
+ .auto_query(request.GET[SEARCH_VAR])
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+ )
+
+ can_show_all = result_count <= self.list_max_show_all
+ multi_page = result_count > self.list_per_page
+
+ try:
+ result_list = paginator.page(self.page_num + 1).object_list
+ result_list = [result.object for result in result_list]
+ except InvalidPage:
+ result_list = ()
+
+ self.result_count = result_count
+ self.full_result_count = full_result_count
+ self.result_list = result_list
+ self.can_show_all = can_show_all
+ self.multi_page = multi_page
+ self.paginator = paginator
+
+
+class SearchModelAdminMixin(object):
+ haystack_connection = DEFAULT_ALIAS
+
+ @csrf_protect_m
+ def changelist_view(self, request, extra_context=None):
+ if not self.has_change_permission(request, None):
+~~ raise PermissionDenied
+
+ if SEARCH_VAR not in request.GET:
+ return super(SearchModelAdminMixin, self).changelist_view(
+ request, extra_context
+ )
+
+ indexed_models = (
+ connections[self.haystack_connection]
+ .get_unified_index()
+ .get_indexed_models()
+ )
+
+ if self.model not in indexed_models:
+ return super(SearchModelAdminMixin, self).changelist_view(
+ request, extra_context
+ )
+
+ list_display = list(self.list_display)
+
+ kwargs = {
+ "haystack_connection": self.haystack_connection,
+ "request": request,
+ "model": self.model,
+ "list_display": list_display,
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 8 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / admin.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./admin.py)
+
+```python
+# admin.py
+from datetime import datetime
+
+import django
+from django import forms
+from django.conf import settings
+from django.conf.urls import url
+from django.contrib import admin, messages
+from django.contrib.admin.models import ADDITION, CHANGE, DELETION, LogEntry
+from django.contrib.auth import get_permission_codename
+from django.contrib.contenttypes.models import ContentType
+~~from django.core.exceptions import PermissionDenied
+from django.http import HttpResponse, HttpResponseRedirect
+from django.template.response import TemplateResponse
+from django.urls import reverse
+from django.utils.decorators import method_decorator
+from django.utils.encoding import force_str
+from django.utils.module_loading import import_string
+from django.utils.translation import gettext_lazy as _
+from django.views.decorators.http import require_POST
+
+from .formats.base_formats import DEFAULT_FORMATS
+from .forms import ConfirmImportForm, ExportForm, ImportForm, export_action_form_factory
+from .resources import modelresource_factory
+from .results import RowResult
+from .signals import post_export, post_import
+from .tmp_storages import TempFolderStorage
+
+SKIP_ADMIN_LOG = getattr(settings, 'IMPORT_EXPORT_SKIP_ADMIN_LOG', False)
+TMP_STORAGE_CLASS = getattr(settings, 'IMPORT_EXPORT_TMP_STORAGE_CLASS',
+ TempFolderStorage)
+
+
+if isinstance(TMP_STORAGE_CLASS, str):
+ TMP_STORAGE_CLASS = import_string(TMP_STORAGE_CLASS)
+
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+ name='%s_%s_import' % info),
+ ]
+ return my_urls + urls
+
+ def get_resource_kwargs(self, request, *args, **kwargs):
+ return {}
+
+ def get_import_resource_kwargs(self, request, *args, **kwargs):
+ return self.get_resource_kwargs(request, *args, **kwargs)
+
+ def get_resource_class(self):
+ if not self.resource_class:
+ return modelresource_factory(self.model)
+ else:
+ return self.resource_class
+
+ def get_import_resource_class(self):
+ return self.get_resource_class()
+
+ def get_import_formats(self):
+ return [f for f in self.formats if f().can_import()]
+
+ @method_decorator(require_POST)
+ def process_import(self, request, *args, **kwargs):
+ if not self.has_import_permission(request):
+~~ raise PermissionDenied
+
+ form_type = self.get_confirm_import_form()
+ confirm_form = form_type(request.POST)
+ if confirm_form.is_valid():
+ import_formats = self.get_import_formats()
+ input_format = import_formats[
+ int(confirm_form.cleaned_data['input_format'])
+ ]()
+ tmp_storage = self.get_tmp_storage_class()(name=confirm_form.cleaned_data['import_file_name'])
+ data = tmp_storage.read(input_format.get_read_mode())
+ if not input_format.is_binary() and self.from_encoding:
+ data = force_str(data, self.from_encoding)
+ dataset = input_format.create_dataset(data)
+
+ result = self.process_dataset(dataset, confirm_form, request, *args, **kwargs)
+
+ tmp_storage.remove()
+
+ return self.process_result(result, request)
+
+ def process_dataset(self, dataset, confirm_form, request, *args, **kwargs):
+
+ res_kwargs = self.get_import_resource_kwargs(request, form=confirm_form, *args, **kwargs)
+ resource = self.get_import_resource_class()(**res_kwargs)
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+
+ def get_confirm_import_form(self):
+ return ConfirmImportForm
+
+ def get_form_kwargs(self, form, *args, **kwargs):
+ return kwargs
+
+ def get_import_data_kwargs(self, request, *args, **kwargs):
+ form = kwargs.get('form')
+ if form:
+ kwargs.pop('form')
+ return kwargs
+ return {}
+
+ def write_to_tmp_storage(self, import_file, input_format):
+ tmp_storage = self.get_tmp_storage_class()()
+ data = bytes()
+ for chunk in import_file.chunks():
+ data += chunk
+
+ tmp_storage.save(data, input_format.get_read_mode())
+ return tmp_storage
+
+ def import_action(self, request, *args, **kwargs):
+ if not self.has_import_permission(request):
+~~ raise PermissionDenied
+
+ context = self.get_import_context_data()
+
+ import_formats = self.get_import_formats()
+ form_type = self.get_import_form()
+ form_kwargs = self.get_form_kwargs(form_type, *args, **kwargs)
+ form = form_type(import_formats,
+ request.POST or None,
+ request.FILES or None,
+ **form_kwargs)
+
+ if request.POST and form.is_valid():
+ input_format = import_formats[
+ int(form.cleaned_data['input_format'])
+ ]()
+ import_file = form.cleaned_data['import_file']
+ tmp_storage = self.write_to_tmp_storage(import_file, input_format)
+
+ try:
+ data = tmp_storage.read(input_format.get_read_mode())
+ if not input_format.is_binary() and self.from_encoding:
+ data = force_str(data, self.from_encoding)
+ dataset = input_format.create_dataset(data)
+ except UnicodeDecodeError as e:
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+
+ ChangeList = self.get_changelist(request)
+ changelist_kwargs = {
+ 'request': request,
+ 'model': self.model,
+ 'list_display': list_display,
+ 'list_display_links': list_display_links,
+ 'list_filter': list_filter,
+ 'date_hierarchy': self.date_hierarchy,
+ 'search_fields': search_fields,
+ 'list_select_related': self.list_select_related,
+ 'list_per_page': self.list_per_page,
+ 'list_max_show_all': self.list_max_show_all,
+ 'list_editable': self.list_editable,
+ 'model_admin': self,
+ }
+ if django.VERSION >= (2, 1):
+ changelist_kwargs['sortable_by'] = self.sortable_by
+ cl = ChangeList(**changelist_kwargs)
+
+ return cl.get_queryset(request)
+
+ def get_export_data(self, file_format, queryset, *args, **kwargs):
+ request = kwargs.pop("request")
+ if not self.has_export_permission(request):
+~~ raise PermissionDenied
+
+ resource_class = self.get_export_resource_class()
+ data = resource_class(**self.get_export_resource_kwargs(request)).export(queryset, *args, **kwargs)
+ export_data = file_format.export_data(data)
+ return export_data
+
+ def get_export_context_data(self, **kwargs):
+ return self.get_context_data(**kwargs)
+
+ def get_context_data(self, **kwargs):
+ return {}
+
+ def export_action(self, request, *args, **kwargs):
+ if not self.has_export_permission(request):
+~~ raise PermissionDenied
+
+ formats = self.get_export_formats()
+ form = ExportForm(formats, request.POST or None)
+ if form.is_valid():
+ file_format = formats[
+ int(form.cleaned_data['file_format'])
+ ]()
+
+ queryset = self.get_export_queryset(request)
+ export_data = self.get_export_data(file_format, queryset, request=request)
+ content_type = file_format.get_content_type()
+ response = HttpResponse(export_data, content_type=content_type)
+ response['Content-Disposition'] = 'attachment; filename="%s"' % (
+ self.get_export_filename(request, queryset, file_format),
+ )
+
+ post_export.send(sender=None, model=self.model)
+ return response
+
+ context = self.get_export_context_data()
+
+ context.update(self.admin_site.each_context(request))
+
+ context['title'] = _("Export")
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 9 from django-loginas
+[django-loginas](https://github.com/skorokithakis/django-loginas)
+([PyPI package information](https://pypi.org/project/django-loginas/))
+is [Django](/django.html) code library for admins to log into an application
+as another user, typically for debugging purposes.
+
+django-loginas is open source under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/skorokithakis/django-loginas/blob/master/LICENSE).
+
+[**django-loginas / loginas / views.py**](https://github.com/skorokithakis/django-loginas/blob/master/loginas/./views.py)
+
+```python
+# views.py
+from django.contrib import messages
+from django.contrib.admin.utils import unquote
+~~from django.core.exceptions import ImproperlyConfigured, PermissionDenied
+from django.shortcuts import redirect
+from django.utils.translation import gettext_lazy as _
+from django.views.decorators.csrf import csrf_protect
+from django.views.decorators.http import require_POST
+
+from . import settings as la_settings
+from .utils import login_as, restore_original_login
+
+try:
+ from importlib import import_module
+except ImportError:
+ from django.utils.importlib import import_module # type: ignore
+
+
+try:
+ from django.contrib.auth import get_user_model
+
+ User = get_user_model()
+except ImportError:
+ from django.contrib.auth.models import User # type: ignore
+
+
+def _load_module(path):
+
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+ except ValueError:
+ raise ImproperlyConfigured("Error importing CAN_LOGIN_AS" " function. Is CAN_LOGIN_AS a" " string?")
+
+ try:
+ can_login_as = getattr(mod, attr)
+ except AttributeError:
+ raise ImproperlyConfigured("Module {0} does not define a {1} " "function.".format(module, attr))
+ return can_login_as
+
+
+@csrf_protect
+@require_POST
+def user_login(request, user_id):
+ user = User.objects.get(pk=unquote(user_id))
+
+ if isinstance(la_settings.CAN_LOGIN_AS, str):
+ can_login_as = _load_module(la_settings.CAN_LOGIN_AS)
+ elif hasattr(la_settings.CAN_LOGIN_AS, "__call__"):
+ can_login_as = la_settings.CAN_LOGIN_AS
+ else:
+ raise ImproperlyConfigured("The CAN_LOGIN_AS setting is neither a valid module nor callable.")
+ no_permission_error = None
+ try:
+ if not can_login_as(request, user):
+ no_permission_error = _("You do not have permission to do that.")
+~~ except PermissionDenied as e:
+ no_permission_error = str(e)
+ if no_permission_error is not None:
+ messages.error(request, no_permission_error, extra_tags=la_settings.MESSAGE_EXTRA_TAGS, fail_silently=True)
+ return redirect(request.META.get("HTTP_REFERER", "/"))
+
+ try:
+ login_as(user, request)
+ except ImproperlyConfigured as e:
+ messages.error(request, str(e), extra_tags=la_settings.MESSAGE_EXTRA_TAGS, fail_silently=True)
+ return redirect(request.META.get("HTTP_REFERER", "/"))
+
+ return redirect(la_settings.LOGIN_REDIRECT)
+
+
+def user_logout(request):
+ restore_original_login(request)
+
+ return redirect(la_settings.LOGOUT_REDIRECT)
+
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 10 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / metadata.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./metadata.py)
+
+```python
+# metadata.py
+from collections import OrderedDict
+
+~~from django.core.exceptions import PermissionDenied
+from django.http import Http404
+from django.utils.encoding import force_str
+
+from rest_framework import exceptions, serializers
+from rest_framework.request import clone_request
+from rest_framework.utils.field_mapping import ClassLookupDict
+
+
+class BaseMetadata:
+ def determine_metadata(self, request, view):
+ raise NotImplementedError(".determine_metadata() must be overridden.")
+
+
+class SimpleMetadata(BaseMetadata):
+ label_lookup = ClassLookupDict({
+ serializers.Field: 'field',
+ serializers.BooleanField: 'boolean',
+ serializers.NullBooleanField: 'boolean',
+ serializers.CharField: 'string',
+ serializers.UUIDField: 'string',
+ serializers.URLField: 'url',
+ serializers.EmailField: 'email',
+ serializers.RegexField: 'regex',
+ serializers.SlugField: 'slug',
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
+
+## Example 11 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / admin / auth.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/admin/auth.py)
+
+```python
+# auth.py
+import types
+from functools import wraps
+
+import l18n
+
+from django.contrib.auth import get_user_model
+from django.contrib.auth.views import redirect_to_login as auth_redirect_to_login
+~~from django.core.exceptions import PermissionDenied
+from django.db.models import Q
+from django.shortcuts import redirect
+from django.urls import reverse
+from django.utils.timezone import activate as activate_tz
+from django.utils.translation import gettext as _
+from django.utils.translation import override
+
+from wagtail.admin import messages
+from wagtail.core.models import GroupPagePermission
+
+
+def users_with_page_permission(page, permission_type, include_superusers=True):
+ User = get_user_model()
+
+ ancestors_and_self = list(page.get_ancestors()) + [page]
+ perm = GroupPagePermission.objects.filter(permission_type=permission_type, page__in=ancestors_and_self)
+ q = Q(groups__page_permissions__in=perm)
+
+ if include_superusers:
+ q |= Q(is_superuser=True)
+
+ return User.objects.filter(is_active=True).filter(q).distinct()
+
+
+def permission_denied(request):
+ if request.is_ajax():
+~~ raise PermissionDenied
+
+ from wagtail.admin import messages
+
+ messages.error(request, _('Sorry, you do not have permission to access this area.'))
+ return redirect('wagtailadmin_home')
+
+
+def user_passes_test(test):
+ def decorator(view_func):
+
+ @wraps(view_func)
+ def wrapped_view_func(request, *args, **kwargs):
+ if test(request.user):
+ return view_func(request, *args, **kwargs)
+ else:
+ return permission_denied(request)
+
+ return wrapped_view_func
+
+ return decorator
+
+
+def permission_required(permission_name):
+ def test(user):
+
+
+## ... source file abbreviated to get to PermissionDenied examples ...
+
+
+ return user_passes_test(test)
+
+ def require_any(self, *actions):
+ def test(user):
+ return self.policy.user_has_any_permission(user, actions)
+
+ return user_passes_test(test)
+
+
+def user_has_any_page_permission(user):
+ if not user.is_active:
+ return False
+
+ if user.is_superuser:
+ return True
+
+ if GroupPagePermission.objects.filter(group__in=user.groups.all()).exists():
+ return True
+
+
+ return False
+
+
+def reject_request(request):
+ if request.is_ajax():
+~~ raise PermissionDenied
+
+ return auth_redirect_to_login(
+ request.get_full_path(), login_url=reverse('wagtailadmin_login'))
+
+
+def require_admin_access(view_func):
+ def decorated_view(request, *args, **kwargs):
+
+ user = request.user
+
+ if user.is_anonymous:
+ return reject_request(request)
+
+ if user.has_perms(['wagtailadmin.access_admin']):
+ preferred_language = None
+ if hasattr(user, 'wagtail_userprofile'):
+ preferred_language = user.wagtail_userprofile.get_preferred_language()
+ l18n.set_language(preferred_language)
+ time_zone = user.wagtail_userprofile.get_current_time_zone()
+ activate_tz(time_zone)
+ if preferred_language:
+ with override(preferred_language):
+ response = view_func(request, *args, **kwargs)
+ if hasattr(response, "render"):
+
+
+## ... source file continues with no further PermissionDenied examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-suspiciousfileoperation.markdown b/content/pages/examples/django/django-core-exceptions-suspiciousfileoperation.markdown
new file mode 100644
index 000000000..a660aef52
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-suspiciousfileoperation.markdown
@@ -0,0 +1,73 @@
+title: django.core.exceptions SuspiciousFileOperation Example Code
+category: page
+slug: django-core-exceptions-suspiciousfileoperation-examples
+sortorder: 500011106
+toc: False
+sidebartitle: django.core.exceptions SuspiciousFileOperation
+meta: Python example code for the SuspiciousFileOperation class from the django.core.exceptions module of the Django project.
+
+
+SuspiciousFileOperation is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from django-markdown-view
+[django-markdown-view](https://github.com/rgs258/django-markdown-view)
+([PyPI package information](https://pypi.org/project/django-markdown-view/))
+is a Django extension for serving [Markdown](/markdown.html) files as
+[Django templates](/django-templates.html). The project is open
+sourced under the
+[BSD 3-Clause "New" or "Revised" license](https://github.com/rgs258/django-markdown-view/blob/master/LICENSE).
+
+[**django-markdown-view / markdown_view / loaders.py**](https://github.com/rgs258/django-markdown-view/blob/master/markdown_view/./loaders.py)
+
+```python
+# loaders.py
+from django.conf import settings
+~~from django.core.exceptions import SuspiciousFileOperation
+from django.template import Origin
+from django.template.loaders.filesystem import Loader as FilesystemLoader
+from django.template.utils import get_app_template_dirs
+from django.utils._os import safe_join
+
+
+class MarkdownLoader(FilesystemLoader):
+
+ def get_dirs(self):
+ base_dir = getattr(
+ settings,
+ "MARKDOWN_VIEW_BASE_DIR",
+ getattr(
+ settings,
+ "BASE_DIR",
+ None)
+ )
+ dirs = [*get_app_template_dirs('')]
+ if base_dir:
+ dirs.extend([base_dir])
+ return dirs
+
+ def get_template_sources(self, template_name):
+ if template_name.endswith('.md'):
+ template_split = template_name.split("/")
+ template_split.reverse()
+ template_app_dir = template_split.pop()
+ template_split.reverse()
+ for template_dir in self.get_dirs():
+ if template_dir.endswith(template_app_dir):
+ try:
+ name = safe_join(template_dir, *template_split)
+~~ except SuspiciousFileOperation:
+ continue
+
+ yield Origin(
+ name=name,
+ template_name=template_name,
+ loader=self,
+ )
+
+
+
+## ... source file continues with no further SuspiciousFileOperation examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-suspiciousmultipartform.markdown b/content/pages/examples/django/django-core-exceptions-suspiciousmultipartform.markdown
new file mode 100644
index 000000000..c1ce0e7a4
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-suspiciousmultipartform.markdown
@@ -0,0 +1,56 @@
+title: django.core.exceptions SuspiciousMultipartForm Example Code
+category: page
+slug: django-core-exceptions-suspiciousmultipartform-examples
+sortorder: 500011107
+toc: False
+sidebartitle: django.core.exceptions SuspiciousMultipartForm
+meta: Python example code for the SuspiciousMultipartForm class from the django.core.exceptions module of the Django project.
+
+
+SuspiciousMultipartForm is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from django-angular
+[django-angular](https://github.com/jrief/django-angular)
+([project examples website](https://django-angular.awesto.com/classic_form/))
+is a library with helper code to make it easier to use
+[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
+The code for django-angular is
+[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
+
+[**django-angular / djng / views / upload.py**](https://github.com/jrief/django-angular/blob/master/djng/views/upload.py)
+
+```python
+# upload.py
+~~from django.core.exceptions import SuspiciousMultipartForm
+from django.core import signing
+from django.views.generic import View
+from django.http import JsonResponse
+
+from djng import app_settings
+from djng.forms.fields import FileField, ImageField
+
+
+class FileUploadView(View):
+ storage = app_settings.upload_storage
+ thumbnail_size = app_settings.THUMBNAIL_OPTIONS
+ signer = signing.Signer()
+
+ def post(self, request, *args, **kwargs):
+ if request.POST.get('filetype') == 'file':
+ field = FileField
+ elif request.POST.get('filetype') == 'image':
+ field = ImageField
+ else:
+~~ raise SuspiciousMultipartForm("Missing attribute 'filetype' in form data.")
+ data = {}
+ for name, file_obj in request.FILES.items():
+ data[name] = field.preview(file_obj)
+ return JsonResponse(data)
+
+
+
+## ... source file continues with no further SuspiciousMultipartForm examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions-validationerror.markdown b/content/pages/examples/django/django-core-exceptions-validationerror.markdown
new file mode 100644
index 000000000..8c255d787
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions-validationerror.markdown
@@ -0,0 +1,2128 @@
+title: django.core.exceptions ValidationError Example Code
+category: page
+slug: django-core-exceptions-validationerror-examples
+sortorder: 500011108
+toc: False
+sidebartitle: django.core.exceptions ValidationError
+meta: Python example code for the ValidationError class from the django.core.exceptions module of the Django project.
+
+
+ValidationError is a class within the django.core.exceptions module of the Django project.
+
+
+## Example 1 from AuditLog
+[Auditlog](https://github.com/jjkester/django-auditlog)
+([project documentation](https://django-auditlog.readthedocs.io/en/latest/))
+is a [Django](/django.html) app that logs changes to Python objects,
+similar to the Django admin's logs but with more details and
+output formats. Auditlog's source code is provided as open source under the
+[MIT license](https://github.com/jjkester/django-auditlog/blob/master/LICENSE).
+
+[**AuditLog / src / auditlog_tests / tests.py**](https://github.com/jjkester/django-auditlog/blob/master/src/auditlog_tests/tests.py)
+
+```python
+# tests.py
+import datetime
+import django
+from django.conf import settings
+from django.contrib import auth
+from django.contrib.auth.models import User, AnonymousUser
+~~from django.core.exceptions import ValidationError
+from django.db.models.signals import pre_save
+from django.http import HttpResponse
+from django.test import TestCase, RequestFactory
+from django.utils import dateformat, formats, timezone
+from dateutil.tz import gettz
+
+from auditlog.middleware import AuditlogMiddleware
+from auditlog.models import LogEntry
+from auditlog.registry import auditlog
+from auditlog_tests.models import SimpleModel, AltPrimaryKeyModel, UUIDPrimaryKeyModel, \
+ ProxyModel, SimpleIncludeModel, SimpleExcludeModel, SimpleMappingModel, RelatedModel, \
+ ManyRelatedModel, AdditionalDataIncludedModel, DateTimeFieldModel, ChoicesFieldModel, \
+ CharfieldTextfieldModel, PostgresArrayFieldModel, NoDeleteHistoryModel
+from auditlog import compat
+
+
+class SimpleModelTest(TestCase):
+ def setUp(self):
+ self.obj = SimpleModel.objects.create(text='I am not difficult.')
+
+ def test_create(self):
+ obj = self.obj
+
+ self.assertTrue(obj.history.count() == 1, msg="There is one log entry")
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ def test_request(self):
+ request = self.factory.get('/')
+ request.user = self.user
+ self.middleware.process_request(request)
+
+ self.assertTrue(pre_save.has_listeners(LogEntry))
+
+ self.middleware.process_exception(request, None)
+
+ def test_response(self):
+ request = self.factory.get('/')
+ request.user = self.user
+
+ self.middleware.process_request(request)
+ self.assertTrue(pre_save.has_listeners(LogEntry)) # The signal should be present before trying to disconnect it.
+ self.middleware.process_response(request, HttpResponse())
+
+ self.assertFalse(pre_save.has_listeners(LogEntry))
+
+ def test_exception(self):
+ request = self.factory.get('/')
+ request.user = self.user
+
+ self.middleware.process_request(request)
+ self.assertTrue(pre_save.has_listeners(LogEntry)) # The signal should be present before trying to disconnect it.
+~~ self.middleware.process_exception(request, ValidationError("Test"))
+
+ self.assertFalse(pre_save.has_listeners(LogEntry))
+
+
+class SimpeIncludeModelTest(TestCase):
+
+ def test_register_include_fields(self):
+ sim = SimpleIncludeModel(label='Include model', text='Looong text')
+ sim.save()
+ self.assertTrue(sim.history.count() == 1, msg="There is one log entry")
+
+ sim.label = 'Changed label'
+ sim.save()
+ self.assertTrue(sim.history.count() == 2, msg="There are two log entries")
+
+ sim.text = 'Short text'
+ sim.save()
+ self.assertTrue(sim.history.count() == 2, msg="There are two log entries")
+
+
+class SimpeExcludeModelTest(TestCase):
+
+ def test_register_exclude_fields(self):
+ sem = SimpleExcludeModel(label='Exclude model', text='Looong text')
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 2 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / chair_mail / forms.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/chair_mail/forms.py)
+
+```python
+# forms.py
+from django import forms
+from django.conf import settings
+~~from django.core.exceptions import ValidationError
+from django.core.mail import send_mail
+from django.template import Template, Context
+from django.utils import timezone
+from html2text import html2text
+from markdown import markdown
+
+from chair_mail.context import get_conference_context, get_user_context, \
+ get_submission_context
+from chair_mail.mailing_lists import find_list
+from chair_mail.utility import get_object_model
+from submissions.models import Submission
+from users.models import User
+from .models import EmailFrame, MSG_TYPE_USER, MSG_TYPE_SUBMISSION, \
+ SystemNotification
+
+
+def parse_mailing_lists(names_string, separator=','):
+ names = names_string.split(separator)
+ names = [name for name in names if name.strip()]
+ return [find_list(name) for name in names]
+
+
+def parse_objects(obj_class, pks_string, separator=','):
+ int_pks = [s for s in pks_string.split(separator) if s.strip()]
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ from_email=settings.DEFAULT_FROM_EMAIL,
+ recipient_list=[user.email],
+ html_message=html,
+ )
+
+
+class MessageForm(forms.Form):
+ subject = forms.CharField()
+ body = forms.CharField(widget=forms.Textarea(), required=False)
+ lists = forms.CharField(
+ required=False, max_length=1000, widget=forms.HiddenInput)
+ objects = forms.CharField(
+ required=False, max_length=10000, widget=forms.HiddenInput)
+
+ def __init__(self, *args, msg_type=None, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.msg_type = msg_type
+ self.object_type = get_object_model(msg_type)
+ self.cleaned_lists = []
+ self.cleaned_objects = []
+
+ def clean_lists(self):
+ _lists = parse_mailing_lists(self.cleaned_data['lists'])
+ for ml in _lists:
+ if ml.type != self.msg_type:
+~~ raise ValidationError(
+ f'unexpected {ml.type} mailing list {ml.name}')
+ self.cleaned_lists = _lists
+ return self.cleaned_data['lists']
+
+ def clean_objects(self):
+ self.cleaned_objects = parse_objects(
+ self.object_type, self.cleaned_data['objects'], ',')
+ return self.cleaned_data['objects']
+
+ def clean(self):
+ if not self.cleaned_lists and not self.cleaned_objects:
+~~ raise ValidationError('You must specify at least one recipient')
+ return self.cleaned_data
+
+
+class PreviewMessageForm(forms.Form):
+ subject = forms.CharField(
+ required=False,
+ widget=forms.TextInput(attrs={
+ 'hidden': True
+ })
+ )
+
+ body = forms.CharField(
+ required=False,
+ widget=forms.Textarea(attrs={
+ 'hidden': True
+ })
+ )
+
+ def get_context(self, conference):
+ raise NotImplementedError
+
+ def render_html(self, conference):
+ ctx_data = self.get_context(conference)
+ context = Context(ctx_data, autoescape=False)
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 3 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / socialaccount / fields.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/fields.py)
+
+```python
+# fields.py
+import json
+
+import django
+~~from django.core.exceptions import ValidationError
+from django.db import models
+
+
+class JSONField(models.TextField):
+ if django.VERSION < (3, 0):
+ def from_db_value(self, value, expression, connection, context):
+ return self.to_python(value)
+ else:
+ def from_db_value(self, value, expression, connection):
+ return self.to_python(value)
+
+ def to_python(self, value):
+ if self.blank and not value:
+ return None
+ if isinstance(value, str):
+ try:
+ return json.loads(value)
+ except Exception as e:
+~~ raise ValidationError(str(e))
+ else:
+ return value
+
+ def validate(self, value, model_instance):
+ if isinstance(value, str):
+ super(JSONField, self).validate(value, model_instance)
+ try:
+ json.loads(value)
+ except Exception as e:
+~~ raise ValidationError(str(e))
+
+ def get_prep_value(self, value):
+ try:
+ return json.dumps(value)
+ except Exception as e:
+~~ raise ValidationError(str(e))
+
+ def value_from_object(self, obj):
+ val = super(JSONField, self).value_from_object(obj)
+ return self.get_prep_value(val)
+
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 4 from django-angular
+[django-angular](https://github.com/jrief/django-angular)
+([project examples website](https://django-angular.awesto.com/classic_form/))
+is a library with helper code to make it easier to use
+[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
+The code for django-angular is
+[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
+
+[**django-angular / djng / forms / angular_base.py**](https://github.com/jrief/django-angular/blob/master/djng/forms/angular_base.py)
+
+```python
+# angular_base.py
+from base64 import b64encode
+from collections import UserList
+import json
+import warnings
+
+from django.forms import forms
+from django.http import QueryDict
+from django.utils.html import format_html, format_html_join, escape, conditional_escape
+from django.utils.encoding import force_text
+from django.utils.module_loading import import_string
+from django.utils.safestring import mark_safe, SafeText, SafeData
+~~from django.core.exceptions import ValidationError, ImproperlyConfigured
+
+from .fields import DefaultFieldMixin
+
+
+class SafeTuple(SafeData, tuple):
+
+
+class TupleErrorList(UserList, list):
+ def __init__(self, initlist=None, error_class=None):
+ super(TupleErrorList, self).__init__(initlist)
+
+ if error_class is None:
+ self.error_class = 'errorlist'
+ else:
+ self.error_class = 'errorlist {}'.format(error_class)
+
+ def as_data(self):
+~~ return ValidationError(self.data).error_list
+
+ def get_json_data(self, escape_html=False):
+ errors = []
+ for error in self.as_data():
+ message = list(error)[0]
+ errors.append({
+ 'message': escape(message) if escape_html else message,
+ 'code': error.code or '',
+ })
+ return errors
+
+ def as_json(self, escape_html=False):
+ return json.dumps(self.get_json_data(escape_html))
+
+ def extend(self, iterable):
+ for item in iterable:
+ if not isinstance(item, str):
+ self.append(item)
+ return None
+
+ def as_ul(self):
+ if not self:
+ return SafeText()
+ first = self[0]
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ return ''
+ if isinstance(self[0], tuple):
+ return '\n'.join(['* %s' % force_text(e[5]) for e in self if bool(e[5])])
+ return '\n'.join(['* %s' % force_text(e) for e in self])
+
+ def __str__(self):
+ return self.as_ul()
+
+ def __repr__(self):
+ if self and isinstance(self[0], tuple):
+ return repr([force_text(e[5]) for e in self])
+ return repr([force_text(e) for e in self])
+
+ def __contains__(self, item):
+ return item in list(self)
+
+ def __eq__(self, other):
+ return list(self) == other
+
+ def __ne__(self, other):
+ return list(self) != other
+
+ def __getitem__(self, i):
+ error = self.data[i]
+ if isinstance(error, tuple):
+~~ if isinstance(error[5], ValidationError):
+ error[5] = list(error[5])[0]
+ return error
+~~ if isinstance(error, ValidationError):
+ return list(error)[0]
+ return force_text(error)
+
+
+class NgWidgetMixin(object):
+ def get_context(self, name, value, attrs):
+ context = super(NgWidgetMixin, self).get_context(name, value, attrs)
+ if callable(getattr(self._field, 'update_widget_rendering_context', None)):
+ self._field.update_widget_rendering_context(context)
+ return context
+
+
+class NgBoundField(forms.BoundField):
+ @property
+ def errors(self):
+ if not hasattr(self, '_errors_cache'):
+ self._errors_cache = self.form.get_field_errors(self)
+ return self._errors_cache
+
+ def css_classes(self, extra_classes=None):
+ if hasattr(extra_classes, 'split'):
+ extra_classes = extra_classes.split()
+ extra_classes = set(extra_classes or [])
+ field_css_classes = getattr(self.form, 'field_css_classes', None)
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 5 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / api.py**](https://github.com/divio/django-cms/blob/develop/cms/./api.py)
+
+```python
+# api.py
+import datetime
+
+from django.contrib.auth import get_user_model
+from django.contrib.sites.models import Site
+from django.core.exceptions import FieldError
+from django.core.exceptions import PermissionDenied
+~~from django.core.exceptions import ValidationError
+from django.db import transaction
+from django.template.defaultfilters import slugify
+from django.template.loader import get_template
+from django.utils.translation import activate
+
+from six import string_types
+
+from cms import constants
+from cms.app_base import CMSApp
+from cms.apphook_pool import apphook_pool
+from cms.constants import TEMPLATE_INHERITANCE_MAGIC
+from cms.models.pagemodel import Page
+from cms.models.permissionmodels import (PageUser, PagePermission, GlobalPagePermission,
+ ACCESS_PAGE_AND_DESCENDANTS)
+from cms.models.placeholdermodel import Placeholder
+from cms.models.pluginmodel import CMSPlugin
+from cms.models.titlemodels import Title
+from cms.plugin_base import CMSPluginBase
+from cms.plugin_pool import plugin_pool
+from cms.utils import copy_plugins, get_current_site
+from cms.utils.conf import get_cms_setting
+from cms.utils.i18n import get_language_list
+from cms.utils.page import get_available_slug
+from cms.utils.permissions import _thread_locals, current_user
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+
+
+
+
+def _verify_apphook(apphook, namespace):
+ apphook_pool.discover_apps()
+ if isinstance(apphook, CMSApp):
+ try:
+ assert apphook.__class__ in [app.__class__ for app in apphook_pool.apps.values()]
+ except AssertionError:
+ print(apphook_pool.apps.values())
+ raise
+ apphook_name = apphook.__class__.__name__
+ elif hasattr(apphook, '__module__') and issubclass(apphook, CMSApp):
+ return apphook.__name__
+ elif isinstance(apphook, string_types):
+ try:
+ assert apphook in apphook_pool.apps
+ except AssertionError:
+ print(apphook_pool.apps.values())
+ raise
+ apphook_name = apphook
+ else:
+ raise TypeError("apphook must be string or CMSApp instance")
+ if apphook_pool.apps[apphook_name].app_name and not namespace:
+~~ raise ValidationError('apphook with app_name must define a namespace')
+ return apphook_name
+
+
+def _verify_plugin_type(plugin_type):
+ if (hasattr(plugin_type, '__module__') and
+ issubclass(plugin_type, CMSPluginBase)):
+ plugin_model = plugin_type.model
+ assert plugin_type in plugin_pool.plugins.values()
+ plugin_type = plugin_type.__name__
+ elif isinstance(plugin_type, string_types):
+ try:
+ plugin_model = plugin_pool.get_plugin(plugin_type).model
+ except KeyError:
+ raise TypeError(
+ 'plugin_type must be CMSPluginBase subclass or string'
+ )
+ else:
+ raise TypeError('plugin_type must be CMSPluginBase subclass or string')
+ return plugin_model, plugin_type
+
+
+
+@transaction.atomic
+def create_page(title, template, language, menu_title=None, slug=None,
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 6 from django-extensions
+[django-extensions](https://github.com/django-extensions/django-extensions)
+([project documentation](https://django-extensions.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-extensions/))
+is a [Django](/django.html) project that adds a bunch of additional
+useful commands to the `manage.py` interface. This
+[GoDjango video](https://www.youtube.com/watch?v=1F6G3ONhr4k) provides a
+quick overview of what you get when you install it into your Python
+environment.
+
+The django-extensions project is open sourced under the
+[MIT license](https://github.com/django-extensions/django-extensions/blob/master/LICENSE).
+
+[**django-extensions / django_extensions / validators.py**](https://github.com/django-extensions/django-extensions/blob/master/django_extensions/./validators.py)
+
+```python
+# validators.py
+import unicodedata
+import binascii
+
+
+~~from django.core.exceptions import ValidationError
+from django.utils.deconstruct import deconstructible
+from django.utils.encoding import force_str
+from django.utils.translation import gettext_lazy as _
+
+
+@deconstructible
+class NoControlCharactersValidator:
+ message = _("Control Characters like new lines or tabs are not allowed.")
+ code = "no_control_characters"
+ whitelist = None
+
+ def __init__(self, message=None, code=None, whitelist=None):
+ if message:
+ self.message = message
+ if code:
+ self.code = code
+ if whitelist:
+ self.whitelist = whitelist
+
+ def __call__(self, value):
+ value = force_str(value)
+ whitelist = self.whitelist
+ category = unicodedata.category
+ for character in value:
+ if whitelist and character in whitelist:
+ continue
+ if category(character)[0] == "C":
+ params = {'value': value, 'whitelist': whitelist}
+~~ raise ValidationError(self.message, code=self.code, params=params)
+
+ def __eq__(self, other):
+ return (
+ isinstance(other, NoControlCharactersValidator) and
+ (self.whitelist == other.whitelist) and
+ (self.message == other.message) and
+ (self.code == other.code)
+ )
+
+
+@deconstructible
+class NoWhitespaceValidator:
+ message = _("Leading and Trailing whitespaces are not allowed.")
+ code = "no_whitespace"
+
+ def __init__(self, message=None, code=None, whitelist=None):
+ if message:
+ self.message = message
+ if code:
+ self.code = code
+
+ def __call__(self, value):
+ value = force_str(value)
+ if value != value.strip():
+ params = {'value': value}
+~~ raise ValidationError(self.message, code=self.code, params=params)
+
+ def __eq__(self, other):
+ return (
+ isinstance(other, NoWhitespaceValidator) and
+ (self.message == other.message) and
+ (self.code == other.code)
+ )
+
+
+@deconstructible
+class HexValidator:
+ messages = {
+ 'invalid': _("Only a hex string is allowed."),
+ 'length': _("Invalid length. Must be %(length)d characters."),
+ 'min_length': _("Ensure that there are more than %(min)s characters."),
+ 'max_length': _("Ensure that there are no more than %(max)s characters."),
+ }
+ code = "hex_only"
+
+ def __init__(self, length=None, min_length=None, max_length=None, message=None, code=None):
+ self.length = length
+ self.min_length = min_length
+ self.max_length = max_length
+ if message:
+ self.message = message
+ if code:
+ self.code = code
+
+ def __call__(self, value):
+ value = force_str(value)
+ if self.length and len(value) != self.length:
+~~ raise ValidationError(self.messages['length'], code='hex_only_length', params={'length': self.length})
+ if self.min_length and len(value) < self.min_length:
+~~ raise ValidationError(self.messages['min_length'], code='hex_only_min_length', params={'min': self.min_length})
+ if self.max_length and len(value) < self.max_length:
+~~ raise ValidationError(self.messages['max_length'], code='hex_only_max_length', params={'max': self.max_length})
+
+ try:
+ binascii.unhexlify(value)
+ except (TypeError, binascii.Error):
+~~ raise ValidationError(self.messages['invalid'], code='hex_only')
+
+ def __eq__(self, other):
+ return (
+ isinstance(other, HexValidator) and
+ (self.message == other.message) and
+ (self.code == other.code)
+ )
+
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 7 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / admin / forms.py**](https://github.com/divio/django-filer/blob/develop/filer/admin/forms.py)
+
+```python
+# forms.py
+from __future__ import absolute_import
+
+from django import forms
+from django.conf import settings
+from django.contrib.admin import widgets
+~~from django.core.exceptions import ValidationError
+from django.db import models
+from django.utils.translation import ugettext as _
+
+from ..models import ThumbnailOption
+from ..utils.files import get_valid_filename
+
+
+class AsPWithHelpMixin(object):
+ def as_p_with_help(self):
+ "Returns this form rendered as HTML
s with help text formated for admin."
+ return self._html_output(
+ normal_row='
%(label)s %(field)s
%(help_text)s',
+ error_row='%s',
+ row_ender='
',
+ help_text_html='
%s
',
+ errors_on_separate_row=True)
+
+
+class CopyFilesAndFoldersForm(forms.Form, AsPWithHelpMixin):
+ suffix = forms.CharField(required=False, help_text=_("Suffix which will be appended to filenames of copied files."))
+
+ def clean_suffix(self):
+ valid = get_valid_filename(self.cleaned_data['suffix'])
+ if valid != self.cleaned_data['suffix']:
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ }
+ except KeyError as e:
+ raise forms.ValidationError(_('Unknown rename format value key "%(key)s".') % {'key': e.args[0]})
+ except Exception as e:
+ raise forms.ValidationError(_('Invalid rename format: %(error)s.') % {'error': e})
+ return self.cleaned_data['rename_format']
+
+
+class ResizeImagesForm(forms.Form, AsPWithHelpMixin):
+ if 'cmsplugin_filer_image' in settings.INSTALLED_APPS:
+ thumbnail_option = models.ForeignKey(
+ ThumbnailOption,
+ null=True,
+ blank=True,
+ verbose_name=_("thumbnail option"),
+ on_delete=models.CASCADE,
+ ).formfield()
+ width = models.PositiveIntegerField(_("width"), null=True, blank=True).formfield(widget=widgets.AdminIntegerFieldWidget)
+ height = models.PositiveIntegerField(_("height"), null=True, blank=True).formfield(widget=widgets.AdminIntegerFieldWidget)
+ crop = models.BooleanField(_("crop"), default=True).formfield()
+ upscale = models.BooleanField(_("upscale"), default=True).formfield()
+
+ def clean(self):
+ if not (self.cleaned_data.get('thumbnail_option') or ((self.cleaned_data.get('width') or 0) + (self.cleaned_data.get('height') or 0))):
+ if 'cmsplugin_filer_image' in settings.INSTALLED_APPS:
+~~ raise ValidationError(_('Thumbnail option or resize parameters must be choosen.'))
+ else:
+~~ raise ValidationError(_('Resize parameters must be choosen.'))
+ return self.cleaned_data
+
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 8 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / models / models.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/models/models.py)
+
+```python
+# models.py
+from django.contrib.auth.models import Group, Permission
+from django.contrib.contenttypes.fields import GenericForeignKey
+from django.contrib.contenttypes.models import ContentType
+~~from django.core.exceptions import ValidationError
+from django.db import models
+from django.utils.translation import gettext_lazy as _
+from guardian.compat import user_model_label
+from guardian.ctypes import get_content_type
+from guardian.managers import GroupObjectPermissionManager, UserObjectPermissionManager
+
+
+class BaseObjectPermission(models.Model):
+ permission = models.ForeignKey(Permission, on_delete=models.CASCADE)
+
+ class Meta:
+ abstract = True
+
+ def __str__(self):
+ return '{} | {} | {}'.format(
+ str(self.content_object),
+ str(getattr(self, 'user', False) or self.group),
+ str(self.permission.codename))
+
+ def save(self, *args, **kwargs):
+ content_type = get_content_type(self.content_object)
+ if content_type != self.permission.content_type:
+~~ raise ValidationError("Cannot persist permission not designed for "
+ "this class (permission's type is %r and object's type is %r)"
+ % (self.permission.content_type, content_type))
+ return super().save(*args, **kwargs)
+
+
+class BaseGenericObjectPermission(models.Model):
+ content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
+ object_pk = models.CharField(_('object ID'), max_length=255)
+ content_object = GenericForeignKey(fk_field='object_pk')
+
+ class Meta:
+ abstract = True
+ indexes = [
+ models.Index(fields=['content_type', 'object_pk']),
+ ]
+
+
+class UserObjectPermissionBase(BaseObjectPermission):
+ user = models.ForeignKey(user_model_label, on_delete=models.CASCADE)
+
+ objects = UserObjectPermissionManager()
+
+ class Meta:
+ abstract = True
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 9 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / resources.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./resources.py)
+
+```python
+# resources.py
+import functools
+import logging
+import tablib
+import traceback
+from collections import OrderedDict
+from copy import deepcopy
+
+from diff_match_patch import diff_match_patch
+
+import django
+from django.conf import settings
+~~from django.core.exceptions import ImproperlyConfigured, ValidationError
+from django.core.management.color import no_style
+from django.core.paginator import Paginator
+from django.db import DEFAULT_DB_ALIAS, connections
+from django.db.models.fields.related import ForeignObjectRel
+from django.db.models.query import QuerySet
+from django.db.transaction import (
+ TransactionManagementError,
+ atomic,
+ savepoint,
+ savepoint_commit,
+ savepoint_rollback
+)
+from django.utils.encoding import force_str
+from django.utils.safestring import mark_safe
+
+from . import widgets
+from .fields import Field
+from .instance_loaders import ModelInstanceLoader
+from .results import Error, Result, RowResult
+from .utils import atomic_if_using_transaction
+
+if django.VERSION[0] >= 3:
+ from django.core.exceptions import FieldDoesNotExist
+else:
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ else:
+ delete_ids = [o.pk for o in self.delete_instances]
+ self._meta.model.objects.filter(pk__in=delete_ids).delete()
+ except Exception as e:
+ logger.exception(e)
+ if raise_errors:
+ raise e
+ finally:
+ self.delete_instances.clear()
+
+ def validate_instance(self, instance, import_validation_errors=None, validate_unique=True):
+ if import_validation_errors is None:
+ errors = {}
+ else:
+ errors = import_validation_errors.copy()
+ if self._meta.clean_model_instances:
+ try:
+ instance.full_clean(
+ exclude=errors.keys(),
+ validate_unique=validate_unique,
+ )
+~~ except ValidationError as e:
+ errors = e.update_error_dict(errors)
+
+ if errors:
+~~ raise ValidationError(errors)
+
+ def save_instance(self, instance, using_transactions=True, dry_run=False):
+ self.before_save_instance(instance, using_transactions, dry_run)
+ if self._meta.use_bulk:
+ if instance.pk:
+ self.update_instances.append(instance)
+ else:
+ self.create_instances.append(instance)
+ else:
+ if not using_transactions and dry_run:
+ pass
+ else:
+ instance.save()
+ self.after_save_instance(instance, using_transactions, dry_run)
+
+ def before_save_instance(self, instance, using_transactions, dry_run):
+ pass
+
+ def after_save_instance(self, instance, using_transactions, dry_run):
+ pass
+
+ def delete_instance(self, instance, using_transactions=True, dry_run=False):
+ self.before_delete_instance(instance, dry_run)
+ if self._meta.use_bulk:
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ else:
+ instance.delete()
+ self.after_delete_instance(instance, dry_run)
+
+ def before_delete_instance(self, instance, dry_run):
+ pass
+
+ def after_delete_instance(self, instance, dry_run):
+ pass
+
+ def import_field(self, field, obj, data, is_m2m=False):
+ if field.attribute and field.column_name in data:
+ field.save(obj, data, is_m2m)
+
+ def get_import_fields(self):
+ return self.get_fields()
+
+ def import_obj(self, obj, data, dry_run):
+ errors = {}
+ for field in self.get_import_fields():
+ if isinstance(field.widget, widgets.ManyToManyWidget):
+ continue
+ try:
+ self.import_field(field, obj, data)
+ except ValueError as e:
+~~ errors[field.attribute] = ValidationError(
+ force_str(e), code="invalid")
+ if errors:
+~~ raise ValidationError(errors)
+
+ def save_m2m(self, obj, data, using_transactions, dry_run):
+ if (not using_transactions and dry_run) or self._meta.use_bulk:
+ pass
+ else:
+ for field in self.get_import_fields():
+ if not isinstance(field.widget, widgets.ManyToManyWidget):
+ continue
+ self.import_field(field, obj, data, True)
+
+ def for_delete(self, row, instance):
+ return False
+
+ def skip_row(self, instance, original):
+ if not self._meta.skip_unchanged or self._meta.skip_diff:
+ return False
+ for field in self.get_import_fields():
+ try:
+ if list(field.get_value(instance).all()) != list(field.get_value(original).all()):
+ return False
+ except AttributeError:
+ if field.get_value(instance) != field.get_value(original):
+ return False
+ return True
+ try:
+ if len(self.delete_instances) > 0:
+ if not using_transactions and dry_run:
+ pass
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ self.before_import_row(row, **kwargs)
+ instance, new = self.get_or_init_instance(instance_loader, row)
+ self.after_import_instance(instance, new, **kwargs)
+ if new:
+ row_result.import_type = RowResult.IMPORT_TYPE_NEW
+ else:
+ row_result.import_type = RowResult.IMPORT_TYPE_UPDATE
+ row_result.new_record = new
+ if not skip_diff:
+ original = deepcopy(instance)
+ diff = self.get_diff_class()(self, original, new)
+ if self.for_delete(row, instance):
+ if new:
+ row_result.import_type = RowResult.IMPORT_TYPE_SKIP
+ if not skip_diff:
+ diff.compare_with(self, None, dry_run)
+ else:
+ row_result.import_type = RowResult.IMPORT_TYPE_DELETE
+ self.delete_instance(instance, using_transactions, dry_run)
+ if not skip_diff:
+ diff.compare_with(self, None, dry_run)
+ else:
+ import_validation_errors = {}
+ try:
+ self.import_obj(instance, row, dry_run)
+~~ except ValidationError as e:
+ import_validation_errors = e.update_error_dict(import_validation_errors)
+ if self.skip_row(instance, original):
+ row_result.import_type = RowResult.IMPORT_TYPE_SKIP
+ else:
+ self.validate_instance(instance, import_validation_errors)
+ self.save_instance(instance, using_transactions, dry_run)
+ self.save_m2m(instance, row, using_transactions, dry_run)
+ row_result.object_id = instance.pk
+ row_result.object_repr = force_str(instance)
+ if not skip_diff:
+ diff.compare_with(self, instance, dry_run)
+
+ if not skip_diff:
+ row_result.diff = diff.as_html()
+ self.after_import_row(row, row_result, **kwargs)
+
+~~ except ValidationError as e:
+ row_result.import_type = RowResult.IMPORT_TYPE_INVALID
+ row_result.validation_error = e
+ except Exception as e:
+ row_result.import_type = RowResult.IMPORT_TYPE_ERROR
+ if not isinstance(e, TransactionManagementError):
+ logger.debug(e, exc_info=e)
+ tb_info = traceback.format_exc()
+ row_result.errors.append(self.get_error_result_class()(e, tb_info, row))
+
+ if self._meta.use_bulk:
+ if len(self.create_instances) == self._meta.batch_size:
+ self.bulk_create(using_transactions, dry_run, raise_errors, batch_size=self._meta.batch_size)
+ if len(self.update_instances) == self._meta.batch_size:
+ self.bulk_update(using_transactions, dry_run, raise_errors, batch_size=self._meta.batch_size)
+ if len(self.delete_instances) == self._meta.batch_size:
+ self.bulk_delete(using_transactions, dry_run, raise_errors)
+
+ return row_result
+
+ def import_data(self, dataset, dry_run=False, raise_errors=False,
+ use_transactions=None, collect_failed_rows=False, **kwargs):
+
+ if use_transactions is None:
+ use_transactions = self.get_use_transactions()
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 10 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / forms.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/./forms.py)
+
+```python
+# forms.py
+import json
+from django import forms
+from django.contrib.auth.models import Permission
+from django.contrib.contenttypes.models import ContentType
+~~from django.core.exceptions import ValidationError
+from django.db.models import Q
+import operator
+
+from jet.models import Bookmark, PinnedApplication
+from jet.utils import get_model_instance_label, user_is_authenticated
+from functools import reduce
+
+try:
+ from django.apps import apps
+ get_model = apps.get_model
+except ImportError:
+ from django.db.models.loading import get_model
+
+
+class AddBookmarkForm(forms.ModelForm):
+ def __init__(self, request, *args, **kwargs):
+ self.request = request
+ super(AddBookmarkForm, self).__init__(*args, **kwargs)
+
+ class Meta:
+ model = Bookmark
+ fields = ['url', 'title']
+
+ def clean(self):
+ data = super(AddBookmarkForm, self).clean()
+ if not user_is_authenticated(self.request.user) or not self.request.user.is_staff:
+~~ raise ValidationError('error')
+ if not self.request.user.has_perm('jet.change_bookmark'):
+~~ raise ValidationError('error')
+ return data
+
+ def save(self, commit=True):
+ self.instance.user = self.request.user.pk
+ return super(AddBookmarkForm, self).save(commit)
+
+
+class RemoveBookmarkForm(forms.ModelForm):
+ def __init__(self, request, *args, **kwargs):
+ self.request = request
+ super(RemoveBookmarkForm, self).__init__(*args, **kwargs)
+
+ class Meta:
+ model = Bookmark
+ fields = []
+
+ def clean(self):
+ data = super(RemoveBookmarkForm, self).clean()
+ if not user_is_authenticated(self.request.user) or not self.request.user.is_staff:
+~~ raise ValidationError('error')
+ if self.instance.user != self.request.user.pk:
+~~ raise ValidationError('error')
+ return data
+
+ def save(self, commit=True):
+ if commit:
+ self.instance.delete()
+
+
+class ToggleApplicationPinForm(forms.ModelForm):
+ def __init__(self, request, *args, **kwargs):
+ self.request = request
+ super(ToggleApplicationPinForm, self).__init__(*args, **kwargs)
+
+ class Meta:
+ model = PinnedApplication
+ fields = ['app_label']
+
+ def clean(self):
+ data = super(ToggleApplicationPinForm, self).clean()
+ if not user_is_authenticated(self.request.user) or not self.request.user.is_staff:
+~~ raise ValidationError('error')
+ return data
+
+ def save(self, commit=True):
+ if commit:
+ try:
+ pinned_app = PinnedApplication.objects.get(
+ app_label=self.cleaned_data['app_label'],
+ user=self.request.user.pk
+ )
+ pinned_app.delete()
+ return False
+ except PinnedApplication.DoesNotExist:
+ PinnedApplication.objects.create(
+ app_label=self.cleaned_data['app_label'],
+ user=self.request.user.pk
+ )
+ return True
+
+
+class ModelLookupForm(forms.Form):
+ app_label = forms.CharField()
+ model = forms.CharField()
+ q = forms.CharField(required=False)
+ page = forms.IntegerField(required=False)
+ page_size = forms.IntegerField(required=False, min_value=1, max_value=1000)
+ object_id = forms.IntegerField(required=False)
+ model_cls = None
+
+ def __init__(self, request, *args, **kwargs):
+ self.request = request
+ super(ModelLookupForm, self).__init__(*args, **kwargs)
+
+ def clean(self):
+ data = super(ModelLookupForm, self).clean()
+
+ if not user_is_authenticated(self.request.user) or not self.request.user.is_staff:
+~~ raise ValidationError('error')
+
+ try:
+ self.model_cls = get_model(data['app_label'], data['model'])
+ except:
+~~ raise ValidationError('error')
+
+ content_type = ContentType.objects.get_for_model(self.model_cls)
+ permission = Permission.objects.filter(content_type=content_type, codename__startswith='change_').first()
+
+ if not self.request.user.has_perm('{}.{}'.format(data['app_label'], permission.codename)):
+~~ raise ValidationError('error')
+
+ return data
+
+ def lookup(self):
+ qs = self.model_cls.objects
+
+ if self.cleaned_data['q']:
+ if getattr(self.model_cls, 'autocomplete_search_fields', None):
+ search_fields = self.model_cls.autocomplete_search_fields()
+ filter_data = [Q((field + '__icontains', self.cleaned_data['q'])) for field in search_fields]
+ qs = qs.filter(reduce(operator.or_, filter_data)).distinct()
+ else:
+ qs = qs.none()
+
+ limit = self.cleaned_data['page_size'] or 100
+ page = self.cleaned_data['page'] or 1
+ offset = (page - 1) * limit
+
+ items = list(map(
+ lambda instance: {'id': instance.pk, 'text': get_model_instance_label(instance)},
+ qs.all()[offset:offset + limit]
+ ))
+ total = qs.count()
+
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 11 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / fields.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./fields.py)
+
+```python
+# fields.py
+import uuid
+from django.db import models
+from django.conf import settings
+~~from django.core.exceptions import ValidationError
+from django.utils.timezone import now
+
+DEFAULT_CHOICES_NAME = 'STATUS'
+
+
+class AutoCreatedField(models.DateTimeField):
+
+ def __init__(self, *args, **kwargs):
+ kwargs.setdefault('editable', False)
+ kwargs.setdefault('default', now)
+ super().__init__(*args, **kwargs)
+
+
+class AutoLastModifiedField(AutoCreatedField):
+ def get_default(self):
+ if not hasattr(self, "_default"):
+ self._default = self._get_default()
+ return self._default
+
+ def pre_save(self, model_instance, add):
+ value = now()
+ if add:
+ current_value = getattr(model_instance, self.attname, self.get_default())
+ if current_value != self.get_default():
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ excerpt = get_excerpt(value.content)
+ setattr(model_instance, _excerpt_field_name(self.attname), excerpt)
+ return value.content
+
+ def value_to_string(self, obj):
+ value = self.value_from_object(obj)
+ return value.content
+
+ def get_prep_value(self, value):
+ try:
+ return value.content
+ except AttributeError:
+ return value
+
+ def deconstruct(self):
+ name, path, args, kwargs = super().deconstruct()
+ kwargs['no_excerpt_field'] = True
+ return name, path, args, kwargs
+
+
+class UUIDField(models.UUIDField):
+
+ def __init__(self, primary_key=True, version=4, editable=False, *args, **kwargs):
+
+ if version == 2:
+~~ raise ValidationError(
+ 'UUID version 2 is not supported.')
+
+ if version < 1 or version > 5:
+~~ raise ValidationError(
+ 'UUID version is not valid.')
+
+ if version == 1:
+ default = uuid.uuid1
+ elif version == 3:
+ default = uuid.uuid3
+ elif version == 4:
+ default = uuid.uuid4
+ elif version == 5:
+ default = uuid.uuid5
+
+ kwargs.setdefault('primary_key', primary_key)
+ kwargs.setdefault('editable', editable)
+ kwargs.setdefault('default', default)
+ super().__init__(*args, **kwargs)
+
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 12 from django-oauth-toolkit
+[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit)
+([project website](http://dot.evonove.it/) and
+[PyPI package information](https://pypi.org/project/django-oauth-toolkit/1.2.0/))
+is a code library for adding and handling [OAuth2](https://oauth.net/)
+flows within your [Django](/django.html) web application and
+[API](/application-programming-interfaces.html).
+
+The django-oauth-toolkit project is open sourced under the
+[FreeBSD license](https://github.com/jazzband/django-oauth-toolkit/blob/master/LICENSE)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-oauth-toolkit / oauth2_provider / models.py**](https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/./models.py)
+
+```python
+# models.py
+import logging
+from datetime import timedelta
+from urllib.parse import parse_qsl, urlparse
+
+from django.apps import apps
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+from django.db import models, transaction
+from django.urls import reverse
+from django.utils import timezone
+from django.utils.translation import gettext_lazy as _
+
+from .generators import generate_client_id, generate_client_secret
+from .scopes import get_scopes_backend
+from .settings import oauth2_settings
+from .validators import RedirectURIValidator, WildcardSet
+
+
+logger = logging.getLogger(__name__)
+
+
+class AbstractApplication(models.Model):
+ CLIENT_CONFIDENTIAL = "confidential"
+ CLIENT_PUBLIC = "public"
+ CLIENT_TYPES = (
+ (CLIENT_CONFIDENTIAL, _("Confidential")),
+ (CLIENT_PUBLIC, _("Public")),
+ )
+
+ GRANT_AUTHORIZATION_CODE = "authorization-code"
+ GRANT_IMPLICIT = "implicit"
+ GRANT_PASSWORD = "password"
+ GRANT_CLIENT_CREDENTIALS = "client-credentials"
+ GRANT_TYPES = (
+ (GRANT_AUTHORIZATION_CODE, _("Authorization code")),
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+
+ assert False, (
+ "If you are using implicit, authorization_code"
+ "or all-in-one grant_type, you must define "
+ "redirect_uris field in your Application model"
+ )
+
+ def redirect_uri_allowed(self, uri):
+ parsed_uri = urlparse(uri)
+ uqs_set = set(parse_qsl(parsed_uri.query))
+ for allowed_uri in self.redirect_uris.split():
+ parsed_allowed_uri = urlparse(allowed_uri)
+
+ if (parsed_allowed_uri.scheme == parsed_uri.scheme and
+ parsed_allowed_uri.netloc == parsed_uri.netloc and
+ parsed_allowed_uri.path == parsed_uri.path):
+
+ aqs_set = set(parse_qsl(parsed_allowed_uri.query))
+
+ if aqs_set.issubset(uqs_set):
+ return True
+
+ return False
+
+ def clean(self):
+~~ from django.core.exceptions import ValidationError
+
+ grant_types = (
+ AbstractApplication.GRANT_AUTHORIZATION_CODE,
+ AbstractApplication.GRANT_IMPLICIT,
+ )
+
+ redirect_uris = self.redirect_uris.strip().split()
+ allowed_schemes = set(s.lower() for s in self.get_allowed_schemes())
+
+ if redirect_uris:
+ validator = RedirectURIValidator(WildcardSet())
+ for uri in redirect_uris:
+ validator(uri)
+ scheme = urlparse(uri).scheme
+ if scheme not in allowed_schemes:
+~~ raise ValidationError(_(
+ "Unauthorized redirect scheme: {scheme}"
+ ).format(scheme=scheme))
+
+ elif self.authorization_grant_type in grant_types:
+~~ raise ValidationError(_(
+ "redirect_uris cannot be empty with grant_type {grant_type}"
+ ).format(grant_type=self.authorization_grant_type))
+
+ def get_absolute_url(self):
+ return reverse("oauth2_provider:detail", args=[str(self.id)])
+
+ def get_allowed_schemes(self):
+ return oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES
+
+ def allows_grant_type(self, *grant_types):
+ return self.authorization_grant_type in grant_types
+
+ def is_usable(self, request):
+ return True
+
+
+class ApplicationManager(models.Manager):
+ def get_by_natural_key(self, client_id):
+ return self.get(client_id=client_id)
+
+
+class Application(AbstractApplication):
+ objects = ApplicationManager()
+
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 13 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / fields.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./fields.py)
+
+```python
+# fields.py
+import copy
+import datetime
+import decimal
+import functools
+import inspect
+import re
+import uuid
+import warnings
+from collections import OrderedDict
+from collections.abc import Mapping
+
+from django.conf import settings
+from django.core.exceptions import ObjectDoesNotExist
+~~from django.core.exceptions import ValidationError as DjangoValidationError
+from django.core.validators import (
+ EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
+ MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
+ URLValidator, ip_address_validators
+)
+from django.forms import FilePathField as DjangoFilePathField
+from django.forms import ImageField as DjangoImageField
+from django.utils import timezone
+from django.utils.dateparse import (
+ parse_date, parse_datetime, parse_duration, parse_time
+)
+from django.utils.duration import duration_string
+from django.utils.encoding import is_protected_type, smart_str
+from django.utils.formats import localize_input, sanitize_separators
+from django.utils.ipv6 import clean_ipv6_address
+from django.utils.timezone import utc
+from django.utils.translation import gettext_lazy as _
+from pytz.exceptions import InvalidTimeError
+
+from rest_framework import (
+ ISO_8601, RemovedInDRF313Warning, RemovedInDRF314Warning
+)
+from rest_framework.exceptions import ErrorDetail, ValidationError
+from rest_framework.settings import api_settings
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ def run_validators(self, value):
+ errors = []
+ for validator in self.validators:
+ if hasattr(validator, 'set_context'):
+ warnings.warn(
+ "Method `set_context` on validators is deprecated and will "
+ "no longer be called starting with 3.13. Instead set "
+ "`requires_context = True` on the class, and accept the "
+ "context as an additional argument.",
+ RemovedInDRF313Warning, stacklevel=2
+ )
+ validator.set_context(self)
+
+ try:
+ if getattr(validator, 'requires_context', False):
+ validator(value, self)
+ else:
+ validator(value)
+~~ except ValidationError as exc:
+ if isinstance(exc.detail, dict):
+ raise
+ errors.extend(exc.detail)
+ except DjangoValidationError as exc:
+ errors.extend(get_error_detail(exc))
+ if errors:
+~~ raise ValidationError(errors)
+
+ def to_internal_value(self, data):
+ raise NotImplementedError(
+ '{cls}.to_internal_value() must be implemented for field '
+ '{field_name}. If you do not need to support write operations '
+ 'you probably want to subclass `ReadOnlyField` instead.'.format(
+ cls=self.__class__.__name__,
+ field_name=self.field_name,
+ )
+ )
+
+ def to_representation(self, value):
+ raise NotImplementedError(
+ '{cls}.to_representation() must be implemented for field {field_name}.'.format(
+ cls=self.__class__.__name__,
+ field_name=self.field_name,
+ )
+ )
+
+ def fail(self, key, **kwargs):
+ try:
+ msg = self.error_messages[key]
+ except KeyError:
+ class_name = self.__class__.__name__
+ msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key)
+ raise AssertionError(msg)
+ message_string = msg.format(**kwargs)
+~~ raise ValidationError(message_string, code=key)
+
+ @property
+ def root(self):
+ root = self
+ while root.parent is not None:
+ root = root.parent
+ return root
+
+ @property
+ def context(self):
+ return getattr(self.root, '_context', {})
+
+ def __new__(cls, *args, **kwargs):
+ instance = super().__new__(cls)
+ instance._args = args
+ instance._kwargs = kwargs
+ return instance
+
+ def __deepcopy__(self, memo):
+ args = [
+ copy.deepcopy(item) if not isinstance(item, REGEX_TYPE) else item
+ for item in self._args
+ ]
+ kwargs = {
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+
+ def to_internal_value(self, data):
+ if html.is_html_input(data):
+ data = html.parse_html_list(data, default=[])
+ if isinstance(data, (str, Mapping)) or not hasattr(data, '__iter__'):
+ self.fail('not_a_list', input_type=type(data).__name__)
+ if not self.allow_empty and len(data) == 0:
+ self.fail('empty')
+ return self.run_child_validation(data)
+
+ def to_representation(self, data):
+ return [self.child.to_representation(item) if item is not None else None for item in data]
+
+ def run_child_validation(self, data):
+ result = []
+ errors = OrderedDict()
+
+ for idx, item in enumerate(data):
+ try:
+ result.append(self.child.run_validation(item))
+~~ except ValidationError as e:
+ errors[idx] = e.detail
+
+ if not errors:
+ return result
+~~ raise ValidationError(errors)
+
+
+class DictField(Field):
+ child = _UnvalidatedField()
+ initial = {}
+ default_error_messages = {
+ 'not_a_dict': _('Expected a dictionary of items but got type "{input_type}".'),
+ 'empty': _('This dictionary may not be empty.'),
+ }
+
+ def __init__(self, *args, **kwargs):
+ self.child = kwargs.pop('child', copy.deepcopy(self.child))
+ self.allow_empty = kwargs.pop('allow_empty', True)
+
+ assert not inspect.isclass(self.child), '`child` has not been instantiated.'
+ assert self.child.source is None, (
+ "The `source` argument is not meaningful when applied to a `child=` field. "
+ "Remove `source=` from the field declaration."
+ )
+
+ super().__init__(*args, **kwargs)
+ self.child.bind(field_name='', parent=self)
+
+ def get_value(self, dictionary):
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ if not self.allow_empty and len(data) == 0:
+ self.fail('empty')
+
+ return self.run_child_validation(data)
+
+ def to_representation(self, value):
+ return {
+ str(key): self.child.to_representation(val) if val is not None else None
+ for key, val in value.items()
+ }
+
+ def run_child_validation(self, data):
+ result = {}
+ errors = OrderedDict()
+
+ for key, value in data.items():
+ key = str(key)
+
+ try:
+ result[key] = self.child.run_validation(value)
+~~ except ValidationError as e:
+ errors[key] = e.detail
+
+ if not errors:
+ return result
+~~ raise ValidationError(errors)
+
+
+class HStoreField(DictField):
+ child = CharField(allow_blank=True, allow_null=True)
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ assert isinstance(self.child, CharField), (
+ "The `child` argument must be an instance of `CharField`, "
+ "as the hstore extension stores values as strings."
+ )
+
+
+class JSONField(Field):
+ default_error_messages = {
+ 'invalid': _('Value must be valid JSON.')
+ }
+
+ def __init__(self, *args, **kwargs):
+ self.binary = kwargs.pop('binary', False)
+ self.encoder = kwargs.pop('encoder', None)
+ super().__init__(*args, **kwargs)
+
+ def get_value(self, dictionary):
+ (is_empty_value, data) = self.validate_empty_values(data)
+ if is_empty_value:
+ return data
+ value = self.to_internal_value(data)
+ self.run_validators(value)
+ return value
+
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ if len(val) > 0:
+ return val
+ return html.parse_html_list(dictionary, prefix=self.field_name, default=empty)
+
+ return dictionary.get(self.field_name, empty)
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ def to_internal_value(self, data):
+ if html.is_html_input(data):
+ data = html.parse_html_dict(data)
+ if not isinstance(data, dict):
+ self.fail('not_a_dict', input_type=type(data).__name__)
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 14 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / models / urlpath.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/models/urlpath.py)
+
+```python
+# urlpath.py
+import logging
+import warnings
+
+from django.contrib.contenttypes.fields import GenericRelation
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.sites.models import Site
+~~from django.core.exceptions import ValidationError
+from django.db import models
+from django.db import transaction
+from django.db.models.signals import post_save
+from django.db.models.signals import pre_delete
+from django.urls import reverse
+from django.utils.translation import gettext
+from django.utils.translation import gettext_lazy as _
+from mptt.fields import TreeForeignKey
+from mptt.models import MPTTModel
+from wiki import managers
+from wiki.conf import settings
+from wiki.core.exceptions import MultipleRootURLs
+from wiki.core.exceptions import NoRootURL
+from wiki.decorators import disable_signal_for_loaddata
+from wiki.models.article import Article
+from wiki.models.article import ArticleForObject
+from wiki.models.article import ArticleRevision
+
+__all__ = [
+ "URLPath",
+]
+
+
+log = logging.getLogger(__name__)
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ raise NoRootURL("You need to create a root article on site '%s'" % site)
+ if no_paths > 1:
+ raise MultipleRootURLs("Somehow you have multiple roots on %s" % site)
+ return root_nodes[0]
+
+ class MPTTMeta:
+ pass
+
+ def __str__(self):
+ path = self.path
+ return path if path else gettext("(root)")
+
+ def delete(self, *args, **kwargs):
+ assert not (
+ self.parent and self.get_children()
+ ), "You cannot delete a root article with children."
+ super().delete(*args, **kwargs)
+
+ class Meta:
+ verbose_name = _("URL path")
+ verbose_name_plural = _("URL paths")
+ unique_together = ("site", "parent", "slug")
+
+ def clean(self, *args, **kwargs):
+ if self.slug and not self.parent:
+~~ raise ValidationError(
+ _("Sorry but you cannot have a root article with a slug.")
+ )
+ if not self.slug and self.parent:
+~~ raise ValidationError(_("A non-root note must always have a slug."))
+ if not self.parent:
+ if URLPath.objects.root_nodes().filter(site=self.site).exclude(id=self.id):
+~~ raise ValidationError(
+ _("There is already a root node on %s") % self.site
+ )
+
+ @classmethod
+ def get_by_path(cls, path, select_related=False):
+
+
+ path = path.lstrip("/")
+ path = path.rstrip("/")
+
+ if not path:
+ return cls.root()
+
+ slugs = path.split("/")
+ level = 1
+ parent = cls.root()
+ for slug in slugs:
+ if settings.URL_CASE_SENSITIVE:
+ child = parent.get_children().select_related_common().get(slug=slug)
+ child.cached_ancestors = parent.cached_ancestors + [parent]
+ parent = child
+ else:
+ child = (
+ parent.get_children().select_related_common().get(slug__iexact=slug)
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 15 from register
+[register](https://github.com/ORGAN-IZE/register) is a [Django](/django.html),
+[Bootstrap](/bootstrap-css.html), [PostgreSQL](/postgresql.html) project that is
+open source under the
+[GNU General Public License v3.0](https://github.com/ORGAN-IZE/register/blob/master/LICENSE).
+This web application makes it easier for people to register as organ donors.
+You can see the application live at
+[https://register.organize.org/](https://register.organize.org/).
+
+[**register / registration / forms.py**](https://github.com/ORGAN-IZE/register/blob/master/registration/./forms.py)
+
+```python
+# forms.py
+from __future__ import unicode_literals
+
+import logging
+import re
+import collections
+import datetime
+
+import django.forms
+import django.forms.utils
+import django.forms.widgets
+import django.core.validators
+~~import django.core.exceptions
+from django.conf import settings
+from django.utils.translation import ugettext_lazy as _
+from django.utils.safestring import mark_safe
+
+import form_utils.forms
+import requests
+import dateutil.parser
+import validate_email
+
+logger = logging.getLogger(__name__)
+
+
+REGISTRATION_CONFIGURATION_NAME = 'registration_configuration'
+
+RE_NON_DECIMAL = re.compile(r'[^\d]+')
+RE_NON_ALPHA = re.compile('[\W]+')
+RE_POSTAL_CODE = re.compile(r'^[0-9]{5}$')
+validate_postal_code = django.core.validators.RegexValidator(
+ RE_POSTAL_CODE, _("Enter a valid postal code consisting 5 numbers."), 'invalid')
+
+
+CHOICES_GENDER = (
+ ('M', _('Male')),
+ ('F', _('Female')),
+)
+
+
+class MultiEmailField(django.forms.CharField):
+ message = _('Enter valid email addresses.')
+ code = 'invalid'
+ widget = django.forms.widgets.TextInput
+
+ def to_python(self, value):
+ "Normalize data to a list of strings."
+ if not value:
+ return []
+ return [v.strip() for v in re.findall(validate_email.ADDR_SPEC, value)]
+
+ def validate(self, value):
+ "Check if value consists only of valid emails."
+
+ super(MultiEmailField, self).validate(value)
+ try:
+ for email in value:
+ django.core.validators.validate_email(email)
+ except django.core.exceptions.ValidationError:
+~~ raise django.core.exceptions.ValidationError(self.message, code=self.code)
+
+
+
+
+
+class StateLookupForm(django.forms.Form):
+ email = django.forms.EmailField(label=_('Email'), help_text=_('so we can send you confirmation of your registration'))
+ postal_code = django.forms.CharField(
+ label=_('Postal Code'),
+ max_length=5, min_length=5, validators=[validate_postal_code],
+ help_text=_('to determine which series of state-based questions we will ask next'))
+
+ def clean_email(self):
+ email = self.cleaned_data['email']
+ if settings.DISABLE_EMAIL_VALIDATION:
+ logger.warning('Email validation disabled: DISABLE_EMAIL_VALIDATION '
+ 'is set')
+ return email
+ if not hasattr(settings, 'MAILGUN_PUBLIC_API_KEY'):
+ logger.warning(
+ 'Cannot validate email: MAILGUN_PUBLIC_API_KEY not set')
+ return email
+ r = requests.get(
+ 'https://api.mailgun.net/v2/address/validate',
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 16 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / snippets / tests.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/snippets/tests.py)
+
+```python
+# tests.py
+import json
+
+from django.contrib.admin.utils import quote
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import AnonymousUser, Permission
+from django.core import checks
+~~from django.core.exceptions import ValidationError
+from django.core.files.base import ContentFile
+from django.core.files.uploadedfile import SimpleUploadedFile
+from django.http import HttpRequest, HttpResponse
+from django.test import RequestFactory, TestCase
+from django.test.utils import override_settings
+from django.urls import reverse
+from taggit.models import Tag
+
+from wagtail.admin.edit_handlers import FieldPanel
+from wagtail.admin.forms import WagtailAdminModelForm
+from wagtail.core.models import Page
+from wagtail.snippets.blocks import SnippetChooserBlock
+from wagtail.snippets.edit_handlers import SnippetChooserPanel
+from wagtail.snippets.models import SNIPPET_MODELS, register_snippet
+from wagtail.snippets.views.snippets import get_snippet_edit_handler
+from wagtail.tests.snippets.forms import FancySnippetForm
+from wagtail.tests.snippets.models import (
+ AlphaSnippet, FancySnippet, FileUploadSnippet, RegisterDecorator, RegisterFunction,
+ SearchableSnippet, StandardSnippet, StandardSnippetWithCustomPrimaryKey, ZuluSnippet)
+from wagtail.tests.testapp.models import (
+ Advert, AdvertWithCustomPrimaryKey, AdvertWithCustomUUIDPrimaryKey, AdvertWithTabbedInterface,
+ SnippetChooserModel, SnippetChooserModelWithCustomPrimaryKey)
+from wagtail.tests.utils import WagtailTestUtils
+
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ self.assertInHTML('', empty_form_html)
+ self.assertIn('createSnippetChooser("advert", "tests/advert");', empty_form_html)
+
+ test_advert = Advert.objects.get(text='test_advert')
+ test_advert_form_html = block.render_form(test_advert, 'advert')
+ expected_html = '' % test_advert.id
+ self.assertInHTML(expected_html, test_advert_form_html)
+ self.assertIn("pick an advert, any advert", test_advert_form_html)
+
+ def test_form_response(self):
+ block = SnippetChooserBlock(Advert)
+ test_advert = Advert.objects.get(text='test_advert')
+
+ value = block.value_from_datadict({'advert': str(test_advert.id)}, {}, 'advert')
+ self.assertEqual(value, test_advert)
+
+ empty_value = block.value_from_datadict({'advert': ''}, {}, 'advert')
+ self.assertEqual(empty_value, None)
+
+ def test_clean(self):
+ required_block = SnippetChooserBlock(Advert)
+ nonrequired_block = SnippetChooserBlock(Advert, required=False)
+ test_advert = Advert.objects.get(text='test_advert')
+
+ self.assertEqual(required_block.clean(test_advert), test_advert)
+~~ with self.assertRaises(ValidationError):
+ required_block.clean(None)
+
+ self.assertEqual(nonrequired_block.clean(test_advert), test_advert)
+ self.assertEqual(nonrequired_block.clean(None), None)
+
+
+class TestSnippetListViewWithCustomPrimaryKey(TestCase, WagtailTestUtils):
+ def setUp(self):
+ self.login()
+
+ self.snippet_a = StandardSnippetWithCustomPrimaryKey.objects.create(snippet_id="snippet/01", text="Hello")
+ self.snippet_b = StandardSnippetWithCustomPrimaryKey.objects.create(snippet_id="snippet/02", text="Hello")
+ self.snippet_c = StandardSnippetWithCustomPrimaryKey.objects.create(snippet_id="snippet/03", text="Hello")
+
+ def get(self, params={}):
+ return self.client.get(reverse('wagtailsnippets:list',
+ args=('snippetstests', 'standardsnippetwithcustomprimarykey')),
+ params)
+
+ def test_simple(self):
+ response = self.get()
+ self.assertEqual(response.status_code, 200)
+ self.assertTemplateUsed(response, 'wagtailsnippets/snippets/type_index.html')
+
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ self.assertInHTML('', empty_form_html)
+ self.assertIn('createSnippetChooser("advertwithcustomprimarykey", "tests/advertwithcustomprimarykey");', empty_form_html)
+
+ test_advert = AdvertWithCustomPrimaryKey.objects.get(pk='advert/01')
+ test_advert_form_html = block.render_form(test_advert, 'advertwithcustomprimarykey')
+ expected_html = '' % test_advert.pk
+ self.assertInHTML(expected_html, test_advert_form_html)
+ self.assertIn("pick an advert, any advert", test_advert_form_html)
+
+ def test_form_response(self):
+ block = SnippetChooserBlock(AdvertWithCustomPrimaryKey)
+ test_advert = AdvertWithCustomPrimaryKey.objects.get(pk='advert/01')
+
+ value = block.value_from_datadict({'advertwithcustomprimarykey': str(test_advert.pk)}, {}, 'advertwithcustomprimarykey')
+ self.assertEqual(value, test_advert)
+
+ empty_value = block.value_from_datadict({'advertwithcustomprimarykey': ''}, {}, 'advertwithcustomprimarykey')
+ self.assertEqual(empty_value, None)
+
+ def test_clean(self):
+ required_block = SnippetChooserBlock(AdvertWithCustomPrimaryKey)
+ nonrequired_block = SnippetChooserBlock(AdvertWithCustomPrimaryKey, required=False)
+ test_advert = AdvertWithCustomPrimaryKey.objects.get(pk='advert/01')
+
+ self.assertEqual(required_block.clean(test_advert), test_advert)
+~~ with self.assertRaises(ValidationError):
+ required_block.clean(None)
+
+ self.assertEqual(nonrequired_block.clean(test_advert), test_advert)
+ self.assertEqual(nonrequired_block.clean(None), None)
+
+
+class TestSnippetChooserPanelWithCustomPrimaryKey(TestCase, WagtailTestUtils):
+ fixtures = ['test.json']
+
+ def setUp(self):
+ self.request = RequestFactory().get('/')
+ user = AnonymousUser() # technically, Anonymous users cannot access the admin
+ self.request.user = user
+
+ model = SnippetChooserModelWithCustomPrimaryKey
+ self.advert_text = 'Test advert text'
+ test_snippet = model.objects.create(
+ advertwithcustomprimarykey=AdvertWithCustomPrimaryKey.objects.create(
+ advert_id="advert/02",
+ text=self.advert_text
+ )
+ )
+
+ self.edit_handler = get_snippet_edit_handler(model)
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-exceptions.markdown b/content/pages/examples/django/django-core-exceptions.markdown
new file mode 100644
index 000000000..b1aa162cf
--- /dev/null
+++ b/content/pages/examples/django/django-core-exceptions.markdown
@@ -0,0 +1,183 @@
+title: django.core exceptions code examples
+category: page
+slug: django-core-exceptions-examples
+sortorder: 500011080
+toc: False
+sidebartitle: django.core exceptions
+meta: Python example code for the exceptions function from the django.core module of the Django project.
+
+
+exceptions is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / account / forms.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py)
+
+```python
+# forms.py
+from __future__ import absolute_import
+
+import warnings
+from importlib import import_module
+
+from django import forms
+from django.contrib.auth.tokens import PasswordResetTokenGenerator
+from django.contrib.sites.shortcuts import get_current_site
+~~from django.core import exceptions, validators
+from django.urls import reverse
+from django.utils.translation import gettext, gettext_lazy as _, pgettext
+
+from ..utils import (
+ build_absolute_uri,
+ get_username_max_length,
+ set_form_field_order,
+)
+from . import app_settings
+from .adapter import get_adapter
+from .app_settings import AuthenticationMethod
+from .models import EmailAddress
+from .utils import (
+ filter_users_by_email,
+ get_user_model,
+ perform_login,
+ setup_user_email,
+ sync_user_email_addresses,
+ url_str_to_user_pk,
+ user_email,
+ user_pk_to_url_str,
+ user_username,
+)
+
+
+
+## ... source file abbreviated to get to exceptions examples ...
+
+
+ credentials["email"] = login
+ elif (
+ app_settings.AUTHENTICATION_METHOD ==
+ AuthenticationMethod.USERNAME):
+ credentials["username"] = login
+ else:
+ if self._is_login_email(login):
+ credentials["email"] = login
+ credentials["username"] = login
+ credentials["password"] = self.cleaned_data["password"]
+ return credentials
+
+ def clean_login(self):
+ login = self.cleaned_data['login']
+ return login.strip()
+
+ def _is_login_email(self, login):
+ try:
+ validators.validate_email(login)
+ ret = True
+~~ except exceptions.ValidationError:
+ ret = False
+ return ret
+
+ def clean(self):
+ super(LoginForm, self).clean()
+ if self._errors:
+ return
+ credentials = self.user_credentials()
+ user = get_adapter(self.request).authenticate(
+ self.request,
+ **credentials)
+ if user:
+ self.user = user
+ else:
+ auth_method = app_settings.AUTHENTICATION_METHOD
+ if auth_method == app_settings.AuthenticationMethod.USERNAME_EMAIL:
+ login = self.cleaned_data['login']
+ if self._is_login_email(login):
+ auth_method = app_settings.AuthenticationMethod.EMAIL
+ else:
+ auth_method = app_settings.AuthenticationMethod.USERNAME
+ raise forms.ValidationError(
+ self.error_messages['%s_password_mismatch' % auth_method])
+ return self.cleaned_data
+
+
+## ... source file abbreviated to get to exceptions examples ...
+
+
+ remember = self.cleaned_data['remember']
+ if remember:
+ request.session.set_expiry(app_settings.SESSION_COOKIE_AGE)
+ else:
+ request.session.set_expiry(0)
+ return ret
+
+
+class _DummyCustomSignupForm(forms.Form):
+
+ def signup(self, request, user):
+ pass
+
+
+def _base_signup_form_class():
+ if not app_settings.SIGNUP_FORM_CLASS:
+ return _DummyCustomSignupForm
+ try:
+ fc_module, fc_classname = app_settings.SIGNUP_FORM_CLASS.rsplit('.', 1)
+ except ValueError:
+~~ raise exceptions.ImproperlyConfigured('%s does not point to a form'
+ ' class'
+ % app_settings.SIGNUP_FORM_CLASS)
+ try:
+ mod = import_module(fc_module)
+ except ImportError as e:
+~~ raise exceptions.ImproperlyConfigured('Error importing form class %s:'
+ ' "%s"' % (fc_module, e))
+ try:
+ fc_class = getattr(mod, fc_classname)
+ except AttributeError:
+~~ raise exceptions.ImproperlyConfigured('Module "%s" does not define a'
+ ' "%s" class' % (fc_module,
+ fc_classname))
+ if not hasattr(fc_class, 'signup'):
+ if hasattr(fc_class, 'save'):
+ warnings.warn("The custom signup form must offer"
+ " a `def signup(self, request, user)` method",
+ DeprecationWarning)
+ else:
+~~ raise exceptions.ImproperlyConfigured(
+ 'The custom signup form must implement a "signup" method')
+ return fc_class
+
+
+class BaseSignupForm(_base_signup_form_class()):
+ username = forms.CharField(label=_("Username"),
+ min_length=app_settings.USERNAME_MIN_LENGTH,
+ widget=forms.TextInput(
+ attrs={'placeholder':
+ _('Username'),
+ 'autofocus': 'autofocus'}))
+ email = forms.EmailField(widget=forms.TextInput(
+ attrs={'type': 'email',
+ 'placeholder': _('E-mail address')}))
+
+ def __init__(self, *args, **kwargs):
+ email_required = kwargs.pop('email_required',
+ app_settings.EMAIL_REQUIRED)
+ self.username_required = kwargs.pop('username_required',
+ app_settings.USERNAME_REQUIRED)
+ super(BaseSignupForm, self).__init__(*args, **kwargs)
+ username_field = self.fields['username']
+ username_field.max_length = get_username_max_length()
+ username_field.validators.append(
+
+
+## ... source file continues with no further exceptions examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-mail.markdown b/content/pages/examples/django/django-core-mail.markdown
new file mode 100644
index 000000000..bb83122b4
--- /dev/null
+++ b/content/pages/examples/django/django-core-mail.markdown
@@ -0,0 +1,348 @@
+title: django.core mail code examples
+category: page
+slug: django-core-mail-examples
+sortorder: 500011081
+toc: False
+sidebartitle: django.core mail
+meta: Python example code for the mail function from the django.core module of the Django project.
+
+
+mail is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / account / tests.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/account/tests.py)
+
+```python
+# tests.py
+from __future__ import absolute_import
+
+import json
+import uuid
+from datetime import timedelta
+
+from django import forms
+from django.conf import settings
+from django.contrib.auth.models import AbstractUser, AnonymousUser
+from django.contrib.sites.models import Site
+~~from django.core import mail, validators
+from django.core.exceptions import ValidationError
+from django.db import models
+from django.http import HttpResponseRedirect
+from django.template import Context, Template
+from django.test.client import Client, RequestFactory
+from django.test.utils import override_settings
+from django.urls import reverse
+from django.utils.timezone import now
+
+from allauth.account.forms import BaseSignupForm, ResetPasswordForm, SignupForm
+from allauth.account.models import (
+ EmailAddress,
+ EmailConfirmation,
+ EmailConfirmationHMAC,
+)
+from allauth.tests import Mock, TestCase, patch
+from allauth.utils import get_user_model, get_username_max_length
+
+from . import app_settings
+from .adapter import get_adapter
+from .auth_backends import AuthenticationBackend
+from .signals import user_logged_in, user_logged_out
+from .utils import (
+ filter_users_by_username,
+
+
+## ... source file abbreviated to get to mail examples ...
+
+
+
+ def _password_set_or_change_redirect(self, urlname, usable_password):
+ self._create_user_and_login(usable_password)
+ return self.client.get(reverse(urlname))
+
+ def test_ajax_password_change(self):
+ self._create_user_and_login()
+ resp = self.client.post(
+ reverse('account_change_password'),
+ data={'oldpassword': 'doe',
+ 'password1': 'AbCdEf!123',
+ 'password2': 'AbCdEf!123456'},
+ HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ self.assertEqual(resp['content-type'], 'application/json')
+ data = json.loads(resp.content.decode('utf8'))
+ assert ('same password' in
+ data['form']['fields']['password2']['errors'][0])
+
+ def test_password_forgotten_username_hint(self):
+ user = self._request_new_password()
+~~ body = mail.outbox[0].body
+ assert user.username in body
+
+ @override_settings(
+ ACCOUNT_AUTHENTICATION_METHOD=app_settings.AuthenticationMethod.EMAIL)
+ def test_password_forgotten_no_username_hint(self):
+ user = self._request_new_password()
+~~ body = mail.outbox[0].body
+ assert user.username not in body
+
+ def _request_new_password(self):
+ user = get_user_model().objects.create(
+ username='john', email="john@example.org", is_active=True)
+ user.set_password('doe')
+ user.save()
+ self.client.post(
+ reverse('account_reset_password'),
+ data={'email': 'john@example.org'})
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertEqual(mail.outbox[0].to, ["john@example.org"])
+ return user
+
+ def test_password_reset_flow_with_empty_session(self):
+ self._request_new_password()
+~~ body = mail.outbox[0].body
+ self.assertGreater(body.find('https://'), 0)
+
+ url = body[body.find('/password/reset/'):].split()[0]
+ resp = self.client.get(url)
+
+ reset_pass_url = resp.url
+
+ resp = self.client_class().get(reset_pass_url)
+
+ self.assertTemplateUsed(
+ resp,
+ 'account/password_reset_from_key.%s' %
+ app_settings.TEMPLATE_EXTENSION)
+
+ self.assertTrue(resp.context_data['token_fail'])
+
+ def test_password_reset_flow(self):
+ user = self._request_new_password()
+~~ body = mail.outbox[0].body
+ self.assertGreater(body.find('https://'), 0)
+
+ url = body[body.find('/password/reset/'):].split()[0]
+ resp = self.client.get(url)
+ url = resp.url
+ resp = self.client.get(url)
+ self.assertTemplateUsed(
+ resp,
+ 'account/password_reset_from_key.%s' %
+ app_settings.TEMPLATE_EXTENSION)
+ self.assertFalse('token_fail' in resp.context_data)
+
+ resp = self.client.post(url,
+ {'password1': 'newpass123',
+ 'password2': 'newpass123'})
+ self.assertRedirects(resp,
+ reverse('account_reset_password_from_key_done'))
+
+ user = get_user_model().objects.get(pk=user.pk)
+ self.assertTrue(user.check_password('newpass123'))
+
+ resp = self.client.post(url,
+ {'password1': 'newpass123',
+ 'password2': 'newpass123'})
+
+
+## ... source file abbreviated to get to mail examples ...
+
+
+ app_settings.TEMPLATE_EXTENSION)
+ self.assertTrue(resp.context_data['token_fail'])
+
+ response = self.client.get(url)
+ self.assertTemplateUsed(
+ response,
+ 'account/password_reset_from_key.%s' %
+ app_settings.TEMPLATE_EXTENSION)
+ self.assertTrue(response.context_data['token_fail'])
+
+ response = self.client.post(url,
+ {'password1': 'newpass123',
+ 'password2': 'newpass123'},
+ HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ self.assertEqual(response.status_code, 400)
+ data = json.loads(response.content.decode('utf8'))
+ assert 'invalid' in data['form']['errors'][0]
+
+ def test_password_reset_flow_with_email_changed(self):
+ user = self._request_new_password()
+~~ body = mail.outbox[0].body
+ self.assertGreater(body.find('https://'), 0)
+ EmailAddress.objects.create(
+ user=user,
+ email='other@email.org')
+ url = body[body.find('/password/reset/'):].split()[0]
+ resp = self.client.get(url)
+ self.assertTemplateUsed(
+ resp,
+ 'account/password_reset_from_key.%s' %
+ app_settings.TEMPLATE_EXTENSION)
+ self.assertTrue('token_fail' in resp.context_data)
+
+ @override_settings(ACCOUNT_LOGIN_ON_PASSWORD_RESET=True)
+ def test_password_reset_ACCOUNT_LOGIN_ON_PASSWORD_RESET(self):
+ user = self._request_new_password()
+~~ body = mail.outbox[0].body
+ url = body[body.find('/password/reset/'):].split()[0]
+ resp = self.client.get(url)
+ resp = self.client.post(
+ resp.url,
+ {'password1': 'newpass123',
+ 'password2': 'newpass123'})
+ self.assertTrue(user.is_authenticated)
+ self.assertRedirects(resp, '/confirm-email/')
+
+ @override_settings(ACCOUNT_EMAIL_CONFIRMATION_HMAC=False)
+ def test_email_verification_mandatory(self):
+ c = Client()
+ resp = c.post(reverse('account_signup'),
+ {'username': 'johndoe',
+ 'email': 'john@example.com',
+ 'password1': 'johndoe',
+ 'password2': 'johndoe'},
+ follow=True)
+ self.assertEqual(resp.status_code, 200)
+ self.assertEqual(mail.outbox[0].to, ['john@example.com'])
+ self.assertGreater(mail.outbox[0].body.find('https://'), 0)
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertTemplateUsed(
+ resp,
+
+
+## ... source file continues with no further mail examples...
+
+```
+
+
+## Example 2 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / tests / test_mail.py**](https://github.com/divio/django-cms/blob/develop/cms/tests/test_mail.py)
+
+```python
+# test_mail.py
+from django.contrib.auth import get_user_model
+~~from django.core import mail
+
+from cms.api import create_page_user
+from cms.test_utils.testcases import CMSTestCase
+from cms.utils.mail import mail_page_user_change
+
+
+class MailTestCase(CMSTestCase):
+ def setUp(self):
+~~ mail.outbox = [] # reset outbox
+
+ def test_mail_page_user_change(self):
+ user = get_user_model().objects.create_superuser("username", "username@django-cms.org", "username")
+ user = create_page_user(user, user, grant_all=True)
+ mail_page_user_change(user)
+ self.assertEqual(len(mail.outbox), 1)
+
+
+
+## ... source file continues with no further mail examples...
+
+```
+
+
+## Example 3 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / tests / test_tasks.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/tests/test_tasks.py)
+
+```python
+# test_tasks.py
+from django.test import TestCase
+from explorer.app_settings import EXPLORER_DEFAULT_CONNECTION as CONN
+from explorer.tasks import execute_query, snapshot_queries, truncate_querylogs, build_schema_cache_async
+from explorer.tests.factories import SimpleQueryFactory
+~~from django.core import mail
+from mock import Mock, patch
+from six import StringIO
+from explorer.models import QueryLog
+from datetime import datetime, timedelta
+
+
+class TestTasks(TestCase):
+
+ @patch('explorer.tasks.s3_upload')
+ def test_async_results(self, mocked_upload):
+ mocked_upload.return_value = 'http://s3.com/your-file.csv'
+
+ q = SimpleQueryFactory(sql='select 1 "a", 2 "b", 3 "c";', title="testquery")
+ execute_query(q.id, 'cc@epantry.com')
+
+ output = StringIO()
+ output.write('a,b,c\r\n1,2,3\r\n')
+
+ self.assertEqual(len(mail.outbox), 2)
+~~ self.assertIn('[SQL Explorer] Your query is running', mail.outbox[0].subject)
+~~ self.assertIn('[SQL Explorer] Report ', mail.outbox[1].subject)
+ self.assertEqual(mocked_upload.call_args[0][1].getvalue(), output.getvalue())
+ self.assertEqual(mocked_upload.call_count, 1)
+
+ @patch('explorer.tasks.s3_upload')
+ def test_async_results_fails_with_message(self, mocked_upload):
+ mocked_upload.return_value = 'http://s3.com/your-file.csv'
+
+ q = SimpleQueryFactory(sql='select x from foo;', title="testquery")
+ execute_query(q.id, 'cc@epantry.com')
+
+ output = StringIO()
+ output.write('a,b,c\r\n1,2,3\r\n')
+
+ self.assertEqual(len(mail.outbox), 2)
+~~ self.assertIn('[SQL Explorer] Error ', mail.outbox[1].subject)
+ self.assertEqual(mocked_upload.call_count, 0)
+
+ @patch('explorer.tasks.s3_upload')
+ def test_snapshots(self, mocked_upload):
+ mocked_upload.return_value = 'http://s3.com/your-file.csv'
+
+ SimpleQueryFactory(snapshot=True)
+ SimpleQueryFactory(snapshot=True)
+ SimpleQueryFactory(snapshot=True)
+ SimpleQueryFactory(snapshot=False)
+
+ snapshot_queries()
+ self.assertEqual(mocked_upload.call_count, 3)
+
+ def test_truncating_querylogs(self):
+ QueryLog(sql='foo').save()
+ QueryLog.objects.filter(sql='foo').update(run_at=datetime.now() - timedelta(days=30))
+ QueryLog(sql='bar').save()
+ QueryLog.objects.filter(sql='bar').update(run_at=datetime.now() - timedelta(days=29))
+ truncate_querylogs(30)
+ self.assertEqual(QueryLog.objects.count(), 1)
+
+ @patch('explorer.schema.build_schema_info')
+ def test_build_schema_cache_async(self, mocked_build):
+
+
+## ... source file continues with no further mail examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-management.markdown b/content/pages/examples/django/django-core-management.markdown
new file mode 100644
index 000000000..2f6a72bf9
--- /dev/null
+++ b/content/pages/examples/django/django-core-management.markdown
@@ -0,0 +1,568 @@
+title: django.core management code examples
+category: page
+slug: django-core-management-examples
+sortorder: 500011082
+toc: False
+sidebartitle: django.core management
+meta: Python example code for the management function from the django.core module of the Django project.
+
+
+management is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / tests / test_management.py**](https://github.com/divio/django-cms/blob/develop/cms/tests/test_management.py)
+
+```python
+# test_management.py
+from __future__ import unicode_literals
+import uuid
+from cms.test_utils.project.sampleapp.cms_apps import SampleApp
+from cms.test_utils.util.context_managers import apphooks
+
+from six.moves import StringIO
+
+from django.conf import settings
+from django.contrib.sites.models import Site
+~~from django.core import management
+from django.core.management import CommandError
+from django.test.utils import override_settings
+
+from cms.api import create_page, add_plugin, create_title
+from cms.management.commands.subcommands.list import plugin_report
+from cms.models import Page, StaticPlaceholder
+from cms.models.placeholdermodel import Placeholder
+from cms.models.pluginmodel import CMSPlugin
+from cms.test_utils.fixtures.navextenders import NavextendersFixture
+from cms.test_utils.testcases import CMSTestCase
+from djangocms_text_ckeditor.cms_plugins import TextPlugin
+
+
+APPHOOK = "SampleApp"
+PLUGIN = "TextPlugin"
+
+TEST_INSTALLED_APPS = [
+ "django.contrib.auth",
+ "cms",
+ "menus",
+ "sekizai",
+ "treebeard",
+] + settings.PLUGIN_APPS
+if settings.AUTH_USER_MODEL == "emailuserapp.EmailUser":
+ TEST_INSTALLED_APPS.append("cms.test_utils.project.emailuserapp")
+if settings.AUTH_USER_MODEL == "customuserapp.User":
+ TEST_INSTALLED_APPS.append("cms.test_utils.project.customuserapp")
+
+
+class ManagementTestCase(CMSTestCase):
+ @override_settings(INSTALLED_APPS=TEST_INSTALLED_APPS)
+ def test_list_apphooks(self):
+ with apphooks(SampleApp):
+ out = StringIO()
+ create_page('Hello Title', "nav_playground.html", "en", apphook=APPHOOK)
+ self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 1)
+~~ management.call_command(
+ "cms",
+ "list",
+ "apphooks",
+ interactive=False,
+ stdout=out,
+ )
+ self.assertEqual(out.getvalue(), "SampleApp (draft)\n")
+
+ def test_uninstall_apphooks_without_apphook(self):
+ with apphooks():
+ out = StringIO()
+~~ management.call_command(
+ "cms",
+ "uninstall",
+ "apphooks",
+ APPHOOK,
+ interactive=False,
+ stdout=out,
+ )
+ self.assertEqual(out.getvalue(), "no 'SampleApp' apphooks found\n")
+
+ def test_fix_tree(self):
+ create_page("home", "nav_playground.html", "en")
+ page1 = create_page("page", "nav_playground.html", "en")
+ page1.node.depth = 3
+ page1.node.numchild = 4
+ page1.node.path = "00100010"
+ page1.node.save()
+ out = StringIO()
+~~ management.call_command('cms', 'fix-tree', interactive=False, stdout=out)
+ self.assertEqual(out.getvalue(), 'fixing page tree\nfixing plugin tree\nall done\n')
+ page1 = page1.reload()
+ self.assertEqual(page1.node.path, "0002")
+ self.assertEqual(page1.node.depth, 1)
+ self.assertEqual(page1.node.numchild, 0)
+
+ def test_fix_tree_regression_5641(self):
+ alpha = create_page("Alpha", "nav_playground.html", "en", published=True)
+ beta = create_page("Beta", "nav_playground.html", "en", published=False)
+ gamma = create_page("Gamma", "nav_playground.html", "en", published=False)
+ delta = create_page("Delta", "nav_playground.html", "en", published=True)
+ theta = create_page("Theta", "nav_playground.html", "en", published=True)
+
+ beta.move_page(alpha.node, position='last-child')
+ gamma.move_page(beta.node, position='last-child')
+ delta.move_page(gamma.node, position='last-child')
+ theta.move_page(delta.node, position='last-child')
+
+ out = StringIO()
+~~ management.call_command('cms', 'fix-tree', interactive=False, stdout=out)
+
+ tree = [
+ (alpha, '0001'),
+ (beta, '00010001'),
+ (gamma, '000100010001'),
+ (delta, '0001000100010001'),
+ (theta, '00010001000100010001'),
+ ]
+
+ for page, path in tree:
+ self.assertEqual(page.node.path, path)
+
+ @override_settings(INSTALLED_APPS=TEST_INSTALLED_APPS)
+ def test_uninstall_apphooks_with_apphook(self):
+ with apphooks(SampleApp):
+ out = StringIO()
+ create_page('Hello Title', "nav_playground.html", "en", apphook=APPHOOK)
+ self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 1)
+~~ management.call_command(
+ "cms",
+ "uninstall",
+ "apphooks",
+ APPHOOK,
+ interactive=False,
+ stdout=out,
+ )
+ self.assertEqual(out.getvalue(), "1 'SampleApp' apphooks uninstalled\n")
+ self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 0)
+
+ @override_settings(INSTALLED_APPS=TEST_INSTALLED_APPS)
+ def test_list_plugins(self):
+ out = StringIO()
+ placeholder = Placeholder.objects.create(slot="test")
+ add_plugin(placeholder, TextPlugin, "en", body="en body")
+ add_plugin(placeholder, TextPlugin, "en", body="en body")
+ link_plugin = add_plugin(placeholder, "LinkPlugin", "en",
+ name="A Link", external_link="https://www.django-cms.org")
+ self.assertEqual(
+ CMSPlugin.objects.filter(plugin_type=PLUGIN).count(),
+ 2)
+ self.assertEqual(
+ CMSPlugin.objects.filter(plugin_type="LinkPlugin").count(),
+ 1)
+
+ instanceless_plugin = CMSPlugin(language="en", plugin_type="TextPlugin")
+ instanceless_plugin.save()
+
+ bogus_plugin = CMSPlugin(language="en", plugin_type="BogusPlugin")
+ bogus_plugin.save()
+
+~~ management.call_command('cms', 'list', 'plugins', interactive=False, stdout=out)
+ report = plugin_report()
+
+ self.assertEqual(
+ len(report),
+ 3)
+
+ bogus_plugins_report = report[0]
+ self.assertEqual(
+ bogus_plugins_report["model"],
+ None)
+
+ self.assertEqual(
+ bogus_plugins_report["type"],
+ u'BogusPlugin')
+
+ self.assertEqual(
+ bogus_plugins_report["instances"][0],
+ bogus_plugin)
+
+ link_plugins_report = report[1]
+ self.assertEqual(
+ link_plugins_report["model"],
+ link_plugin.__class__)
+
+
+
+## ... source file abbreviated to get to management examples ...
+
+
+ bogus_plugins_report = report[0]
+ self.assertEqual(
+ len(bogus_plugins_report["instances"]),
+ 1)
+
+ link_plugins_report = report[1]
+ self.assertEqual(
+ len(link_plugins_report["instances"]),
+ 1)
+
+ text_plugins_report = report[2]
+ self.assertEqual(
+ len(text_plugins_report["instances"]),
+ 3)
+
+ self.assertEqual(
+ len(text_plugins_report["unsaved_instances"]),
+ 1)
+
+ out = StringIO()
+~~ management.call_command('cms', 'delete-orphaned-plugins', interactive=False, stdout=out)
+ report = plugin_report()
+
+ self.assertEqual(
+ len(report),
+ 2)
+
+ link_plugins_report = report[0]
+ self.assertEqual(
+ len(link_plugins_report["instances"]),
+ 1)
+
+ text_plugins_report = report[1]
+ self.assertEqual(
+ len(text_plugins_report["instances"]),
+ 2)
+
+ self.assertEqual(
+ len(text_plugins_report["unsaved_instances"]),
+ 0)
+
+ def test_uninstall_plugins_without_plugin(self):
+ out = StringIO()
+~~ management.call_command('cms', 'uninstall', 'plugins', PLUGIN, interactive=False, stdout=out)
+ self.assertEqual(out.getvalue(), "no 'TextPlugin' plugins found\n")
+
+ @override_settings(INSTALLED_APPS=TEST_INSTALLED_APPS)
+ def test_uninstall_plugins_with_plugin(self):
+ out = StringIO()
+ placeholder = Placeholder.objects.create(slot="test")
+ add_plugin(placeholder, TextPlugin, "en", body="en body")
+ self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 1)
+~~ management.call_command('cms', 'uninstall', 'plugins', PLUGIN, interactive=False, stdout=out)
+ self.assertEqual(out.getvalue(), "1 'TextPlugin' plugins uninstalled\n")
+ self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 0)
+
+ def test_publisher_public(self):
+ admin = self.get_superuser()
+ create_page(
+ 'home',
+ published=True,
+ language='de',
+ template='nav_playground.html',
+ created_by=admin,
+ )
+ page_1 = create_page(
+ 'página 1',
+ published=True,
+ language='de',
+ template='nav_playground.html',
+ created_by=admin,
+ )
+ page_1.unpublish('de')
+
+ page_2 = create_page(
+ 'página 2',
+ published=True,
+ language='de',
+ template='nav_playground.html',
+ created_by=admin,
+ )
+ page_2.unpublish('de')
+
+~~ management.call_command(
+ 'cms',
+ 'publisher-publish',
+ '-l de',
+ '--unpublished',
+ interactive=False,
+ )
+
+ self.assertEqual(Page.objects.public().count(), 3)
+
+
+class PageFixtureManagementTestCase(NavextendersFixture, CMSTestCase):
+
+ def _fill_page_body(self, page, lang):
+ ph_en = page.placeholders.get(slot="body")
+ mcol1 = add_plugin(ph_en, "MultiColumnPlugin", lang, position="first-child")
+ add_plugin(ph_en, "ColumnPlugin", lang, position="first-child", target=mcol1)
+ col2 = add_plugin(ph_en, "ColumnPlugin", lang, position="first-child", target=mcol1)
+ mcol2 = add_plugin(ph_en, "MultiColumnPlugin", lang, position="first-child", target=col2)
+ add_plugin(ph_en, "ColumnPlugin", lang, position="first-child", target=mcol2)
+ col4 = add_plugin(ph_en, "ColumnPlugin", lang, position="first-child", target=mcol2)
+ add_plugin(ph_en, "LinkPlugin", lang, target=col4,
+ name="A Link", external_link="https://www.django-cms.org")
+ static_placeholder = StaticPlaceholder(code=str(uuid.uuid4()), site_id=1)
+ static_placeholder.save()
+ add_plugin(static_placeholder.draft, "TextPlugin", lang, body="example content")
+
+ def setUp(self):
+ pages = Page.objects.drafts()
+ for page in pages:
+ self._fill_page_body(page, "en")
+
+ def test_copy_langs(self):
+ site = 1
+ number_start_plugins = CMSPlugin.objects.all().count()
+
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', interactive=False, stdout=out
+ )
+ pages = Page.objects.on_site(site).drafts()
+ for page in pages:
+ self.assertEqual(set((u'en', u'de')), set(page.get_languages()))
+ self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins*2)
+ self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_start_plugins)
+
+ root_page = Page.objects.get_home(site)
+ root_plugins = CMSPlugin.objects.filter(placeholder=root_page.placeholders.get(slot="body"))
+
+ first_plugin_en, _ = root_plugins.get(language='en', parent=None).get_plugin_instance()
+ first_plugin_de, _ = root_plugins.get(language='de', parent=None).get_plugin_instance()
+
+ self.assertEqual(first_plugin_en.plugin_type, first_plugin_de.plugin_type)
+
+ link_en, _ = root_plugins.get(language='en', plugin_type='LinkPlugin').get_plugin_instance()
+ link_de, _ = root_plugins.get(language='de', plugin_type='LinkPlugin').get_plugin_instance()
+
+ self.assertEqual(link_en.external_link, link_de.external_link)
+ self.assertEqual(link_en.get_position_in_placeholder(), link_de.get_position_in_placeholder())
+
+ stack_plugins = CMSPlugin.objects.filter(placeholder=StaticPlaceholder.objects.order_by('?')[0].draft)
+
+ stack_text_en, _ = stack_plugins.get(language='en', plugin_type='TextPlugin').get_plugin_instance()
+ stack_text_de, _ = stack_plugins.get(language='de', plugin_type='TextPlugin').get_plugin_instance()
+
+ self.assertEqual(stack_text_en.plugin_type, stack_text_de.plugin_type)
+ self.assertEqual(stack_text_en.body, stack_text_de.body)
+
+ def test_copy_langs_no_content(self):
+ site = 1
+ number_start_plugins = CMSPlugin.objects.all().count()
+
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', '--skip-content',
+ interactive=False, stdout=out
+ )
+ pages = Page.objects.on_site(site).drafts()
+ for page in pages:
+ self.assertEqual(set((u'en', u'de')), set(page.get_languages()))
+ self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='de').count(), 0)
+
+ root_page = Page.objects.get_home(site)
+ root_plugins = CMSPlugin.objects.filter(
+ placeholder=root_page.placeholders.get(slot="body"))
+
+ first_plugin_en, _ = root_plugins.get(language='en', parent=None).get_plugin_instance()
+ first_plugin_de = None
+ with self.assertRaises(CMSPlugin.DoesNotExist):
+ first_plugin_de, _ = root_plugins.get(language='de', parent=None).get_plugin_instance()
+
+ self.assertIsNone(first_plugin_de)
+
+ stack_plugins = CMSPlugin.objects.filter(
+ placeholder=StaticPlaceholder.objects.order_by('?')[0].draft)
+
+ stack_text_en, _ = stack_plugins.get(language='en',
+ plugin_type='TextPlugin').get_plugin_instance()
+ with self.assertRaises(CMSPlugin.DoesNotExist):
+ stack_text_de, _ = stack_plugins.get(language='de',
+ plugin_type='TextPlugin').get_plugin_instance()
+
+ def test_copy_sites(self):
+ site_1_pk = 1
+ site_1 = Site.objects.get(pk=site_1_pk)
+ site_2 = Site.objects.create(name='site 2')
+ site_2_pk = site_2.pk
+ phs = []
+ for page in Page.objects.on_site(site_1_pk).drafts():
+ phs.extend(page.placeholders.values_list('pk', flat=True))
+ number_start_plugins = CMSPlugin.objects.filter(placeholder__in=phs).count()
+
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'site', '--from-site=%s' % site_1_pk, '--to-site=%s' % site_2_pk,
+ stdout=out
+ )
+ for page in Page.objects.on_site(site_1_pk).drafts():
+ page.publish('en')
+ for page in Page.objects.on_site(site_2_pk).drafts():
+ page.publish('en')
+
+ pages_1 = list(Page.objects.drafts().on_site(site_1).select_related('node').order_by('node__path'))
+ pages_2 = list(Page.objects.drafts().on_site(site_2).select_related('node').order_by('node__path'))
+ for index, page in enumerate(pages_1):
+ self.assertEqual(page.get_title('en'), pages_2[index].get_title('en'))
+ self.assertEqual(page.node.depth, pages_2[index].node.depth)
+
+ phs_1 = []
+ phs_2 = []
+ for page in Page.objects.on_site(site_1_pk).drafts():
+ phs_1.extend(page.placeholders.values_list('pk', flat=True))
+ for page in Page.objects.on_site(site_2_pk).drafts():
+ phs_2.extend(page.placeholders.values_list('pk', flat=True))
+
+ self.assertEqual(CMSPlugin.objects.filter(placeholder__in=phs_1).count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(placeholder__in=phs_2).count(), number_start_plugins)
+
+
+
+## ... source file abbreviated to get to management examples ...
+
+
+
+ first_plugin_1, _ = root_plugins_1.get(language='en', parent=None).get_plugin_instance()
+ first_plugin_2, _ = root_plugins_2.get(language='en', parent=None).get_plugin_instance()
+
+ self.assertEqual(first_plugin_1.plugin_type, first_plugin_2.plugin_type)
+
+ link_1, _ = root_plugins_1.get(language='en', plugin_type='LinkPlugin').get_plugin_instance()
+ link_2, _ = root_plugins_2.get(language='en', plugin_type='LinkPlugin').get_plugin_instance()
+
+ self.assertEqual(link_1.external_link, link_2.external_link)
+ self.assertEqual(link_1.get_position_in_placeholder(), link_2.get_position_in_placeholder())
+
+ def test_copy_existing_title(self):
+ site = 1
+ number_start_plugins = CMSPlugin.objects.all().count()
+
+ root_page = Page.objects.get_home(site)
+ create_title("de", "root page de", root_page)
+
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', interactive=False, stdout=out
+ )
+ pages = Page.objects.on_site(site).drafts()
+ for page in pages:
+ self.assertEqual(set((u'en', u'de')), set(page.get_languages()))
+
+ self.assertEqual("root page de", Page.objects.get_home(site).get_title("de"))
+
+ self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins*2)
+ self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_start_plugins)
+
+ def test_copy_filled_placeholder(self):
+ site = 1
+ number_start_plugins = CMSPlugin.objects.all().count()
+
+ root_page = Page.objects.get_home(site)
+ create_title("de", "root page de", root_page)
+ ph = root_page.placeholders.get(slot="body")
+ add_plugin(ph, "TextPlugin", "de", body="Hello World")
+
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', interactive=False, stdout=out
+ )
+
+ self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_start_plugins-6)
+
+ def test_copy_filled_placeholder_force_copy(self):
+ site = 1
+ number_start_plugins = CMSPlugin.objects.all().count()
+
+ root_page = Page.objects.get_home(site)
+ create_title("de", "root page de", root_page)
+ ph = root_page.placeholders.get(slot="body")
+ add_plugin(ph, "TextPlugin", "de", body="Hello World")
+
+ root_plugins = CMSPlugin.objects.filter(placeholder=ph)
+ text_de_orig, _ = root_plugins.get(language='de', plugin_type='TextPlugin').get_plugin_instance()
+
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', '--force', interactive=False,
+ stdout=out
+ )
+
+ CMSPlugin.objects.filter(placeholder=root_page.placeholders.get(slot="body"))
+
+ self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_start_plugins+1)
+
+ def test_copy_from_non_existing_lang(self):
+ site = 1
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=de', '--to-lang=fr', verbosity=3,
+ interactive=False, stdout=out
+ )
+ text = out.getvalue()
+ page_count = Page.objects.on_site(site).drafts().count() + 1
+ for idx in range(1, page_count):
+ self.assertTrue("Skipping page page%d, language de not defined" % idx in text)
+
+ def test_copy_site_safe(self):
+ site_other = 1
+ site_active = 2
+ origina_site1_langs = {}
+
+ number_start_plugins = CMSPlugin.objects.all().count()
+ site_obj = Site.objects.create(domain="sample2.com", name="sample2.com", pk=site_active)
+
+ for page in Page.objects.on_site(1).drafts():
+ origina_site1_langs[page.pk] = set(page.get_languages())
+
+ p1 = create_page('page1', published=True, in_navigation=True, language='de', template='nav_playground.html', site=site_obj)
+ create_page('page4', published=True, in_navigation=True, language='de', template='nav_playground.html', site=site_obj)
+ create_page('page2', published=True, in_navigation=True, parent=p1, language='de', template='nav_playground.html', site=site_obj)
+
+ for page in Page.objects.on_site(site_active).drafts():
+ self._fill_page_body(page, 'de')
+
+ number_site2_plugins = CMSPlugin.objects.all().count() - number_start_plugins
+
+ out = StringIO()
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=de', '--to-lang=fr', '--site=%s' % site_active,
+ interactive=False, stdout=out
+ )
+
+ for page in Page.objects.on_site(site_other).drafts():
+ self.assertEqual(origina_site1_langs[page.pk], set(page.get_languages()))
+
+ for page in Page.objects.on_site(site_active).drafts():
+ self.assertEqual(set(('de', 'fr')), set(page.get_languages()))
+
+ self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_site2_plugins)
+ self.assertEqual(CMSPlugin.objects.filter(language='fr').count(), number_site2_plugins)
+ self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins + number_site2_plugins*2)
+
+ def test_copy_bad_languages(self):
+ out = StringIO()
+ with self.assertRaises(CommandError) as command_error:
+~~ management.call_command(
+ 'cms', 'copy', 'lang', '--from-lang=it', '--to-lang=fr', interactive=False,
+ stdout=out
+ )
+
+ self.assertEqual(str(command_error.exception), 'Both languages have to be present in settings.LANGUAGES and settings.CMS_LANGUAGES')
+
+
+
+## ... source file continues with no further management examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-serializers.markdown b/content/pages/examples/django/django-core-serializers.markdown
new file mode 100644
index 000000000..e6d03713a
--- /dev/null
+++ b/content/pages/examples/django/django-core-serializers.markdown
@@ -0,0 +1,111 @@
+title: django.core serializers code examples
+category: page
+slug: django-core-serializers-examples
+sortorder: 500011083
+toc: False
+sidebartitle: django.core serializers
+meta: Python example code for the serializers function from the django.core module of the Django project.
+
+
+serializers is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-angular
+[django-angular](https://github.com/jrief/django-angular)
+([project examples website](https://django-angular.awesto.com/classic_form/))
+is a library with helper code to make it easier to use
+[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
+The code for django-angular is
+[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
+
+[**django-angular / djng / views / crud.py**](https://github.com/jrief/django-angular/blob/master/djng/views/crud.py)
+
+```python
+# crud.py
+import json
+
+from django.core.exceptions import ValidationError
+~~from django.core import serializers
+from django.forms.models import modelform_factory
+from django.views.generic import FormView
+
+from djng.views.mixins import JSONBaseMixin, JSONResponseException
+
+
+class NgMissingParameterError(ValueError):
+ pass
+
+
+class NgCRUDView(JSONBaseMixin, FormView):
+ model = None
+ fields = None
+ form_class = None
+ slug_field = 'slug'
+ serializer_name = 'python'
+ serialize_natural_keys = False
+
+ allowed_methods = ['GET', 'POST', 'DELETE']
+ exclude_methods = []
+
+ def get_allowed_methods(self):
+ return [method for method in self.allowed_methods if method not in self.exclude_methods]
+
+
+
+## ... source file abbreviated to get to serializers examples ...
+
+
+ def get_form_class(self):
+ return self.form_class or modelform_factory(self.model, exclude=[])
+
+ def build_json_response(self, data, **kwargs):
+ return self.json_response(self.serialize_queryset(data), separators=(',', ':'), **kwargs)
+
+ def error_json_response(self, message, status_code=400, detail=None):
+ response_data = {
+ "message": message,
+ "detail": detail,
+ }
+ return self.json_response(response_data, status=status_code, separators=(',', ':'))
+
+ def serialize_queryset(self, queryset):
+ object_data = []
+ is_queryset = False
+ query_fields = self.get_fields()
+ try:
+ iter(queryset)
+ is_queryset = True
+~~ raw_data = serializers.serialize(self.serializer_name, queryset, fields=query_fields,
+ use_natural_keys=self.serialize_natural_keys)
+ except TypeError: # Not iterable
+~~ raw_data = serializers.serialize(self.serializer_name, [queryset, ], fields=query_fields,
+ use_natural_keys=self.serialize_natural_keys)
+
+ for obj in raw_data: # Add pk to fields
+ obj['fields']['pk'] = obj['pk']
+ object_data.append(obj['fields'])
+
+ if is_queryset:
+ return object_data
+ return object_data[0] # If there's only one object
+
+ def get_form_kwargs(self):
+ kwargs = super(NgCRUDView, self).get_form_kwargs()
+ kwargs['data'] = json.loads(self.request.body.decode('utf-8'))
+
+ if 'pk' in self.request.GET or self.slug_field in self.request.GET:
+ kwargs['instance'] = self.get_object()
+ return kwargs
+
+ def get_object(self):
+ if 'pk' in self.request.GET:
+ return self.model.objects.get(pk=self.request.GET['pk'])
+ elif self.slug_field in self.request.GET:
+ return self.model.objects.get(**{self.slug_field: self.request.GET[self.slug_field]})
+ raise NgMissingParameterError(
+
+
+## ... source file continues with no further serializers examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-signals.markdown b/content/pages/examples/django/django-core-signals.markdown
new file mode 100644
index 000000000..409f32a45
--- /dev/null
+++ b/content/pages/examples/django/django-core-signals.markdown
@@ -0,0 +1,164 @@
+title: django.core signals code examples
+category: page
+slug: django-core-signals-examples
+sortorder: 500011084
+toc: False
+sidebartitle: django.core signals
+meta: Python example code for the signals function from the django.core module of the Django project.
+
+
+signals is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-webtest
+[django-webtest](https://github.com/django-webtest/django-webtest)
+([PyPI package information](https://pypi.org/project/django-webtest/))
+is a [Django](/django.html) extension that makes it easier to use
+[WebTest](http://docs.pylonsproject.org/projects/webtest/) with
+your projects.
+
+The project is open sourced under the
+[MIT license](https://github.com/django-webtest/django-webtest/blob/master/LICENSE.txt).
+
+[**django-webtest / django_webtest / __init__.py**](https://github.com/django-webtest/django-webtest/blob/master/django_webtest/./__init__.py)
+
+```python
+# __init__.py
+import copy
+
+from django.conf import settings
+from django.test.signals import template_rendered
+from django.core.handlers.wsgi import WSGIHandler
+from django.test import TestCase, TransactionTestCase
+from django.test.client import store_rendered_templates
+
+from functools import partial
+
+try:
+ from importlib import import_module
+except ImportError:
+ from django.utils.importlib import import_module
+
+~~from django.core import signals
+try:
+ from django.db import close_old_connections
+except ImportError:
+ from django.db import close_connection
+ close_old_connections = None
+try:
+ from django.core.servers.basehttp import (
+ AdminMediaHandler as StaticFilesHandler)
+except ImportError:
+ from django.contrib.staticfiles.handlers import StaticFilesHandler
+
+from webtest import TestApp
+try:
+ from webtest.utils import NoDefault
+except ImportError:
+ NoDefault = ''
+
+from django_webtest.response import DjangoWebtestResponse
+from django_webtest.compat import to_string, to_wsgi_safe_string
+
+
+_notgiven = object()
+
+
+
+
+## ... source file abbreviated to get to signals examples ...
+
+
+ def set_user(self, user):
+ if user is None and 'WEBTEST_USER' in self.extra_environ:
+ del self.extra_environ['WEBTEST_USER']
+ if user is not None:
+ self.extra_environ = self._update_environ(self.extra_environ, user)
+
+ def _update_environ(self, environ, user=_notgiven):
+ environ = environ or {}
+
+ if user is not _notgiven:
+ if user is None:
+ environ['WEBTEST_USER'] = ''
+ else:
+ username = _get_username(user)
+ environ['WEBTEST_USER'] = to_wsgi_safe_string(username)
+
+ return environ
+
+ def do_request(self, req, status, expect_errors):
+ if close_old_connections is not None: # Django 1.6+
+~~ signals.request_started.disconnect(close_old_connections)
+~~ signals.request_finished.disconnect(close_old_connections)
+ else: # Django < 1.6
+~~ signals.request_finished.disconnect(close_connection)
+
+ try:
+ req.environ.setdefault('REMOTE_ADDR', '127.0.0.1')
+
+ req.environ['REMOTE_ADDR'] = to_string(req.environ['REMOTE_ADDR'])
+ req.environ['PATH_INFO'] = to_string(req.environ['PATH_INFO'])
+
+ data = {}
+ on_template_render = partial(store_rendered_templates, data)
+ template_rendered.connect(on_template_render)
+
+ response = super(DjangoTestApp, self).do_request(req, status,
+ expect_errors)
+
+ def flattend(detail):
+ if len(data[detail]) == 1:
+ return data[detail][0]
+ return data[detail]
+
+ response.context = None
+ response.template = None
+ response.templates = data.get('templates', None)
+
+ if data.get('context'):
+ response.context = flattend('context')
+
+ if data.get('template'):
+ response.template = flattend('template')
+ elif data.get('templates'):
+ response.template = flattend('templates')
+
+ response.__class__ = self.response_class
+ return response
+ finally:
+ if close_old_connections: # Django 1.6+
+~~ signals.request_started.connect(close_old_connections)
+~~ signals.request_finished.connect(close_old_connections)
+ else: # Django < 1.6
+~~ signals.request_finished.connect(close_connection)
+
+ def get(self, url, *args, **kwargs):
+ extra_environ = kwargs.get('extra_environ')
+ user = kwargs.pop('user', _notgiven)
+ auto_follow = kwargs.pop('auto_follow', False)
+
+ kwargs['extra_environ'] = self._update_environ(extra_environ, user)
+ response = super(DjangoTestApp, self).get(url, *args, **kwargs)
+
+ def is_redirect(r):
+ return r.status_int >= 300 and r.status_int < 400
+ while auto_follow and is_redirect(response):
+ response = response.follow(**kwargs)
+
+ return response
+
+ def post(self, url, *args, **kwargs):
+ extra_environ = kwargs.get('extra_environ')
+ user = kwargs.pop('user', _notgiven)
+ kwargs['extra_environ'] = self._update_environ(extra_environ, user)
+ return super(DjangoTestApp, self).post(url, *args, **kwargs)
+
+ def put(self, url, *args, **kwargs):
+ extra_environ = kwargs.get('extra_environ')
+
+
+## ... source file continues with no further signals examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-signing.markdown b/content/pages/examples/django/django-core-signing.markdown
new file mode 100644
index 000000000..3d3050903
--- /dev/null
+++ b/content/pages/examples/django/django-core-signing.markdown
@@ -0,0 +1,310 @@
+title: django.core signing code examples
+category: page
+slug: django-core-signing-examples
+sortorder: 500011085
+toc: False
+sidebartitle: django.core signing
+meta: Python example code for the signing function from the django.core module of the Django project.
+
+
+signing is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / account / models.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/account/models.py)
+
+```python
+# models.py
+from __future__ import unicode_literals
+
+import datetime
+
+~~from django.core import signing
+from django.db import models, transaction
+from django.utils import timezone
+from django.utils.crypto import get_random_string
+from django.utils.translation import gettext_lazy as _
+
+from .. import app_settings as allauth_app_settings
+from . import app_settings, signals
+from .adapter import get_adapter
+from .managers import EmailAddressManager, EmailConfirmationManager
+from .utils import user_email
+
+
+class EmailAddress(models.Model):
+
+ user = models.ForeignKey(allauth_app_settings.USER_MODEL,
+ verbose_name=_('user'),
+ on_delete=models.CASCADE)
+ email = models.EmailField(unique=app_settings.UNIQUE_EMAIL,
+ max_length=app_settings.EMAIL_MAX_LENGTH,
+ verbose_name=_('e-mail address'))
+ verified = models.BooleanField(verbose_name=_('verified'), default=False)
+ primary = models.BooleanField(verbose_name=_('primary'), default=False)
+
+ objects = EmailAddressManager()
+
+
+## ... source file abbreviated to get to signing examples ...
+
+
+ email_address=email_address)
+ return email_address
+
+ def send(self, request=None, signup=False):
+ get_adapter(request).send_confirmation_mail(request, self, signup)
+ self.sent = timezone.now()
+ self.save()
+ signals.email_confirmation_sent.send(sender=self.__class__,
+ request=request,
+ confirmation=self,
+ signup=signup)
+
+
+class EmailConfirmationHMAC:
+
+ def __init__(self, email_address):
+ self.email_address = email_address
+
+ @property
+ def key(self):
+~~ return signing.dumps(
+ obj=self.email_address.pk,
+ salt=app_settings.SALT)
+
+ @classmethod
+ def from_key(cls, key):
+ try:
+ max_age = (
+ 60 * 60 * 24 * app_settings.EMAIL_CONFIRMATION_EXPIRE_DAYS)
+~~ pk = signing.loads(
+ key,
+ max_age=max_age,
+ salt=app_settings.SALT)
+ ret = EmailConfirmationHMAC(EmailAddress.objects.get(pk=pk))
+ except (signing.SignatureExpired,
+~~ signing.BadSignature,
+ EmailAddress.DoesNotExist):
+ ret = None
+ return ret
+
+ def confirm(self, request):
+ if not self.email_address.verified:
+ email_address = self.email_address
+ get_adapter(request).confirm_email(request, email_address)
+ signals.email_confirmed.send(sender=self.__class__,
+ request=request,
+ email_address=email_address)
+ return email_address
+
+ def send(self, request=None, signup=False):
+ get_adapter(request).send_confirmation_mail(request, self, signup)
+ signals.email_confirmation_sent.send(sender=self.__class__,
+ request=request,
+ confirmation=self,
+ signup=signup)
+
+
+
+## ... source file continues with no further signing examples...
+
+```
+
+
+## Example 2 from django-angular
+[django-angular](https://github.com/jrief/django-angular)
+([project examples website](https://django-angular.awesto.com/classic_form/))
+is a library with helper code to make it easier to use
+[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
+The code for django-angular is
+[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
+
+[**django-angular / djng / forms / fields.py**](https://github.com/jrief/django-angular/blob/master/djng/forms/fields.py)
+
+```python
+# fields.py
+import re
+import mimetypes
+
+from django.conf import settings
+from django.contrib.staticfiles.storage import staticfiles_storage
+~~from django.core import signing
+from django.core.exceptions import ImproperlyConfigured, ValidationError
+from django.core.files.storage import default_storage
+from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
+from django.urls import reverse_lazy
+from django.forms import fields, models as model_fields, widgets
+from django.utils.html import format_html
+from django.utils.module_loading import import_string
+from django.utils.safestring import mark_safe
+from django.utils.translation import ugettext_lazy as _, ungettext_lazy
+
+from djng import app_settings
+from .widgets import DropFileWidget, DropImageWidget
+
+
+class DefaultFieldMixin(object):
+ render_label = True
+
+ def has_subwidgets(self):
+ return False
+
+ def get_potential_errors(self):
+ return self.get_input_required_errors()
+
+ def get_input_required_errors(self):
+
+
+## ... source file abbreviated to get to signing examples ...
+
+
+ pass
+
+
+class TypedMultipleChoiceField(MultipleFieldMixin, fields.TypedMultipleChoiceField):
+ pass
+
+
+class UUIDField(DefaultFieldMixin, fields.UUIDField):
+ def get_potential_errors(self):
+ errors = self.get_input_required_errors()
+ errors.extend(self.get_min_max_length_errors())
+ return errors
+
+
+class FileFieldMixin(DefaultFieldMixin):
+ def to_python(self, value):
+ try:
+ current_file = None
+ if ':' in value['current_file']:
+ current_file = self.signer.unsign(value['current_file'])
+~~ except signing.BadSignature:
+ raise ValidationError("Got bogus upstream data")
+ except (KeyError, TypeError):
+ pass
+
+ try:
+ obj = ''
+ if ':' in value['temp_name']:
+ temp_name = self.signer.unsign(value['temp_name'])
+ temp_file = self.storage.open(temp_name, 'rb')
+ file_size = self.storage.size(temp_name)
+ if file_size < settings.FILE_UPLOAD_MAX_MEMORY_SIZE:
+ obj = InMemoryUploadedFile(
+ file=temp_file,
+ field_name=None,
+ name=value['file_name'],
+ charset=value['charset'],
+ content_type=value['content_type'],
+ content_type_extra=value['content_type_extra'],
+ size=file_size,
+ )
+ else:
+ obj = TemporaryUploadedFile(
+ value['file_name'],
+ value['content_type'],
+ 0,
+ value['charset'],
+ content_type_extra=value['content_type_extra'],
+ )
+ while True:
+ chunk = temp_file.read(0x10000)
+ if not chunk:
+ break
+ obj.file.write(chunk)
+ obj.file.seek(0)
+ obj.file.size = file_size
+ self.storage.delete(temp_name)
+ self.remove_current(current_file)
+ elif value['temp_name'] == 'delete':
+ self.remove_current(current_file)
+~~ except signing.BadSignature:
+ raise ValidationError("Got bogus upstream data")
+ except (IOError, KeyError, TypeError):
+ obj = current_file
+ except Exception as excp:
+ raise ValidationError("File upload failed. {}: {}".format(excp.__class__.__name__, excp))
+ return obj
+
+ def remove_current(self, filename):
+ if filename:
+ default_storage.delete(filename)
+
+
+class FileField(FileFieldMixin, fields.FileField):
+ storage = app_settings.upload_storage
+~~ signer = signing.Signer()
+
+ def __init__(self, *args, **kwargs):
+ accept = kwargs.pop('accept', '*/*')
+ fileupload_url = kwargs.pop('fileupload_url', reverse_lazy('fileupload'))
+ area_label = kwargs.pop('area_label', _("Drop file here or click to upload"))
+ attrs = {
+ 'accept': accept,
+ 'ngf-pattern': accept,
+ }
+ kwargs.update(widget=DropFileWidget(area_label, fileupload_url, attrs=attrs))
+ super(FileField, self).__init__(*args, **kwargs)
+
+ @classmethod
+ def preview(cls, file_obj):
+ available_name = cls.storage.get_available_name(file_obj.name)
+ temp_name = cls.storage.save(available_name, file_obj)
+ extension = mimetypes.guess_extension(file_obj.content_type)
+ if extension:
+ extension = extension[1:]
+ else:
+ extension = '_blank'
+ icon_url = staticfiles_storage.url('djng/icons/{}.png'.format(extension))
+ return {
+ 'url': 'url({})'.format(icon_url),
+ 'temp_name': cls.signer.sign(temp_name),
+ 'file_name': file_obj.name,
+ 'file_size': file_obj.size,
+ 'charset': file_obj.charset,
+ 'content_type': file_obj.content_type,
+ 'content_type_extra': file_obj.content_type_extra,
+ }
+
+
+class ImageField(FileFieldMixin, fields.ImageField):
+ storage = app_settings.upload_storage
+~~ signer = signing.Signer()
+
+ def __init__(self, *args, **kwargs):
+ if 'easy_thumbnails' not in settings.INSTALLED_APPS:
+ raise ImproperlyConfigured("'djng.forms.fields.ImageField' requires 'easy-thubnails' to be installed")
+ accept = kwargs.pop('accept', 'image/*')
+ fileupload_url = kwargs.pop('fileupload_url', reverse_lazy('fileupload'))
+ area_label = kwargs.pop('area_label', _("Drop image here or click to upload"))
+ attrs = {
+ 'accept': accept,
+ 'ngf-pattern': accept,
+ }
+ kwargs.update(widget=DropImageWidget(area_label, fileupload_url, attrs=attrs))
+ super(ImageField, self).__init__(*args, **kwargs)
+
+ def remove_current(self, image_name):
+ from easy_thumbnails.models import Source, Thumbnail
+
+ try:
+ source = Source.objects.get(name=image_name)
+ for thumb in Thumbnail.objects.filter(source=source):
+ default_storage.delete(thumb.name)
+ thumb.delete()
+ source.delete()
+ except Source.DoesNotExist:
+
+
+## ... source file continues with no further signing examples...
+
+```
+
diff --git a/content/pages/examples/django/django-core-validators.markdown b/content/pages/examples/django/django-core-validators.markdown
new file mode 100644
index 000000000..3d795e521
--- /dev/null
+++ b/content/pages/examples/django/django-core-validators.markdown
@@ -0,0 +1,436 @@
+title: django.core validators code examples
+category: page
+slug: django-core-validators-examples
+sortorder: 500011086
+toc: False
+sidebartitle: django.core validators
+meta: Python example code for the validators function from the django.core module of the Django project.
+
+
+validators is a function within the django.core module of the Django project.
+
+
+## Example 1 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / account / forms.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py)
+
+```python
+# forms.py
+from __future__ import absolute_import
+
+import warnings
+from importlib import import_module
+
+from django import forms
+from django.contrib.auth.tokens import PasswordResetTokenGenerator
+from django.contrib.sites.shortcuts import get_current_site
+~~from django.core import exceptions, validators
+from django.urls import reverse
+from django.utils.translation import gettext, gettext_lazy as _, pgettext
+
+from ..utils import (
+ build_absolute_uri,
+ get_username_max_length,
+ set_form_field_order,
+)
+from . import app_settings
+from .adapter import get_adapter
+from .app_settings import AuthenticationMethod
+from .models import EmailAddress
+from .utils import (
+ filter_users_by_email,
+ get_user_model,
+ perform_login,
+ setup_user_email,
+ sync_user_email_addresses,
+ url_str_to_user_pk,
+ user_email,
+ user_pk_to_url_str,
+ user_username,
+)
+
+
+
+## ... source file abbreviated to get to validators examples ...
+
+
+ login = self.cleaned_data["login"]
+ if app_settings.AUTHENTICATION_METHOD == AuthenticationMethod.EMAIL:
+ credentials["email"] = login
+ elif (
+ app_settings.AUTHENTICATION_METHOD ==
+ AuthenticationMethod.USERNAME):
+ credentials["username"] = login
+ else:
+ if self._is_login_email(login):
+ credentials["email"] = login
+ credentials["username"] = login
+ credentials["password"] = self.cleaned_data["password"]
+ return credentials
+
+ def clean_login(self):
+ login = self.cleaned_data['login']
+ return login.strip()
+
+ def _is_login_email(self, login):
+ try:
+~~ validators.validate_email(login)
+ ret = True
+ except exceptions.ValidationError:
+ ret = False
+ return ret
+
+ def clean(self):
+ super(LoginForm, self).clean()
+ if self._errors:
+ return
+ credentials = self.user_credentials()
+ user = get_adapter(self.request).authenticate(
+ self.request,
+ **credentials)
+ if user:
+ self.user = user
+ else:
+ auth_method = app_settings.AUTHENTICATION_METHOD
+ if auth_method == app_settings.AuthenticationMethod.USERNAME_EMAIL:
+ login = self.cleaned_data['login']
+ if self._is_login_email(login):
+ auth_method = app_settings.AuthenticationMethod.EMAIL
+ else:
+ auth_method = app_settings.AuthenticationMethod.USERNAME
+ raise forms.ValidationError(
+
+
+## ... source file abbreviated to get to validators examples ...
+
+
+class BaseSignupForm(_base_signup_form_class()):
+ username = forms.CharField(label=_("Username"),
+ min_length=app_settings.USERNAME_MIN_LENGTH,
+ widget=forms.TextInput(
+ attrs={'placeholder':
+ _('Username'),
+ 'autofocus': 'autofocus'}))
+ email = forms.EmailField(widget=forms.TextInput(
+ attrs={'type': 'email',
+ 'placeholder': _('E-mail address')}))
+
+ def __init__(self, *args, **kwargs):
+ email_required = kwargs.pop('email_required',
+ app_settings.EMAIL_REQUIRED)
+ self.username_required = kwargs.pop('username_required',
+ app_settings.USERNAME_REQUIRED)
+ super(BaseSignupForm, self).__init__(*args, **kwargs)
+ username_field = self.fields['username']
+ username_field.max_length = get_username_max_length()
+ username_field.validators.append(
+~~ validators.MaxLengthValidator(username_field.max_length))
+ username_field.widget.attrs['maxlength'] = str(
+ username_field.max_length)
+
+ default_field_order = [
+ 'email',
+ 'email2', # ignored when not present
+ 'username',
+ 'password1',
+ 'password2' # ignored when not present
+ ]
+ if app_settings.SIGNUP_EMAIL_ENTER_TWICE:
+ self.fields["email2"] = forms.EmailField(
+ label=_("E-mail (again)"),
+ widget=forms.TextInput(
+ attrs={
+ 'type': 'email',
+ 'placeholder': _('E-mail address confirmation')
+ }
+ )
+ )
+ if email_required:
+ self.fields['email'].label = gettext("E-mail")
+ self.fields['email'].required = True
+ else:
+
+
+## ... source file continues with no further validators examples...
+
+```
+
+
+## Example 2 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / utils / field_mapping.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/utils/field_mapping.py)
+
+```python
+# field_mapping.py
+import inspect
+
+~~from django.core import validators
+from django.db import models
+from django.utils.text import capfirst
+
+from rest_framework.compat import postgres_fields
+from rest_framework.validators import UniqueValidator
+
+NUMERIC_FIELD_TYPES = (
+ models.IntegerField, models.FloatField, models.DecimalField, models.DurationField,
+)
+
+
+class ClassLookupDict:
+ def __init__(self, mapping):
+ self.mapping = mapping
+
+ def __getitem__(self, key):
+ if hasattr(key, '_proxy_class'):
+ base_class = key._proxy_class
+ else:
+ base_class = key.__class__
+
+ for cls in inspect.getmro(base_class):
+ if cls in self.mapping:
+ return self.mapping[cls]
+
+
+## ... source file abbreviated to get to validators examples ...
+
+
+ if isinstance(model_field, models.FilePathField):
+ kwargs['path'] = model_field.path
+
+ if model_field.match is not None:
+ kwargs['match'] = model_field.match
+
+ if model_field.recursive is not False:
+ kwargs['recursive'] = model_field.recursive
+
+ if model_field.allow_files is not True:
+ kwargs['allow_files'] = model_field.allow_files
+
+ if model_field.allow_folders is not False:
+ kwargs['allow_folders'] = model_field.allow_folders
+
+ if model_field.choices:
+ kwargs['choices'] = model_field.choices
+ else:
+ max_value = next((
+ validator.limit_value for validator in validator_kwarg
+~~ if isinstance(validator, validators.MaxValueValidator)
+ ), None)
+ if max_value is not None and isinstance(model_field, NUMERIC_FIELD_TYPES):
+ kwargs['max_value'] = max_value
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if not isinstance(validator, validators.MaxValueValidator)
+ ]
+
+ min_value = next((
+ validator.limit_value for validator in validator_kwarg
+~~ if isinstance(validator, validators.MinValueValidator)
+ ), None)
+ if min_value is not None and isinstance(model_field, NUMERIC_FIELD_TYPES):
+ kwargs['min_value'] = min_value
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if not isinstance(validator, validators.MinValueValidator)
+ ]
+
+ if isinstance(model_field, models.URLField):
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if not isinstance(validator, validators.URLValidator)
+ ]
+
+ if isinstance(model_field, models.EmailField):
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if validator is not validators.validate_email
+ ]
+
+ if isinstance(model_field, models.SlugField):
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if validator is not validators.validate_slug
+ ]
+
+ if isinstance(model_field, models.GenericIPAddressField):
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if validator is not validators.validate_ipv46_address
+ ]
+ if isinstance(model_field, models.DecimalField):
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if not isinstance(validator, validators.DecimalValidator)
+ ]
+
+ max_length = getattr(model_field, 'max_length', None)
+ if max_length is not None and (isinstance(model_field, (models.CharField, models.TextField, models.FileField))):
+ kwargs['max_length'] = max_length
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if not isinstance(validator, validators.MaxLengthValidator)
+ ]
+
+ min_length = next((
+ validator.limit_value for validator in validator_kwarg
+~~ if isinstance(validator, validators.MinLengthValidator)
+ ), None)
+ if min_length is not None and isinstance(model_field, models.CharField):
+ kwargs['min_length'] = min_length
+ validator_kwarg = [
+ validator for validator in validator_kwarg
+~~ if not isinstance(validator, validators.MinLengthValidator)
+ ]
+
+ if getattr(model_field, 'unique', False):
+ unique_error_message = model_field.error_messages.get('unique', None)
+ if unique_error_message:
+ unique_error_message = unique_error_message % {
+ 'model_name': model_field.model._meta.verbose_name,
+ 'field_label': model_field.verbose_name
+ }
+ validator = UniqueValidator(
+ queryset=model_field.model._default_manager,
+ message=unique_error_message)
+ validator_kwarg.append(validator)
+
+ if validator_kwarg:
+ kwargs['validators'] = validator_kwarg
+
+ return kwargs
+
+
+def get_relation_kwargs(field_name, relation_info):
+ model_field, related_model, to_many, to_field, has_through_model, reverse = relation_info
+ kwargs = {
+ 'queryset': related_model._default_manager,
+
+
+## ... source file continues with no further validators examples...
+
+```
+
+
+## Example 3 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / forms.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./forms.py)
+
+```python
+# forms.py
+ "UserUpdateForm",
+ "WikiSlugField",
+ "SpamProtectionMixin",
+ "CreateRootForm",
+ "MoveForm",
+ "EditForm",
+ "SelectWidgetBootstrap",
+ "TextInputPrepend",
+ "CreateForm",
+ "DeleteForm",
+ "PermissionsForm",
+ "DirFilterForm",
+ "SearchForm",
+]
+
+from datetime import timedelta
+
+from django import forms
+from django.apps import apps
+from django.contrib.auth import get_user_model
+~~from django.core import validators
+from django.core.validators import RegexValidator
+from django.forms.widgets import HiddenInput
+from django.shortcuts import get_object_or_404
+from django.urls import Resolver404, resolve
+from django.utils import timezone
+from django.utils.safestring import mark_safe
+from django.utils.translation import gettext, gettext_lazy as _, pgettext_lazy
+from wiki import models
+from wiki.conf import settings
+from wiki.core import permissions
+from wiki.core.diff import simple_merge
+from wiki.core.plugins.base import PluginSettingsFormMixin
+from wiki.editors import getEditor
+
+from .forms_account_handling import UserCreationForm, UserUpdateForm
+
+validate_slug_numbers = RegexValidator(
+ r"^[0-9]+$",
+ _("A 'slug' cannot consist solely of numbers."),
+ "invalid",
+ inverse_match=True,
+)
+
+
+class WikiSlugField(forms.CharField):
+
+ default_validators = [validators.validate_slug, validate_slug_numbers]
+
+ def __init__(self, *args, **kwargs):
+ self.allow_unicode = kwargs.pop("allow_unicode", False)
+ if self.allow_unicode:
+ self.default_validators = [
+~~ validators.validate_unicode_slug,
+ validate_slug_numbers,
+ ]
+ super().__init__(*args, **kwargs)
+
+
+def _clean_slug(slug, urlpath):
+ if slug.startswith("_"):
+ raise forms.ValidationError(gettext("A slug may not begin with an underscore."))
+ if slug == "admin":
+ raise forms.ValidationError(gettext("'admin' is not a permitted slug name."))
+
+ if settings.URL_CASE_SENSITIVE:
+ already_existing_slug = models.URLPath.objects.filter(slug=slug, parent=urlpath)
+ else:
+ slug = slug.lower()
+ already_existing_slug = models.URLPath.objects.filter(
+ slug__iexact=slug, parent=urlpath
+ )
+ if already_existing_slug:
+ already_urlpath = already_existing_slug[0]
+ if already_urlpath.article and already_urlpath.article.current_revision.deleted:
+ raise forms.ValidationError(
+ gettext('A deleted article with slug "%s" already exists.')
+ % already_urlpath.slug
+
+
+## ... source file continues with no further validators examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-backends-utils.markdown b/content/pages/examples/django/django-db-backends-utils.markdown
new file mode 100644
index 000000000..0a182a01b
--- /dev/null
+++ b/content/pages/examples/django/django-db-backends-utils.markdown
@@ -0,0 +1,148 @@
+title: django.db.backends utils Example Code
+category: page
+slug: django-db-backends-utils-examples
+sortorder: 500011170
+toc: False
+sidebartitle: django.db.backends utils
+meta: Python example code for the utils callable from the django.db.backends module of the Django project.
+
+
+utils is a callable within the django.db.backends module of the Django project.
+
+
+## Example 1 from django-extensions
+[django-extensions](https://github.com/django-extensions/django-extensions)
+([project documentation](https://django-extensions.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-extensions/))
+is a [Django](/django.html) project that adds a bunch of additional
+useful commands to the `manage.py` interface. This
+[GoDjango video](https://www.youtube.com/watch?v=1F6G3ONhr4k) provides a
+quick overview of what you get when you install it into your Python
+environment.
+
+The django-extensions project is open sourced under the
+[MIT license](https://github.com/django-extensions/django-extensions/blob/master/LICENSE).
+
+[**django-extensions / django_extensions / management / debug_cursor.py**](https://github.com/django-extensions/django-extensions/blob/master/django_extensions/management/debug_cursor.py)
+
+```python
+# debug_cursor.py
+import six
+import time
+import traceback
+from contextlib import contextmanager
+
+import django
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db.backends import utils
+
+
+@contextmanager
+def monkey_patch_cursordebugwrapper(print_sql=None, print_sql_location=False, truncate=None, logger=six.print_, confprefix="DJANGO_EXTENSIONS"):
+ if not print_sql:
+ yield
+ else:
+ truncate = getattr(settings, '%s_PRINT_SQL_TRUNCATE' % confprefix, 1000)
+
+ sqlparse = None
+ if getattr(settings, '%s_SQLPARSE_ENABLED' % confprefix, True):
+ try:
+ import sqlparse
+
+ sqlparse_format_kwargs_defaults = dict(
+ reindent_aligned=True,
+ truncate_strings=500,
+ )
+ sqlparse_format_kwargs = getattr(settings, '%s_SQLPARSE_FORMAT_KWARGS' % confprefix, sqlparse_format_kwargs_defaults)
+ except ImportError:
+ sqlparse = None
+
+ pygments = None
+ if getattr(settings, '%s_PYGMENTS_ENABLED' % confprefix, True):
+ try:
+ import pygments.lexers
+ import pygments.formatters
+
+ pygments_formatter = getattr(settings, '%s_PYGMENTS_FORMATTER' % confprefix, pygments.formatters.TerminalFormatter)
+ pygments_formatter_kwargs = getattr(settings, '%s_PYGMENTS_FORMATTER_KWARGS' % confprefix, {})
+ except ImportError:
+ pass
+
+ class PrintQueryWrapperMixin:
+ def execute(self, sql, params=()):
+ starttime = time.time()
+ try:
+~~ return utils.CursorWrapper.execute(self, sql, params)
+ finally:
+ execution_time = time.time() - starttime
+ raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
+ if truncate:
+ raw_sql = raw_sql[:truncate]
+
+ if sqlparse:
+ raw_sql = sqlparse.format(raw_sql, **sqlparse_format_kwargs)
+
+ if pygments:
+ raw_sql = pygments.highlight(
+ raw_sql,
+ pygments.lexers.get_lexer_by_name("sql"),
+ pygments_formatter(**pygments_formatter_kwargs),
+ )
+
+ logger(raw_sql)
+ logger("Execution time: %.6fs [Database: %s]" % (execution_time, self.db.alias))
+ if print_sql_location:
+ logger("Location of SQL Call:")
+ logger(''.join(traceback.format_stack()))
+
+~~ _CursorDebugWrapper = utils.CursorDebugWrapper
+
+ class PrintCursorQueryWrapper(PrintQueryWrapperMixin, _CursorDebugWrapper):
+ pass
+
+ try:
+ from django.db import connections
+ _force_debug_cursor = {}
+ for connection_name in connections:
+ _force_debug_cursor[connection_name] = connections[connection_name].force_debug_cursor
+ except Exception:
+ connections = None
+
+~~ utils.CursorDebugWrapper = PrintCursorQueryWrapper
+
+ postgresql_base = None
+ if django.VERSION >= (3, 0):
+ try:
+ from django.db.backends.postgresql import base as postgresql_base
+ _PostgreSQLCursorDebugWrapper = postgresql_base.CursorDebugWrapper
+
+ class PostgreSQLPrintCursorDebugWrapper(PrintQueryWrapperMixin, _PostgreSQLCursorDebugWrapper):
+ pass
+ except (ImproperlyConfigured, TypeError):
+ postgresql_base = None
+
+ if postgresql_base:
+ postgresql_base.CursorDebugWrapper = PostgreSQLPrintCursorDebugWrapper
+
+ if connections:
+ for connection_name in connections:
+ connections[connection_name].force_debug_cursor = True
+
+ yield
+
+~~ utils.CursorDebugWrapper = _CursorDebugWrapper
+
+ if postgresql_base:
+ postgresql_base.CursorDebugWrapper = _PostgreSQLCursorDebugWrapper
+
+ if connections:
+ for connection_name in connections:
+ connections[connection_name].force_debug_cursor = _force_debug_cursor[connection_name]
+
+
+
+## ... source file continues with no further utils examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-connection.markdown b/content/pages/examples/django/django-db-connection.markdown
new file mode 100644
index 000000000..31ec9447d
--- /dev/null
+++ b/content/pages/examples/django/django-db-connection.markdown
@@ -0,0 +1,486 @@
+title: django.db connection Example Code
+category: page
+slug: django-db-connection-examples
+sortorder: 500011164
+toc: False
+sidebartitle: django.db connection
+meta: Python example code for the connection callable from the django.db module of the Django project.
+
+
+connection is a callable within the django.db module of the Django project.
+
+
+## Example 1 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / shortcuts.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./shortcuts.py)
+
+```python
+# shortcuts.py
+import warnings
+from collections import defaultdict
+from functools import partial
+from itertools import groupby
+
+from django.apps import apps
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import Group, Permission
+from django.contrib.contenttypes.models import ContentType
+~~from django.db import connection
+from django.db.models import Count, Q, QuerySet
+from django.shortcuts import _get_queryset
+from django.db.models.expressions import Value
+from django.db.models.functions import Cast, Replace
+from django.db.models import (
+ AutoField,
+ BigIntegerField,
+ CharField,
+ ForeignKey,
+ IntegerField,
+ PositiveIntegerField,
+ PositiveSmallIntegerField,
+ SmallIntegerField,
+ UUIDField,
+)
+from guardian.core import ObjectPermissionChecker
+from guardian.ctypes import get_content_type
+from guardian.exceptions import MixedContentTypeError, WrongAppError, MultipleIdentityAndObjectError
+from guardian.utils import get_anonymous_user, get_group_obj_perms_model, get_identity, get_user_obj_perms_model
+GroupObjectPermission = get_group_obj_perms_model()
+UserObjectPermission = get_user_obj_perms_model()
+
+
+def assign_perm(perm, user_or_group, obj=None):
+
+
+## ... source file abbreviated to get to connection examples ...
+
+
+
+ values = values.values_list(field_pk, flat=True)
+ return queryset.filter(pk__in=values)
+
+
+def _handle_pk_field(queryset):
+ pk = queryset.model._meta.pk
+
+ if isinstance(pk, ForeignKey):
+ return _handle_pk_field(pk.target_field)
+
+ if isinstance(
+ pk,
+ (
+ IntegerField,
+ AutoField,
+ BigIntegerField,
+ PositiveIntegerField,
+ PositiveSmallIntegerField,
+ SmallIntegerField,
+ ),
+ ):
+ return partial(Cast, output_field=BigIntegerField())
+
+ if isinstance(pk, UUIDField):
+~~ if connection.features.has_native_uuid_field:
+ return partial(Cast, output_field=UUIDField())
+ return partial(
+ Replace,
+ text=Value('-'),
+ replacement=Value(''),
+ output_field=CharField(),
+ )
+
+ return None
+
+
+
+## ... source file continues with no further connection examples...
+
+```
+
+
+## Example 2 from django-push-notifications
+[django-push-notifications](https://github.com/jazzband/django-push-notifications)
+is a [Django](/django.html) app for storing and interacting with
+push notification services such as
+[Google's Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
+and
+[Apple Notifications](https://developer.apple.com/notifications/).
+The django-push-notification project's source code is available
+open source under the
+[MIT license](https://github.com/jazzband/django-push-notifications/blob/master/LICENSE).
+
+[**django-push-notifications / push_notifications / fields.py**](https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/./fields.py)
+
+```python
+# fields.py
+import re
+import struct
+
+from django import forms
+from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
+~~from django.db import connection, models
+from django.utils.translation import ugettext_lazy as _
+
+
+__all__ = ["HexadecimalField", "HexIntegerField"]
+
+UNSIGNED_64BIT_INT_MIN_VALUE = 0
+UNSIGNED_64BIT_INT_MAX_VALUE = 2 ** 64 - 1
+
+
+hex_re = re.compile(r"^(([0-9A-f])|(0x[0-9A-f]))+$")
+signed_integer_engines = [
+ "django.db.backends.postgresql",
+ "django.db.backends.postgresql_psycopg2",
+ "django.contrib.gis.db.backends.postgis",
+ "django.db.backends.sqlite3"
+]
+
+
+def _using_signed_storage():
+~~ return connection.settings_dict["ENGINE"] in signed_integer_engines
+
+
+def _signed_to_unsigned_integer(value):
+ return struct.unpack("Q", struct.pack("q", value))[0]
+
+
+def _unsigned_to_signed_integer(value):
+ return struct.unpack("q", struct.pack("Q", value))[0]
+
+
+def _hex_string_to_unsigned_integer(value):
+ return int(value, 16)
+
+
+def _unsigned_integer_to_hex_string(value):
+ return hex(value).rstrip("L")
+
+
+class HexadecimalField(forms.CharField):
+ def __init__(self, *args, **kwargs):
+ self.default_validators = [
+ RegexValidator(hex_re, _("Enter a valid hexadecimal number"), "invalid")
+ ]
+ super(HexadecimalField, self).__init__(*args, **kwargs)
+
+ def prepare_value(self, value):
+ if value and not isinstance(value, str) \
+~~ and connection.vendor in ("mysql", "sqlite"):
+ value = _unsigned_integer_to_hex_string(value)
+ return super(forms.CharField, self).prepare_value(value)
+
+
+class HexIntegerField(models.BigIntegerField):
+
+ validators = [
+ MinValueValidator(UNSIGNED_64BIT_INT_MIN_VALUE),
+ MaxValueValidator(UNSIGNED_64BIT_INT_MAX_VALUE)
+ ]
+
+ def db_type(self, connection):
+~~ engine = connection.settings_dict["ENGINE"]
+ if "mysql" in engine:
+ return "bigint unsigned"
+ elif "sqlite" in engine:
+ return "UNSIGNED BIG INT"
+ else:
+ return super(HexIntegerField, self).db_type(connection=connection)
+
+ def get_prep_value(self, value):
+ if value is None or value == "":
+ return None
+ if isinstance(value, str):
+ value = _hex_string_to_unsigned_integer(value)
+ if _using_signed_storage():
+ value = _unsigned_to_signed_integer(value)
+ return value
+
+ def from_db_value(self, value, *args):
+ if value is None:
+ return value
+ if _using_signed_storage():
+ value = _signed_to_unsigned_integer(value)
+ return value
+
+ def to_python(self, value):
+
+
+## ... source file continues with no further connection examples...
+
+```
+
+
+## Example 3 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / views.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./views.py)
+
+```python
+# views.py
+from django.conf import settings
+from django.core.exceptions import PermissionDenied
+~~from django.db import connection, models, transaction
+from django.http import Http404
+from django.http.response import HttpResponseBase
+from django.utils.cache import cc_delim_re, patch_vary_headers
+from django.utils.encoding import smart_str
+from django.views.decorators.csrf import csrf_exempt
+from django.views.generic import View
+
+from rest_framework import exceptions, status
+from rest_framework.request import Request
+from rest_framework.response import Response
+from rest_framework.schemas import DefaultSchema
+from rest_framework.settings import api_settings
+from rest_framework.utils import formatting
+
+
+def get_view_name(view):
+ name = getattr(view, 'name', None)
+ if name is not None:
+ return name
+
+ name = view.__class__.__name__
+ name = formatting.remove_trailing_string(name, 'View')
+ name = formatting.remove_trailing_string(name, 'ViewSet')
+ name = formatting.camelcase_to_spaces(name)
+
+ suffix = getattr(view, 'suffix', None)
+ if suffix:
+ name += ' ' + suffix
+
+ return name
+
+
+def get_view_description(view, html=False):
+ description = getattr(view, 'description', None)
+ if description is None:
+ description = view.__class__.__doc__ or ''
+
+ description = formatting.dedent(smart_str(description))
+ if html:
+ return formatting.markup_description(description)
+ return description
+
+
+def set_rollback():
+~~ atomic_requests = connection.settings_dict.get('ATOMIC_REQUESTS', False)
+~~ if atomic_requests and connection.in_atomic_block:
+ transaction.set_rollback(True)
+
+
+def exception_handler(exc, context):
+ if isinstance(exc, Http404):
+ exc = exceptions.NotFound()
+ elif isinstance(exc, PermissionDenied):
+ exc = exceptions.PermissionDenied()
+
+ if isinstance(exc, exceptions.APIException):
+ headers = {}
+ if getattr(exc, 'auth_header', None):
+ headers['WWW-Authenticate'] = exc.auth_header
+ if getattr(exc, 'wait', None):
+ headers['Retry-After'] = '%d' % exc.wait
+
+ if isinstance(exc.detail, (list, dict)):
+ data = exc.detail
+ else:
+ data = {'detail': exc.detail}
+
+ set_rollback()
+ return Response(data, status=exc.status_code, headers=headers)
+
+
+
+## ... source file continues with no further connection examples...
+
+```
+
+
+## Example 4 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / apps.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./apps.py)
+
+```python
+# apps.py
+from django.apps import AppConfig
+~~from django.db import connections as djcs
+from django.core.exceptions import ImproperlyConfigured
+
+
+class ExplorerAppConfig(AppConfig):
+
+ name = 'explorer'
+
+ def ready(self):
+ from explorer.schema import build_async_schemas
+ _validate_connections()
+ build_async_schemas()
+
+
+def _get_default():
+ from explorer.app_settings import EXPLORER_DEFAULT_CONNECTION
+ return EXPLORER_DEFAULT_CONNECTION
+
+
+def _get_explorer_connections():
+ from explorer.app_settings import EXPLORER_CONNECTIONS
+ return EXPLORER_CONNECTIONS
+
+
+def _validate_connections():
+
+ if _get_default() not in _get_explorer_connections().values():
+ raise ImproperlyConfigured(
+ 'EXPLORER_DEFAULT_CONNECTION is %s, but that alias is not present in the values of EXPLORER_CONNECTIONS'
+ % _get_default())
+
+ for name, conn_name in _get_explorer_connections().items():
+ if conn_name not in djcs:
+ raise ImproperlyConfigured(
+~~ 'EXPLORER_CONNECTIONS contains (%s, %s), but %s is not a valid Django DB connection.'
+ % (name, conn_name, conn_name))
+
+
+
+## ... source file continues with no further connection examples...
+
+```
+
+
+## Example 5 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / managers.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/./managers.py)
+
+```python
+# managers.py
+import uuid
+from operator import attrgetter
+
+from django import VERSION
+from django.conf import settings
+from django.contrib.contenttypes.fields import GenericRelation
+from django.contrib.contenttypes.models import ContentType
+~~from django.db import connections, models, router
+from django.db.models import signals
+from django.db.models.fields.related import (
+ ManyToManyRel,
+ OneToOneRel,
+ RelatedField,
+ lazy_related_operation,
+)
+from django.db.models.query_utils import PathInfo
+from django.utils.text import capfirst
+from django.utils.translation import gettext_lazy as _
+
+from taggit.forms import TagField
+from taggit.models import (
+ CommonGenericTaggedItemBase,
+ GenericUUIDTaggedItemBase,
+ TaggedItem,
+)
+from taggit.utils import require_instance_manager
+
+
+class ExtraJoinRestriction:
+
+ contains_aggregate = False
+
+
+
+## ... source file abbreviated to get to connection examples ...
+
+
+ kwargs = extra_filters if extra_filters else {}
+ return self.through.tags_for(self.model, self.instance, **kwargs)
+
+ def get_prefetch_queryset(self, instances, queryset=None):
+ if queryset is not None:
+ raise ValueError("Custom queryset can't be used for this lookup.")
+
+ instance = instances[0]
+ db = self._db or router.db_for_read(type(instance), instance=instance)
+
+ fieldname = (
+ "object_id"
+ if issubclass(self.through, CommonGenericTaggedItemBase)
+ else "content_object"
+ )
+ fk = self.through._meta.get_field(fieldname)
+ query = {
+ "%s__%s__in"
+ % (self.through.tag_relname(), fk.name): {
+ obj._get_pk_val() for obj in instances
+ }
+ }
+ join_table = self.through._meta.db_table
+ source_col = fk.column
+ connection = connections[db]
+~~ qn = connection.ops.quote_name
+ qs = (
+ self.get_queryset(query)
+ .using(db)
+ .extra(
+ select={
+ "_prefetch_related_val": "{}.{}".format(
+ qn(join_table), qn(source_col)
+ )
+ }
+ )
+ )
+
+ if issubclass(self.through, GenericUUIDTaggedItemBase):
+
+ def uuid_rel_obj_attr(v):
+ value = attrgetter("_prefetch_related_val")(v)
+ if value is not None and not isinstance(value, uuid.UUID):
+ input_form = "int" if isinstance(value, int) else "hex"
+ value = uuid.UUID(**{input_form: value})
+ return value
+
+ rel_obj_attr = uuid_rel_obj_attr
+ else:
+ rel_obj_attr = attrgetter("_prefetch_related_val")
+
+
+## ... source file continues with no further connection examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-connections.markdown b/content/pages/examples/django/django-db-connections.markdown
new file mode 100644
index 000000000..5bf5bfe44
--- /dev/null
+++ b/content/pages/examples/django/django-db-connections.markdown
@@ -0,0 +1,138 @@
+title: django.db connections Example Code
+category: page
+slug: django-db-connections-examples
+sortorder: 500011165
+toc: False
+sidebartitle: django.db connections
+meta: Python example code for the connections callable from the django.db module of the Django project.
+
+
+connections is a callable within the django.db module of the Django project.
+
+
+## Example 1 from django-extensions
+[django-extensions](https://github.com/django-extensions/django-extensions)
+([project documentation](https://django-extensions.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-extensions/))
+is a [Django](/django.html) project that adds a bunch of additional
+useful commands to the `manage.py` interface. This
+[GoDjango video](https://www.youtube.com/watch?v=1F6G3ONhr4k) provides a
+quick overview of what you get when you install it into your Python
+environment.
+
+The django-extensions project is open sourced under the
+[MIT license](https://github.com/django-extensions/django-extensions/blob/master/LICENSE).
+
+[**django-extensions / django_extensions / management / debug_cursor.py**](https://github.com/django-extensions/django-extensions/blob/master/django_extensions/management/debug_cursor.py)
+
+```python
+# debug_cursor.py
+import six
+import time
+import traceback
+from contextlib import contextmanager
+
+import django
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+from django.db.backends import utils
+
+
+@contextmanager
+def monkey_patch_cursordebugwrapper(print_sql=None, print_sql_location=False, truncate=None, logger=six.print_, confprefix="DJANGO_EXTENSIONS"):
+ if not print_sql:
+ yield
+ else:
+ truncate = getattr(settings, '%s_PRINT_SQL_TRUNCATE' % confprefix, 1000)
+
+ sqlparse = None
+ if getattr(settings, '%s_SQLPARSE_ENABLED' % confprefix, True):
+ try:
+ import sqlparse
+
+ sqlparse_format_kwargs_defaults = dict(
+ reindent_aligned=True,
+ truncate_strings=500,
+ )
+ sqlparse_format_kwargs = getattr(settings, '%s_SQLPARSE_FORMAT_KWARGS' % confprefix, sqlparse_format_kwargs_defaults)
+ except ImportError:
+ sqlparse = None
+
+ pygments = None
+ if getattr(settings, '%s_PYGMENTS_ENABLED' % confprefix, True):
+ try:
+ import pygments.lexers
+
+
+## ... source file abbreviated to get to connections examples ...
+
+
+ if truncate:
+ raw_sql = raw_sql[:truncate]
+
+ if sqlparse:
+ raw_sql = sqlparse.format(raw_sql, **sqlparse_format_kwargs)
+
+ if pygments:
+ raw_sql = pygments.highlight(
+ raw_sql,
+ pygments.lexers.get_lexer_by_name("sql"),
+ pygments_formatter(**pygments_formatter_kwargs),
+ )
+
+ logger(raw_sql)
+ logger("Execution time: %.6fs [Database: %s]" % (execution_time, self.db.alias))
+ if print_sql_location:
+ logger("Location of SQL Call:")
+ logger(''.join(traceback.format_stack()))
+
+ _CursorDebugWrapper = utils.CursorDebugWrapper
+
+ class PrintCursorQueryWrapper(PrintQueryWrapperMixin, _CursorDebugWrapper):
+ pass
+
+ try:
+~~ from django.db import connections
+ _force_debug_cursor = {}
+~~ for connection_name in connections:
+ _force_debug_cursor[connection_name] = connections[connection_name].force_debug_cursor
+ except Exception:
+ connections = None
+
+ utils.CursorDebugWrapper = PrintCursorQueryWrapper
+
+ postgresql_base = None
+ if django.VERSION >= (3, 0):
+ try:
+ from django.db.backends.postgresql import base as postgresql_base
+ _PostgreSQLCursorDebugWrapper = postgresql_base.CursorDebugWrapper
+
+ class PostgreSQLPrintCursorDebugWrapper(PrintQueryWrapperMixin, _PostgreSQLCursorDebugWrapper):
+ pass
+ except (ImproperlyConfigured, TypeError):
+ postgresql_base = None
+
+ if postgresql_base:
+ postgresql_base.CursorDebugWrapper = PostgreSQLPrintCursorDebugWrapper
+
+ if connections:
+~~ for connection_name in connections:
+ connections[connection_name].force_debug_cursor = True
+
+ yield
+
+ utils.CursorDebugWrapper = _CursorDebugWrapper
+
+ if postgresql_base:
+ postgresql_base.CursorDebugWrapper = _PostgreSQLCursorDebugWrapper
+
+ if connections:
+~~ for connection_name in connections:
+ connections[connection_name].force_debug_cursor = _force_debug_cursor[connection_name]
+
+
+
+## ... source file continues with no further connections examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-databaseerror.markdown b/content/pages/examples/django/django-db-databaseerror.markdown
new file mode 100644
index 000000000..3af45c352
--- /dev/null
+++ b/content/pages/examples/django/django-db-databaseerror.markdown
@@ -0,0 +1,95 @@
+title: django.db DatabaseError Example Code
+category: page
+slug: django-db-databaseerror-examples
+sortorder: 500011160
+toc: False
+sidebartitle: django.db DatabaseError
+meta: Python example code for the DatabaseError class from the django.db module of the Django project.
+
+
+DatabaseError is a class within the django.db module of the Django project.
+
+
+## Example 1 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / tasks.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./tasks.py)
+
+```python
+# tasks.py
+from datetime import date, datetime, timedelta
+import random
+import string
+
+from django.core.mail import send_mail
+from django.core.cache import cache
+~~from django.db import DatabaseError
+
+from explorer import app_settings
+from explorer.exporters import get_exporter_class
+from explorer.models import Query, QueryLog
+
+if app_settings.ENABLE_TASKS:
+ from celery import task
+ from celery.utils.log import get_task_logger
+ from explorer.utils import s3_upload
+ logger = get_task_logger(__name__)
+else:
+ from explorer.utils import noop_decorator as task
+ import logging
+ logger = logging.getLogger(__name__)
+
+
+@task
+def execute_query(query_id, email_address):
+ q = Query.objects.get(pk=query_id)
+ send_mail('[SQL Explorer] Your query is running...',
+ '%s is running and should be in your inbox soon!' % q.title,
+ app_settings.FROM_EMAIL,
+ [email_address])
+
+ exporter = get_exporter_class('csv')(q)
+ random_part = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20))
+ try:
+ url = s3_upload('%s.csv' % random_part, exporter.get_file_output())
+ subj = '[SQL Explorer] Report "%s" is ready' % q.title
+ msg = 'Download results:\n\r%s' % url
+~~ except DatabaseError as e:
+ subj = '[SQL Explorer] Error running report %s' % q.title
+ msg = 'Error: %s\nPlease contact an administrator' % e
+ logger.warning('%s: %s' % (subj, e))
+ send_mail(subj, msg, app_settings.FROM_EMAIL, [email_address])
+
+
+@task
+def snapshot_query(query_id):
+ try:
+ logger.info("Starting snapshot for query %s..." % query_id)
+ q = Query.objects.get(pk=query_id)
+ exporter = get_exporter_class('csv')(q)
+ k = 'query-%s/snap-%s.csv' % (q.id, date.today().strftime('%Y%m%d-%H:%M:%S'))
+ logger.info("Uploading snapshot for query %s as %s..." % (query_id, k))
+ url = s3_upload(k, exporter.get_file_output())
+ logger.info("Done uploading snapshot for query %s. URL: %s" % (query_id, url))
+ except Exception as e:
+ logger.warning("Failed to snapshot query %s (%s). Retrying..." % (query_id, e))
+ snapshot_query.retry()
+
+
+@task
+def snapshot_queries():
+ logger.info("Starting query snapshots...")
+
+
+## ... source file continues with no further DatabaseError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-dataerror.markdown b/content/pages/examples/django/django-db-dataerror.markdown
new file mode 100644
index 000000000..70270fa35
--- /dev/null
+++ b/content/pages/examples/django/django-db-dataerror.markdown
@@ -0,0 +1,79 @@
+title: django.db DataError Example Code
+category: page
+slug: django-db-dataerror-examples
+sortorder: 500011159
+toc: False
+sidebartitle: django.db DataError
+meta: Python example code for the DataError class from the django.db module of the Django project.
+
+
+DataError is a class within the django.db module of the Django project.
+
+
+## Example 1 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / validators.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./validators.py)
+
+```python
+# validators.py
+~~from django.db import DataError
+from django.utils.translation import gettext_lazy as _
+
+from rest_framework.exceptions import ValidationError
+from rest_framework.utils.representation import smart_repr
+
+
+def qs_exists(queryset):
+ try:
+ return queryset.exists()
+~~ except (TypeError, ValueError, DataError):
+ return False
+
+
+def qs_filter(queryset, **kwargs):
+ try:
+ return queryset.filter(**kwargs)
+~~ except (TypeError, ValueError, DataError):
+ return queryset.none()
+
+
+class UniqueValidator:
+ message = _('This field must be unique.')
+ requires_context = True
+
+ def __init__(self, queryset, message=None, lookup='exact'):
+ self.queryset = queryset
+ self.message = message or self.message
+ self.lookup = lookup
+
+ def filter_queryset(self, value, queryset, field_name):
+ filter_kwargs = {'%s__%s' % (field_name, self.lookup): value}
+ return qs_filter(queryset, **filter_kwargs)
+
+ def exclude_current_instance(self, queryset, instance):
+ if instance is not None:
+ return queryset.exclude(pk=instance.pk)
+ return queryset
+
+ def __call__(self, value, serializer_field):
+ field_name = serializer_field.source_attrs[-1]
+ instance = getattr(serializer_field.parent, 'instance', None)
+
+
+## ... source file continues with no further DataError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-default-db-alias.markdown b/content/pages/examples/django/django-db-default-db-alias.markdown
new file mode 100644
index 000000000..762579708
--- /dev/null
+++ b/content/pages/examples/django/django-db-default-db-alias.markdown
@@ -0,0 +1,185 @@
+title: django.db DEFAULT_DB_ALIAS Example Code
+category: page
+slug: django-db-default-db-alias-examples
+sortorder: 500011158
+toc: False
+sidebartitle: django.db DEFAULT_DB_ALIAS
+meta: Python example code for the DEFAULT_DB_ALIAS constant from the django.db module of the Django project.
+
+
+DEFAULT_DB_ALIAS is a constant within the django.db module of the Django project.
+
+
+## Example 1 from AuditLog
+[Auditlog](https://github.com/jjkester/django-auditlog)
+([project documentation](https://django-auditlog.readthedocs.io/en/latest/))
+is a [Django](/django.html) app that logs changes to Python objects,
+similar to the Django admin's logs but with more details and
+output formats. Auditlog's source code is provided as open source under the
+[MIT license](https://github.com/jjkester/django-auditlog/blob/master/LICENSE).
+
+[**AuditLog / src / auditlog / models.py**](https://github.com/jjkester/django-auditlog/blob/master/src/auditlog/models.py)
+
+```python
+# models.py
+from __future__ import unicode_literals
+
+import json
+import ast
+
+from django.conf import settings
+from django.contrib.contenttypes.fields import GenericRelation
+from django.contrib.contenttypes.models import ContentType
+from django.core.exceptions import FieldDoesNotExist
+~~from django.db import models, DEFAULT_DB_ALIAS
+from django.db.models import QuerySet, Q
+from django.utils import formats, timezone
+from django.utils.encoding import python_2_unicode_compatible, smart_text
+from django.utils.six import iteritems, integer_types
+from django.utils.translation import ugettext_lazy as _
+
+from jsonfield.fields import JSONField
+from dateutil import parser
+from dateutil.tz import gettz
+
+
+class LogEntryManager(models.Manager):
+
+ def log_create(self, instance, **kwargs):
+ changes = kwargs.get('changes', None)
+ pk = self._get_pk_value(instance)
+
+ if changes is not None:
+ kwargs.setdefault('content_type', ContentType.objects.get_for_model(instance))
+ kwargs.setdefault('object_pk', pk)
+ kwargs.setdefault('object_repr', smart_text(instance))
+
+ if isinstance(pk, integer_types):
+ kwargs.setdefault('object_id', pk)
+
+
+## ... source file continues with no further DEFAULT_DB_ALIAS examples...
+
+```
+
+
+## Example 2 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / resources.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./resources.py)
+
+```python
+# resources.py
+import functools
+import logging
+import tablib
+import traceback
+from collections import OrderedDict
+from copy import deepcopy
+
+from diff_match_patch import diff_match_patch
+
+import django
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured, ValidationError
+from django.core.management.color import no_style
+from django.core.paginator import Paginator
+~~from django.db import DEFAULT_DB_ALIAS, connections
+from django.db.models.fields.related import ForeignObjectRel
+from django.db.models.query import QuerySet
+from django.db.transaction import (
+ TransactionManagementError,
+ atomic,
+ savepoint,
+ savepoint_commit,
+ savepoint_rollback
+)
+from django.utils.encoding import force_str
+from django.utils.safestring import mark_safe
+
+from . import widgets
+from .fields import Field
+from .instance_loaders import ModelInstanceLoader
+from .results import Error, Result, RowResult
+from .utils import atomic_if_using_transaction
+
+if django.VERSION[0] >= 3:
+ from django.core.exceptions import FieldDoesNotExist
+else:
+ from django.db.models.fields import FieldDoesNotExist
+
+
+
+
+## ... source file continues with no further DEFAULT_DB_ALIAS examples...
+
+```
+
+
+## Example 3 from django-migration-linter
+[django-migration-linter](https://github.com/3YOURMIND/django-migration-linter)
+([PyPI package information](https://pypi.org/project/django-migration-linter/))
+checks for backwards-incompatible changes in [Django ORM](/django-orm.html)
+schema migrations and warns you about them. The purpose of the project is
+to save time in older and larger projects by detecting field migrations
+that will be a problem so you do not run into issues later, and make it
+easier to enable continuous [deployment](/deployment.html) configurations
+with database changes. There is a
+[blog post on keeping Django database migrations backward compatible](https://medium.com/3yourmind/keeping-django-database-migrations-backward-compatible-727820260dbb)
+that goes into further detail on the tool.
+
+The django-migration-linter project is open sourced under the
+[Apache 2.0 license](https://github.com/3YOURMIND/django-migration-linter/blob/master/LICENSE).
+
+[**django-migration-linter / django_migration_linter / migration_linter.py**](https://github.com/3YOURMIND/django-migration-linter/blob/master/django_migration_linter/./migration_linter.py)
+
+```python
+# migration_linter.py
+from __future__ import print_function
+
+import hashlib
+import inspect
+import logging
+import os
+import re
+from subprocess import Popen, PIPE
+
+from django.conf import settings
+from django.core.management import call_command
+~~from django.db import DEFAULT_DB_ALIAS, connections, ProgrammingError
+from django.db.migrations import RunPython
+from enum import Enum, unique
+from six import PY2
+
+from .cache import Cache
+from .constants import (
+ DEFAULT_CACHE_PATH,
+ EXPECTED_DATA_MIGRATION_ARGS,
+ DJANGO_APPS_WITH_MIGRATIONS,
+)
+from .utils import clean_bytes_to_str, get_migration_abspath, split_migration_path
+from .operations import IgnoreMigration
+from .sql_analyser import analyse_sql_statements
+
+logger = logging.getLogger(__name__)
+
+
+@unique
+class MessageType(Enum):
+ OK = "ok"
+ IGNORE = "ignore"
+ WARNING = "warning"
+ ERROR = "error"
+
+
+
+## ... source file continues with no further DEFAULT_DB_ALIAS examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-integrityerror.markdown b/content/pages/examples/django/django-db-integrityerror.markdown
new file mode 100644
index 000000000..be8d2e5a2
--- /dev/null
+++ b/content/pages/examples/django/django-db-integrityerror.markdown
@@ -0,0 +1,97 @@
+title: django.db IntegrityError Example Code
+category: page
+slug: django-db-integrityerror-examples
+sortorder: 500011161
+toc: False
+sidebartitle: django.db IntegrityError
+meta: Python example code for the IntegrityError class from the django.db module of the Django project.
+
+
+IntegrityError is a class within the django.db module of the Django project.
+
+
+## Example 1 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / models.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/./models.py)
+
+```python
+# models.py
+from django.contrib.contenttypes.fields import GenericForeignKey
+from django.contrib.contenttypes.models import ContentType
+~~from django.db import IntegrityError, models, router, transaction
+from django.utils.text import slugify
+from django.utils.translation import gettext, gettext_lazy as _
+
+try:
+ from unidecode import unidecode
+except ImportError:
+
+ def unidecode(tag):
+ return tag
+
+
+class TagBase(models.Model):
+ name = models.CharField(verbose_name=_("name"), unique=True, max_length=100)
+ slug = models.SlugField(verbose_name=_("slug"), unique=True, max_length=100)
+
+ def __str__(self):
+ return self.name
+
+ def __gt__(self, other):
+ return self.name.lower() > other.name.lower()
+
+ def __lt__(self, other):
+ return self.name.lower() < other.name.lower()
+
+ class Meta:
+ abstract = True
+
+ def save(self, *args, **kwargs):
+ if self._state.adding and not self.slug:
+ self.slug = self.slugify(self.name)
+ using = kwargs.get("using") or router.db_for_write(
+ type(self), instance=self
+ )
+ kwargs["using"] = using
+ try:
+ with transaction.atomic(using=using):
+ res = super().save(*args, **kwargs)
+ return res
+~~ except IntegrityError:
+ pass
+ slugs = set(
+ type(self)
+ ._default_manager.filter(slug__startswith=self.slug)
+ .values_list("slug", flat=True)
+ )
+ i = 1
+ while True:
+ slug = self.slugify(self.name, i)
+ if slug not in slugs:
+ self.slug = slug
+ return super().save(*args, **kwargs)
+ i += 1
+ else:
+ return super().save(*args, **kwargs)
+
+ def slugify(self, tag, i=None):
+ slug = slugify(unidecode(tag))
+ if i is not None:
+ slug += "_%d" % i
+ return slug
+
+
+class Tag(TagBase):
+
+
+## ... source file continues with no further IntegrityError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-autodetector-migrationautodetector.markdown b/content/pages/examples/django/django-db-migrations-autodetector-migrationautodetector.markdown
new file mode 100644
index 000000000..70f3cd1d0
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-autodetector-migrationautodetector.markdown
@@ -0,0 +1,74 @@
+title: django.db.migrations.autodetector MigrationAutodetector Example Code
+category: page
+slug: django-db-migrations-autodetector-migrationautodetector-examples
+sortorder: 500011172
+toc: False
+sidebartitle: django.db.migrations.autodetector MigrationAutodetector
+meta: Python example code for the MigrationAutodetector class from the django.db.migrations.autodetector module of the Django project.
+
+
+MigrationAutodetector is a class within the django.db.migrations.autodetector module of the Django project.
+
+
+## Example 1 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / tests / test_models.py**](https://github.com/jazzband/django-axes/blob/master/axes/tests/test_models.py)
+
+```python
+# test_models.py
+from django.apps.registry import apps
+from django.db import connection
+~~from django.db.migrations.autodetector import MigrationAutodetector
+from django.db.migrations.executor import MigrationExecutor
+from django.db.migrations.state import ProjectState
+
+from axes.models import AccessAttempt, AccessLog
+from axes.tests.base import AxesTestCase
+
+
+class ModelsTestCase(AxesTestCase):
+ def setUp(self):
+ self.failures_since_start = 42
+
+ self.access_attempt = AccessAttempt(
+ failures_since_start=self.failures_since_start
+ )
+ self.access_log = AccessLog()
+
+ def test_access_attempt_str(self):
+ self.assertIn("Access", str(self.access_attempt))
+
+ def test_access_log_str(self):
+ self.assertIn("Access", str(self.access_log))
+
+
+class MigrationsTestCase(AxesTestCase):
+ def test_missing_migrations(self):
+ executor = MigrationExecutor(connection)
+~~ autodetector = MigrationAutodetector(
+ executor.loader.project_state(), ProjectState.from_apps(apps)
+ )
+
+ changes = autodetector.changes(graph=executor.loader.graph)
+
+ self.assertEqual({}, changes)
+
+
+
+## ... source file continues with no further MigrationAutodetector examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-exceptions-irreversibleerror.markdown b/content/pages/examples/django/django-db-migrations-exceptions-irreversibleerror.markdown
new file mode 100644
index 000000000..ecb9a015e
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-exceptions-irreversibleerror.markdown
@@ -0,0 +1,43 @@
+title: django.db.migrations.exceptions IrreversibleError Example Code
+category: page
+slug: django-db-migrations-exceptions-irreversibleerror-examples
+sortorder: 500011173
+toc: False
+sidebartitle: django.db.migrations.exceptions IrreversibleError
+meta: Python example code for the IrreversibleError class from the django.db.migrations.exceptions module of the Django project.
+
+
+IrreversibleError is a class within the django.db.migrations.exceptions module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / migrations / __init__.py**](https://github.com/divio/django-cms/blob/develop/cms/migrations/__init__.py)
+
+```python
+# __init__.py
+from django.db import migrations
+
+try:
+ IrreversibleError = migrations.Migration.IrreversibleError
+except AttributeError:
+~~ from django.db.migrations.exceptions import IrreversibleError
+
+
+class IrreversibleMigration(migrations.Migration):
+
+ def unapply(self, project_state, schema_editor, collect_sql=False):
+~~ raise IrreversibleError('Migration %s is not reversible' % self.name)
+
+
+
+## ... source file continues with no further IrreversibleError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-executor-migrationexecutor.markdown b/content/pages/examples/django/django-db-migrations-executor-migrationexecutor.markdown
new file mode 100644
index 000000000..2d2937edc
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-executor-migrationexecutor.markdown
@@ -0,0 +1,74 @@
+title: django.db.migrations.executor MigrationExecutor Example Code
+category: page
+slug: django-db-migrations-executor-migrationexecutor-examples
+sortorder: 500011174
+toc: False
+sidebartitle: django.db.migrations.executor MigrationExecutor
+meta: Python example code for the MigrationExecutor class from the django.db.migrations.executor module of the Django project.
+
+
+MigrationExecutor is a class within the django.db.migrations.executor module of the Django project.
+
+
+## Example 1 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / tests / test_models.py**](https://github.com/jazzband/django-axes/blob/master/axes/tests/test_models.py)
+
+```python
+# test_models.py
+from django.apps.registry import apps
+from django.db import connection
+from django.db.migrations.autodetector import MigrationAutodetector
+~~from django.db.migrations.executor import MigrationExecutor
+from django.db.migrations.state import ProjectState
+
+from axes.models import AccessAttempt, AccessLog
+from axes.tests.base import AxesTestCase
+
+
+class ModelsTestCase(AxesTestCase):
+ def setUp(self):
+ self.failures_since_start = 42
+
+ self.access_attempt = AccessAttempt(
+ failures_since_start=self.failures_since_start
+ )
+ self.access_log = AccessLog()
+
+ def test_access_attempt_str(self):
+ self.assertIn("Access", str(self.access_attempt))
+
+ def test_access_log_str(self):
+ self.assertIn("Access", str(self.access_log))
+
+
+class MigrationsTestCase(AxesTestCase):
+ def test_missing_migrations(self):
+~~ executor = MigrationExecutor(connection)
+ autodetector = MigrationAutodetector(
+ executor.loader.project_state(), ProjectState.from_apps(apps)
+ )
+
+ changes = autodetector.changes(graph=executor.loader.graph)
+
+ self.assertEqual({}, changes)
+
+
+
+## ... source file continues with no further MigrationExecutor examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-loader-migrationloader.markdown b/content/pages/examples/django/django-db-migrations-loader-migrationloader.markdown
new file mode 100644
index 000000000..46a09f2ee
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-loader-migrationloader.markdown
@@ -0,0 +1,129 @@
+title: django.db.migrations.loader MigrationLoader Example Code
+category: page
+slug: django-db-migrations-loader-migrationloader-examples
+sortorder: 500011176
+toc: False
+sidebartitle: django.db.migrations.loader MigrationLoader
+meta: Python example code for the MigrationLoader class from the django.db.migrations.loader module of the Django project.
+
+
+MigrationLoader is a class within the django.db.migrations.loader module of the Django project.
+
+
+## Example 1 from django-migration-linter
+[django-migration-linter](https://github.com/3YOURMIND/django-migration-linter)
+([PyPI package information](https://pypi.org/project/django-migration-linter/))
+checks for backwards-incompatible changes in [Django ORM](/django-orm.html)
+schema migrations and warns you about them. The purpose of the project is
+to save time in older and larger projects by detecting field migrations
+that will be a problem so you do not run into issues later, and make it
+easier to enable continuous [deployment](/deployment.html) configurations
+with database changes. There is a
+[blog post on keeping Django database migrations backward compatible](https://medium.com/3yourmind/keeping-django-database-migrations-backward-compatible-727820260dbb)
+that goes into further detail on the tool.
+
+The django-migration-linter project is open sourced under the
+[Apache 2.0 license](https://github.com/3YOURMIND/django-migration-linter/blob/master/LICENSE).
+
+[**django-migration-linter / django_migration_linter / migration_linter.py**](https://github.com/3YOURMIND/django-migration-linter/blob/master/django_migration_linter/./migration_linter.py)
+
+```python
+# migration_linter.py
+from __future__ import print_function
+
+import hashlib
+import inspect
+import logging
+import os
+import re
+from subprocess import Popen, PIPE
+
+from django.conf import settings
+from django.core.management import call_command
+from django.db import DEFAULT_DB_ALIAS, connections, ProgrammingError
+from django.db.migrations import RunPython
+from enum import Enum, unique
+from six import PY2
+
+from .cache import Cache
+from .constants import (
+ DEFAULT_CACHE_PATH,
+ EXPECTED_DATA_MIGRATION_ARGS,
+ DJANGO_APPS_WITH_MIGRATIONS,
+)
+from .utils import clean_bytes_to_str, get_migration_abspath, split_migration_path
+from .operations import IgnoreMigration
+from .sql_analyser import analyse_sql_statements
+
+logger = logging.getLogger(__name__)
+
+
+@unique
+class MessageType(Enum):
+ OK = "ok"
+ IGNORE = "ignore"
+ WARNING = "warning"
+ ERROR = "error"
+
+
+## ... source file abbreviated to get to MigrationLoader examples ...
+
+
+ exclude_migration_tests=None,
+ quiet=None,
+ warnings_as_errors=False,
+ ):
+ self.django_path = path
+ self.ignore_name_contains = ignore_name_contains
+ self.ignore_name = ignore_name or tuple()
+ self.include_apps = include_apps
+ self.exclude_apps = exclude_apps
+ self.exclude_migration_tests = exclude_migration_tests or []
+ self.database = database or DEFAULT_DB_ALIAS
+ self.cache_path = cache_path or DEFAULT_CACHE_PATH
+ self.no_cache = no_cache
+ self.only_applied_migrations = only_applied_migrations
+ self.only_unapplied_migrations = only_unapplied_migrations
+ self.quiet = quiet or []
+ self.warnings_as_errors = warnings_as_errors
+
+ self.reset_counters()
+
+ if self.should_use_cache():
+ self.old_cache = Cache(self.django_path, self.database, self.cache_path)
+ self.new_cache = Cache(self.django_path, self.database, self.cache_path)
+ self.old_cache.load()
+
+~~ from django.db.migrations.loader import MigrationLoader
+
+~~ self.migration_loader = MigrationLoader(
+ connection=connections[self.database], load=True
+ )
+
+ def reset_counters(self):
+ self.nb_valid = 0
+ self.nb_ignored = 0
+ self.nb_warnings = 0
+ self.nb_erroneous = 0
+ self.nb_total = 0
+
+ def should_use_cache(self):
+ return self.django_path and not self.no_cache
+
+ def lint_all_migrations(self, git_commit_id=None, migrations_file_path=None):
+ migrations_list = self.read_migrations_list(migrations_file_path)
+ if git_commit_id:
+ migrations = self._gather_migrations_git(git_commit_id, migrations_list)
+ else:
+ migrations = self._gather_all_migrations(migrations_list)
+
+ sorted_migrations = sorted(
+ migrations, key=lambda migration: (migration.app_label, migration.name)
+ )
+ for m in sorted_migrations:
+
+
+## ... source file continues with no further MigrationLoader examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-loader-migrations-module-name.markdown b/content/pages/examples/django/django-db-migrations-loader-migrations-module-name.markdown
new file mode 100644
index 000000000..fddea6c62
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-loader-migrations-module-name.markdown
@@ -0,0 +1,130 @@
+title: django.db.migrations.loader MIGRATIONS_MODULE_NAME Example Code
+category: page
+slug: django-db-migrations-loader-migrations-module-name-examples
+sortorder: 500011175
+toc: False
+sidebartitle: django.db.migrations.loader MIGRATIONS_MODULE_NAME
+meta: Python example code for the MIGRATIONS_MODULE_NAME constant from the django.db.migrations.loader module of the Django project.
+
+
+MIGRATIONS_MODULE_NAME is a constant within the django.db.migrations.loader module of the Django project.
+
+
+## Example 1 from django-migration-linter
+[django-migration-linter](https://github.com/3YOURMIND/django-migration-linter)
+([PyPI package information](https://pypi.org/project/django-migration-linter/))
+checks for backwards-incompatible changes in [Django ORM](/django-orm.html)
+schema migrations and warns you about them. The purpose of the project is
+to save time in older and larger projects by detecting field migrations
+that will be a problem so you do not run into issues later, and make it
+easier to enable continuous [deployment](/deployment.html) configurations
+with database changes. There is a
+[blog post on keeping Django database migrations backward compatible](https://medium.com/3yourmind/keeping-django-database-migrations-backward-compatible-727820260dbb)
+that goes into further detail on the tool.
+
+The django-migration-linter project is open sourced under the
+[Apache 2.0 license](https://github.com/3YOURMIND/django-migration-linter/blob/master/LICENSE).
+
+[**django-migration-linter / django_migration_linter / migration_linter.py**](https://github.com/3YOURMIND/django-migration-linter/blob/master/django_migration_linter/./migration_linter.py)
+
+```python
+# migration_linter.py
+from __future__ import print_function
+
+import hashlib
+import inspect
+import logging
+import os
+import re
+from subprocess import Popen, PIPE
+
+from django.conf import settings
+from django.core.management import call_command
+from django.db import DEFAULT_DB_ALIAS, connections, ProgrammingError
+from django.db.migrations import RunPython
+from enum import Enum, unique
+from six import PY2
+
+from .cache import Cache
+from .constants import (
+ DEFAULT_CACHE_PATH,
+ EXPECTED_DATA_MIGRATION_ARGS,
+ DJANGO_APPS_WITH_MIGRATIONS,
+)
+from .utils import clean_bytes_to_str, get_migration_abspath, split_migration_path
+from .operations import IgnoreMigration
+from .sql_analyser import analyse_sql_statements
+
+logger = logging.getLogger(__name__)
+
+
+@unique
+class MessageType(Enum):
+ OK = "ok"
+ IGNORE = "ignore"
+ WARNING = "warning"
+ ERROR = "error"
+
+
+## ... source file abbreviated to get to MIGRATIONS_MODULE_NAME examples ...
+
+
+ "Calling sqlmigrate command {} {}".format(app_label, migration_name)
+ )
+ dev_null = open(os.devnull, "w")
+ try:
+ sql_statement = call_command(
+ "sqlmigrate",
+ app_label,
+ migration_name,
+ database=self.database,
+ stdout=dev_null,
+ )
+ except (ValueError, ProgrammingError):
+ logger.warning(
+ (
+ "Error while executing sqlmigrate on (%s, %s). "
+ "Continuing execution with empty SQL."
+ ),
+ app_label,
+ migration_name,
+ )
+ sql_statement = ""
+ return sql_statement.splitlines()
+
+ @staticmethod
+ def is_migration_file(filename):
+~~ from django.db.migrations.loader import MIGRATIONS_MODULE_NAME
+
+ return (
+~~ re.search(r"/{0}/.*\.py".format(MIGRATIONS_MODULE_NAME), filename)
+ and "__init__" not in filename
+ )
+
+ @classmethod
+ def read_migrations_list(cls, migrations_file_path):
+ if not migrations_file_path:
+ return None
+
+ migrations = []
+ try:
+ with open(migrations_file_path, "r") as file:
+ for line in file:
+ if cls.is_migration_file(line):
+ app_label, name = split_migration_path(line)
+ migrations.append((app_label, name))
+ except IOError:
+ logger.exception("Migrations list path not found %s", migrations_file_path)
+ raise Exception("Error while reading migrations list file")
+
+ if not migrations:
+ logger.info(
+ "No valid migration paths found in the migrations file %s",
+ migrations_file_path,
+ )
+
+
+## ... source file continues with no further MIGRATIONS_MODULE_NAME examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-operations-base-operation.markdown b/content/pages/examples/django/django-db-migrations-operations-base-operation.markdown
new file mode 100644
index 000000000..151b91c0b
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-operations-base-operation.markdown
@@ -0,0 +1,57 @@
+title: django.db.migrations.operations.base Operation Example Code
+category: page
+slug: django-db-migrations-operations-base-operation-examples
+sortorder: 500011177
+toc: False
+sidebartitle: django.db.migrations.operations.base Operation
+meta: Python example code for the Operation class from the django.db.migrations.operations.base module of the Django project.
+
+
+Operation is a class within the django.db.migrations.operations.base module of the Django project.
+
+
+## Example 1 from django-migration-linter
+[django-migration-linter](https://github.com/3YOURMIND/django-migration-linter)
+([PyPI package information](https://pypi.org/project/django-migration-linter/))
+checks for backwards-incompatible changes in [Django ORM](/django-orm.html)
+schema migrations and warns you about them. The purpose of the project is
+to save time in older and larger projects by detecting field migrations
+that will be a problem so you do not run into issues later, and make it
+easier to enable continuous [deployment](/deployment.html) configurations
+with database changes. There is a
+[blog post on keeping Django database migrations backward compatible](https://medium.com/3yourmind/keeping-django-database-migrations-backward-compatible-727820260dbb)
+that goes into further detail on the tool.
+
+The django-migration-linter project is open sourced under the
+[Apache 2.0 license](https://github.com/3YOURMIND/django-migration-linter/blob/master/LICENSE).
+
+[**django-migration-linter / django_migration_linter / operations.py**](https://github.com/3YOURMIND/django-migration-linter/blob/master/django_migration_linter/./operations.py)
+
+```python
+# operations.py
+~~from django.db.migrations.operations.base import Operation
+
+
+~~class IgnoreMigration(Operation):
+
+ reversible = True
+ reduces_to_sql = False
+
+ def state_forwards(self, app_label, state):
+ pass
+
+ def database_forwards(self, app_label, schema_editor, from_state, to_state):
+ pass
+
+ def database_backwards(self, app_label, schema_editor, from_state, to_state):
+ pass
+
+ def describe(self):
+ return "The Django migration linter will ignore this migration"
+
+
+
+## ... source file continues with no further Operation examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-runpython.markdown b/content/pages/examples/django/django-db-migrations-runpython.markdown
new file mode 100644
index 000000000..ed25366f5
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-runpython.markdown
@@ -0,0 +1,129 @@
+title: django.db.migrations RunPython Example Code
+category: page
+slug: django-db-migrations-runpython-examples
+sortorder: 500011171
+toc: False
+sidebartitle: django.db.migrations RunPython
+meta: Python example code for the RunPython class from the django.db.migrations module of the Django project.
+
+
+RunPython is a class within the django.db.migrations module of the Django project.
+
+
+## Example 1 from django-migration-linter
+[django-migration-linter](https://github.com/3YOURMIND/django-migration-linter)
+([PyPI package information](https://pypi.org/project/django-migration-linter/))
+checks for backwards-incompatible changes in [Django ORM](/django-orm.html)
+schema migrations and warns you about them. The purpose of the project is
+to save time in older and larger projects by detecting field migrations
+that will be a problem so you do not run into issues later, and make it
+easier to enable continuous [deployment](/deployment.html) configurations
+with database changes. There is a
+[blog post on keeping Django database migrations backward compatible](https://medium.com/3yourmind/keeping-django-database-migrations-backward-compatible-727820260dbb)
+that goes into further detail on the tool.
+
+The django-migration-linter project is open sourced under the
+[Apache 2.0 license](https://github.com/3YOURMIND/django-migration-linter/blob/master/LICENSE).
+
+[**django-migration-linter / django_migration_linter / migration_linter.py**](https://github.com/3YOURMIND/django-migration-linter/blob/master/django_migration_linter/./migration_linter.py)
+
+```python
+# migration_linter.py
+from __future__ import print_function
+
+import hashlib
+import inspect
+import logging
+import os
+import re
+from subprocess import Popen, PIPE
+
+from django.conf import settings
+from django.core.management import call_command
+from django.db import DEFAULT_DB_ALIAS, connections, ProgrammingError
+~~from django.db.migrations import RunPython
+from enum import Enum, unique
+from six import PY2
+
+from .cache import Cache
+from .constants import (
+ DEFAULT_CACHE_PATH,
+ EXPECTED_DATA_MIGRATION_ARGS,
+ DJANGO_APPS_WITH_MIGRATIONS,
+)
+from .utils import clean_bytes_to_str, get_migration_abspath, split_migration_path
+from .operations import IgnoreMigration
+from .sql_analyser import analyse_sql_statements
+
+logger = logging.getLogger(__name__)
+
+
+@unique
+class MessageType(Enum):
+ OK = "ok"
+ IGNORE = "ignore"
+ WARNING = "warning"
+ ERROR = "error"
+
+ @staticmethod
+
+
+## ... source file abbreviated to get to RunPython examples ...
+
+
+ or (self.exclude_apps and app_label in self.exclude_apps)
+ or any(isinstance(o, IgnoreMigration) for o in operations)
+ or (
+ self.ignore_name_contains
+ and self.ignore_name_contains in migration_name
+ )
+ or (migration_name in self.ignore_name)
+ or (
+ self.only_applied_migrations
+ and (app_label, migration_name)
+ not in self.migration_loader.applied_migrations
+ )
+ or (
+ self.only_unapplied_migrations
+ and (app_label, migration_name)
+ in self.migration_loader.applied_migrations
+ )
+ )
+
+ def analyse_data_migration(self, migration):
+ errors = []
+ ignored = []
+ warnings = []
+
+ for operation in migration.operations:
+~~ if isinstance(operation, RunPython):
+ op_errors, op_ignored, op_warnings = self.lint_runpython(operation)
+ if op_errors:
+ errors += op_errors
+ if op_ignored:
+ ignored += op_ignored
+ if op_warnings:
+ warnings += op_warnings
+
+ return errors, ignored, warnings
+
+ def lint_runpython(self, runpython):
+ function_name = runpython.code.__name__
+ error = []
+ ignored = []
+ warning = []
+
+ if not runpython.reversible:
+ issue = {
+ "code": "REVERSIBLE_DATA_MIGRATION",
+ "msg": "'{}': RunPython data migration is not reversible".format(
+ function_name
+ ),
+ }
+ if issue["code"] in self.exclude_migration_tests:
+
+
+## ... source file continues with no further RunPython examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations-state-projectstate.markdown b/content/pages/examples/django/django-db-migrations-state-projectstate.markdown
new file mode 100644
index 000000000..6ae15a7dd
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations-state-projectstate.markdown
@@ -0,0 +1,74 @@
+title: django.db.migrations.state ProjectState Example Code
+category: page
+slug: django-db-migrations-state-projectstate-examples
+sortorder: 500011178
+toc: False
+sidebartitle: django.db.migrations.state ProjectState
+meta: Python example code for the ProjectState class from the django.db.migrations.state module of the Django project.
+
+
+ProjectState is a class within the django.db.migrations.state module of the Django project.
+
+
+## Example 1 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / tests / test_models.py**](https://github.com/jazzband/django-axes/blob/master/axes/tests/test_models.py)
+
+```python
+# test_models.py
+from django.apps.registry import apps
+from django.db import connection
+from django.db.migrations.autodetector import MigrationAutodetector
+from django.db.migrations.executor import MigrationExecutor
+~~from django.db.migrations.state import ProjectState
+
+from axes.models import AccessAttempt, AccessLog
+from axes.tests.base import AxesTestCase
+
+
+class ModelsTestCase(AxesTestCase):
+ def setUp(self):
+ self.failures_since_start = 42
+
+ self.access_attempt = AccessAttempt(
+ failures_since_start=self.failures_since_start
+ )
+ self.access_log = AccessLog()
+
+ def test_access_attempt_str(self):
+ self.assertIn("Access", str(self.access_attempt))
+
+ def test_access_log_str(self):
+ self.assertIn("Access", str(self.access_log))
+
+
+class MigrationsTestCase(AxesTestCase):
+ def test_missing_migrations(self):
+ executor = MigrationExecutor(connection)
+ autodetector = MigrationAutodetector(
+~~ executor.loader.project_state(), ProjectState.from_apps(apps)
+ )
+
+ changes = autodetector.changes(graph=executor.loader.graph)
+
+ self.assertEqual({}, changes)
+
+
+
+## ... source file continues with no further ProjectState examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-migrations.markdown b/content/pages/examples/django/django-db-migrations.markdown
new file mode 100644
index 000000000..9749b1cae
--- /dev/null
+++ b/content/pages/examples/django/django-db-migrations.markdown
@@ -0,0 +1,987 @@
+title: django.db migrations Example Code
+category: page
+slug: django-db-migrations-examples
+sortorder: 500011166
+toc: False
+sidebartitle: django.db migrations
+meta: Python example code for the migrations callable from the django.db module of the Django project.
+
+
+migrations is a callable within the django.db module of the Django project.
+
+
+## Example 1 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / migrations / 0004_auto_20181024_1538.py**](https://github.com/jazzband/django-axes/blob/master/axes/migrations/0004_auto_20181024_1538.py)
+
+```python
+# 0004_auto_20181024_1538.py
+~~from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("axes", "0003_auto_20160322_0929")]
+
+ operations = [
+~~ migrations.AlterModelOptions(
+ name="accessattempt",
+ options={
+ "verbose_name": "access attempt",
+ "verbose_name_plural": "access attempts",
+ },
+ ),
+~~ migrations.AlterModelOptions(
+ name="accesslog",
+ options={
+ "verbose_name": "access log",
+ "verbose_name_plural": "access logs",
+ },
+ ),
+~~ migrations.AlterField(
+ model_name="accessattempt",
+ name="attempt_time",
+ field=models.DateTimeField(auto_now_add=True, verbose_name="Attempt Time"),
+ ),
+~~ migrations.AlterField(
+ model_name="accessattempt",
+ name="user_agent",
+ field=models.CharField(
+ db_index=True, max_length=255, verbose_name="User Agent"
+ ),
+ ),
+~~ migrations.AlterField(
+ model_name="accessattempt",
+ name="username",
+ field=models.CharField(
+ db_index=True, max_length=255, null=True, verbose_name="Username"
+ ),
+ ),
+~~ migrations.AlterField(
+ model_name="accesslog",
+ name="attempt_time",
+ field=models.DateTimeField(auto_now_add=True, verbose_name="Attempt Time"),
+ ),
+~~ migrations.AlterField(
+ model_name="accesslog",
+ name="logout_time",
+ field=models.DateTimeField(
+ blank=True, null=True, verbose_name="Logout Time"
+ ),
+ ),
+~~ migrations.AlterField(
+ model_name="accesslog",
+ name="user_agent",
+ field=models.CharField(
+ db_index=True, max_length=255, verbose_name="User Agent"
+ ),
+ ),
+~~ migrations.AlterField(
+ model_name="accesslog",
+ name="username",
+ field=models.CharField(
+ db_index=True, max_length=255, null=True, verbose_name="Username"
+ ),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 2 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / migrations / 0008_auto_20150208_2149.py**](https://github.com/divio/django-cms/blob/develop/cms/migrations/0008_auto_20150208_2149.py)
+
+```python
+# 0008_auto_20150208_2149.py
+from __future__ import unicode_literals
+
+~~from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('cms', '0007_auto_20141028_1559'),
+ ]
+
+ operations = [
+~~ migrations.AlterField(
+ model_name='title',
+ name='redirect',
+ field=models.CharField(max_length=2048, null=True, verbose_name='redirect', blank=True),
+ preserve_default=True,
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 3 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / migrations / 0006_auto_20160623_1627.py**](https://github.com/divio/django-filer/blob/develop/filer/migrations/0006_auto_20160623_1627.py)
+
+```python
+# 0006_auto_20160623_1627.py
+from __future__ import unicode_literals
+
+import django.db.models.deletion
+~~from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('filer', '0005_auto_20160623_1425'),
+ ]
+
+ operations = [
+~~ migrations.AlterField(
+ model_name='image',
+ name='file_ptr',
+ field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, related_name='%(app_label)s_%(class)s_file', serialize=False, to='filer.File'),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 4 from django-flexible-subscriptions
+[django-flexible-subscriptions](https://github.com/studybuffalo/django-flexible-subscriptions)
+([project documentation](https://django-flexible-subscriptions.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-flexible-subscriptions/))
+provides boilerplate code for adding subscription and recurrent billing
+to [Django](/django.html) web applications. Various payment providers
+can be added on the back end to run the transactions.
+
+The django-flexible-subscriptions project is open sourced under the
+[GNU General Public License v3.0](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/LICENSE).
+
+[**django-flexible-subscriptions / subscriptions / migrations / 0006_add_slugs.py**](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/subscriptions/migrations/0006_add_slugs.py)
+
+```python
+# 0006_add_slugs.py
+
+~~from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('subscriptions', '0005_update_recurrence_unit_default'),
+ ]
+
+ operations = [
+~~ migrations.AddField(
+ model_name='plancost',
+ name='slug',
+ field=models.SlugField(
+ blank=True,
+ help_text='slug to reference these cost details',
+ max_length=128,
+ null=True,
+ unique=True,
+ ),
+ ),
+~~ migrations.AddField(
+ model_name='subscriptionplan',
+ name='slug',
+ field=models.SlugField(
+ blank=True,
+ help_text='slug to reference the subscription plan',
+ max_length=128,
+ null=True,
+ unique=True,
+ ),
+ ),
+~~ migrations.AddField(
+ model_name='planlist',
+ name='slug',
+ field=models.SlugField(
+ blank=True,
+ help_text='slug to reference the subscription plan list',
+ max_length=128,
+ null=True,
+ unique=True,
+ ),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 5 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / migrations / 0001_initial.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/migrations/0001_initial.py)
+
+```python
+# 0001_initial.py
+~~from django.db import models, migrations
+from django.conf import settings
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('contenttypes', '0001_initial'),
+ ('auth', '0001_initial'),
+~~ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+~~ migrations.CreateModel(
+ name='GroupObjectPermission',
+ fields=[
+ ('id', models.AutoField(primary_key=True,
+ serialize=False, auto_created=True, verbose_name='ID')),
+ ('object_pk', models.CharField(
+ max_length=255, verbose_name='object ID')),
+ ('content_type', models.ForeignKey(to='contenttypes.ContentType', on_delete=models.CASCADE)),
+ ('group', models.ForeignKey(to='auth.Group', on_delete=models.CASCADE)),
+ ('permission', models.ForeignKey(to='auth.Permission', on_delete=models.CASCADE)),
+ ],
+ options={
+ },
+ bases=(models.Model,),
+ ),
+~~ migrations.CreateModel(
+ name='UserObjectPermission',
+ fields=[
+ ('id', models.AutoField(primary_key=True,
+ serialize=False, auto_created=True, verbose_name='ID')),
+ ('object_pk', models.CharField(
+ max_length=255, verbose_name='object ID')),
+ ('content_type', models.ForeignKey(to='contenttypes.ContentType', on_delete=models.CASCADE)),
+ ('permission', models.ForeignKey(to='auth.Permission', on_delete=models.CASCADE)),
+ ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)),
+ ],
+ options={
+ },
+ bases=(models.Model,),
+ ),
+~~ migrations.AlterUniqueTogether(
+ name='userobjectpermission',
+ unique_together={('user', 'permission', 'object_pk')},
+ ),
+~~ migrations.AlterUniqueTogether(
+ name='groupobjectpermission',
+ unique_together={('group', 'permission', 'object_pk')},
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 6 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / migrations / 0002_delete_userdashboardmodule.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/migrations/0002_delete_userdashboardmodule.py)
+
+```python
+# 0002_delete_userdashboardmodule.py
+from __future__ import unicode_literals
+
+~~from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('jet', '0001_initial'),
+ ]
+
+ operations = [
+~~ migrations.DeleteModel(
+ name='UserDashboardModule',
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 7 from django-oauth-toolkit
+[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit)
+([project website](http://dot.evonove.it/) and
+[PyPI package information](https://pypi.org/project/django-oauth-toolkit/1.2.0/))
+is a code library for adding and handling [OAuth2](https://oauth.net/)
+flows within your [Django](/django.html) web application and
+[API](/application-programming-interfaces.html).
+
+The django-oauth-toolkit project is open sourced under the
+[FreeBSD license](https://github.com/jazzband/django-oauth-toolkit/blob/master/LICENSE)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-oauth-toolkit / oauth2_provider / migrations / 0001_initial.py**](https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/migrations/0001_initial.py)
+
+```python
+# 0001_initial.py
+from django.conf import settings
+import django.db.models.deletion
+~~from django.db import migrations, models
+
+import oauth2_provider.generators
+import oauth2_provider.validators
+from oauth2_provider.settings import oauth2_settings
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+~~ migrations.swappable_dependency(settings.AUTH_USER_MODEL)
+ ]
+
+ operations = [
+~~ migrations.CreateModel(
+ name='Application',
+ fields=[
+ ('id', models.BigAutoField(serialize=False, primary_key=True)),
+ ('client_id', models.CharField(default=oauth2_provider.generators.generate_client_id, unique=True, max_length=100, db_index=True)),
+ ('redirect_uris', models.TextField(help_text='Allowed URIs list, space separated', blank=True)),
+ ('client_type', models.CharField(max_length=32, choices=[('confidential', 'Confidential'), ('public', 'Public')])),
+ ('authorization_grant_type', models.CharField(max_length=32, choices=[('authorization-code', 'Authorization code'), ('implicit', 'Implicit'), ('password', 'Resource owner password-based'), ('client-credentials', 'Client credentials')])),
+ ('client_secret', models.CharField(default=oauth2_provider.generators.generate_client_secret, max_length=255, db_index=True, blank=True)),
+ ('name', models.CharField(max_length=255, blank=True)),
+ ('user', models.ForeignKey(related_name="oauth2_provider_application", blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)),
+ ('skip_authorization', models.BooleanField(default=False)),
+ ('created', models.DateTimeField(auto_now_add=True)),
+ ('updated', models.DateTimeField(auto_now=True)),
+ ],
+ options={
+ 'abstract': False,
+ 'swappable': 'OAUTH2_PROVIDER_APPLICATION_MODEL',
+ },
+ ),
+~~ migrations.CreateModel(
+ name='AccessToken',
+ fields=[
+ ('id', models.BigAutoField(serialize=False, primary_key=True)),
+ ('token', models.CharField(unique=True, max_length=255)),
+ ('expires', models.DateTimeField()),
+ ('scope', models.TextField(blank=True)),
+ ('application', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=oauth2_settings.APPLICATION_MODEL)),
+ ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='oauth2_provider_accesstoken', to=settings.AUTH_USER_MODEL)),
+ ('created', models.DateTimeField(auto_now_add=True)),
+ ('updated', models.DateTimeField(auto_now=True)),
+ ],
+ options={
+ 'abstract': False,
+ 'swappable': 'OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL',
+ },
+ ),
+~~ migrations.CreateModel(
+ name='Grant',
+ fields=[
+ ('id', models.BigAutoField(serialize=False, primary_key=True)),
+ ('code', models.CharField(unique=True, max_length=255)),
+ ('expires', models.DateTimeField()),
+ ('redirect_uri', models.CharField(max_length=255)),
+ ('scope', models.TextField(blank=True)),
+ ('application', models.ForeignKey(to=oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE)),
+ ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='oauth2_provider_grant', to=settings.AUTH_USER_MODEL)),
+ ('created', models.DateTimeField(auto_now_add=True)),
+ ('updated', models.DateTimeField(auto_now=True)),
+ ],
+ options={
+ 'abstract': False,
+ 'swappable': 'OAUTH2_PROVIDER_GRANT_MODEL',
+ },
+ ),
+~~ migrations.CreateModel(
+ name='RefreshToken',
+ fields=[
+ ('id', models.BigAutoField(serialize=False, primary_key=True)),
+ ('token', models.CharField(max_length=255)),
+ ('access_token', models.OneToOneField(blank=True, null=True, related_name="refresh_token", to=oauth2_settings.ACCESS_TOKEN_MODEL, on_delete=models.SET_NULL)),
+ ('application', models.ForeignKey(to=oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE)),
+ ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='oauth2_provider_refreshtoken', to=settings.AUTH_USER_MODEL)),
+ ('created', models.DateTimeField(auto_now_add=True)),
+ ('updated', models.DateTimeField(auto_now=True)),
+ ('revoked', models.DateTimeField(null=True)),
+ ],
+ options={
+ 'abstract': False,
+ 'swappable': 'OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL',
+ 'unique_together': set([("token", "revoked")]),
+ },
+ ),
+~~ migrations.AddField(
+ model_name='AccessToken',
+ name='source_refresh_token',
+ field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=oauth2_settings.REFRESH_TOKEN_MODEL, related_name="refreshed_access_token"),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 8 from django-push-notifications
+[django-push-notifications](https://github.com/jazzband/django-push-notifications)
+is a [Django](/django.html) app for storing and interacting with
+push notification services such as
+[Google's Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
+and
+[Apple Notifications](https://developer.apple.com/notifications/).
+The django-push-notification project's source code is available
+open source under the
+[MIT license](https://github.com/jazzband/django-push-notifications/blob/master/LICENSE).
+
+[**django-push-notifications / push_notifications / migrations / 0002_auto_20160106_0850.py**](https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/migrations/0002_auto_20160106_0850.py)
+
+```python
+# 0002_auto_20160106_0850.py
+~~from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('push_notifications', '0001_initial'),
+ ]
+
+ operations = [
+~~ migrations.AlterField(
+ model_name='apnsdevice',
+ name='registration_id',
+ field=models.CharField(max_length=200, unique=True, verbose_name='Registration ID'),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 9 from django-sitetree
+[django-sitetree](https://github.com/idlesign/django-sitetree)
+([project documentation](https://django-sitetree.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-sitetree/))
+is a [Django](/django.html) extension that makes it easier for
+developers to add site trees, menus and breadcrumb navigation elements
+to their web applications.
+
+The django-sitetree project is provided as open source under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/idlesign/django-sitetree/blob/master/LICENSE).
+
+[**django-sitetree / sitetree / migrations / 0001_initial.py**](https://github.com/idlesign/django-sitetree/blob/master/sitetree/migrations/0001_initial.py)
+
+```python
+# 0001_initial.py
+from __future__ import unicode_literals
+
+~~from django.db import models, migrations
+import sitetree.models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('auth', '0001_initial'),
+ ]
+
+ operations = [
+~~ migrations.CreateModel(
+ name='Tree',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('title', models.CharField(help_text='Site tree title for presentational purposes.', max_length=100, verbose_name='Title', blank=True)),
+ ('alias', models.CharField(help_text='Short name to address site tree from templates. Note: change with care.', unique=True, max_length=80, verbose_name='Alias', db_index=True)),
+ ],
+ options={
+ 'abstract': False,
+ 'verbose_name': 'Site Tree',
+ 'verbose_name_plural': 'Site Trees',
+ },
+ bases=(models.Model,),
+ ),
+~~ migrations.CreateModel(
+ name='TreeItem',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('title', models.CharField(help_text='Site tree item title. Can contain template variables E.g.: {{ mytitle }}.', max_length=100, verbose_name='Title')),
+ ('hint', models.CharField(default='', help_text='Some additional information about this item that is used as a hint.', max_length=200, verbose_name='Hint', blank=True)),
+ ('url', models.CharField(help_text='Exact URL or URL pattern (see "Additional settings") for this item.', max_length=200, verbose_name='URL', db_index=True)),
+ ('urlaspattern', models.BooleanField(default=False, help_text='Whether the given URL should be treated as a pattern. Note: Refer to Django "URL dispatcher" documentation (e.g. "Naming URL patterns" part).', db_index=True, verbose_name='URL as Pattern')),
+ ('hidden', models.BooleanField(default=False, help_text='Whether to show this item in navigation.', db_index=True, verbose_name='Hidden')),
+ ('alias', sitetree.models.CharFieldNullable(max_length=80, blank=True, help_text='Short name to address site tree item from a template. Reserved aliases: "trunk", "this-children", "this-siblings", "this-ancestor-children", "this-parent-siblings".', null=True, verbose_name='Alias', db_index=True)),
+ ('description', models.TextField(default='', help_text='Additional comments on this item.', verbose_name='Description', blank=True)),
+ ('inmenu', models.BooleanField(default=True, help_text='Whether to show this item in a menu.', db_index=True, verbose_name='Show in menu')),
+ ('inbreadcrumbs', models.BooleanField(default=True, help_text='Whether to show this item in a breadcrumb path.', db_index=True, verbose_name='Show in breadcrumb path')),
+ ('insitetree', models.BooleanField(default=True, help_text='Whether to show this item in a site tree.', db_index=True, verbose_name='Show in site tree')),
+ ('access_loggedin', models.BooleanField(default=False, help_text='Check it to grant access to this item to authenticated users only.', db_index=True, verbose_name='Logged in only')),
+ ('access_guest', models.BooleanField(default=False, help_text='Check it to grant access to this item to guests only.', db_index=True, verbose_name='Guests only')),
+ ('access_restricted', models.BooleanField(default=False, help_text='Check it to restrict user access to this item, using Django permissions system.', db_index=True, verbose_name='Restrict access to permissions')),
+ ('access_perm_type', models.IntegerField(default=1, help_text='Any — user should have any of chosen permissions. All — user should have all chosen permissions.', verbose_name='Permissions interpretation', choices=[(1, 'Any'), (2, 'All')])),
+ ('sort_order', models.IntegerField(default=0, help_text='Item position among other site tree items under the same parent.', verbose_name='Sort order', db_index=True)),
+ ('access_permissions', models.ManyToManyField(to='auth.Permission', verbose_name='Permissions granting access', blank=True)),
+ ('parent', models.ForeignKey(related_name='treeitem_parent', on_delete=models.CASCADE, blank=True, to='sitetree.TreeItem', help_text='Parent site tree item.', null=True, verbose_name='Parent')),
+ ('tree', models.ForeignKey(related_name='treeitem_tree', on_delete=models.CASCADE, verbose_name='Site Tree', to='sitetree.Tree', help_text='Site tree this item belongs to.')),
+ ],
+ options={
+ 'abstract': False,
+ 'verbose_name': 'Site Tree Item',
+ 'verbose_name_plural': 'Site Tree Items',
+ },
+ bases=(models.Model,),
+ ),
+~~ migrations.AlterUniqueTogether(
+ name='treeitem',
+ unique_together=set([('tree', 'alias')]),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 10 from django-smithy
+[django-smithy](https://github.com/jamiecounsell/django-smithy) is
+a [Django](/django.html) code library that allows users to send
+HTTP requests from the Django admin user interface. The code for
+the project is open source under the
+[MIT license](https://github.com/jamiecounsell/django-smithy/blob/master/LICENSE).
+
+[**django-smithy / smithy / migrations / 0004_auto_20190721_2012.py**](https://github.com/jamiecounsell/django-smithy/blob/master/smithy/migrations/0004_auto_20190721_2012.py)
+
+```python
+# 0004_auto_20190721_2012.py
+
+~~from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('smithy', '0003_auto_20190721_2004'),
+ ]
+
+ operations = [
+~~ migrations.AlterField(
+ model_name='bodyparameter',
+ name='value',
+ field=models.TextField(blank=True),
+ ),
+~~ migrations.AlterField(
+ model_name='cookie',
+ name='value',
+ field=models.TextField(blank=True),
+ ),
+~~ migrations.AlterField(
+ model_name='header',
+ name='value',
+ field=models.TextField(blank=True),
+ ),
+~~ migrations.AlterField(
+ model_name='queryparameter',
+ name='value',
+ field=models.TextField(blank=True),
+ ),
+~~ migrations.AlterField(
+ model_name='variable',
+ name='value',
+ field=models.TextField(blank=True),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 11 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / migrations / 0002_auto_20150501_1515.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/migrations/0002_auto_20150501_1515.py)
+
+```python
+# 0002_auto_20150501_1515.py
+from __future__ import unicode_literals
+
+~~from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('explorer', '0001_initial'),
+ ]
+
+ operations = [
+~~ migrations.RemoveField(
+ model_name='querylog',
+ name='is_playground',
+ ),
+~~ migrations.AlterField(
+ model_name='querylog',
+ name='sql',
+ field=models.TextField(null=True, blank=True),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 12 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / migrations / 0002_auto_20150616_2121.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/migrations/0002_auto_20150616_2121.py)
+
+```python
+# 0002_auto_20150616_2121.py
+~~from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("taggit", "0001_initial")]
+
+ operations = [
+~~ migrations.AlterIndexTogether(
+ name="taggeditem", index_together={("content_type", "object_id")}
+ )
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 13 from django-user-visit
+[django-user-visit](https://github.com/yunojuno/django-user-visit)
+([PyPI package information](https://pypi.org/project/django-user-visit/))
+is a [Django](/django.html) app and
+[middleware](https://docs.djangoproject.com/en/stable/topics/http/middleware/)
+for tracking daily user visits to your web application. The goal
+is to record per user per day instead of for every request a user
+sends to the application. The project is provided as open source
+under the
+[MIT license](https://github.com/yunojuno/django-user-visit/blob/master/LICENSE).
+
+[**django-user-visit / user_visit / migrations / 0002_add_created_at.py**](https://github.com/yunojuno/django-user-visit/blob/master/user_visit/migrations/0002_add_created_at.py)
+
+```python
+# 0002_add_created_at.py
+import django.utils.timezone
+~~from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("user_visit", "0001_initial"),
+ ]
+
+ operations = [
+~~ migrations.AlterModelOptions(
+ name="uservisit", options={"get_latest_by": "timestamp"},
+ ),
+~~ migrations.AddField(
+ model_name="uservisit",
+ name="created_at",
+ field=models.DateTimeField(
+ auto_now_add=True,
+ default=django.utils.timezone.now,
+ help_text=(
+ "The time at which the database record was created (!=timestamp)"
+ ),
+ ),
+ preserve_default=False,
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 14 from django-webshell
+[django-webshell](https://github.com/onrik/django-webshell) is an extension
+for executing arbitrary code in the
+[Django admin](https://docs.djangoproject.com/en/stable/ref/contrib/admin/),
+similar to how you can run code by using the `django manage.py shell`
+command from the terminal.
+
+The django-webshell project is provided as open source under the
+[MIT license](https://github.com/onrik/django-webshell/blob/master/LICENSE).
+
+[**django-webshell / webshell / migrations / 0001_initial.py**](https://github.com/onrik/django-webshell/blob/master/webshell/migrations/0001_initial.py)
+
+```python
+# 0001_initial.py
+from __future__ import unicode_literals
+
+~~from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ]
+
+ operations = [
+~~ migrations.CreateModel(
+ name='Script',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('name', models.CharField(max_length=100, verbose_name='Name')),
+ ('source', models.TextField(verbose_name='Source')),
+ ],
+ options={
+ 'verbose_name': 'Script',
+ 'verbose_name_plural': 'Scripts',
+ },
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 15 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / migrations / 0002_urlpath_moved_to.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/migrations/0002_urlpath_moved_to.py)
+
+```python
+# 0002_urlpath_moved_to.py
+import django.db.models.deletion
+import mptt.fields
+~~from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("wiki", "0001_initial"),
+ ]
+
+ operations = [
+~~ migrations.AddField(
+ model_name="urlpath",
+ name="moved_to",
+ field=mptt.fields.TreeForeignKey(
+ blank=True,
+ help_text="Article path was moved to this location",
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name="moved_from",
+ to="wiki.URLPath",
+ verbose_name="Moved to",
+ ),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 16 from dmd-interpreter
+[dmd-interpreter](https://github.com/mitchalexbailey/dmd-interpreter)
+([running web app](http://www.dmd.nl/DOVE))
+is a Python tool to aggregate clinically relevant information related
+to variants in the DMD gene and display that [data](/data.html) to a user
+with a [Django](/django.html) web application.
+
+[**dmd-interpreter / interpreter / migrations / 0001_initial.py**](https://github.com/mitchalexbailey/dmd-interpreter/blob/master/interpreter/migrations/0001_initial.py)
+
+```python
+# 0001_initial.py
+from __future__ import unicode_literals
+
+~~from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ]
+
+ operations = [
+~~ migrations.CreateModel(
+ name='Choice',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('choice_text', models.CharField(max_length=200)),
+ ('votes', models.IntegerField(default=0)),
+ ],
+ ),
+~~ migrations.CreateModel(
+ name='Question',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('question_text', models.CharField(max_length=200)),
+ ('pub_date', models.DateTimeField(verbose_name=b'Date Published')),
+ ],
+ ),
+~~ migrations.AddField(
+ model_name='choice',
+ name='question',
+ field=models.ForeignKey(to='interpreter.Question'),
+ ),
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
+
+## Example 17 from elasticsearch-django
+[elasticsearch-django](https://github.com/yunojuno/elasticsearch-django)
+([PyPI package information](https://pypi.org/project/elasticsearch-django/))
+is a [Django](/django.html) app for managing
+[ElasticSearch](https://github.com/elastic/elasticsearch) indexes
+populated by [Django ORM](/django-orm.html) models. The project is
+available as open source under the
+[MIT license](https://github.com/yunojuno/elasticsearch-django/blob/master/LICENSE).
+
+[**elasticsearch-django / elasticsearch_django / migrations / 0009_searchquery_query_type.py**](https://github.com/yunojuno/elasticsearch-django/blob/master/elasticsearch_django/migrations/0009_searchquery_query_type.py)
+
+```python
+# 0009_searchquery_query_type.py
+
+~~from django.db import migrations, models
+
+from ..models import SearchQuery
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("elasticsearch_django", "0008_searchquery_search_terms")]
+
+ operations = [
+~~ migrations.AddField(
+ model_name="searchquery",
+ name="query_type",
+ field=models.CharField(
+ choices=(lambda: SearchQuery.QUERY_TYPE_CHOICES)(),
+ default="SEARCH",
+ help_text="Does this query return results, or just the hit count?",
+ max_length=10,
+ ),
+ )
+ ]
+
+
+
+## ... source file continues with no further migrations examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-baseiterable.markdown b/content/pages/examples/django/django-db-models-query-baseiterable.markdown
new file mode 100644
index 000000000..5271e2e6e
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-baseiterable.markdown
@@ -0,0 +1,101 @@
+title: django.db.models.query BaseIterable Example Code
+category: page
+slug: django-db-models-query-baseiterable-examples
+sortorder: 500011238
+toc: False
+sidebartitle: django.db.models.query BaseIterable
+meta: Python example code for the BaseIterable class from the django.db.models.query module of the Django project.
+
+
+BaseIterable is a class within the django.db.models.query module of the Django project.
+
+
+## Example 1 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / query.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/query.py)
+
+```python
+# query.py
+import posixpath
+import warnings
+from collections import defaultdict
+
+from django.apps import apps
+from django.contrib.contenttypes.models import ContentType
+from django.db.models import CharField, Q
+from django.db.models.functions import Length, Substr
+~~from django.db.models.query import BaseIterable
+from treebeard.mp_tree import MP_NodeQuerySet
+
+from wagtail.search.queryset import SearchableQuerySetMixin
+
+
+class TreeQuerySet(MP_NodeQuerySet):
+ def delete(self):
+ super().delete()
+
+ delete.queryset_only = True
+
+ def descendant_of_q(self, other, inclusive=False):
+ q = Q(path__startswith=other.path) & Q(depth__gte=other.depth)
+
+ if not inclusive:
+ q &= -Q(pk=other.pk)
+
+ return q
+
+ def descendant_of(self, other, inclusive=False):
+ return self.filter(self.descendant_of_q(other, inclusive))
+
+ def not_descendant_of(self, other, inclusive=False):
+ return self.exclude(self.descendant_of_q(other, inclusive))
+
+
+## ... source file abbreviated to get to BaseIterable examples ...
+
+
+
+ if missing_pks:
+ generic_pages = Page.objects.filter(pk__in=missing_pks).select_related('content_type').in_bulk()
+ warnings.warn(
+ "Specific versions of the following pages could not be found. "
+ "This is most likely because a database migration has removed "
+ "the relevant table or record since the page was created:\n{}".format([
+ {'id': p.id, 'title': p.title, 'type': p.content_type}
+ for p in generic_pages.values()
+ ]), category=RuntimeWarning
+ )
+ else:
+ generic_pages = {}
+
+ for pk, content_type in pks_and_types:
+ try:
+ page = pages_by_type[content_type][pk]
+ except KeyError:
+ page = generic_pages[pk]
+ if annotation_aliases:
+ for annotation, value in annotations_by_pk.get(page.pk, {}).items():
+ setattr(page, annotation, value)
+ yield page
+
+
+~~class SpecificIterable(BaseIterable):
+ def __iter__(self):
+ return specific_iterator(self.queryset)
+
+
+~~class DeferredSpecificIterable(BaseIterable):
+ def __iter__(self):
+ return specific_iterator(self.queryset, defer=True)
+
+
+
+## ... source file continues with no further BaseIterable examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-emptyqueryset.markdown b/content/pages/examples/django/django-db-models-query-emptyqueryset.markdown
new file mode 100644
index 000000000..aa5c5ea3a
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-emptyqueryset.markdown
@@ -0,0 +1,195 @@
+title: django.db.models.query EmptyQuerySet Example Code
+category: page
+slug: django-db-models-query-emptyqueryset-examples
+sortorder: 500011239
+toc: False
+sidebartitle: django.db.models.query EmptyQuerySet
+meta: Python example code for the EmptyQuerySet class from the django.db.models.query module of the Django project.
+
+
+EmptyQuerySet is a class within the django.db.models.query module of the Django project.
+
+
+## Example 1 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / managers.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./managers.py)
+
+```python
+# managers.py
+from django.db import models
+from django.db.models import Count
+from django.db.models import Q
+~~from django.db.models.query import EmptyQuerySet
+from django.db.models.query import QuerySet
+from mptt.managers import TreeManager
+
+
+class ArticleQuerySet(QuerySet):
+ def can_read(self, user):
+ if user.has_perm("wiki.moderator"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(other_read=True)
+ else:
+ q = self.filter(
+ Q(other_read=True)
+ | Q(owner=user)
+ | (Q(group__user=user) & Q(group_read=True))
+ ).annotate(Count("id"))
+ return q
+
+ def can_write(self, user):
+ if user.has_perm("wiki.moderator"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(other_write=True)
+ else:
+ q = self.filter(
+ Q(other_write=True)
+ | Q(owner=user)
+ | (Q(group__user=user) & Q(group_write=True))
+ )
+ return q
+
+ def active(self):
+ return self.filter(current_revision__deleted=False)
+
+
+~~class ArticleEmptyQuerySet(EmptyQuerySet):
+ def can_read(self, user):
+ return self
+
+ def can_write(self, user):
+ return self
+
+ def active(self):
+ return self
+
+
+class ArticleFkQuerySetMixin:
+ def can_read(self, user):
+ if user.has_perm("wiki.moderate"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(article__other_read=True)
+ else:
+ q = self.filter(
+ Q(article__other_read=True)
+ | Q(article__owner=user)
+ | (Q(article__group__user=user) & Q(article__group_read=True))
+ ).annotate(Count("id"))
+ return q
+
+
+
+## ... source file abbreviated to get to EmptyQuerySet examples ...
+
+
+
+ def can_read(self, user):
+ return self.get_queryset().can_read(user)
+
+ def can_write(self, user):
+ return self.get_queryset().can_write(user)
+
+
+class ArticleFkManager(models.Manager):
+ def get_empty_query_set(self):
+ return self.get_queryset().none()
+
+ def get_queryset(self):
+ return ArticleFkQuerySet(self.model, using=self._db)
+
+ def active(self):
+ return self.get_queryset().active()
+
+ def can_read(self, user):
+ return self.get_queryset().can_read(user)
+
+ def can_write(self, user):
+ return self.get_queryset().can_write(user)
+
+
+~~class URLPathEmptyQuerySet(EmptyQuerySet, ArticleFkEmptyQuerySetMixin):
+ def select_related_common(self):
+ return self
+
+ def default_order(self):
+ return self
+
+
+class URLPathQuerySet(QuerySet, ArticleFkQuerySetMixin):
+ def select_related_common(self):
+ return self.select_related(
+ "parent", "article__current_revision", "article__owner"
+ )
+
+ def default_order(self):
+ return self.order_by("article__current_revision__title")
+
+
+class URLPathManager(TreeManager):
+ def get_empty_query_set(self):
+ return self.get_queryset().none()
+
+ def get_queryset(self):
+ return URLPathQuerySet(self.model, using=self._db).order_by(
+ self.tree_id_attr, self.left_attr
+ Q(article__other_write=True)
+ | Q(article__owner=user)
+ | (Q(article__group__user=user) & Q(article__group_write=True))
+ ).annotate(Count("id"))
+ return q
+
+ def active(self):
+ return self.filter(article__current_revision__deleted=False)
+
+
+class ArticleFkEmptyQuerySetMixin:
+ def can_read(self, user):
+ return self
+
+ def can_write(self, user):
+ return self
+
+ def active(self):
+ return self
+
+
+class ArticleFkQuerySet(ArticleFkQuerySetMixin, QuerySet):
+ pass
+
+
+~~class ArticleFkEmptyQuerySet(ArticleFkEmptyQuerySetMixin, EmptyQuerySet):
+ pass
+
+
+class ArticleManager(models.Manager):
+ def get_empty_query_set(self):
+ return self.get_queryset().none()
+
+ def get_queryset(self):
+ return ArticleQuerySet(self.model, using=self._db)
+
+ def active(self):
+ return self.get_queryset().active()
+
+
+## ... source file continues with no further EmptyQuerySet examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-modeliterable.markdown b/content/pages/examples/django/django-db-models-query-modeliterable.markdown
new file mode 100644
index 000000000..7f203b449
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-modeliterable.markdown
@@ -0,0 +1,72 @@
+title: django.db.models.query ModelIterable Example Code
+category: page
+slug: django-db-models-query-modeliterable-examples
+sortorder: 500011240
+toc: False
+sidebartitle: django.db.models.query ModelIterable
+meta: Python example code for the ModelIterable class from the django.db.models.query module of the Django project.
+
+
+ModelIterable is a class within the django.db.models.query module of the Django project.
+
+
+## Example 1 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / managers.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./managers.py)
+
+```python
+# managers.py
+import django
+from django.core.exceptions import ObjectDoesNotExist
+from django.db import connection
+from django.db import models
+from django.db.models.constants import LOOKUP_SEP
+from django.db.models.fields.related import OneToOneField, OneToOneRel
+~~from django.db.models.query import ModelIterable
+from django.db.models.query import QuerySet
+from django.db.models.sql.datastructures import Join
+
+
+~~class InheritanceIterable(ModelIterable):
+ def __iter__(self):
+ queryset = self.queryset
+~~ iter = ModelIterable(queryset)
+ if getattr(queryset, 'subclasses', False):
+ extras = tuple(queryset.query.extra.keys())
+ subclasses = sorted(queryset.subclasses, key=len, reverse=True)
+ for obj in iter:
+ sub_obj = None
+ for s in subclasses:
+ sub_obj = queryset._get_sub_obj_recurse(obj, s)
+ if sub_obj:
+ break
+ if not sub_obj:
+ sub_obj = obj
+
+ if getattr(queryset, '_annotated', False):
+ for k in queryset._annotated:
+ setattr(sub_obj, k, getattr(obj, k))
+
+ for k in extras:
+ setattr(sub_obj, k, getattr(obj, k))
+
+ yield sub_obj
+ else:
+ yield from iter
+
+
+
+
+## ... source file continues with no further ModelIterable examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-prefetch-related-objects.markdown b/content/pages/examples/django/django-db-models-query-prefetch-related-objects.markdown
new file mode 100644
index 000000000..0aaefd46f
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-prefetch-related-objects.markdown
@@ -0,0 +1,110 @@
+title: django.db.models.query prefetch_related_objects Example Code
+category: page
+slug: django-db-models-query-prefetch-related-objects-examples
+sortorder: 500011244
+toc: False
+sidebartitle: django.db.models.query prefetch_related_objects
+meta: Python example code for the prefetch_related_objects callable from the django.db.models.query module of the Django project.
+
+
+prefetch_related_objects is a callable within the django.db.models.query module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / cms_menus.py**](https://github.com/divio/django-cms/blob/develop/cms/./cms_menus.py)
+
+```python
+# cms_menus.py
+~~from django.db.models.query import Prefetch, prefetch_related_objects
+from django.urls import reverse
+from django.utils.functional import SimpleLazyObject
+from django.utils.translation import override as force_language
+
+from cms import constants
+from cms.api import get_page_draft
+from cms.apphook_pool import apphook_pool
+from cms.models import EmptyTitle
+from cms.utils.conf import get_cms_setting
+from cms.utils.i18n import (
+ get_fallback_languages,
+ get_public_languages,
+ hide_untranslated,
+ is_valid_site_language,
+)
+from cms.utils.permissions import get_view_restrictions
+from cms.utils.page import get_page_queryset
+from cms.utils.page_permissions import user_can_view_all_pages
+
+from menus.base import Menu, NavigationNode, Modifier
+from menus.menu_pool import menu_pool
+
+
+def get_visible_nodes(request, pages, site):
+
+
+## ... source file abbreviated to get to prefetch_related_objects examples ...
+
+
+ .distinct()
+ )
+
+ if not self.renderer.draft_mode_active:
+ pages = pages.select_related('publisher_public__node')
+ pages = get_visible_nodes(request, pages, site)
+
+ if not pages:
+ return []
+
+ try:
+ homepage = [page for page in pages if page.is_home][0]
+ except IndexError:
+ homepage = None
+
+ titles = Title.objects.filter(
+ language__in=languages,
+ publisher_is_draft=self.renderer.draft_mode_active,
+ )
+
+ lookup = Prefetch(
+ 'title_set',
+ to_attr='filtered_translations',
+ queryset=titles,
+ )
+~~ prefetch_related_objects(pages, lookup)
+ blank_title_cache = {language: EmptyTitle(language=language) for language in languages}
+
+ if lang not in blank_title_cache:
+ blank_title_cache[lang] = EmptyTitle(language=lang)
+
+ node_id_to_page = {}
+
+ def _page_to_node(page):
+ page.title_cache = blank_title_cache.copy()
+
+ for trans in page.filtered_translations:
+ page.title_cache[trans.language] = trans
+ menu_node = get_menu_node_for_page(
+ self.renderer,
+ page,
+ language=lang,
+ fallbacks=fallbacks,
+ )
+ return menu_node
+
+ menu_nodes = []
+
+ for page in pages:
+ node = page.node
+
+
+## ... source file continues with no further prefetch_related_objects examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-prefetch.markdown b/content/pages/examples/django/django-db-models-query-prefetch.markdown
new file mode 100644
index 000000000..cc8bd2e08
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-prefetch.markdown
@@ -0,0 +1,110 @@
+title: django.db.models.query Prefetch Example Code
+category: page
+slug: django-db-models-query-prefetch-examples
+sortorder: 500011241
+toc: False
+sidebartitle: django.db.models.query Prefetch
+meta: Python example code for the Prefetch class from the django.db.models.query module of the Django project.
+
+
+Prefetch is a class within the django.db.models.query module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / cms_menus.py**](https://github.com/divio/django-cms/blob/develop/cms/./cms_menus.py)
+
+```python
+# cms_menus.py
+~~from django.db.models.query import Prefetch, prefetch_related_objects
+from django.urls import reverse
+from django.utils.functional import SimpleLazyObject
+from django.utils.translation import override as force_language
+
+from cms import constants
+from cms.api import get_page_draft
+from cms.apphook_pool import apphook_pool
+from cms.models import EmptyTitle
+from cms.utils.conf import get_cms_setting
+from cms.utils.i18n import (
+ get_fallback_languages,
+ get_public_languages,
+ hide_untranslated,
+ is_valid_site_language,
+)
+from cms.utils.permissions import get_view_restrictions
+from cms.utils.page import get_page_queryset
+from cms.utils.page_permissions import user_can_view_all_pages
+
+from menus.base import Menu, NavigationNode, Modifier
+from menus.menu_pool import menu_pool
+
+
+def get_visible_nodes(request, pages, site):
+
+
+## ... source file abbreviated to get to Prefetch examples ...
+
+
+ pages = (
+ pages
+ .filter(title_set__language__in=languages)
+ .select_related('node')
+ .order_by('node__path')
+ .distinct()
+ )
+
+ if not self.renderer.draft_mode_active:
+ pages = pages.select_related('publisher_public__node')
+ pages = get_visible_nodes(request, pages, site)
+
+ if not pages:
+ return []
+
+ try:
+ homepage = [page for page in pages if page.is_home][0]
+ except IndexError:
+ homepage = None
+
+ titles = Title.objects.filter(
+ language__in=languages,
+ publisher_is_draft=self.renderer.draft_mode_active,
+ )
+
+~~ lookup = Prefetch(
+ 'title_set',
+ to_attr='filtered_translations',
+ queryset=titles,
+ )
+ prefetch_related_objects(pages, lookup)
+ blank_title_cache = {language: EmptyTitle(language=language) for language in languages}
+
+ if lang not in blank_title_cache:
+ blank_title_cache[lang] = EmptyTitle(language=lang)
+
+ node_id_to_page = {}
+
+ def _page_to_node(page):
+ page.title_cache = blank_title_cache.copy()
+
+ for trans in page.filtered_translations:
+ page.title_cache[trans.language] = trans
+ menu_node = get_menu_node_for_page(
+ self.renderer,
+ page,
+ language=lang,
+ fallbacks=fallbacks,
+ )
+ return menu_node
+
+
+## ... source file continues with no further Prefetch examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-q.markdown b/content/pages/examples/django/django-db-models-query-q.markdown
new file mode 100644
index 000000000..2ba3f36e3
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-q.markdown
@@ -0,0 +1,136 @@
+title: django.db.models.query Q Example Code
+category: page
+slug: django-db-models-query-q-examples
+sortorder: 500011242
+toc: False
+sidebartitle: django.db.models.query Q
+meta: Python example code for the Q constant from the django.db.models.query module of the Django project.
+
+
+Q is a constant within the django.db.models.query module of the Django project.
+
+
+## Example 1 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / managers.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./managers.py)
+
+```python
+# managers.py
+from django.db import models
+from django.db.models import Count
+from django.db.models import Q
+from django.db.models.query import EmptyQuerySet
+~~from django.db.models.query import QuerySet
+from mptt.managers import TreeManager
+
+
+class ArticleQuerySet(QuerySet):
+ def can_read(self, user):
+ if user.has_perm("wiki.moderator"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(other_read=True)
+ else:
+ q = self.filter(
+~~ Q(other_read=True)
+~~ | Q(owner=user)
+~~ | (Q(group__user=user) & Q(group_read=True))
+ ).annotate(Count("id"))
+ return q
+
+ def can_write(self, user):
+ if user.has_perm("wiki.moderator"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(other_write=True)
+ else:
+ q = self.filter(
+~~ Q(other_write=True)
+~~ | Q(owner=user)
+~~ | (Q(group__user=user) & Q(group_write=True))
+ )
+ return q
+
+ def active(self):
+ return self.filter(current_revision__deleted=False)
+
+
+class ArticleEmptyQuerySet(EmptyQuerySet):
+ def can_read(self, user):
+ return self
+
+ def can_write(self, user):
+ return self
+
+ def active(self):
+ return self
+
+
+class ArticleFkQuerySetMixin:
+ def can_read(self, user):
+ if user.has_perm("wiki.moderate"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(article__other_read=True)
+ else:
+ q = self.filter(
+~~ Q(article__other_read=True)
+~~ | Q(article__owner=user)
+~~ | (Q(article__group__user=user) & Q(article__group_read=True))
+ ).annotate(Count("id"))
+ return q
+
+ def can_write(self, user):
+ if user.has_perm("wiki.moderate"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(article__other_write=True)
+ else:
+ q = self.filter(
+~~ Q(article__other_write=True)
+~~ | Q(article__owner=user)
+~~ | (Q(article__group__user=user) & Q(article__group_write=True))
+ ).annotate(Count("id"))
+ return q
+
+ def active(self):
+ return self.filter(article__current_revision__deleted=False)
+
+
+class ArticleFkEmptyQuerySetMixin:
+ def can_read(self, user):
+ return self
+
+ def can_write(self, user):
+ return self
+
+ def active(self):
+ return self
+
+
+class ArticleFkQuerySet(ArticleFkQuerySetMixin, QuerySet):
+ pass
+
+
+class ArticleFkEmptyQuerySet(ArticleFkEmptyQuerySetMixin, EmptyQuerySet):
+ pass
+
+
+## ... source file continues with no further Q examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-queryset.markdown b/content/pages/examples/django/django-db-models-query-queryset.markdown
new file mode 100644
index 000000000..fcc29c3fc
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-queryset.markdown
@@ -0,0 +1,900 @@
+title: django.db.models.query QuerySet Example Code
+category: page
+slug: django-db-models-query-queryset-examples
+sortorder: 500011243
+toc: False
+sidebartitle: django.db.models.query QuerySet
+meta: Python example code for the QuerySet class from the django.db.models.query module of the Django project.
+
+
+QuerySet is a class within the django.db.models.query module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / publisher / query.py**](https://github.com/divio/django-cms/blob/develop/cms/publisher/query.py)
+
+```python
+# query.py
+~~from django.db.models.query import QuerySet
+
+
+~~class PublisherQuerySet(QuerySet):
+ def drafts(self):
+ return self.filter(publisher_is_draft=True)
+
+ def public(self):
+ return self.filter(publisher_is_draft=False)
+
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 2 from django-extensions
+[django-extensions](https://github.com/django-extensions/django-extensions)
+([project documentation](https://django-extensions.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-extensions/))
+is a [Django](/django.html) project that adds a bunch of additional
+useful commands to the `manage.py` interface. This
+[GoDjango video](https://www.youtube.com/watch?v=1F6G3ONhr4k) provides a
+quick overview of what you get when you install it into your Python
+environment.
+
+The django-extensions project is open sourced under the
+[MIT license](https://github.com/django-extensions/django-extensions/blob/master/LICENSE).
+
+[**django-extensions / django_extensions / admin / __init__.py**](https://github.com/django-extensions/django-extensions/blob/master/django_extensions/admin/__init__.py)
+
+```python
+# __init__.py
+import six
+import operator
+from functools import update_wrapper
+from six.moves import reduce
+from typing import Tuple, Dict, Callable # NOQA
+
+from django.apps import apps
+from django.http import HttpResponse, HttpResponseNotFound
+from django.conf import settings
+from django.db import models
+~~from django.db.models.query import QuerySet
+from django.utils.encoding import smart_str
+from django.utils.translation import gettext as _
+from django.utils.text import get_text_list
+from django.contrib import admin
+
+from django_extensions.admin.widgets import ForeignKeySearchInput
+
+
+class ForeignKeyAutocompleteAdminMixin:
+
+ related_search_fields = {} # type: Dict[str, Tuple[str]]
+ related_string_functions = {} # type: Dict[str, Callable]
+ autocomplete_limit = getattr(settings, 'FOREIGNKEY_AUTOCOMPLETE_LIMIT', None)
+
+ def get_urls(self):
+ from django.urls import path
+
+ def wrap(view):
+ def wrapper(*args, **kwargs):
+ return self.admin_site.admin_view(view)(*args, **kwargs)
+ return update_wrapper(wrapper, view)
+
+ return [
+ path('foreignkey_autocomplete/', wrap(self.foreignkey_autocomplete),
+
+
+## ... source file abbreviated to get to QuerySet examples ...
+
+
+ object_pk = request.GET.get('object_pk', None)
+
+ try:
+ to_string_function = self.related_string_functions[model_name]
+ except KeyError:
+ to_string_function = lambda x: x.__str__()
+
+ if search_fields and app_label and model_name and (query or object_pk):
+ def construct_search(field_name):
+ if field_name.startswith('^'):
+ return "%s__istartswith" % field_name[1:]
+ elif field_name.startswith('='):
+ return "%s__iexact" % field_name[1:]
+ elif field_name.startswith('@'):
+ return "%s__search" % field_name[1:]
+ else:
+ return "%s__icontains" % field_name
+
+ model = apps.get_model(app_label, model_name)
+
+ queryset = model._default_manager.all()
+ data = ''
+ if query:
+ for bit in query.split():
+ or_queries = [models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')]
+~~ other_qs = QuerySet(model)
+ other_qs.query.select_related = queryset.query.select_related
+ other_qs = other_qs.filter(reduce(operator.or_, or_queries))
+ queryset = queryset & other_qs
+
+ additional_filter = self.get_related_filter(model, request)
+ if additional_filter:
+ queryset = queryset.filter(additional_filter)
+
+ if self.autocomplete_limit:
+ queryset = queryset[:self.autocomplete_limit]
+
+ data = ''.join([six.u('%s|%s\n') % (to_string_function(f), f.pk) for f in queryset])
+ elif object_pk:
+ try:
+ obj = queryset.get(pk=object_pk)
+ except Exception: # FIXME: use stricter exception checking
+ pass
+ else:
+ data = to_string_function(obj)
+ return HttpResponse(data, content_type='text/plain')
+ return HttpResponseNotFound()
+
+ def get_related_filter(self, model, request):
+ return None
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 3 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / core.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./core.py)
+
+```python
+# core.py
+from itertools import chain
+
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import Permission
+~~from django.db.models.query import QuerySet
+from django.utils.encoding import force_str
+
+from guardian.conf import settings as guardian_settings
+from guardian.ctypes import get_content_type
+from guardian.utils import get_group_obj_perms_model, get_identity, get_user_obj_perms_model
+
+
+def _get_pks_model_and_ctype(objects):
+
+~~ if isinstance(objects, QuerySet):
+ model = objects.model
+ pks = [force_str(pk) for pk in objects.values_list('pk', flat=True)]
+ ctype = get_content_type(model)
+ else:
+ pks = []
+ for idx, obj in enumerate(objects):
+ if not idx:
+ model = type(obj)
+ ctype = get_content_type(model)
+ pks.append(force_str(obj.pk))
+
+ return pks, model, ctype
+
+
+class ObjectPermissionChecker:
+
+ def __init__(self, user_or_group=None):
+ self.user, self.group = get_identity(user_or_group)
+ self._obj_perms_cache = {}
+
+ def has_perm(self, perm, obj):
+ if self.user and not self.user.is_active:
+ return False
+ elif self.user and self.user.is_superuser:
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 4 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / resources.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./resources.py)
+
+```python
+# resources.py
+import functools
+import logging
+import tablib
+import traceback
+from collections import OrderedDict
+from copy import deepcopy
+
+from diff_match_patch import diff_match_patch
+
+import django
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured, ValidationError
+from django.core.management.color import no_style
+from django.core.paginator import Paginator
+from django.db import DEFAULT_DB_ALIAS, connections
+from django.db.models.fields.related import ForeignObjectRel
+~~from django.db.models.query import QuerySet
+from django.db.transaction import (
+ TransactionManagementError,
+ atomic,
+ savepoint,
+ savepoint_commit,
+ savepoint_rollback
+)
+from django.utils.encoding import force_str
+from django.utils.safestring import mark_safe
+
+from . import widgets
+from .fields import Field
+from .instance_loaders import ModelInstanceLoader
+from .results import Error, Result, RowResult
+from .utils import atomic_if_using_transaction
+
+if django.VERSION[0] >= 3:
+ from django.core.exceptions import FieldDoesNotExist
+else:
+ from django.db.models.fields import FieldDoesNotExist
+
+
+logger = logging.getLogger(__name__)
+logger.addHandler(logging.NullHandler())
+
+
+## ... source file abbreviated to get to QuerySet examples ...
+
+
+ method = getattr(self, 'dehydrate_%s' % field_name, None)
+ if method is not None:
+ return method(obj)
+ return field.export(obj)
+
+ def get_export_fields(self):
+ return self.get_fields()
+
+ def export_resource(self, obj):
+ return [self.export_field(field, obj) for field in self.get_export_fields()]
+
+ def get_export_headers(self):
+ headers = [
+ force_str(field.column_name) for field in self.get_export_fields()]
+ return headers
+
+ def get_user_visible_headers(self):
+ headers = [
+ force_str(field.column_name) for field in self.get_user_visible_fields()]
+ return headers
+
+ def get_user_visible_fields(self):
+ return self.get_fields()
+
+ def iter_queryset(self, queryset):
+~~ if not isinstance(queryset, QuerySet):
+ yield from queryset
+ elif queryset._prefetch_related_lookups:
+ if not queryset.query.order_by:
+ queryset = queryset.order_by('pk')
+ paginator = Paginator(queryset, self.get_chunk_size())
+ for index in range(paginator.num_pages):
+ yield from paginator.get_page(index + 1)
+ else:
+ yield from queryset.iterator(chunk_size=self.get_chunk_size())
+
+ def export(self, queryset=None, *args, **kwargs):
+
+ self.before_export(queryset, *args, **kwargs)
+
+ if queryset is None:
+ queryset = self.get_queryset()
+ headers = self.get_export_headers()
+ data = tablib.Dataset(headers=headers)
+
+ for obj in self.iter_queryset(queryset):
+ data.append(self.export_resource(obj))
+
+ self.after_export(queryset, data, *args, **kwargs)
+
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 5 from django-jsonfield
+[django-jsonfield](https://github.com/dmkoch/django-jsonfield)
+([jsonfield on PyPi](https://pypi.org/project/jsonfield/)) is a
+[Django](/django.html) code library that makes it easier to store validated
+JSON in a [Django object-relational mapper (ORM)](/django-orm.html) database
+model.
+
+The django-jsonfield project is open source under the
+[MIT license](https://github.com/dmkoch/django-jsonfield/blob/master/LICENSE).
+
+[**django-jsonfield / src/jsonfield / encoder.py**](https://github.com/dmkoch/django-jsonfield/blob/master/src/jsonfield/./encoder.py)
+
+```python
+# encoder.py
+import datetime
+import decimal
+import json
+import uuid
+
+~~from django.db.models.query import QuerySet
+from django.utils import timezone
+from django.utils.encoding import force_str
+from django.utils.functional import Promise
+
+
+class JSONEncoder(json.JSONEncoder):
+ def default(self, obj): # noqa: C901
+ if isinstance(obj, Promise):
+ return force_str(obj)
+ elif isinstance(obj, datetime.datetime):
+ representation = obj.isoformat()
+ if representation.endswith('+00:00'):
+ representation = representation[:-6] + 'Z'
+ return representation
+ elif isinstance(obj, datetime.date):
+ return obj.isoformat()
+ elif isinstance(obj, datetime.time):
+ if timezone and timezone.is_aware(obj):
+ raise ValueError("JSON can't represent timezone-aware times.")
+ representation = obj.isoformat()
+ return representation
+ elif isinstance(obj, datetime.timedelta):
+ return str(obj.total_seconds())
+ elif isinstance(obj, decimal.Decimal):
+ return float(obj)
+ elif isinstance(obj, uuid.UUID):
+ return str(obj)
+~~ elif isinstance(obj, QuerySet):
+ return tuple(obj)
+ elif isinstance(obj, bytes):
+ return obj.decode()
+ elif hasattr(obj, 'tolist'):
+ return obj.tolist()
+ elif hasattr(obj, '__getitem__'):
+ cls = (list if isinstance(obj, (list, tuple)) else dict)
+ try:
+ return cls(obj)
+ except Exception:
+ pass
+ elif hasattr(obj, '__iter__'):
+ return tuple(item for item in obj)
+ return super().default(obj)
+
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 6 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / managers.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./managers.py)
+
+```python
+# managers.py
+import django
+from django.core.exceptions import ObjectDoesNotExist
+from django.db import connection
+from django.db import models
+from django.db.models.constants import LOOKUP_SEP
+from django.db.models.fields.related import OneToOneField, OneToOneRel
+from django.db.models.query import ModelIterable
+~~from django.db.models.query import QuerySet
+from django.db.models.sql.datastructures import Join
+
+
+class InheritanceIterable(ModelIterable):
+ def __iter__(self):
+ queryset = self.queryset
+ iter = ModelIterable(queryset)
+ if getattr(queryset, 'subclasses', False):
+ extras = tuple(queryset.query.extra.keys())
+ subclasses = sorted(queryset.subclasses, key=len, reverse=True)
+ for obj in iter:
+ sub_obj = None
+ for s in subclasses:
+ sub_obj = queryset._get_sub_obj_recurse(obj, s)
+ if sub_obj:
+ break
+ if not sub_obj:
+ sub_obj = obj
+
+ if getattr(queryset, '_annotated', False):
+ for k in queryset._annotated:
+ setattr(sub_obj, k, getattr(obj, k))
+
+ for k in extras:
+
+
+## ... source file abbreviated to get to QuerySet examples ...
+
+
+ if levels or levels is None:
+ parent_model = related.model
+ parent_link = parent_model._meta.get_ancestor_link(
+ self.model)
+ else:
+ parent_link = None
+ return LOOKUP_SEP.join(ancestry)
+
+ def _get_sub_obj_recurse(self, obj, s):
+ rel, _, s = s.partition(LOOKUP_SEP)
+
+ try:
+ node = getattr(obj, rel)
+ except ObjectDoesNotExist:
+ return None
+ if s:
+ child = self._get_sub_obj_recurse(node, s)
+ return child
+ else:
+ return node
+
+ def get_subclass(self, *args, **kwargs):
+ return self.select_subclasses().get(*args, **kwargs)
+
+
+~~class InheritanceQuerySet(InheritanceQuerySetMixin, QuerySet):
+ def instance_of(self, *models):
+
+
+
+ where_queries = []
+ for model in models:
+ where_queries.append('(' + ' AND '.join([
+ '"{}"."{}" IS NOT NULL'.format(
+ model._meta.db_table,
+ field.attname, # Should this be something else?
+ ) for field in model._meta.parents.values()
+ ]) + ')')
+
+ return self.select_subclasses(*models).extra(where=[' OR '.join(where_queries)])
+
+
+class InheritanceManagerMixin:
+ _queryset_class = InheritanceQuerySet
+
+ def get_queryset(self):
+ return self._queryset_class(self.model)
+
+ def select_subclasses(self, *subclasses):
+ return self.get_queryset().select_subclasses(*subclasses)
+
+
+## ... source file abbreviated to get to QuerySet examples ...
+
+
+ self._q = models.Q(**kwargs)
+ self._order_by = None
+ super().__init__()
+
+ def order_by(self, *args):
+ self._order_by = args
+ return self
+
+ def get_queryset(self):
+ qs = super().get_queryset().filter(self._q)
+ if self._order_by is not None:
+ return qs.order_by(*self._order_by)
+ return qs
+
+
+class QueryManager(QueryManagerMixin, models.Manager):
+ pass
+
+
+class SoftDeletableQuerySetMixin:
+
+ def delete(self):
+ self.update(is_removed=True)
+
+
+~~class SoftDeletableQuerySet(SoftDeletableQuerySetMixin, QuerySet):
+ pass
+
+
+class SoftDeletableManagerMixin:
+ _queryset_class = SoftDeletableQuerySet
+
+ def get_queryset(self):
+ kwargs = {'model': self.model, 'using': self._db}
+ if hasattr(self, '_hints'):
+ kwargs['hints'] = self._hints
+
+ return self._queryset_class(**kwargs).filter(is_removed=False)
+
+
+class SoftDeletableManager(SoftDeletableManagerMixin, models.Manager):
+ pass
+
+
+class JoinQueryset(models.QuerySet):
+
+ def get_quoted_query(self, query):
+ query, params = query.sql_with_params()
+
+ params = [
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 7 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / relations.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./relations.py)
+
+```python
+# relations.py
+import sys
+from collections import OrderedDict
+from urllib import parse
+
+from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
+from django.db.models import Manager
+~~from django.db.models.query import QuerySet
+from django.urls import NoReverseMatch, Resolver404, get_script_prefix, resolve
+from django.utils.encoding import smart_str, uri_to_iri
+from django.utils.translation import gettext_lazy as _
+
+from rest_framework.fields import (
+ Field, empty, get_attribute, is_simple_callable, iter_options
+)
+from rest_framework.reverse import reverse
+from rest_framework.settings import api_settings
+from rest_framework.utils import html
+
+
+def method_overridden(method_name, klass, instance):
+ method = getattr(klass, method_name)
+ default_method = getattr(method, '__func__', method) # Python 3 compat
+ return default_method is not getattr(instance, method_name).__func__
+
+
+class ObjectValueError(ValueError):
+
+
+class ObjectTypeError(TypeError):
+
+
+
+
+## ... source file abbreviated to get to QuerySet examples ...
+
+
+ )
+ kwargs.pop('many', None)
+ kwargs.pop('allow_empty', None)
+ super().__init__(**kwargs)
+
+ def __new__(cls, *args, **kwargs):
+ if kwargs.pop('many', False):
+ return cls.many_init(*args, **kwargs)
+ return super().__new__(cls, *args, **kwargs)
+
+ @classmethod
+ def many_init(cls, *args, **kwargs):
+ list_kwargs = {'child_relation': cls(*args, **kwargs)}
+ for key in kwargs:
+ if key in MANY_RELATION_KWARGS:
+ list_kwargs[key] = kwargs[key]
+ return ManyRelatedField(**list_kwargs)
+
+ def run_validation(self, data=empty):
+ if data == '':
+ data = None
+ return super().run_validation(data)
+
+ def get_queryset(self):
+ queryset = self.queryset
+~~ if isinstance(queryset, (QuerySet, Manager)):
+ queryset = queryset.all()
+ return queryset
+
+ def use_pk_only_optimization(self):
+ return False
+
+ def get_attribute(self, instance):
+ if self.use_pk_only_optimization() and self.source_attrs:
+ try:
+ attribute_instance = get_attribute(instance, self.source_attrs[:-1])
+ value = attribute_instance.serializable_value(self.source_attrs[-1])
+ if is_simple_callable(value):
+ value = value().pk
+ return PKOnlyObject(pk=value)
+ except AttributeError:
+ pass
+
+ return super().get_attribute(instance)
+
+ def get_choices(self, cutoff=None):
+ queryset = self.get_queryset()
+ if queryset is None:
+ return {}
+
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 8 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / managers.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./managers.py)
+
+```python
+# managers.py
+from django.db import models
+from django.db.models import Count
+from django.db.models import Q
+from django.db.models.query import EmptyQuerySet
+~~from django.db.models.query import QuerySet
+from mptt.managers import TreeManager
+
+
+~~class ArticleQuerySet(QuerySet):
+ def can_read(self, user):
+ if user.has_perm("wiki.moderator"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(other_read=True)
+ else:
+ q = self.filter(
+ Q(other_read=True)
+ | Q(owner=user)
+ | (Q(group__user=user) & Q(group_read=True))
+ ).annotate(Count("id"))
+ return q
+
+ def can_write(self, user):
+ if user.has_perm("wiki.moderator"):
+ return self
+ if user.is_anonymous:
+ q = self.filter(other_write=True)
+ else:
+ q = self.filter(
+ Q(other_write=True)
+ | Q(owner=user)
+ | (Q(group__user=user) & Q(group_write=True))
+ )
+
+
+## ... source file abbreviated to get to QuerySet examples ...
+
+
+class ArticleFkManager(models.Manager):
+ def get_empty_query_set(self):
+ return self.get_queryset().none()
+
+ def get_queryset(self):
+ return ArticleFkQuerySet(self.model, using=self._db)
+
+ def active(self):
+ return self.get_queryset().active()
+
+ def can_read(self, user):
+ return self.get_queryset().can_read(user)
+
+ def can_write(self, user):
+ return self.get_queryset().can_write(user)
+
+
+class URLPathEmptyQuerySet(EmptyQuerySet, ArticleFkEmptyQuerySetMixin):
+ def select_related_common(self):
+ return self
+
+ def default_order(self):
+ return self
+
+
+~~class URLPathQuerySet(QuerySet, ArticleFkQuerySetMixin):
+ def select_related_common(self):
+ return self.select_related(
+ "parent", "article__current_revision", "article__owner"
+ )
+
+ def default_order(self):
+ return self.order_by("article__current_revision__title")
+
+
+class URLPathManager(TreeManager):
+ def get_empty_query_set(self):
+ return self.get_queryset().none()
+
+ def get_queryset(self):
+ return URLPathQuerySet(self.model, using=self._db).order_by(
+ self.tree_id_attr, self.left_attr
+ )
+
+ def select_related_common(self):
+ return self.get_queryset().common_select_related()
+
+ def active(self):
+ return self.get_queryset().active()
+
+ if user.is_anonymous:
+ q = self.filter(article__other_write=True)
+ else:
+ q = self.filter(
+ Q(article__other_write=True)
+ | Q(article__owner=user)
+ | (Q(article__group__user=user) & Q(article__group_write=True))
+ ).annotate(Count("id"))
+ return q
+
+ def active(self):
+ return self.filter(article__current_revision__deleted=False)
+
+
+class ArticleFkEmptyQuerySetMixin:
+ def can_read(self, user):
+ return self
+
+ def can_write(self, user):
+ return self
+
+ def active(self):
+ return self
+
+
+~~class ArticleFkQuerySet(ArticleFkQuerySetMixin, QuerySet):
+ pass
+
+
+class ArticleFkEmptyQuerySet(ArticleFkEmptyQuerySetMixin, EmptyQuerySet):
+ pass
+
+
+class ArticleManager(models.Manager):
+ def get_empty_query_set(self):
+ return self.get_queryset().none()
+
+ def get_queryset(self):
+ return ArticleQuerySet(self.model, using=self._db)
+
+ def active(self):
+ return self.get_queryset().active()
+
+ def can_read(self, user):
+ return self.get_queryset().can_read(user)
+
+ def can_write(self, user):
+ return self.get_queryset().can_write(user)
+
+
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
+
+## Example 9 from elasticsearch-django
+[elasticsearch-django](https://github.com/yunojuno/elasticsearch-django)
+([PyPI package information](https://pypi.org/project/elasticsearch-django/))
+is a [Django](/django.html) app for managing
+[ElasticSearch](https://github.com/elastic/elasticsearch) indexes
+populated by [Django ORM](/django-orm.html) models. The project is
+available as open source under the
+[MIT license](https://github.com/yunojuno/elasticsearch-django/blob/master/LICENSE).
+
+[**elasticsearch-django / elasticsearch_django / models.py**](https://github.com/yunojuno/elasticsearch-django/blob/master/elasticsearch_django/./models.py)
+
+```python
+# models.py
+from __future__ import annotations
+
+import logging
+import time
+import warnings
+from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
+
+from django.conf import settings
+from django.contrib.postgres.fields import JSONField
+from django.core.cache import cache
+from django.core.serializers.json import DjangoJSONEncoder
+from django.db import models
+from django.db.models.expressions import RawSQL
+from django.db.models.fields import CharField
+~~from django.db.models.query import QuerySet
+from django.utils.timezone import now as tz_now
+from elasticsearch_dsl import Search
+
+from .settings import (
+ get_client,
+ get_model_index_properties,
+ get_model_indexes,
+ get_setting,
+)
+
+if TYPE_CHECKING:
+ from django.contrib.auth.models import AbstractBaseUser
+
+logger = logging.getLogger(__name__)
+
+UPDATE_STRATEGY_FULL = "full"
+UPDATE_STRATEGY_PARTIAL = "partial"
+UPDATE_STRATEGY = get_setting("update_strategy", UPDATE_STRATEGY_FULL)
+
+
+class SearchDocumentManagerMixin(models.Manager):
+
+ def get_search_queryset(self, index: str = "_all") -> QuerySet:
+ raise NotImplementedError(
+
+
+## ... source file continues with no further QuerySet examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-utils-deferredattribute.markdown b/content/pages/examples/django/django-db-models-query-utils-deferredattribute.markdown
new file mode 100644
index 000000000..1211e4cd4
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-utils-deferredattribute.markdown
@@ -0,0 +1,120 @@
+title: django.db.models.query_utils DeferredAttribute Example Code
+category: page
+slug: django-db-models-query-utils-deferredattribute-examples
+sortorder: 500011245
+toc: False
+sidebartitle: django.db.models.query_utils DeferredAttribute
+meta: Python example code for the DeferredAttribute class from the django.db.models.query_utils module of the Django project.
+
+
+DeferredAttribute is a class within the django.db.models.query_utils module of the Django project.
+
+
+## Example 1 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / tracker.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./tracker.py)
+
+```python
+# tracker.py
+from copy import deepcopy
+from functools import wraps
+
+import django
+from django.core.exceptions import FieldError
+from django.db import models
+from django.db.models.fields.files import FileDescriptor
+~~from django.db.models.query_utils import DeferredAttribute
+
+
+class DescriptorMixin:
+ tracker_instance = None
+
+ def __get__(self, instance, owner):
+ if instance is None:
+ return self
+ was_deferred = False
+ field_name = self._get_field_name()
+ if field_name in instance._deferred_fields:
+ instance._deferred_fields.remove(field_name)
+ was_deferred = True
+ value = super().__get__(instance, owner)
+ if was_deferred:
+ self.tracker_instance.saved_data[field_name] = deepcopy(value)
+ return value
+
+ def _get_field_name(self):
+ return self.field_name
+
+
+class DescriptorWrapper:
+
+
+
+## ... source file abbreviated to get to DeferredAttribute examples ...
+
+
+ if self.instance.pk and field in self.deferred_fields and field not in self.saved_data:
+
+ if field not in self.instance.__dict__:
+ self.get_field_value(field)
+
+ else:
+ current_value = self.get_field_value(field)
+ self.instance.refresh_from_db(fields=[field])
+ self.saved_data[field] = deepcopy(self.get_field_value(field))
+ setattr(self.instance, self.field_map[field], current_value)
+
+ return self.saved_data.get(field)
+
+ def changed(self):
+ return {
+ field: self.previous(field)
+ for field in self.fields
+ if self.has_changed(field)
+ }
+
+ def init_deferred_fields(self):
+ self.instance._deferred_fields = set()
+ if hasattr(self.instance, '_deferred') and not self.instance._deferred:
+ return
+
+~~ class DeferredAttributeTracker(DescriptorMixin, DeferredAttribute):
+ tracker_instance = self
+
+ class FileDescriptorTracker(DescriptorMixin, FileDescriptor):
+ tracker_instance = self
+
+ def _get_field_name(self):
+ return self.field.name
+
+ self.instance._deferred_fields = self.instance.get_deferred_fields()
+ for field in self.instance._deferred_fields:
+ field_obj = self.instance.__class__.__dict__.get(field)
+ if isinstance(field_obj, FileDescriptor):
+ field_tracker = FileDescriptorTracker(field_obj.field)
+ setattr(self.instance.__class__, field, field_tracker)
+ else:
+ field_tracker = DeferredAttributeTracker(field)
+ setattr(self.instance.__class__, field, field_tracker)
+
+
+class FieldTracker:
+
+ tracker_class = FieldInstanceTracker
+
+ def __init__(self, fields=None):
+
+
+## ... source file continues with no further DeferredAttribute examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-utils-pathinfo.markdown b/content/pages/examples/django/django-db-models-query-utils-pathinfo.markdown
new file mode 100644
index 000000000..941641c13
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-utils-pathinfo.markdown
@@ -0,0 +1,162 @@
+title: django.db.models.query_utils PathInfo Example Code
+category: page
+slug: django-db-models-query-utils-pathinfo-examples
+sortorder: 500011246
+toc: False
+sidebartitle: django.db.models.query_utils PathInfo
+meta: Python example code for the PathInfo class from the django.db.models.query_utils module of the Django project.
+
+
+PathInfo is a class within the django.db.models.query_utils module of the Django project.
+
+
+## Example 1 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / managers.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/./managers.py)
+
+```python
+# managers.py
+import uuid
+from operator import attrgetter
+
+from django import VERSION
+from django.conf import settings
+from django.contrib.contenttypes.fields import GenericRelation
+from django.contrib.contenttypes.models import ContentType
+from django.db import connections, models, router
+from django.db.models import signals
+from django.db.models.fields.related import (
+ ManyToManyRel,
+ OneToOneRel,
+ RelatedField,
+ lazy_related_operation,
+)
+~~from django.db.models.query_utils import PathInfo
+from django.utils.text import capfirst
+from django.utils.translation import gettext_lazy as _
+
+from taggit.forms import TagField
+from taggit.models import (
+ CommonGenericTaggedItemBase,
+ GenericUUIDTaggedItemBase,
+ TaggedItem,
+)
+from taggit.utils import require_instance_manager
+
+
+class ExtraJoinRestriction:
+
+ contains_aggregate = False
+
+ def __init__(self, alias, col, content_types):
+ self.alias = alias
+ self.col = col
+ self.content_types = content_types
+
+ def as_sql(self, compiler, connection):
+ qn = compiler.quote_name_unless_alias
+ if len(self.content_types) == 1:
+
+
+## ... source file abbreviated to get to PathInfo examples ...
+
+
+ filtered_relation=filtered_relation
+ )
+ else:
+ if VERSION < (2, 0):
+ join1infos = linkfield2.get_reverse_path_info()
+ join2infos = linkfield1.get_path_info()
+ else:
+ join1infos = linkfield2.get_reverse_path_info(
+ filtered_relation=filtered_relation
+ )
+ join2infos = linkfield1.get_path_info(
+ filtered_relation=filtered_relation
+ )
+ pathinfos.extend(join1infos)
+ pathinfos.extend(join2infos)
+ return pathinfos
+
+ def _get_gfk_case_path_info(self, direct=False, filtered_relation=None):
+ pathinfos = []
+ from_field = self.model._meta.pk
+ opts = self.through._meta
+ linkfield = self.through._meta.get_field(self.m2m_reverse_field_name())
+ if direct:
+ if VERSION < (2, 0):
+ join1infos = [
+~~ PathInfo(
+ self.model._meta,
+ opts,
+ [from_field],
+ self.remote_field,
+ True,
+ False,
+ )
+ ]
+ join2infos = linkfield.get_path_info()
+ else:
+ join1infos = [
+~~ PathInfo(
+ self.model._meta,
+ opts,
+ [from_field],
+ self.remote_field,
+ True,
+ False,
+ filtered_relation,
+ )
+ ]
+ join2infos = linkfield.get_path_info(
+ filtered_relation=filtered_relation
+ )
+ else:
+ if VERSION < (2, 0):
+ join1infos = linkfield.get_reverse_path_info()
+ join2infos = [
+~~ PathInfo(opts, self.model._meta, [from_field], self, True, False)
+ ]
+ else:
+ join1infos = linkfield.get_reverse_path_info(
+ filtered_relation=filtered_relation
+ )
+ join2infos = [
+~~ PathInfo(
+ opts,
+ self.model._meta,
+ [from_field],
+ self,
+ True,
+ False,
+ filtered_relation,
+ )
+ ]
+ pathinfos.extend(join1infos)
+ pathinfos.extend(join2infos)
+ return pathinfos
+
+ def get_path_info(self, filtered_relation=None):
+ if self.use_gfk:
+ return self._get_gfk_case_path_info(
+ direct=True, filtered_relation=filtered_relation
+ )
+ else:
+ return self._get_mm_case_path_info(
+ direct=True, filtered_relation=filtered_relation
+ )
+
+ def get_reverse_path_info(self, filtered_relation=None):
+
+
+## ... source file continues with no further PathInfo examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-query-utils-q.markdown b/content/pages/examples/django/django-db-models-query-utils-q.markdown
new file mode 100644
index 000000000..b13b3dfea
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-query-utils-q.markdown
@@ -0,0 +1,117 @@
+title: django.db.models.query_utils Q Example Code
+category: page
+slug: django-db-models-query-utils-q-examples
+sortorder: 500011247
+toc: False
+sidebartitle: django.db.models.query_utils Q
+meta: Python example code for the Q constant from the django.db.models.query_utils module of the Django project.
+
+
+Q is a constant within the django.db.models.query_utils module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / utils / placeholder.py**](https://github.com/divio/django-cms/blob/develop/cms/utils/placeholder.py)
+
+```python
+# placeholder.py
+import operator
+import warnings
+from collections import OrderedDict
+
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db.models.query_utils import Q
+from django.template import TemplateSyntaxError, NodeList, Variable, Context, Template, engines
+from django.template.base import VariableNode
+from django.template.loader import get_template
+from django.template.loader_tags import BlockNode, ExtendsNode, IncludeNode
+
+from six import string_types
+
+from sekizai.helpers import get_varname
+
+from cms.exceptions import DuplicatePlaceholderWarning
+from cms.utils.conf import get_cms_setting
+
+
+def _get_nodelist(tpl):
+ if hasattr(tpl, 'template'):
+ return tpl.template.nodelist
+ else:
+ return tpl.nodelist
+
+
+def get_context():
+ if engines is not None:
+ context = Context()
+ context.template = Template('')
+
+
+## ... source file abbreviated to get to Q examples ...
+
+
+
+ def copy(self, **kwargs):
+ return False
+
+ def get_copy_languages(self, **kwargs):
+ return []
+
+
+class MLNGPlaceholderActions(PlaceholderNoAction):
+ can_copy = True
+
+ def copy(self, target_placeholder, source_language, fieldname, model, target_language, **kwargs):
+ from cms.utils.copy_plugins import copy_plugins_to
+ trgt = model.objects.get(**{fieldname: target_placeholder})
+ src = model.objects.get(master=trgt.master, language_code=source_language)
+
+ source_placeholder = getattr(src, fieldname, None)
+ if not source_placeholder:
+ return False
+ return copy_plugins_to(source_placeholder.get_plugins_list(),
+ target_placeholder, target_language)
+
+ def get_copy_languages(self, placeholder, model, fieldname, **kwargs):
+ manager = model.objects
+ src = manager.get(**{fieldname: placeholder})
+~~ query = Q(master=src.master)
+~~ query &= Q(**{'%s__cmsplugin__isnull' % fieldname: False})
+ query &= -Q(pk=src.pk)
+
+ language_codes = manager.filter(query).values_list('language_code', flat=True).distinct()
+ return [(lc, dict(settings.LANGUAGES)[lc]) for lc in language_codes]
+
+
+def restore_sekizai_context(context, changes):
+ varname = get_varname()
+ sekizai_container = context.get(varname)
+ for key, values in changes.items():
+ sekizai_namespace = sekizai_container[key]
+ for value in values:
+ sekizai_namespace.append(value)
+
+
+def _scan_placeholders(nodelist, node_class=None, current_block=None, ignore_blocks=None):
+ from cms.templatetags.cms_tags import Placeholder
+
+ if not node_class:
+ node_class = Placeholder
+
+ nodes = []
+
+ if ignore_blocks is None:
+
+
+## ... source file continues with no further Q examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-signals-post-delete.markdown b/content/pages/examples/django/django-db-models-signals-post-delete.markdown
new file mode 100644
index 000000000..7a5dd1965
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-signals-post-delete.markdown
@@ -0,0 +1,63 @@
+title: django.db.models.signals post_delete Example Code
+category: page
+slug: django-db-models-signals-post-delete-examples
+sortorder: 500011248
+toc: False
+sidebartitle: django.db.models.signals post_delete
+meta: Python example code for the post_delete callable from the django.db.models.signals module of the Django project.
+
+
+post_delete is a callable within the django.db.models.signals module of the Django project.
+
+
+## Example 1 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / signal_handlers.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/signal_handlers.py)
+
+```python
+# signal_handlers.py
+import logging
+
+from django.core.cache import cache
+~~from django.db.models.signals import post_delete, post_save, pre_delete
+
+from wagtail.core.models import Page, Site
+
+logger = logging.getLogger('wagtail.core')
+
+
+def post_save_site_signal_handler(instance, update_fields=None, **kwargs):
+ cache.delete('wagtail_site_root_paths')
+
+
+def post_delete_site_signal_handler(instance, **kwargs):
+ cache.delete('wagtail_site_root_paths')
+
+
+def pre_delete_page_unpublish(sender, instance, **kwargs):
+ if instance.live:
+ instance.unpublish(commit=False)
+
+
+def post_delete_page_log_deletion(sender, instance, **kwargs):
+ logger.info("Page deleted: \"%s\" id=%d", instance.title, instance.id)
+
+
+def register_signal_handlers():
+ post_save.connect(post_save_site_signal_handler, sender=Site)
+~~ post_delete.connect(post_delete_site_signal_handler, sender=Site)
+
+ pre_delete.connect(pre_delete_page_unpublish, sender=Page)
+~~ post_delete.connect(post_delete_page_log_deletion, sender=Page)
+
+
+
+## ... source file continues with no further post_delete examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-signals-post-save.markdown b/content/pages/examples/django/django-db-models-signals-post-save.markdown
new file mode 100644
index 000000000..5646633c8
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-signals-post-save.markdown
@@ -0,0 +1,149 @@
+title: django.db.models.signals post_save Example Code
+category: page
+slug: django-db-models-signals-post-save-examples
+sortorder: 500011249
+toc: False
+sidebartitle: django.db.models.signals post_save
+meta: Python example code for the post_save callable from the django.db.models.signals module of the Django project.
+
+
+post_save is a callable within the django.db.models.signals module of the Django project.
+
+
+## Example 1 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / models.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./models.py)
+
+```python
+# models.py
+from django.core.exceptions import ImproperlyConfigured
+from django.db import models, transaction, router
+~~from django.db.models.signals import post_save, pre_save
+from django.utils.translation import gettext_lazy as _
+
+from model_utils.fields import (
+ AutoCreatedField,
+ AutoLastModifiedField,
+ StatusField,
+ MonitorField,
+ UUIDField,
+)
+from model_utils.managers import (
+ QueryManager,
+ SoftDeletableManager,
+)
+
+from django.db.models.functions import Now
+now = Now()
+
+
+class TimeStampedModel(models.Model):
+ created = AutoCreatedField(_('created'))
+ modified = AutoLastModifiedField(_('modified'))
+
+ def save(self, *args, **kwargs):
+ if 'update_fields' in kwargs and 'modified' not in kwargs['update_fields']:
+
+
+## ... source file abbreviated to get to post_save examples ...
+
+
+
+ def save_base(self, raw=False, force_insert=False,
+ force_update=False, using=None, update_fields=None):
+ using = using or router.db_for_write(self.__class__, instance=self)
+ assert not (force_insert and (force_update or update_fields))
+ assert update_fields is None or len(update_fields) > 0
+ cls = origin = self.__class__
+
+ if cls._meta.proxy:
+ cls = cls._meta.concrete_model
+ meta = cls._meta
+ if not meta.auto_created and 'pre_save' not in self.signals_to_disable:
+ pre_save.send(
+ sender=origin, instance=self, raw=raw, using=using,
+ update_fields=update_fields,
+ )
+ with transaction.atomic(using=using, savepoint=False):
+ if not raw:
+ self._save_parents(cls, using, update_fields)
+ updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
+
+ self._state.db = using
+ self._state.adding = False
+
+ if not meta.auto_created and 'post_save' not in self.signals_to_disable:
+~~ post_save.send(
+ sender=origin, instance=self, created=(not updated),
+ update_fields=update_fields, raw=raw, using=using,
+ )
+
+ self.signals_to_disable = []
+
+
+
+## ... source file continues with no further post_save examples...
+
+```
+
+
+## Example 2 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / signal_handlers.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/signal_handlers.py)
+
+```python
+# signal_handlers.py
+import logging
+
+from django.core.cache import cache
+~~from django.db.models.signals import post_delete, post_save, pre_delete
+
+from wagtail.core.models import Page, Site
+
+logger = logging.getLogger('wagtail.core')
+
+
+def post_save_site_signal_handler(instance, update_fields=None, **kwargs):
+ cache.delete('wagtail_site_root_paths')
+
+
+def post_delete_site_signal_handler(instance, **kwargs):
+ cache.delete('wagtail_site_root_paths')
+
+
+def pre_delete_page_unpublish(sender, instance, **kwargs):
+ if instance.live:
+ instance.unpublish(commit=False)
+
+
+def post_delete_page_log_deletion(sender, instance, **kwargs):
+ logger.info("Page deleted: \"%s\" id=%d", instance.title, instance.id)
+
+
+def register_signal_handlers():
+~~ post_save.connect(post_save_site_signal_handler, sender=Site)
+ post_delete.connect(post_delete_site_signal_handler, sender=Site)
+
+ pre_delete.connect(pre_delete_page_unpublish, sender=Page)
+ post_delete.connect(post_delete_page_log_deletion, sender=Page)
+
+
+
+## ... source file continues with no further post_save examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-signals-pre-delete.markdown b/content/pages/examples/django/django-db-models-signals-pre-delete.markdown
new file mode 100644
index 000000000..1585599c7
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-signals-pre-delete.markdown
@@ -0,0 +1,63 @@
+title: django.db.models.signals pre_delete Example Code
+category: page
+slug: django-db-models-signals-pre-delete-examples
+sortorder: 500011250
+toc: False
+sidebartitle: django.db.models.signals pre_delete
+meta: Python example code for the pre_delete callable from the django.db.models.signals module of the Django project.
+
+
+pre_delete is a callable within the django.db.models.signals module of the Django project.
+
+
+## Example 1 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / signal_handlers.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/signal_handlers.py)
+
+```python
+# signal_handlers.py
+import logging
+
+from django.core.cache import cache
+~~from django.db.models.signals import post_delete, post_save, pre_delete
+
+from wagtail.core.models import Page, Site
+
+logger = logging.getLogger('wagtail.core')
+
+
+def post_save_site_signal_handler(instance, update_fields=None, **kwargs):
+ cache.delete('wagtail_site_root_paths')
+
+
+def post_delete_site_signal_handler(instance, **kwargs):
+ cache.delete('wagtail_site_root_paths')
+
+
+def pre_delete_page_unpublish(sender, instance, **kwargs):
+ if instance.live:
+ instance.unpublish(commit=False)
+
+
+def post_delete_page_log_deletion(sender, instance, **kwargs):
+ logger.info("Page deleted: \"%s\" id=%d", instance.title, instance.id)
+
+
+def register_signal_handlers():
+ post_save.connect(post_save_site_signal_handler, sender=Site)
+ post_delete.connect(post_delete_site_signal_handler, sender=Site)
+
+~~ pre_delete.connect(pre_delete_page_unpublish, sender=Page)
+ post_delete.connect(post_delete_page_log_deletion, sender=Page)
+
+
+
+## ... source file continues with no further pre_delete examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models-signals-pre-save.markdown b/content/pages/examples/django/django-db-models-signals-pre-save.markdown
new file mode 100644
index 000000000..a83fdc5c0
--- /dev/null
+++ b/content/pages/examples/django/django-db-models-signals-pre-save.markdown
@@ -0,0 +1,240 @@
+title: django.db.models.signals pre_save Example Code
+category: page
+slug: django-db-models-signals-pre-save-examples
+sortorder: 500011251
+toc: False
+sidebartitle: django.db.models.signals pre_save
+meta: Python example code for the pre_save callable from the django.db.models.signals module of the Django project.
+
+
+pre_save is a callable within the django.db.models.signals module of the Django project.
+
+
+## Example 1 from AuditLog
+[Auditlog](https://github.com/jjkester/django-auditlog)
+([project documentation](https://django-auditlog.readthedocs.io/en/latest/))
+is a [Django](/django.html) app that logs changes to Python objects,
+similar to the Django admin's logs but with more details and
+output formats. Auditlog's source code is provided as open source under the
+[MIT license](https://github.com/jjkester/django-auditlog/blob/master/LICENSE).
+
+[**AuditLog / src / auditlog / middleware.py**](https://github.com/jjkester/django-auditlog/blob/master/src/auditlog/middleware.py)
+
+```python
+# middleware.py
+from __future__ import unicode_literals
+
+import threading
+import time
+
+from django.conf import settings
+~~from django.db.models.signals import pre_save
+from django.utils.functional import curry
+from django.apps import apps
+from auditlog.models import LogEntry
+from auditlog.compat import is_authenticated
+
+try:
+ from django.utils.deprecation import MiddlewareMixin
+except ImportError:
+ MiddlewareMixin = object
+
+
+threadlocal = threading.local()
+
+
+class AuditlogMiddleware(MiddlewareMixin):
+
+ def process_request(self, request):
+ threadlocal.auditlog = {
+ 'signal_duid': (self.__class__, time.time()),
+ 'remote_addr': request.META.get('REMOTE_ADDR'),
+ }
+
+ if request.META.get('HTTP_X_FORWARDED_FOR'):
+ threadlocal.auditlog['remote_addr'] = request.META.get('HTTP_X_FORWARDED_FOR').split(',')[0]
+
+ if hasattr(request, 'user') and is_authenticated(request.user):
+ set_actor = curry(self.set_actor, user=request.user, signal_duid=threadlocal.auditlog['signal_duid'])
+~~ pre_save.connect(set_actor, sender=LogEntry, dispatch_uid=threadlocal.auditlog['signal_duid'], weak=False)
+
+ def process_response(self, request, response):
+ if hasattr(threadlocal, 'auditlog'):
+~~ pre_save.disconnect(sender=LogEntry, dispatch_uid=threadlocal.auditlog['signal_duid'])
+
+ return response
+
+ def process_exception(self, request, exception):
+ if hasattr(threadlocal, 'auditlog'):
+~~ pre_save.disconnect(sender=LogEntry, dispatch_uid=threadlocal.auditlog['signal_duid'])
+
+ return None
+
+ @staticmethod
+ def set_actor(user, sender, instance, signal_duid, **kwargs):
+ if hasattr(threadlocal, 'auditlog'):
+ if signal_duid != threadlocal.auditlog['signal_duid']:
+ return
+ try:
+ app_label, model_name = settings.AUTH_USER_MODEL.split('.')
+ auth_user_model = apps.get_model(app_label, model_name)
+ except ValueError:
+ auth_user_model = apps.get_model('auth', 'user')
+ if sender == LogEntry and isinstance(user, auth_user_model) and instance.actor is None:
+ instance.actor = user
+
+ instance.remote_addr = threadlocal.auditlog['remote_addr']
+
+
+
+## ... source file continues with no further pre_save examples...
+
+```
+
+
+## Example 2 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / models.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./models.py)
+
+```python
+# models.py
+from django.core.exceptions import ImproperlyConfigured
+from django.db import models, transaction, router
+~~from django.db.models.signals import post_save, pre_save
+from django.utils.translation import gettext_lazy as _
+
+from model_utils.fields import (
+ AutoCreatedField,
+ AutoLastModifiedField,
+ StatusField,
+ MonitorField,
+ UUIDField,
+)
+from model_utils.managers import (
+ QueryManager,
+ SoftDeletableManager,
+)
+
+from django.db.models.functions import Now
+now = Now()
+
+
+class TimeStampedModel(models.Model):
+ created = AutoCreatedField(_('created'))
+ modified = AutoLastModifiedField(_('modified'))
+
+ def save(self, *args, **kwargs):
+ if 'update_fields' in kwargs and 'modified' not in kwargs['update_fields']:
+
+
+## ... source file abbreviated to get to pre_save examples ...
+
+
+ class Meta:
+ abstract = True
+
+
+class SaveSignalHandlingModel(models.Model):
+ class Meta:
+ abstract = True
+
+ def save(self, signals_to_disable=None, *args, **kwargs):
+
+ self.signals_to_disable = signals_to_disable or []
+
+ super().save(*args, **kwargs)
+
+ def save_base(self, raw=False, force_insert=False,
+ force_update=False, using=None, update_fields=None):
+ using = using or router.db_for_write(self.__class__, instance=self)
+ assert not (force_insert and (force_update or update_fields))
+ assert update_fields is None or len(update_fields) > 0
+ cls = origin = self.__class__
+
+ if cls._meta.proxy:
+ cls = cls._meta.concrete_model
+ meta = cls._meta
+ if not meta.auto_created and 'pre_save' not in self.signals_to_disable:
+~~ pre_save.send(
+ sender=origin, instance=self, raw=raw, using=using,
+ update_fields=update_fields,
+ )
+ with transaction.atomic(using=using, savepoint=False):
+ if not raw:
+ self._save_parents(cls, using, update_fields)
+ updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
+
+ self._state.db = using
+ self._state.adding = False
+
+ if not meta.auto_created and 'post_save' not in self.signals_to_disable:
+ post_save.send(
+ sender=origin, instance=self, created=(not updated),
+ update_fields=update_fields, raw=raw, using=using,
+ )
+
+ self.signals_to_disable = []
+
+
+
+## ... source file continues with no further pre_save examples...
+
+```
+
+
+## Example 3 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / images / signal_handlers.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/images/signal_handlers.py)
+
+```python
+# signal_handlers.py
+from django.conf import settings
+from django.db import transaction
+~~from django.db.models.signals import post_delete, pre_save
+
+from wagtail.images import get_image_model
+
+
+def post_delete_file_cleanup(instance, **kwargs):
+ transaction.on_commit(lambda: instance.file.delete(False))
+
+
+def post_delete_purge_rendition_cache(instance, **kwargs):
+ instance.purge_from_cache()
+
+
+def pre_save_image_feature_detection(instance, **kwargs):
+ if getattr(settings, 'WAGTAILIMAGES_FEATURE_DETECTION_ENABLED', False):
+ if not instance.has_focal_point():
+ instance.set_focal_point(instance.get_suggested_focal_point())
+
+
+def register_signal_handlers():
+ Image = get_image_model()
+ Rendition = Image.get_rendition_model()
+
+~~ pre_save.connect(pre_save_image_feature_detection, sender=Image)
+ post_delete.connect(post_delete_file_cleanup, sender=Image)
+ post_delete.connect(post_delete_file_cleanup, sender=Rendition)
+ post_delete.connect(post_delete_purge_rendition_cache, sender=Rendition)
+
+
+
+## ... source file continues with no further pre_save examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-models.markdown b/content/pages/examples/django/django-db-models.markdown
new file mode 100644
index 000000000..8d4b12427
--- /dev/null
+++ b/content/pages/examples/django/django-db-models.markdown
@@ -0,0 +1,4688 @@
+title: django.db models Example Code
+category: page
+slug: django-db-models-examples
+sortorder: 500011167
+toc: False
+sidebartitle: django.db models
+meta: Python example code for the models callable from the django.db module of the Django project.
+
+
+models is a callable within the django.db module of the Django project.
+
+
+## Example 1 from AuditLog
+[Auditlog](https://github.com/jjkester/django-auditlog)
+([project documentation](https://django-auditlog.readthedocs.io/en/latest/))
+is a [Django](/django.html) app that logs changes to Python objects,
+similar to the Django admin's logs but with more details and
+output formats. Auditlog's source code is provided as open source under the
+[MIT license](https://github.com/jjkester/django-auditlog/blob/master/LICENSE).
+
+[**AuditLog / src / auditlog_tests / models.py**](https://github.com/jjkester/django-auditlog/blob/master/src/auditlog_tests/models.py)
+
+```python
+# models.py
+import uuid
+
+from django.contrib.postgres.fields import ArrayField
+~~from django.db import models
+from auditlog.models import AuditlogHistoryField
+from auditlog.registry import auditlog
+
+from multiselectfield import MultiSelectField
+
+
+@auditlog.register()
+class SimpleModel(models.Model):
+
+~~ text = models.TextField(blank=True)
+~~ boolean = models.BooleanField(default=False)
+~~ integer = models.IntegerField(blank=True, null=True)
+~~ datetime = models.DateTimeField(auto_now=True)
+
+ history = AuditlogHistoryField()
+
+
+class AltPrimaryKeyModel(models.Model):
+
+~~ key = models.CharField(max_length=100, primary_key=True)
+
+~~ text = models.TextField(blank=True)
+~~ boolean = models.BooleanField(default=False)
+~~ integer = models.IntegerField(blank=True, null=True)
+~~ datetime = models.DateTimeField(auto_now=True)
+
+ history = AuditlogHistoryField(pk_indexable=False)
+
+
+class UUIDPrimaryKeyModel(models.Model):
+
+~~ id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
+
+~~ text = models.TextField(blank=True)
+~~ boolean = models.BooleanField(default=False)
+~~ integer = models.IntegerField(blank=True, null=True)
+~~ datetime = models.DateTimeField(auto_now=True)
+
+ history = AuditlogHistoryField(pk_indexable=False)
+
+
+class ProxyModel(SimpleModel):
+
+ class Meta:
+ proxy = True
+
+
+class RelatedModel(models.Model):
+
+~~ related = models.ForeignKey(to='self', on_delete=models.CASCADE)
+
+ history = AuditlogHistoryField()
+
+
+class ManyRelatedModel(models.Model):
+
+~~ related = models.ManyToManyField('self')
+
+ history = AuditlogHistoryField()
+
+
+@auditlog.register(include_fields=['label'])
+class SimpleIncludeModel(models.Model):
+
+~~ label = models.CharField(max_length=100)
+~~ text = models.TextField(blank=True)
+
+ history = AuditlogHistoryField()
+
+
+class SimpleExcludeModel(models.Model):
+
+~~ label = models.CharField(max_length=100)
+~~ text = models.TextField(blank=True)
+
+ history = AuditlogHistoryField()
+
+
+class SimpleMappingModel(models.Model):
+
+~~ sku = models.CharField(max_length=100)
+~~ vtxt = models.CharField(verbose_name='Version', max_length=100)
+~~ not_mapped = models.CharField(max_length=100)
+
+ history = AuditlogHistoryField()
+
+
+class AdditionalDataIncludedModel(models.Model):
+
+~~ label = models.CharField(max_length=100)
+~~ text = models.TextField(blank=True)
+~~ related = models.ForeignKey(to=SimpleModel, on_delete=models.CASCADE)
+
+ history = AuditlogHistoryField()
+
+ def get_additional_data(self):
+ object_details = {
+ 'related_model_id': self.related.id,
+ 'related_model_text': self.related.text
+ }
+ return object_details
+
+
+class DateTimeFieldModel(models.Model):
+~~ label = models.CharField(max_length=100)
+~~ timestamp = models.DateTimeField()
+~~ date = models.DateField()
+~~ time = models.TimeField()
+~~ naive_dt = models.DateTimeField(null=True, blank=True)
+
+ history = AuditlogHistoryField()
+
+
+class ChoicesFieldModel(models.Model):
+ RED = 'r'
+ YELLOW = 'y'
+ GREEN = 'g'
+
+ STATUS_CHOICES = (
+ (RED, 'Red'),
+ (YELLOW, 'Yellow'),
+ (GREEN, 'Green'),
+ )
+
+~~ status = models.CharField(max_length=1, choices=STATUS_CHOICES)
+ multiselect = MultiSelectField(max_length=3, choices=STATUS_CHOICES, max_choices=3)
+~~ multiplechoice = models.CharField(max_length=255, choices=STATUS_CHOICES)
+
+ history = AuditlogHistoryField()
+
+
+class CharfieldTextfieldModel(models.Model):
+
+~~ longchar = models.CharField(max_length=255)
+~~ longtextfield = models.TextField()
+
+ history = AuditlogHistoryField()
+
+
+class PostgresArrayFieldModel(models.Model):
+ RED = 'r'
+ YELLOW = 'y'
+ GREEN = 'g'
+
+ STATUS_CHOICES = (
+ (RED, 'Red'),
+ (YELLOW, 'Yellow'),
+ (GREEN, 'Green'),
+ )
+
+ arrayfield = ArrayField(models.CharField(max_length=1, choices=STATUS_CHOICES), size=3)
+
+ history = AuditlogHistoryField()
+
+
+class NoDeleteHistoryModel(models.Model):
+~~ integer = models.IntegerField(blank=True, null=True)
+
+ history = AuditlogHistoryField(delete_related=False)
+
+
+auditlog.register(AltPrimaryKeyModel)
+auditlog.register(UUIDPrimaryKeyModel)
+auditlog.register(ProxyModel)
+auditlog.register(RelatedModel)
+auditlog.register(ManyRelatedModel)
+auditlog.register(ManyRelatedModel.related.through)
+auditlog.register(SimpleExcludeModel, exclude_fields=['text'])
+auditlog.register(SimpleMappingModel, mapping_fields={'sku': 'Product No.'})
+auditlog.register(AdditionalDataIncludedModel)
+auditlog.register(DateTimeFieldModel)
+auditlog.register(ChoicesFieldModel)
+auditlog.register(CharfieldTextfieldModel)
+auditlog.register(PostgresArrayFieldModel)
+auditlog.register(NoDeleteHistoryModel)
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 2 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / chair / forms.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/chair/forms.py)
+
+```python
+# forms.py
+from functools import reduce
+
+from django import forms
+from django.contrib.auth import get_user_model
+~~from django.db import models
+from django.db.models import Q, F, Count, Max, Subquery, OuterRef, Value
+from django.db.models.functions import Concat
+from django.forms import MultipleChoiceField, ChoiceField, Form
+from django.urls import reverse
+from django.utils.translation import ugettext_lazy as _
+
+from django_countries import countries
+
+from conferences.models import Conference, ArtifactDescriptor
+from gears.widgets import CustomCheckboxSelectMultiple, CustomFileInput
+from review.models import Reviewer, Review, ReviewStats
+from review.utilities import get_average_score
+from submissions.models import Submission, Attachment
+from users.models import Profile
+
+User = get_user_model()
+
+
+def clean_data_to_int(iterable, empty=None):
+ return [int(x) if x != '' else None for x in iterable]
+
+
+def q_or(disjuncts, default=True):
+ if disjuncts:
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ assert isinstance(self.instance, Conference)
+ countries_dict = dict(countries)
+
+ self.fields['countries'].choices = [
+ (code, countries_dict[code]) for code in
+ Profile.objects.filter(country__isnull=False).values_list(
+ 'country', flat=True).order_by('country').distinct()]
+
+ self.fields['affiliations'].choices = [
+ (aff, aff) for aff in
+ Profile.objects.values_list('affiliation', flat=True).order_by(
+ 'affiliation').distinct() if aff]
+
+ def order_profiles(self, profiles):
+ order = self.cleaned_data['order']
+ direction = '-' if self.cleaned_data['direction'] == 'DESC' else ''
+ if order == self.ORDER_BY_ID or not order:
+ return profiles.order_by(f'{direction}pk')
+
+ elif order == self.ORDER_BY_NAME:
+ profiles = profiles.annotate(
+ full_name=Concat(
+ 'last_name', Value(' '), 'first_name',
+~~ output_field=models.CharField()))
+ return profiles.order_by(f'{direction}full_name')
+
+ return profiles
+
+ def apply_term(self, profiles):
+ term = self.cleaned_data['term']
+ for word in term.lower().split():
+ profiles = profiles.filter(
+ Q(pk__icontains=word) | Q(first_name__icontains=word) |
+ Q(last_name__icontains=word) |
+ Q(first_name_rus__icontains=word) |
+ Q(last_name_rus__icontains=word) |
+ Q(middle_name_rus__icontains=word))
+ return profiles
+
+ def apply_countries(self, profiles):
+ data = self.cleaned_data['countries']
+ if data:
+ profiles = profiles.filter(country__in=data)
+ return profiles
+
+ def apply_affiliations(self, profiles):
+ data = self.cleaned_data['affiliations']
+ if data:
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 3 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / tests.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/./tests.py)
+
+```python
+# tests.py
+from __future__ import unicode_literals
+
+import json
+import requests
+from datetime import date, datetime
+
+import django
+from django.core.files.base import ContentFile
+~~from django.db import models
+from django.test import RequestFactory, TestCase
+from django.utils.http import base36_to_int, int_to_base36
+from django.views import csrf
+
+from . import utils
+
+
+try:
+ from unittest.mock import Mock, patch
+except ImportError:
+ from mock import Mock, patch # noqa
+
+
+class MockedResponse(object):
+ def __init__(self, status_code, content, headers=None):
+ if headers is None:
+ headers = {}
+
+ self.status_code = status_code
+ self.content = content.encode('utf8')
+ self.headers = headers
+
+ def json(self):
+ return json.loads(self.text)
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+ def test_email_validation(self):
+ s = 'this.email.address.is.a.bit.too.long.but.should.still.validate@example.com' # noqa
+ self.assertEqual(s, utils.valid_email_or_none(s))
+
+ def test_serializer(self):
+
+ class SomeValue:
+ pass
+
+ some_value = SomeValue()
+
+ class SomeField(models.Field):
+ def get_prep_value(self, value):
+ return 'somevalue'
+ if django.VERSION < (3, 0):
+ def from_db_value(
+ self, value, expression, connection, context
+ ):
+ return some_value
+ else:
+ def from_db_value(self, value, expression, connection):
+ return some_value
+
+ class SomeModel(models.Model):
+~~ dt = models.DateTimeField()
+~~ t = models.TimeField()
+~~ d = models.DateField()
+~~ img1 = models.ImageField()
+~~ img2 = models.ImageField()
+~~ img3 = models.ImageField()
+ something = SomeField()
+
+ def method(self):
+ pass
+
+ instance = SomeModel(dt=datetime.now(),
+ d=date.today(),
+ something=some_value,
+ t=datetime.now().time())
+ content_file = ContentFile(b'%PDF')
+ content_file.name = 'foo.pdf'
+ instance.img1 = content_file
+ instance.img2 = 'foo.png'
+ instance.method = method
+ instance.nonfield = 'hello'
+ data = utils.serialize_instance(instance)
+ instance2 = utils.deserialize_instance(SomeModel, data)
+ self.assertEqual(getattr(instance, 'method', None), method)
+ self.assertEqual(getattr(instance2, 'method', None), None)
+ self.assertEqual(instance2.something, some_value)
+ self.assertEqual(instance2.img1.name, 'foo.pdf')
+ self.assertEqual(instance2.img2.name, 'foo.png')
+ self.assertEqual(instance2.img3.name, '')
+ self.assertEqual(instance.nonfield, instance2.nonfield)
+ self.assertEqual(instance.d, instance2.d)
+ self.assertEqual(instance.dt.date(), instance2.dt.date())
+ for t1, t2 in [(instance.t, instance2.t),
+ (instance.dt.time(), instance2.dt.time())]:
+ self.assertEqual(t1.hour, t2.hour)
+ self.assertEqual(t1.minute, t2.minute)
+ self.assertEqual(t1.second, t2.second)
+ self.assertEqual(int(t1.microsecond / 1000),
+ int(t2.microsecond / 1000))
+
+ def test_serializer_binary_field(self):
+ class SomeBinaryModel(models.Model):
+~~ bb = models.BinaryField()
+~~ bb_empty = models.BinaryField()
+
+ instance = SomeBinaryModel(bb=b'some binary data')
+
+ serialized = utils.serialize_instance(instance)
+ deserialized = utils.deserialize_instance(SomeBinaryModel, serialized)
+
+ self.assertEqual(serialized['bb'], 'c29tZSBiaW5hcnkgZGF0YQ==')
+ self.assertEqual(serialized['bb_empty'], '')
+ self.assertEqual(deserialized.bb, b'some binary data')
+ self.assertEqual(deserialized.bb_empty, b'')
+
+ def test_build_absolute_uri(self):
+ self.assertEqual(
+ utils.build_absolute_uri(None, '/foo'),
+ 'http://example.com/foo')
+ self.assertEqual(
+ utils.build_absolute_uri(None, '/foo', protocol='ftp'),
+ 'ftp://example.com/foo')
+ self.assertEqual(
+ utils.build_absolute_uri(None, 'http://foo.com/bar'),
+ 'http://foo.com/bar')
+
+ def test_int_to_base36(self):
+ n = 55798679658823689999
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 4 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / models.py**](https://github.com/jazzband/django-axes/blob/master/axes/./models.py)
+
+```python
+# models.py
+~~from django.db import models
+from django.utils.translation import gettext_lazy as _
+
+
+class AccessBase(models.Model):
+~~ user_agent = models.CharField(_("User Agent"), max_length=255, db_index=True)
+
+~~ ip_address = models.GenericIPAddressField(_("IP Address"), null=True, db_index=True)
+
+~~ username = models.CharField(_("Username"), max_length=255, null=True, db_index=True)
+
+~~ http_accept = models.CharField(_("HTTP Accept"), max_length=1025)
+
+~~ path_info = models.CharField(_("Path"), max_length=255)
+
+~~ attempt_time = models.DateTimeField(_("Attempt Time"), auto_now_add=True)
+
+ class Meta:
+ app_label = "axes"
+ abstract = True
+ ordering = ["-attempt_time"]
+
+
+class AccessAttempt(AccessBase):
+~~ get_data = models.TextField(_("GET Data"))
+
+~~ post_data = models.TextField(_("POST Data"))
+
+~~ failures_since_start = models.PositiveIntegerField(_("Failed Logins"))
+
+ def __str__(self):
+ return f"Attempted Access: {self.attempt_time}"
+
+ class Meta:
+ verbose_name = _("access attempt")
+ verbose_name_plural = _("access attempts")
+
+
+class AccessLog(AccessBase):
+~~ logout_time = models.DateTimeField(_("Logout Time"), null=True, blank=True)
+
+ def __str__(self):
+ return f"Access Log for {self.username} @ {self.attempt_time}"
+
+ class Meta:
+ verbose_name = _("access log")
+ verbose_name_plural = _("access logs")
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 5 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / migrations / 0008_auto_20150208_2149.py**](https://github.com/divio/django-cms/blob/develop/cms/migrations/0008_auto_20150208_2149.py)
+
+```python
+# 0008_auto_20150208_2149.py
+from __future__ import unicode_literals
+
+~~from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('cms', '0007_auto_20141028_1559'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='title',
+ name='redirect',
+~~ field=models.CharField(max_length=2048, null=True, verbose_name='redirect', blank=True),
+ preserve_default=True,
+ ),
+ ]
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 6 from django-extensions
+[django-extensions](https://github.com/django-extensions/django-extensions)
+([project documentation](https://django-extensions.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-extensions/))
+is a [Django](/django.html) project that adds a bunch of additional
+useful commands to the `manage.py` interface. This
+[GoDjango video](https://www.youtube.com/watch?v=1F6G3ONhr4k) provides a
+quick overview of what you get when you install it into your Python
+environment.
+
+The django-extensions project is open sourced under the
+[MIT license](https://github.com/django-extensions/django-extensions/blob/master/LICENSE).
+
+[**django-extensions / django_extensions / admin / __init__.py**](https://github.com/django-extensions/django-extensions/blob/master/django_extensions/admin/__init__.py)
+
+```python
+# __init__.py
+import six
+import operator
+from functools import update_wrapper
+from six.moves import reduce
+from typing import Tuple, Dict, Callable # NOQA
+
+from django.apps import apps
+from django.http import HttpResponse, HttpResponseNotFound
+from django.conf import settings
+~~from django.db import models
+from django.db.models.query import QuerySet
+from django.utils.encoding import smart_str
+from django.utils.translation import gettext as _
+from django.utils.text import get_text_list
+from django.contrib import admin
+
+from django_extensions.admin.widgets import ForeignKeySearchInput
+
+
+class ForeignKeyAutocompleteAdminMixin:
+
+ related_search_fields = {} # type: Dict[str, Tuple[str]]
+ related_string_functions = {} # type: Dict[str, Callable]
+ autocomplete_limit = getattr(settings, 'FOREIGNKEY_AUTOCOMPLETE_LIMIT', None)
+
+ def get_urls(self):
+ from django.urls import path
+
+ def wrap(view):
+ def wrapper(*args, **kwargs):
+ return self.admin_site.admin_view(view)(*args, **kwargs)
+ return update_wrapper(wrapper, view)
+
+ return [
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ data = ''.join([six.u('%s|%s\n') % (to_string_function(f), f.pk) for f in queryset])
+ elif object_pk:
+ try:
+ obj = queryset.get(pk=object_pk)
+ except Exception: # FIXME: use stricter exception checking
+ pass
+ else:
+ data = to_string_function(obj)
+ return HttpResponse(data, content_type='text/plain')
+ return HttpResponseNotFound()
+
+ def get_related_filter(self, model, request):
+ return None
+
+ def get_help_text(self, field_name, model_name):
+ searchable_fields = self.related_search_fields.get(field_name, None)
+ if searchable_fields:
+ help_kwargs = {
+ 'model_name': model_name,
+ 'field_list': get_text_list(searchable_fields, _('and')),
+ }
+ return _('Use the left field to do %(model_name)s lookups in the fields %(field_list)s.') % help_kwargs
+ return ''
+
+ def formfield_for_dbfield(self, db_field, **kwargs):
+~~ if isinstance(db_field, models.ForeignKey) and db_field.name in self.related_search_fields:
+ help_text = self.get_help_text(db_field.name, db_field.remote_field.model._meta.object_name)
+ if kwargs.get('help_text'):
+ help_text = six.u('%s %s' % (kwargs['help_text'], help_text))
+ kwargs['widget'] = ForeignKeySearchInput(db_field.remote_field, self.related_search_fields[db_field.name])
+ kwargs['help_text'] = help_text
+ return super().formfield_for_dbfield(db_field, **kwargs)
+
+
+class ForeignKeyAutocompleteAdmin(ForeignKeyAutocompleteAdminMixin, admin.ModelAdmin):
+ pass
+
+
+class ForeignKeyAutocompleteTabularInline(ForeignKeyAutocompleteAdminMixin, admin.TabularInline):
+ pass
+
+
+class ForeignKeyAutocompleteStackedInline(ForeignKeyAutocompleteAdminMixin, admin.StackedInline):
+ pass
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 7 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / migrations / 0006_auto_20160623_1627.py**](https://github.com/divio/django-filer/blob/develop/filer/migrations/0006_auto_20160623_1627.py)
+
+```python
+# 0006_auto_20160623_1627.py
+from __future__ import unicode_literals
+
+import django.db.models.deletion
+~~from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('filer', '0005_auto_20160623_1425'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='image',
+ name='file_ptr',
+~~ field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, related_name='%(app_label)s_%(class)s_file', serialize=False, to='filer.File'),
+ ),
+ ]
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 8 from django-filter
+[django-filter](https://github.com/carltongibson/django-filter)
+([project documentation](https://django-filter.readthedocs.io/en/master/)
+and
+[PyPI page](https://pypi.org/project/django-filter/2.2.0/))
+makes it easier to filter down querysets from the
+[Django ORM](/django-orm.html) by providing common bits of boilerplate
+code. django-filter is provided as
+[open source](https://github.com/carltongibson/django-filter/blob/master/LICENSE).
+
+[**django-filter / django_filters / filterset.py**](https://github.com/carltongibson/django-filter/blob/master/django_filters/./filterset.py)
+
+```python
+# filterset.py
+import copy
+from collections import OrderedDict
+
+from django import forms
+~~from django.db import models
+from django.db.models.constants import LOOKUP_SEP
+from django.db.models.fields.related import (
+ ManyToManyRel,
+ ManyToOneRel,
+ OneToOneRel
+)
+
+from .conf import settings
+from .constants import ALL_FIELDS
+from .filters import (
+ BaseInFilter,
+ BaseRangeFilter,
+ BooleanFilter,
+ CharFilter,
+ ChoiceFilter,
+ DateFilter,
+ DateTimeFilter,
+ DurationFilter,
+ Filter,
+ ModelChoiceFilter,
+ ModelMultipleChoiceFilter,
+ NumberFilter,
+ TimeFilter,
+ UUIDFilter
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ if isinstance(obj, Filter)
+ ]
+
+ for filter_name, f in filters:
+ if getattr(f, 'field_name', None) is None:
+ f.field_name = filter_name
+
+ filters.sort(key=lambda x: x[1].creation_counter)
+
+ known = set(attrs)
+
+ def visit(name):
+ known.add(name)
+ return name
+
+ base_filters = [
+ (visit(name), f)
+ for base in bases if hasattr(base, 'declared_filters')
+ for name, f in base.declared_filters.items() if name not in known
+ ]
+
+ return OrderedDict(base_filters + filters)
+
+
+FILTER_FOR_DBFIELD_DEFAULTS = {
+~~ models.AutoField: {'filter_class': NumberFilter},
+~~ models.CharField: {'filter_class': CharFilter},
+~~ models.TextField: {'filter_class': CharFilter},
+~~ models.BooleanField: {'filter_class': BooleanFilter},
+~~ models.DateField: {'filter_class': DateFilter},
+~~ models.DateTimeField: {'filter_class': DateTimeFilter},
+~~ models.TimeField: {'filter_class': TimeFilter},
+~~ models.DurationField: {'filter_class': DurationFilter},
+~~ models.DecimalField: {'filter_class': NumberFilter},
+~~ models.SmallIntegerField: {'filter_class': NumberFilter},
+~~ models.IntegerField: {'filter_class': NumberFilter},
+~~ models.PositiveIntegerField: {'filter_class': NumberFilter},
+~~ models.PositiveSmallIntegerField: {'filter_class': NumberFilter},
+~~ models.FloatField: {'filter_class': NumberFilter},
+~~ models.NullBooleanField: {'filter_class': BooleanFilter},
+~~ models.SlugField: {'filter_class': CharFilter},
+~~ models.EmailField: {'filter_class': CharFilter},
+~~ models.FilePathField: {'filter_class': CharFilter},
+~~ models.URLField: {'filter_class': CharFilter},
+~~ models.GenericIPAddressField: {'filter_class': CharFilter},
+~~ models.CommaSeparatedIntegerField: {'filter_class': CharFilter},
+~~ models.UUIDField: {'filter_class': UUIDFilter},
+
+~~ models.OneToOneField: {
+ 'filter_class': ModelChoiceFilter,
+ 'extra': lambda f: {
+ 'queryset': remote_queryset(f),
+ 'to_field_name': f.remote_field.field_name,
+ 'null_label': settings.NULL_CHOICE_LABEL if f.null else None,
+ }
+ },
+~~ models.ForeignKey: {
+ 'filter_class': ModelChoiceFilter,
+ 'extra': lambda f: {
+ 'queryset': remote_queryset(f),
+ 'to_field_name': f.remote_field.field_name,
+ 'null_label': settings.NULL_CHOICE_LABEL if f.null else None,
+ }
+ },
+~~ models.ManyToManyField: {
+ 'filter_class': ModelMultipleChoiceFilter,
+ 'extra': lambda f: {
+ 'queryset': remote_queryset(f),
+ }
+ },
+
+ OneToOneRel: {
+ 'filter_class': ModelChoiceFilter,
+ 'extra': lambda f: {
+ 'queryset': remote_queryset(f),
+ 'null_label': settings.NULL_CHOICE_LABEL if f.null else None,
+ }
+ },
+ ManyToOneRel: {
+ 'filter_class': ModelMultipleChoiceFilter,
+ 'extra': lambda f: {
+ 'queryset': remote_queryset(f),
+ }
+ },
+ ManyToManyRel: {
+ 'filter_class': ModelMultipleChoiceFilter,
+ 'extra': lambda f: {
+ 'queryset': remote_queryset(f),
+ }
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ queryset = self._meta.model._default_manager.all()
+ model = queryset.model
+
+ self.is_bound = data is not None
+ self.data = data or {}
+ self.queryset = queryset
+ self.request = request
+ self.form_prefix = prefix
+
+ self.filters = copy.deepcopy(self.base_filters)
+
+ for filter_ in self.filters.values():
+ filter_.model = model
+ filter_.parent = self
+
+ def is_valid(self):
+ return self.is_bound and self.form.is_valid()
+
+ @property
+ def errors(self):
+ return self.form.errors
+
+ def filter_queryset(self, queryset):
+ for name, value in self.form.cleaned_data.items():
+ queryset = self.filters[name].filter(queryset, value)
+~~ assert isinstance(queryset, models.QuerySet), \
+ "Expected '%s.%s' to return a QuerySet, but got a %s instead." \
+ % (type(self).__name__, name, type(queryset).__name__)
+ return queryset
+
+ @property
+ def qs(self):
+ if not hasattr(self, '_qs'):
+ qs = self.queryset.all()
+ if self.is_bound:
+ self.errors
+ qs = self.filter_queryset(qs)
+ self._qs = qs
+ return self._qs
+
+ def get_form_class(self):
+ fields = OrderedDict([
+ (name, filter_.field)
+ for name, filter_ in self.filters.items()])
+
+ return type(str('%sForm' % self.__class__.__name__),
+ (self._meta.form,), fields)
+
+ @property
+ def form(self):
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ "%s resolved field '%s' with '%s' lookup to an unrecognized field "
+ "type %s. Try adding an override to 'Meta.filter_overrides'. See: "
+ "https://django-filter.readthedocs.io/en/master/ref/filterset.html"
+ "#customise-filter-generation-with-filter-overrides"
+ ) % (cls.__name__, field_name, lookup_expr, field.__class__.__name__)
+
+ return filter_class(**default)
+
+ @classmethod
+ def filter_for_lookup(cls, field, lookup_type):
+ DEFAULTS = dict(cls.FILTER_DEFAULTS)
+ if hasattr(cls, '_meta'):
+ DEFAULTS.update(cls._meta.filter_overrides)
+
+ data = try_dbfield(DEFAULTS.get, field.__class__) or {}
+ filter_class = data.get('filter_class')
+ params = data.get('extra', lambda field: {})(field)
+
+ if not filter_class:
+ return None, {}
+
+ if lookup_type == 'exact' and getattr(field, 'choices', None):
+ return ChoiceFilter, {'choices': field.choices}
+
+ if lookup_type == 'isnull':
+~~ data = try_dbfield(DEFAULTS.get, models.BooleanField)
+
+ filter_class = data.get('filter_class')
+ params = data.get('extra', lambda field: {})(field)
+ return filter_class, params
+
+ if lookup_type == 'in':
+ class ConcreteInFilter(BaseInFilter, filter_class):
+ pass
+ ConcreteInFilter.__name__ = cls._csv_filter_class_name(
+ filter_class, lookup_type
+ )
+
+ return ConcreteInFilter, params
+
+ if lookup_type == 'range':
+ class ConcreteRangeFilter(BaseRangeFilter, filter_class):
+ pass
+ ConcreteRangeFilter.__name__ = cls._csv_filter_class_name(
+ filter_class, lookup_type
+ )
+
+ return ConcreteRangeFilter, params
+
+ return filter_class, params
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 9 from django-flexible-subscriptions
+[django-flexible-subscriptions](https://github.com/studybuffalo/django-flexible-subscriptions)
+([project documentation](https://django-flexible-subscriptions.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-flexible-subscriptions/))
+provides boilerplate code for adding subscription and recurrent billing
+to [Django](/django.html) web applications. Various payment providers
+can be added on the back end to run the transactions.
+
+The django-flexible-subscriptions project is open sourced under the
+[GNU General Public License v3.0](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/LICENSE).
+
+[**django-flexible-subscriptions / subscriptions / models.py**](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/subscriptions/./models.py)
+
+```python
+# models.py
+from datetime import timedelta
+from uuid import uuid4
+
+from django.contrib.auth import get_user_model
+from django.contrib.auth.models import Group
+from django.core.validators import MinValueValidator
+~~from django.db import models
+from django.utils.translation import gettext_lazy as _
+
+
+ONCE = '0'
+SECOND = '1'
+MINUTE = '2'
+HOUR = '3'
+DAY = '4'
+WEEK = '5'
+MONTH = '6'
+YEAR = '7'
+RECURRENCE_UNIT_CHOICES = (
+ (ONCE, 'once'),
+ (SECOND, 'second'),
+ (MINUTE, 'minute'),
+ (HOUR, 'hour'),
+ (DAY, 'day'),
+ (WEEK, 'week'),
+ (MONTH, 'month'),
+ (YEAR, 'year'),
+)
+
+
+class PlanTag(models.Model):
+~~ tag = models.CharField(
+ help_text=_('the tag name'),
+ max_length=64,
+ unique=True,
+ )
+
+ class Meta:
+ ordering = ('tag',)
+
+ def __str__(self):
+ return self.tag
+
+
+class SubscriptionPlan(models.Model):
+~~ id = models.UUIDField(
+ default=uuid4,
+ editable=False,
+ primary_key=True,
+ verbose_name='ID',
+ )
+~~ plan_name = models.CharField(
+ help_text=_('the name of the subscription plan'),
+ max_length=128,
+ )
+~~ slug = models.SlugField(
+ blank=True,
+ help_text=_('slug to reference the subscription plan'),
+ max_length=128,
+ null=True,
+ unique=True,
+ )
+~~ plan_description = models.CharField(
+ blank=True,
+ help_text=_('a description of the subscription plan'),
+ max_length=512,
+ null=True,
+ )
+~~ group = models.ForeignKey(
+ Group,
+ blank=True,
+ help_text=_('the Django auth group for this plan'),
+ null=True,
+~~ on_delete=models.SET_NULL,
+ related_name='plans',
+ )
+~~ tags = models.ManyToManyField(
+ PlanTag,
+ blank=True,
+ help_text=_('any tags associated with this plan'),
+ related_name='plans',
+ )
+~~ grace_period = models.PositiveIntegerField(
+ default=0,
+ help_text=_(
+ 'how many days after the subscription ends before the '
+ 'subscription expires'
+ ),
+ )
+
+ class Meta:
+ ordering = ('plan_name',)
+ permissions = (
+ ('subscriptions', 'Can interact with subscription details'),
+ )
+
+ def __str__(self):
+ return self.plan_name
+
+ def display_tags(self):
+ if self.tags.count() > 3:
+ return '{}, ...'.format(
+ ', '.join(tag.tag for tag in self.tags.all()[:3])
+ )
+
+ return ', '.join(tag.tag for tag in self.tags.all()[:3])
+
+
+class PlanCost(models.Model):
+~~ id = models.UUIDField(
+ default=uuid4,
+ editable=False,
+ primary_key=True,
+ verbose_name='ID',
+ )
+~~ plan = models.ForeignKey(
+ SubscriptionPlan,
+ help_text=_('the subscription plan for these cost details'),
+~~ on_delete=models.CASCADE,
+ related_name='costs',
+ )
+~~ slug = models.SlugField(
+ blank=True,
+ help_text=_('slug to reference these cost details'),
+ max_length=128,
+ null=True,
+ unique=True,
+ )
+~~ recurrence_period = models.PositiveSmallIntegerField(
+ default=1,
+ help_text=_('how often the plan is billed (per recurrence unit)'),
+ validators=[MinValueValidator(1)],
+ )
+~~ recurrence_unit = models.CharField(
+ choices=RECURRENCE_UNIT_CHOICES,
+ default=MONTH,
+ max_length=1,
+ )
+~~ cost = models.DecimalField(
+ blank=True,
+ decimal_places=4,
+ help_text=_('the cost per recurrence of the plan'),
+ max_digits=19,
+ null=True,
+ )
+
+ class Meta:
+ ordering = ('recurrence_unit', 'recurrence_period', 'cost',)
+
+ @property
+ def display_recurrent_unit_text(self):
+ conversion = {
+ ONCE: 'one-time',
+ SECOND: 'per second',
+ MINUTE: 'per minute',
+ HOUR: 'per hour',
+ DAY: 'per day',
+ WEEK: 'per week',
+ MONTH: 'per month',
+ YEAR: 'per year',
+ }
+
+ return conversion[self.recurrence_unit]
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ if self.recurrence_unit == SECOND:
+ delta = timedelta(seconds=self.recurrence_period)
+ elif self.recurrence_unit == MINUTE:
+ delta = timedelta(minutes=self.recurrence_period)
+ elif self.recurrence_unit == HOUR:
+ delta = timedelta(hours=self.recurrence_period)
+ elif self.recurrence_unit == DAY:
+ delta = timedelta(days=self.recurrence_period)
+ elif self.recurrence_unit == WEEK:
+ delta = timedelta(weeks=self.recurrence_period)
+ elif self.recurrence_unit == MONTH:
+ delta = timedelta(
+ days=30.4368 * self.recurrence_period
+ )
+ elif self.recurrence_unit == YEAR:
+ delta = timedelta(
+ days=365.2425 * self.recurrence_period
+ )
+ else:
+ return None
+
+ return current + delta
+
+
+class UserSubscription(models.Model):
+~~ id = models.UUIDField(
+ default=uuid4,
+ editable=False,
+ primary_key=True,
+ verbose_name='ID',
+ )
+~~ user = models.ForeignKey(
+ get_user_model(),
+ help_text=_('the user this subscription applies to'),
+ null=True,
+~~ on_delete=models.CASCADE,
+ related_name='subscriptions',
+ )
+~~ subscription = models.ForeignKey(
+ PlanCost,
+ help_text=_('the plan costs and billing frequency for this user'),
+ null=True,
+~~ on_delete=models.CASCADE,
+ related_name='subscriptions'
+ )
+~~ date_billing_start = models.DateTimeField(
+ blank=True,
+ help_text=_('the date to start billing this subscription'),
+ null=True,
+ verbose_name='billing start date',
+ )
+~~ date_billing_end = models.DateTimeField(
+ blank=True,
+ help_text=_('the date to finish billing this subscription'),
+ null=True,
+ verbose_name='billing start end',
+ )
+~~ date_billing_last = models.DateTimeField(
+ blank=True,
+ help_text=_('the last date this plan was billed'),
+ null=True,
+ verbose_name='last billing date',
+ )
+~~ date_billing_next = models.DateTimeField(
+ blank=True,
+ help_text=_('the next date billing is due'),
+ null=True,
+ verbose_name='next start date',
+ )
+~~ active = models.BooleanField(
+ default=True,
+ help_text=_('whether this subscription is active or not'),
+ )
+~~ cancelled = models.BooleanField(
+ default=False,
+ help_text=_('whether this subscription is cancelled or not'),
+ )
+
+ class Meta:
+ ordering = ('user', 'date_billing_start',)
+
+
+class SubscriptionTransaction(models.Model):
+~~ id = models.UUIDField(
+ default=uuid4,
+ editable=False,
+ primary_key=True,
+ verbose_name='ID',
+ )
+~~ user = models.ForeignKey(
+ get_user_model(),
+ help_text=_('the user that this subscription was billed for'),
+ null=True,
+~~ on_delete=models.SET_NULL,
+ related_name='subscription_transactions'
+ )
+~~ subscription = models.ForeignKey(
+ PlanCost,
+ help_text=_('the plan costs that were billed'),
+ null=True,
+~~ on_delete=models.SET_NULL,
+ related_name='transactions'
+ )
+~~ date_transaction = models.DateTimeField(
+ help_text=_('the datetime the transaction was billed'),
+ verbose_name='transaction date',
+ )
+~~ amount = models.DecimalField(
+ blank=True,
+ decimal_places=4,
+ help_text=_('how much was billed for the user'),
+ max_digits=19,
+ null=True,
+ )
+
+ class Meta:
+ ordering = ('date_transaction', 'user',)
+
+
+class PlanList(models.Model):
+~~ title = models.TextField(
+ blank=True,
+ help_text=_('title to display on the subscription plan list page'),
+ null=True,
+ )
+~~ slug = models.SlugField(
+ blank=True,
+ help_text=_('slug to reference the subscription plan list'),
+ max_length=128,
+ null=True,
+ unique=True,
+ )
+~~ subtitle = models.TextField(
+ blank=True,
+ help_text=_('subtitle to display on the subscription plan list page'),
+ null=True,
+ )
+~~ header = models.TextField(
+ blank=True,
+ help_text=_('header text to display on the subscription plan list page'),
+ null=True,
+ )
+~~ footer = models.TextField(
+ blank=True,
+ help_text=_('header text to display on the subscription plan list page'),
+ null=True,
+ )
+~~ active = models.BooleanField(
+ default=True,
+ help_text=_('whether this plan list is active or not.'),
+ )
+
+ def __str__(self):
+ return self.title
+
+
+class PlanListDetail(models.Model):
+~~ plan = models.ForeignKey(
+ SubscriptionPlan,
+~~ on_delete=models.CASCADE,
+ related_name='plan_list_details',
+ )
+~~ plan_list = models.ForeignKey(
+ PlanList,
+~~ on_delete=models.CASCADE,
+ related_name='plan_list_details',
+ )
+~~ html_content = models.TextField(
+ blank=True,
+ help_text=_('HTML content to display for plan'),
+ null=True,
+ )
+~~ subscribe_button_text = models.CharField(
+ blank=True,
+ default='Subscribe',
+ max_length=128,
+ null=True,
+ )
+~~ order = models.PositiveIntegerField(
+ default=1,
+ help_text=_('Order to display plan in (lower numbers displayed first)'),
+ )
+
+ def __str__(self):
+ return 'Plan List {} - {}'.format(
+ self.plan_list, self.plan.plan_name
+ )
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 10 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / backends.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./backends.py)
+
+```python
+# backends.py
+from django.contrib.auth import get_user_model
+~~from django.db import models
+from guardian.conf import settings
+from guardian.core import ObjectPermissionChecker
+from guardian.ctypes import get_content_type
+from guardian.exceptions import WrongAppError
+
+
+def check_object_support(obj):
+~~ return isinstance(obj, models.Model)
+
+
+def check_user_support(user_obj):
+ if not user_obj.is_authenticated:
+ if settings.ANONYMOUS_USER_NAME is None:
+ return False, user_obj
+ User = get_user_model()
+ lookup = {User.USERNAME_FIELD: settings.ANONYMOUS_USER_NAME}
+ user_obj = User.objects.get(**lookup)
+
+ return True, user_obj
+
+
+def check_support(user_obj, obj):
+ obj_support = check_object_support(obj)
+ user_support, user_obj = check_user_support(user_obj)
+ return obj_support and user_support, user_obj
+
+
+class ObjectPermissionBackend:
+ supports_object_permissions = True
+ supports_anonymous_user = True
+ supports_inactive_user = True
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 11 from django-haystack
+[django-haystack](https://github.com/django-haystack/django-haystack)
+([project website](http://haystacksearch.org/) and
+[PyPI page](https://pypi.org/project/django-haystack/))
+is a search abstraction layer that separates the Python search code
+in a [Django](/django.html) web application from the search engine
+implementation that it runs on, such as
+[Apache Solr](http://lucene.apache.org/solr/),
+[Elasticsearch](https://www.elastic.co/)
+or [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html).
+
+The django-haystack project is open source under the
+[BSD license](https://github.com/django-haystack/django-haystack/blob/master/LICENSE).
+
+[**django-haystack / haystack / signals.py**](https://github.com/django-haystack/django-haystack/blob/master/haystack/./signals.py)
+
+```python
+# signals.py
+~~from django.db import models
+
+from haystack.exceptions import NotHandled
+
+
+class BaseSignalProcessor(object):
+
+ def __init__(self, connections, connection_router):
+ self.connections = connections
+ self.connection_router = connection_router
+ self.setup()
+
+ def setup(self):
+ pass
+
+ def teardown(self):
+ pass
+
+ def handle_save(self, sender, instance, **kwargs):
+ using_backends = self.connection_router.for_write(instance=instance)
+
+ for using in using_backends:
+ try:
+ index = self.connections[using].get_unified_index().get_index(sender)
+ index.update_object(instance, using=using)
+ except NotHandled:
+ pass
+
+ def handle_delete(self, sender, instance, **kwargs):
+ using_backends = self.connection_router.for_write(instance=instance)
+
+ for using in using_backends:
+ try:
+ index = self.connections[using].get_unified_index().get_index(sender)
+ index.remove_object(instance, using=using)
+ except NotHandled:
+ pass
+
+
+class RealtimeSignalProcessor(BaseSignalProcessor):
+
+ def setup(self):
+~~ models.signals.post_save.connect(self.handle_save)
+~~ models.signals.post_delete.connect(self.handle_delete)
+
+ def teardown(self):
+~~ models.signals.post_save.disconnect(self.handle_save)
+~~ models.signals.post_delete.disconnect(self.handle_delete)
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 12 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / models.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/./models.py)
+
+```python
+# models.py
+~~from django.db import models
+from django.utils import timezone
+from django.utils.encoding import python_2_unicode_compatible
+from django.utils.translation import ugettext_lazy as _
+
+
+@python_2_unicode_compatible
+class Bookmark(models.Model):
+~~ url = models.URLField(verbose_name=_('URL'))
+~~ title = models.CharField(verbose_name=_('title'), max_length=255)
+~~ user = models.PositiveIntegerField(verbose_name=_('user'))
+~~ date_add = models.DateTimeField(verbose_name=_('date created'), default=timezone.now)
+
+ class Meta:
+ verbose_name = _('bookmark')
+ verbose_name_plural = _('bookmarks')
+ ordering = ('date_add',)
+
+ def __str__(self):
+ return self.title
+
+
+@python_2_unicode_compatible
+class PinnedApplication(models.Model):
+~~ app_label = models.CharField(verbose_name=_('application name'), max_length=255)
+~~ user = models.PositiveIntegerField(verbose_name=_('user'))
+~~ date_add = models.DateTimeField(verbose_name=_('date created'), default=timezone.now)
+
+ class Meta:
+ verbose_name = _('pinned application')
+ verbose_name_plural = _('pinned applications')
+ ordering = ('date_add',)
+
+ def __str__(self):
+ return self.app_label
+
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 13 from django-jsonfield
+[django-jsonfield](https://github.com/dmkoch/django-jsonfield)
+([jsonfield on PyPi](https://pypi.org/project/jsonfield/)) is a
+[Django](/django.html) code library that makes it easier to store validated
+JSON in a [Django object-relational mapper (ORM)](/django-orm.html) database
+model.
+
+The django-jsonfield project is open source under the
+[MIT license](https://github.com/dmkoch/django-jsonfield/blob/master/LICENSE).
+
+[**django-jsonfield / src/jsonfield / fields.py**](https://github.com/dmkoch/django-jsonfield/blob/master/src/jsonfield/./fields.py)
+
+```python
+# fields.py
+import copy
+import json
+import warnings
+
+~~from django.db import models
+from django.forms import ValidationError
+from django.utils.translation import gettext_lazy as _
+
+from . import forms
+from .encoder import JSONEncoder
+from .json import JSONString, checked_loads
+
+DEFAULT_DUMP_KWARGS = {
+ 'cls': JSONEncoder,
+}
+
+DEFAULT_LOAD_KWARGS = {}
+
+INVALID_JSON_WARNING = (
+ '{0!s} failed to load invalid json ({1}) from the database. The value has '
+ 'been returned as a string instead.'
+)
+
+
+class JSONFieldMixin(models.Field):
+ form_class = forms.JSONField
+
+ def __init__(self, *args, dump_kwargs=None, load_kwargs=None, **kwargs):
+ self.dump_kwargs = DEFAULT_DUMP_KWARGS if dump_kwargs is None else dump_kwargs
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ def get_prep_value(self, value):
+ if self.null and value is None:
+ return None
+ return json.dumps(value, **self.dump_kwargs)
+
+ def value_to_string(self, obj):
+ value = self.value_from_object(obj)
+ return json.dumps(value, **self.dump_kwargs)
+
+ def formfield(self, **kwargs):
+ kwargs.setdefault('form_class', self.form_class)
+ if issubclass(kwargs['form_class'], forms.JSONField):
+ kwargs.setdefault('dump_kwargs', self.dump_kwargs)
+ kwargs.setdefault('load_kwargs', self.load_kwargs)
+
+ return super().formfield(**kwargs)
+
+ def get_default(self):
+ if self.has_default():
+ if callable(self.default):
+ return self.default()
+ return copy.deepcopy(self.default)
+ return super().get_default()
+
+
+~~class JSONField(JSONFieldMixin, models.TextField):
+
+ def formfield(self, **kwargs):
+ field = super().formfield(**kwargs)
+ if isinstance(field, forms.JSONField):
+ field.dump_kwargs.setdefault('indent', 4)
+ field.dump_kwargs.setdefault('ensure_ascii', False)
+ return field
+
+
+~~class JSONCharField(JSONFieldMixin, models.CharField):
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 14 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / models.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./models.py)
+
+```python
+# models.py
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db import models, transaction, router
+from django.db.models.signals import post_save, pre_save
+from django.utils.translation import gettext_lazy as _
+
+from model_utils.fields import (
+ AutoCreatedField,
+ AutoLastModifiedField,
+ StatusField,
+ MonitorField,
+ UUIDField,
+)
+from model_utils.managers import (
+ QueryManager,
+ SoftDeletableManager,
+)
+
+from django.db.models.functions import Now
+now = Now()
+
+
+class TimeStampedModel(models.Model):
+ created = AutoCreatedField(_('created'))
+ modified = AutoLastModifiedField(_('modified'))
+
+ def save(self, *args, **kwargs):
+ if 'update_fields' in kwargs and 'modified' not in kwargs['update_fields']:
+ kwargs['update_fields'] += ['modified']
+ super().save(*args, **kwargs)
+
+ class Meta:
+ abstract = True
+
+
+class TimeFramedModel(models.Model):
+~~ start = models.DateTimeField(_('start'), null=True, blank=True)
+~~ end = models.DateTimeField(_('end'), null=True, blank=True)
+
+ class Meta:
+ abstract = True
+
+
+class StatusModel(models.Model):
+ status = StatusField(_('status'))
+ status_changed = MonitorField(_('status changed'), monitor='status')
+
+ class Meta:
+ abstract = True
+
+
+def add_status_query_managers(sender, **kwargs):
+ if not issubclass(sender, StatusModel):
+ return
+
+ default_manager = sender._meta.default_manager
+
+ for value, display in getattr(sender, 'STATUS', ()):
+ if _field_exists(sender, value):
+ raise ImproperlyConfigured(
+ "StatusModel: Model '%s' has a field named '%s' which "
+ "conflicts with a status of the same name."
+ % (sender.__name__, value)
+ )
+ sender.add_to_class(value, QueryManager(status=value))
+
+ sender._meta.default_manager_name = default_manager.name
+
+
+def add_timeframed_query_manager(sender, **kwargs):
+ if not issubclass(sender, TimeFramedModel):
+ return
+ if _field_exists(sender, 'timeframed'):
+ raise ImproperlyConfigured(
+ "Model '%s' has a field named 'timeframed' "
+ "which conflicts with the TimeFramedModel manager."
+ % sender.__name__
+ )
+ sender.add_to_class('timeframed', QueryManager(
+~~ (models.Q(start__lte=now) | models.Q(start__isnull=True))
+~~ & (models.Q(end__gte=now) | models.Q(end__isnull=True))
+ ))
+
+
+models.signals.class_prepared.connect(add_status_query_managers)
+models.signals.class_prepared.connect(add_timeframed_query_manager)
+
+
+def _field_exists(model_class, field_name):
+ return field_name in [f.attname for f in model_class._meta.local_fields]
+
+
+class SoftDeletableModel(models.Model):
+~~ is_removed = models.BooleanField(default=False)
+
+ class Meta:
+ abstract = True
+
+ objects = SoftDeletableManager()
+~~ all_objects = models.Manager()
+
+ def delete(self, using=None, soft=True, *args, **kwargs):
+ if soft:
+ self.is_removed = True
+ self.save(using=using)
+ else:
+ return super().delete(using=using, *args, **kwargs)
+
+
+class UUIDModel(models.Model):
+ id = UUIDField(
+ primary_key=True,
+ version=4,
+ editable=False,
+ )
+
+ class Meta:
+ abstract = True
+
+
+class SaveSignalHandlingModel(models.Model):
+ class Meta:
+ abstract = True
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 15 from django-oauth-toolkit
+[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit)
+([project website](http://dot.evonove.it/) and
+[PyPI package information](https://pypi.org/project/django-oauth-toolkit/1.2.0/))
+is a code library for adding and handling [OAuth2](https://oauth.net/)
+flows within your [Django](/django.html) web application and
+[API](/application-programming-interfaces.html).
+
+The django-oauth-toolkit project is open sourced under the
+[FreeBSD license](https://github.com/jazzband/django-oauth-toolkit/blob/master/LICENSE)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-oauth-toolkit / oauth2_provider / models.py**](https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/./models.py)
+
+```python
+# models.py
+import logging
+from datetime import timedelta
+from urllib.parse import parse_qsl, urlparse
+
+from django.apps import apps
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db import models, transaction
+from django.urls import reverse
+from django.utils import timezone
+from django.utils.translation import gettext_lazy as _
+
+from .generators import generate_client_id, generate_client_secret
+from .scopes import get_scopes_backend
+from .settings import oauth2_settings
+from .validators import RedirectURIValidator, WildcardSet
+
+
+logger = logging.getLogger(__name__)
+
+
+class AbstractApplication(models.Model):
+ CLIENT_CONFIDENTIAL = "confidential"
+ CLIENT_PUBLIC = "public"
+ CLIENT_TYPES = (
+ (CLIENT_CONFIDENTIAL, _("Confidential")),
+ (CLIENT_PUBLIC, _("Public")),
+ )
+
+ GRANT_AUTHORIZATION_CODE = "authorization-code"
+ GRANT_IMPLICIT = "implicit"
+ GRANT_PASSWORD = "password"
+ GRANT_CLIENT_CREDENTIALS = "client-credentials"
+ GRANT_TYPES = (
+ (GRANT_AUTHORIZATION_CODE, _("Authorization code")),
+ (GRANT_IMPLICIT, _("Implicit")),
+ (GRANT_PASSWORD, _("Resource owner password-based")),
+ (GRANT_CLIENT_CREDENTIALS, _("Client credentials")),
+ )
+
+~~ id = models.BigAutoField(primary_key=True)
+~~ client_id = models.CharField(
+ max_length=100, unique=True, default=generate_client_id, db_index=True
+ )
+~~ user = models.ForeignKey(
+ settings.AUTH_USER_MODEL,
+ related_name="%(app_label)s_%(class)s",
+~~ null=True, blank=True, on_delete=models.CASCADE
+ )
+
+~~ redirect_uris = models.TextField(
+ blank=True, help_text=_("Allowed URIs list, space separated"),
+ )
+~~ client_type = models.CharField(max_length=32, choices=CLIENT_TYPES)
+~~ authorization_grant_type = models.CharField(
+ max_length=32, choices=GRANT_TYPES
+ )
+~~ client_secret = models.CharField(
+ max_length=255, blank=True, default=generate_client_secret, db_index=True
+ )
+~~ name = models.CharField(max_length=255, blank=True)
+~~ skip_authorization = models.BooleanField(default=False)
+
+~~ created = models.DateTimeField(auto_now_add=True)
+~~ updated = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ abstract = True
+
+ def __str__(self):
+ return self.name or self.client_id
+
+ @property
+ def default_redirect_uri(self):
+ if self.redirect_uris:
+ return self.redirect_uris.split().pop(0)
+
+ assert False, (
+ "If you are using implicit, authorization_code"
+ "or all-in-one grant_type, you must define "
+ "redirect_uris field in your Application model"
+ )
+
+ def redirect_uri_allowed(self, uri):
+ parsed_uri = urlparse(uri)
+ uqs_set = set(parse_qsl(parsed_uri.query))
+ for allowed_uri in self.redirect_uris.split():
+ parsed_allowed_uri = urlparse(allowed_uri)
+
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+
+class ApplicationManager(models.Manager):
+ def get_by_natural_key(self, client_id):
+ return self.get(client_id=client_id)
+
+
+class Application(AbstractApplication):
+ objects = ApplicationManager()
+
+ class Meta(AbstractApplication.Meta):
+ swappable = "OAUTH2_PROVIDER_APPLICATION_MODEL"
+
+ def natural_key(self):
+ return (self.client_id,)
+
+
+class AbstractGrant(models.Model):
+ CODE_CHALLENGE_PLAIN = "plain"
+ CODE_CHALLENGE_S256 = "S256"
+ CODE_CHALLENGE_METHODS = (
+ (CODE_CHALLENGE_PLAIN, "plain"),
+ (CODE_CHALLENGE_S256, "S256")
+ )
+
+~~ id = models.BigAutoField(primary_key=True)
+~~ user = models.ForeignKey(
+~~ settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
+ related_name="%(app_label)s_%(class)s"
+ )
+~~ code = models.CharField(max_length=255, unique=True) # code comes from oauthlib
+~~ application = models.ForeignKey(
+~~ oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE
+ )
+~~ expires = models.DateTimeField()
+~~ redirect_uri = models.CharField(max_length=255)
+~~ scope = models.TextField(blank=True)
+
+~~ created = models.DateTimeField(auto_now_add=True)
+~~ updated = models.DateTimeField(auto_now=True)
+
+~~ code_challenge = models.CharField(max_length=128, blank=True, default="")
+~~ code_challenge_method = models.CharField(
+ max_length=10, blank=True, default="", choices=CODE_CHALLENGE_METHODS)
+
+ def is_expired(self):
+ if not self.expires:
+ return True
+
+ return timezone.now() >= self.expires
+
+ def redirect_uri_allowed(self, uri):
+ return uri == self.redirect_uri
+
+ def __str__(self):
+ return self.code
+
+ class Meta:
+ abstract = True
+
+
+class Grant(AbstractGrant):
+ class Meta(AbstractGrant.Meta):
+ swappable = "OAUTH2_PROVIDER_GRANT_MODEL"
+
+
+class AbstractAccessToken(models.Model):
+~~ id = models.BigAutoField(primary_key=True)
+~~ user = models.ForeignKey(
+~~ settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True,
+ related_name="%(app_label)s_%(class)s"
+ )
+~~ source_refresh_token = models.OneToOneField(
+~~ oauth2_settings.REFRESH_TOKEN_MODEL, on_delete=models.SET_NULL, blank=True, null=True,
+ related_name="refreshed_access_token"
+ )
+~~ token = models.CharField(max_length=255, unique=True, )
+~~ application = models.ForeignKey(
+~~ oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE, blank=True, null=True,
+ )
+~~ expires = models.DateTimeField()
+~~ scope = models.TextField(blank=True)
+
+~~ created = models.DateTimeField(auto_now_add=True)
+~~ updated = models.DateTimeField(auto_now=True)
+
+ def is_valid(self, scopes=None):
+ return not self.is_expired() and self.allow_scopes(scopes)
+
+ def is_expired(self):
+ if not self.expires:
+ return True
+
+ return timezone.now() >= self.expires
+
+ def allow_scopes(self, scopes):
+ if not scopes:
+ return True
+
+ provided_scopes = set(self.scope.split())
+ resource_scopes = set(scopes)
+
+ return resource_scopes.issubset(provided_scopes)
+
+ def revoke(self):
+ self.delete()
+
+ @property
+ def scopes(self):
+ all_scopes = get_scopes_backend().get_all_scopes()
+ token_scopes = self.scope.split()
+ return {name: desc for name, desc in all_scopes.items() if name in token_scopes}
+
+ def __str__(self):
+ return self.token
+
+ class Meta:
+ abstract = True
+
+
+class AccessToken(AbstractAccessToken):
+ class Meta(AbstractAccessToken.Meta):
+ swappable = "OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL"
+
+
+class AbstractRefreshToken(models.Model):
+~~ id = models.BigAutoField(primary_key=True)
+~~ user = models.ForeignKey(
+~~ settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
+ related_name="%(app_label)s_%(class)s"
+ )
+~~ token = models.CharField(max_length=255)
+~~ application = models.ForeignKey(
+~~ oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE)
+~~ access_token = models.OneToOneField(
+~~ oauth2_settings.ACCESS_TOKEN_MODEL, on_delete=models.SET_NULL, blank=True, null=True,
+ related_name="refresh_token"
+ )
+
+~~ created = models.DateTimeField(auto_now_add=True)
+~~ updated = models.DateTimeField(auto_now=True)
+~~ revoked = models.DateTimeField(null=True)
+
+ def revoke(self):
+ access_token_model = get_access_token_model()
+ refresh_token_model = get_refresh_token_model()
+ with transaction.atomic():
+ self = refresh_token_model.objects.filter(
+ pk=self.pk, revoked__isnull=True
+ ).select_for_update().first()
+ if not self:
+ return
+
+ try:
+ access_token_model.objects.get(id=self.access_token_id).revoke()
+ except access_token_model.DoesNotExist:
+ pass
+ self.access_token = None
+ self.revoked = timezone.now()
+ self.save()
+
+ def __str__(self):
+ return self.token
+
+ class Meta:
+ abstract = True
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 16 from django-push-notifications
+[django-push-notifications](https://github.com/jazzband/django-push-notifications)
+is a [Django](/django.html) app for storing and interacting with
+push notification services such as
+[Google's Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
+and
+[Apple Notifications](https://developer.apple.com/notifications/).
+The django-push-notification project's source code is available
+open source under the
+[MIT license](https://github.com/jazzband/django-push-notifications/blob/master/LICENSE).
+
+[**django-push-notifications / push_notifications / models.py**](https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/./models.py)
+
+```python
+# models.py
+~~from django.db import models
+from django.utils.translation import ugettext_lazy as _
+
+from .fields import HexIntegerField
+from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
+
+
+CLOUD_MESSAGE_TYPES = (
+ ("FCM", "Firebase Cloud Message"),
+ ("GCM", "Google Cloud Message"),
+)
+
+BROWSER_TYPES = (
+ ("CHROME", "Chrome"),
+ ("FIREFOX", "Firefox"),
+ ("OPERA", "Opera"),
+)
+
+
+class Device(models.Model):
+~~ name = models.CharField(max_length=255, verbose_name=_("Name"), blank=True, null=True)
+~~ active = models.BooleanField(
+ verbose_name=_("Is active"), default=True,
+ help_text=_("Inactive devices will not be sent notifications")
+ )
+~~ user = models.ForeignKey(
+~~ SETTINGS["USER_MODEL"], blank=True, null=True, on_delete=models.CASCADE
+ )
+~~ date_created = models.DateTimeField(
+ verbose_name=_("Creation date"), auto_now_add=True, null=True
+ )
+~~ application_id = models.CharField(
+ max_length=64, verbose_name=_("Application ID"),
+ help_text=_(
+ "Opaque application identity, should be filled in for multiple"
+ " key/certificate access"
+ ),
+ blank=True, null=True
+ )
+
+ class Meta:
+ abstract = True
+
+ def __str__(self):
+ return (
+ self.name or
+ str(self.device_id or "") or
+ "{} for {}".format(self.__class__.__name__, self.user or "unknown user")
+ )
+
+
+class GCMDeviceManager(models.Manager):
+ def get_queryset(self):
+ return GCMDeviceQuerySet(self.model)
+
+
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+ app_ids = self.filter(active=True).order_by(
+ "application_id"
+ ).values_list("application_id", flat=True).distinct()
+ response = []
+ for cloud_type in ("FCM", "GCM"):
+ for app_id in app_ids:
+ reg_ids = list(
+ self.filter(
+ active=True, cloud_message_type=cloud_type, application_id=app_id).values_list(
+ "registration_id", flat=True
+ )
+ )
+ if reg_ids:
+ r = gcm_send_message(reg_ids, data, cloud_type, application_id=app_id, **kwargs)
+ response.append(r)
+
+ return response
+
+
+class GCMDevice(Device):
+ device_id = HexIntegerField(
+ verbose_name=_("Device ID"), blank=True, null=True, db_index=True,
+ help_text=_("ANDROID_ID / TelephonyManager.getDeviceId() (always as hex)")
+ )
+~~ registration_id = models.TextField(verbose_name=_("Registration ID"), unique=SETTINGS["UNIQUE_REG_ID"])
+~~ cloud_message_type = models.CharField(
+ verbose_name=_("Cloud Message Type"), max_length=3,
+ choices=CLOUD_MESSAGE_TYPES, default="GCM",
+ help_text=_("You should choose FCM or GCM")
+ )
+ objects = GCMDeviceManager()
+
+ class Meta:
+ verbose_name = _("GCM device")
+
+ def send_message(self, message, **kwargs):
+ from .gcm import send_message as gcm_send_message
+
+ data = kwargs.pop("extra", {})
+ if message is not None:
+ data["message"] = message
+
+ return gcm_send_message(
+ self.registration_id, data, self.cloud_message_type,
+ application_id=self.application_id, **kwargs
+ )
+
+
+class APNSDeviceManager(models.Manager):
+ def get_queryset(self):
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+class APNSDeviceQuerySet(models.query.QuerySet):
+ def send_message(self, message, creds=None, **kwargs):
+ if self:
+ from .apns import apns_send_bulk_message
+
+ app_ids = self.filter(active=True).order_by("application_id")\
+ .values_list("application_id", flat=True).distinct()
+ res = []
+ for app_id in app_ids:
+ reg_ids = list(self.filter(active=True, application_id=app_id).values_list(
+ "registration_id", flat=True)
+ )
+ r = apns_send_bulk_message(
+ registration_ids=reg_ids, alert=message, application_id=app_id,
+ creds=creds, **kwargs
+ )
+ if hasattr(r, "keys"):
+ res += [r]
+ elif hasattr(r, "__getitem__"):
+ res += r
+ return res
+
+
+class APNSDevice(Device):
+~~ device_id = models.UUIDField(
+ verbose_name=_("Device ID"), blank=True, null=True, db_index=True,
+ help_text="UDID / UIDevice.identifierForVendor()"
+ )
+~~ registration_id = models.CharField(
+ verbose_name=_("Registration ID"), max_length=200, unique=SETTINGS["UNIQUE_REG_ID"]
+ )
+
+ objects = APNSDeviceManager()
+
+ class Meta:
+ verbose_name = _("APNS device")
+
+ def send_message(self, message, creds=None, **kwargs):
+ from .apns import apns_send_message
+
+ return apns_send_message(
+ registration_id=self.registration_id,
+ alert=message,
+ application_id=self.application_id, creds=creds,
+ **kwargs
+ )
+
+
+class WNSDeviceManager(models.Manager):
+ def get_queryset(self):
+ return WNSDeviceQuerySet(self.model)
+
+
+class WNSDeviceQuerySet(models.query.QuerySet):
+ def send_message(self, message, **kwargs):
+ from .wns import wns_send_bulk_message
+
+ app_ids = self.filter(active=True).order_by("application_id").values_list(
+ "application_id", flat=True
+ ).distinct()
+ res = []
+ for app_id in app_ids:
+ reg_ids = self.filter(active=True, application_id=app_id).values_list(
+ "registration_id", flat=True
+ )
+ r = wns_send_bulk_message(uri_list=list(reg_ids), message=message, **kwargs)
+ if hasattr(r, "keys"):
+ res += [r]
+ elif hasattr(r, "__getitem__"):
+ res += r
+
+ return res
+
+
+class WNSDevice(Device):
+~~ device_id = models.UUIDField(
+ verbose_name=_("Device ID"), blank=True, null=True, db_index=True,
+ help_text=_("GUID()")
+ )
+~~ registration_id = models.TextField(verbose_name=_("Notification URI"), unique=SETTINGS["UNIQUE_REG_ID"])
+
+ objects = WNSDeviceManager()
+
+ class Meta:
+ verbose_name = _("WNS device")
+
+ def send_message(self, message, **kwargs):
+ from .wns import wns_send_message
+
+ return wns_send_message(
+ uri=self.registration_id, message=message, application_id=self.application_id,
+ **kwargs
+ )
+
+
+class WebPushDeviceManager(models.Manager):
+ def get_queryset(self):
+ return WebPushDeviceQuerySet(self.model)
+
+
+class WebPushDeviceQuerySet(models.query.QuerySet):
+ def send_message(self, message, **kwargs):
+ devices = self.filter(active=True).order_by("application_id").distinct()
+ res = []
+ for device in devices:
+ res.append(device.send_message(message))
+
+ return res
+
+
+class WebPushDevice(Device):
+~~ registration_id = models.TextField(verbose_name=_("Registration ID"), unique=SETTINGS["UNIQUE_REG_ID"])
+~~ p256dh = models.CharField(
+ verbose_name=_("User public encryption key"),
+ max_length=88)
+~~ auth = models.CharField(
+ verbose_name=_("User auth secret"),
+ max_length=24)
+~~ browser = models.CharField(
+ verbose_name=_("Browser"), max_length=10,
+ choices=BROWSER_TYPES, default=BROWSER_TYPES[0][0],
+ help_text=_("Currently only support to Chrome, Firefox and Opera browsers")
+ )
+ objects = WebPushDeviceManager()
+
+ class Meta:
+ verbose_name = _("WebPush device")
+
+ @property
+ def device_id(self):
+ return None
+
+ def send_message(self, message, **kwargs):
+ from .webpush import webpush_send_message
+
+ return webpush_send_message(
+ uri=self.registration_id, message=message, browser=self.browser,
+ auth=self.auth, p256dh=self.p256dh, application_id=self.application_id, **kwargs)
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 17 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / serializers.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./serializers.py)
+
+```python
+# serializers.py
+import copy
+import inspect
+import traceback
+from collections import OrderedDict, defaultdict
+from collections.abc import Mapping
+
+from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
+from django.core.exceptions import ValidationError as DjangoValidationError
+~~from django.db import models
+from django.db.models.fields import Field as DjangoModelField
+from django.utils import timezone
+from django.utils.functional import cached_property
+from django.utils.translation import gettext_lazy as _
+
+from rest_framework.compat import postgres_fields
+from rest_framework.exceptions import ErrorDetail, ValidationError
+from rest_framework.fields import get_error_detail, set_value
+from rest_framework.settings import api_settings
+from rest_framework.utils import html, model_meta, representation
+from rest_framework.utils.field_mapping import (
+ ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs,
+ get_relation_kwargs, get_url_kwargs
+)
+from rest_framework.utils.serializer_helpers import (
+ BindingDict, BoundField, JSONBoundField, NestedBoundField, ReturnDict,
+ ReturnList
+)
+from rest_framework.validators import (
+ UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator,
+ UniqueTogetherValidator
+)
+
+from rest_framework.fields import ( # NOQA # isort:skip
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+ if not self.allow_empty and len(data) == 0:
+ message = self.error_messages['empty']
+ raise ValidationError({
+ api_settings.NON_FIELD_ERRORS_KEY: [message]
+ }, code='empty')
+
+ ret = []
+ errors = []
+
+ for item in data:
+ try:
+ validated = self.child.run_validation(item)
+ except ValidationError as exc:
+ errors.append(exc.detail)
+ else:
+ ret.append(validated)
+ errors.append({})
+
+ if any(errors):
+ raise ValidationError(errors)
+
+ return ret
+
+ def to_representation(self, data):
+~~ iterable = data.all() if isinstance(data, models.Manager) else data
+
+ return [
+ self.child.to_representation(item) for item in iterable
+ ]
+
+ def validate(self, attrs):
+ return attrs
+
+ def update(self, instance, validated_data):
+ raise NotImplementedError(
+ "Serializers with many=True do not support multiple update by "
+ "default, only multiple create. For updates it is unclear how to "
+ "deal with insertions and deletions. If you need to support "
+ "multiple update, use a `ListSerializer` class and override "
+ "`.update()` so you can specify the behavior exactly."
+ )
+
+ def create(self, validated_data):
+ return [
+ self.child.create(attrs) for attrs in validated_data
+ ]
+
+ def save(self, **kwargs):
+ assert 'commit' not in kwargs, (
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ module=serializer.__class__.__module__,
+ class_name=serializer.__class__.__name__
+ )
+ )
+
+ assert not any(
+ len(field.source_attrs) > 1 and
+ (field.source_attrs[0] in validated_data) and
+ (field.source_attrs[0] in model_field_info.relations) and
+ isinstance(validated_data[field.source_attrs[0]], (list, dict))
+ for field in serializer._writable_fields
+ ), (
+ 'The `.{method_name}()` method does not support writable dotted-source '
+ 'fields by default.\nWrite an explicit `.{method_name}()` method for '
+ 'serializer `{module}.{class_name}`, or set `read_only=True` on '
+ 'dotted-source serializer fields.'.format(
+ method_name=method_name,
+ module=serializer.__class__.__module__,
+ class_name=serializer.__class__.__name__
+ )
+ )
+
+
+class ModelSerializer(Serializer):
+ serializer_field_mapping = {
+~~ models.AutoField: IntegerField,
+~~ models.BigIntegerField: IntegerField,
+~~ models.BooleanField: BooleanField,
+~~ models.CharField: CharField,
+~~ models.CommaSeparatedIntegerField: CharField,
+~~ models.DateField: DateField,
+~~ models.DateTimeField: DateTimeField,
+~~ models.DecimalField: DecimalField,
+~~ models.DurationField: DurationField,
+~~ models.EmailField: EmailField,
+~~ models.Field: ModelField,
+~~ models.FileField: FileField,
+~~ models.FloatField: FloatField,
+~~ models.ImageField: ImageField,
+~~ models.IntegerField: IntegerField,
+~~ models.NullBooleanField: BooleanField,
+~~ models.PositiveIntegerField: IntegerField,
+~~ models.PositiveSmallIntegerField: IntegerField,
+~~ models.SlugField: SlugField,
+~~ models.SmallIntegerField: IntegerField,
+~~ models.TextField: CharField,
+~~ models.TimeField: TimeField,
+~~ models.URLField: URLField,
+~~ models.UUIDField: UUIDField,
+~~ models.GenericIPAddressField: IPAddressField,
+~~ models.FilePathField: FilePathField,
+ }
+ if postgres_fields:
+ serializer_field_mapping[postgres_fields.HStoreField] = HStoreField
+ serializer_field_mapping[postgres_fields.ArrayField] = ListField
+ serializer_field_mapping[postgres_fields.JSONField] = JSONField
+ serializer_related_field = PrimaryKeyRelatedField
+ serializer_related_to_field = SlugRelatedField
+ serializer_url_field = HyperlinkedIdentityField
+ serializer_choice_field = ChoiceField
+
+ url_field_name = None
+
+ def create(self, validated_data):
+ raise_errors_on_nested_writes('create', self, validated_data)
+
+ ModelClass = self.Meta.model
+
+ info = model_meta.get_field_info(ModelClass)
+ many_to_many = {}
+ for field_name, relation_info in info.relations.items():
+ if relation_info.to_many and (field_name in validated_data):
+ many_to_many[field_name] = validated_data.pop(field_name)
+
+ try:
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 18 from django-sitetree
+[django-sitetree](https://github.com/idlesign/django-sitetree)
+([project documentation](https://django-sitetree.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-sitetree/))
+is a [Django](/django.html) extension that makes it easier for
+developers to add site trees, menus and breadcrumb navigation elements
+to their web applications.
+
+The django-sitetree project is provided as open source under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/idlesign/django-sitetree/blob/master/LICENSE).
+
+[**django-sitetree / sitetree / models.py**](https://github.com/idlesign/django-sitetree/blob/master/sitetree/./models.py)
+
+```python
+# models.py
+from django.contrib.auth.models import Permission
+~~from django.db import models
+from django.utils.translation import gettext_lazy as _
+
+from .settings import MODEL_TREE, TREE_ITEMS_ALIASES
+
+
+class CharFieldNullable(models.CharField):
+ def get_prep_value(self, value):
+ if value is not None:
+ if value.strip() == '':
+ return None
+ return self.to_python(value)
+
+
+class TreeBase(models.Model):
+
+~~ title = models.CharField(
+ _('Title'), max_length=100, help_text=_('Site tree title for presentational purposes.'), blank=True)
+
+~~ alias = models.CharField(
+ _('Alias'), max_length=80,
+ help_text=_('Short name to address site tree from templates. Note: change with care.'),
+ unique=True, db_index=True)
+
+ class Meta:
+ abstract = True
+ verbose_name = _('Site Tree')
+ verbose_name_plural = _('Site Trees')
+
+ def get_title(self) -> str:
+ return self.title or self.alias
+
+ def __str__(self) -> str:
+ return self.alias
+
+
+class TreeItemBase(models.Model):
+
+ PERM_TYPE_ANY = 1
+ PERM_TYPE_ALL = 2
+
+ PERM_TYPE_CHOICES = {
+ PERM_TYPE_ANY: _('Any'),
+ PERM_TYPE_ALL: _('All'),
+ }
+
+~~ title = models.CharField(
+ _('Title'), max_length=100,
+ help_text=_('Site tree item title. Can contain template variables E.g.: {{ mytitle }}.'))
+
+~~ hint = models.CharField(
+ _('Hint'), max_length=200,
+ help_text=_('Some additional information about this item that is used as a hint.'), blank=True, default='')
+
+~~ url = models.CharField(
+ _('URL'), max_length=200,
+ help_text=_('Exact URL or URL pattern (see "Additional settings") for this item.'), db_index=True)
+
+~~ urlaspattern = models.BooleanField(
+ _('URL as Pattern'),
+ help_text=_('Whether the given URL should be treated as a pattern. '
+ 'Note: Refer to Django "URL dispatcher" documentation (e.g. "Naming URL patterns" part).'),
+ db_index=True, default=False)
+
+~~ tree = models.ForeignKey(
+~~ MODEL_TREE, related_name='%(class)s_tree', on_delete=models.CASCADE, verbose_name=_('Site Tree'),
+ help_text=_('Site tree this item belongs to.'), db_index=True)
+
+~~ hidden = models.BooleanField(
+ _('Hidden'), help_text=_('Whether to show this item in navigation.'), db_index=True, default=False)
+
+ alias = CharFieldNullable(
+ _('Alias'), max_length=80,
+ help_text=_(
+ 'Short name to address site tree item from a template. '
+ 'Reserved aliases: "%s".' % '", "'.join(TREE_ITEMS_ALIASES)
+ ),
+ db_index=True, blank=True, null=True)
+
+~~ description = models.TextField(
+ _('Description'),
+ help_text=_('Additional comments on this item.'), blank=True, default='')
+
+~~ inmenu = models.BooleanField(
+ _('Show in menu'),
+ help_text=_('Whether to show this item in a menu.'), db_index=True, default=True)
+
+~~ inbreadcrumbs = models.BooleanField(
+ _('Show in breadcrumb path'),
+ help_text=_('Whether to show this item in a breadcrumb path.'), db_index=True, default=True)
+
+~~ insitetree = models.BooleanField(
+ _('Show in site tree'),
+ help_text=_('Whether to show this item in a site tree.'), db_index=True, default=True)
+
+~~ access_loggedin = models.BooleanField(
+ _('Logged in only'),
+ help_text=_('Check it to grant access to this item to authenticated users only.'),
+ db_index=True, default=False)
+
+~~ access_guest = models.BooleanField(
+ _('Guests only'),
+ help_text=_('Check it to grant access to this item to guests only.'), db_index=True, default=False)
+
+~~ access_restricted = models.BooleanField(
+ _('Restrict access to permissions'),
+ help_text=_('Check it to restrict user access to this item, using Django permissions system.'),
+ db_index=True, default=False)
+
+~~ access_permissions = models.ManyToManyField(
+ Permission, verbose_name=_('Permissions granting access'), blank=True)
+
+~~ access_perm_type = models.IntegerField(
+ _('Permissions interpretation'),
+ help_text=_('Any — user should have any of chosen permissions. '
+ 'All — user should have all chosen permissions.'),
+ choices=PERM_TYPE_CHOICES.items(), default=PERM_TYPE_ANY)
+
+~~ parent = models.ForeignKey(
+~~ 'self', related_name='%(class)s_parent', on_delete=models.CASCADE, verbose_name=_('Parent'),
+ help_text=_('Parent site tree item.'), db_index=True, null=True, blank=True)
+
+~~ sort_order = models.IntegerField(
+ _('Sort order'),
+ help_text=_('Item position among other site tree items under the same parent.'), db_index=True, default=0)
+
+ def save(self, force_insert=False, force_update=False, **kwargs):
+ if self.parent == self:
+ self.parent = None
+
+ id_ = self.id
+ if id_ and self.sort_order == 0:
+ self.sort_order = id_
+
+ super().save(force_insert, force_update, **kwargs)
+
+ if self.sort_order == 0:
+ self.sort_order = self.id
+ self.save()
+
+ class Meta:
+ abstract = True
+ verbose_name = _('Site Tree Item')
+ verbose_name_plural = _('Site Tree Items')
+ unique_together = ('tree', 'alias')
+
+ def __str__(self) -> str:
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 19 from django-smithy
+[django-smithy](https://github.com/jamiecounsell/django-smithy) is
+a [Django](/django.html) code library that allows users to send
+HTTP requests from the Django admin user interface. The code for
+the project is open source under the
+[MIT license](https://github.com/jamiecounsell/django-smithy/blob/master/LICENSE).
+
+[**django-smithy / smithy / models.py**](https://github.com/jamiecounsell/django-smithy/blob/master/smithy/./models.py)
+
+```python
+# models.py
+
+~~from django.db import models
+from django.db.models.signals import post_save
+from django.dispatch import receiver
+from requests import Request as HTTPRequest, Session
+from requests.cookies import create_cookie, RequestsCookieJar
+from urllib.parse import parse_qs, urlparse, urlencode
+from requests_toolbelt.utils import dump
+
+from model_utils.models import TimeStampedModel
+
+from smithy.helpers import render_with_context, parse_dump_result
+
+
+class NameValueModel(TimeStampedModel):
+~~ name = models.CharField(max_length = 200)
+~~ value = models.TextField(blank = True)
+
+ def __str__(self):
+ return self.name
+
+ class Meta:
+ abstract = True
+
+
+class Request(TimeStampedModel):
+ METHODS = (
+ ('GET', 'GET'),
+ ('POST', 'POST'),
+ ('PUT', 'PUT'),
+ ('DELETE', 'DELETE'),
+ ('OPTIONS', 'OPTIONS'),
+ ('HEAD', 'HEAD'),
+ )
+ BODY_TYPES = (
+ ('', 'Other'),
+ ('application/x-www-form-urlencoded', 'x-www-form-urlencoded'),
+ ('application/json', 'JSON'),
+ ('text/plain', 'Text'),
+ ('application/javascript', 'JavaScript'),
+ ('application/xml', 'XML (application/xml)'),
+ ('text/xml', 'XML (text/xml)'),
+ ('text/html', 'HTML'),
+ )
+~~ method = models.CharField(
+ max_length = 7, choices = METHODS,
+ blank = False, null = False)
+~~ name = models.CharField(max_length = 500, blank = False)
+~~ url = models.CharField(max_length = 2083)
+~~ body = models.TextField(blank = True)
+~~ content_type = models.CharField(
+ default = BODY_TYPES[0][0],
+ blank = True, null = True,
+ max_length = 100, choices = BODY_TYPES)
+
+ def __str__(self):
+ if self.name:
+ return self.name
+ return "{} {}".format(
+ self.method,
+ self.url,
+ )
+
+
+class RequestBlueprint(Request):
+~~ follow_redirects = models.BooleanField(
+ default = False, blank = False, null = False)
+
+ @property
+ def name_value_related(self):
+ return [
+ self.headers,
+ self.query_parameters,
+ self.cookies
+ ]
+
+ def send(self, context = None):
+
+ context = context or {}
+ for variable in self.variables.all():
+ context[variable.name] = variable.value
+
+ request = HTTPRequest(
+ url = render_with_context(self.url, context),
+ method = self.method)
+
+ session = Session()
+ record = RequestRecord.objects.create(blueprint = self)
+
+ for qs in self.name_value_related:
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ obj.save()
+
+ if self.content_type == self.BODY_TYPES[0][0]:
+ data = render_with_context(self.body, context)
+ else:
+ data = {}
+ for param in self.body_parameters.all():
+ param.add_to(data, context)
+
+ request.data = data
+ prepared_request = request.prepare()
+
+ response = session.send(prepared_request, stream = True)
+
+ RequestRecord.objects.filter(pk = record.pk).update(
+ raw_request = parse_dump_result(dump._dump_request_data, prepared_request),
+ raw_response = parse_dump_result(dump._dump_response_data, response),
+ status = response.status_code,
+ **RequestRecord.get_clone_args(self, context)
+ )
+
+ return RequestRecord.objects.get(pk = record.pk)
+
+
+class RequestRecord(Request):
+~~ raw_request = models.TextField()
+~~ raw_response = models.TextField()
+~~ status = models.PositiveIntegerField(null = True)
+~~ blueprint = models.ForeignKey(
+ 'smithy.RequestBlueprint',
+~~ on_delete = models.SET_NULL,
+ null = True)
+
+ @property
+ def is_success(self):
+ return self.status and self.status < 400
+
+ @staticmethod
+ def of_blueprint(blueprint):
+ return RequestRecord.objects.filter(
+ blueprint = blueprint)
+
+ @classmethod
+ def get_clone_args(cls, obj, context : dict):
+ return dict([
+ (
+ render_with_context(fld.name, context),
+ render_with_context(getattr(obj, fld.name), context)
+ )
+ for fld \
+ in cls._meta.fields \
+ if fld.name != obj._meta.pk \
+ and fld in obj._meta.fields \
+ and fld.name not in [
+ 'request', 'id', 'created', 'updated'
+ ]])
+
+
+class Variable(NameValueModel):
+~~ request = models.ForeignKey(
+ 'smithy.RequestBlueprint',
+~~ on_delete = models.CASCADE,
+ related_name = 'variables')
+
+
+class BodyParameter(NameValueModel):
+~~ request = models.ForeignKey(
+ 'smithy.RequestBlueprint',
+~~ on_delete = models.CASCADE,
+ related_name = 'body_parameters')
+
+ def add_to(self, payload : dict, context: dict):
+ if self.name and self.value:
+ payload[
+ render_with_context(self.name, context)
+ ] = render_with_context(self.value, context)
+ else:
+ pass # TODO: warn
+
+
+class Header(NameValueModel):
+~~ request = models.ForeignKey(
+ 'smithy.Request',
+~~ on_delete = models.CASCADE,
+ related_name = 'headers')
+
+ def add_to(self, request : HTTPRequest):
+ if self.name and self.value:
+ request.headers[self.name] = self.value
+ else:
+ pass # TODO: warn
+
+
+class QueryParameter(NameValueModel):
+~~ request = models.ForeignKey(
+ 'smithy.Request',
+~~ on_delete = models.CASCADE,
+ related_name = 'query_parameters')
+
+ def add_to(self, request : HTTPRequest):
+ existing_param_str = urlparse(request.url).query
+ param_dict = parse_qs(existing_param_str)
+
+ param_dict[self.name] = self.value
+
+ base_url = request.url.rstrip(existing_param_str)
+ param_str = urlencode(param_dict, doseq=True)
+ request.url = base_url.rstrip("?") + "?" + param_str
+
+
+class Cookie(NameValueModel):
+~~ request = models.ForeignKey(
+ 'smithy.Request',
+~~ on_delete = models.CASCADE,
+ related_name = 'cookies')
+
+ def add_to(self, request : HTTPRequest):
+ request.cookies = request.cookies or RequestsCookieJar()
+ request.cookies.set_cookie(
+ create_cookie(self.name, self.value)
+ )
+
+
+@receiver(post_save, sender = RequestBlueprint)
+def add_content_type(sender, instance : RequestBlueprint, created, **kwargs):
+ if created and not instance.headers.filter(name__iexact = 'content-type').exists():
+ instance.content_type and Header.objects.create(
+ name = 'Content-Type',
+ value = instance.content_type,
+ request = instance.request_ptr
+ )
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 20 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / models.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./models.py)
+
+```python
+# models.py
+from __future__ import unicode_literals
+
+import logging
+from time import time
+import six
+
+~~from django.db import models, DatabaseError
+try:
+ from django.urls import reverse
+except ImportError:
+ from django.core.urlresolvers import reverse
+
+from django.conf import settings
+
+from explorer import app_settings
+from explorer.utils import (
+ passes_blacklist,
+ swap_params,
+ extract_params,
+ shared_dict_update,
+ get_s3_bucket,
+ get_params_for_url,
+ get_valid_connection
+)
+
+MSG_FAILED_BLACKLIST = "Query failed the SQL blacklist: %s"
+
+
+logger = logging.getLogger(__name__)
+
+@six.python_2_unicode_compatible
+class Query(models.Model):
+~~ title = models.CharField(max_length=255)
+~~ sql = models.TextField()
+~~ description = models.TextField(null=True, blank=True)
+~~ created_by_user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
+~~ created_at = models.DateTimeField(auto_now_add=True)
+~~ last_run_date = models.DateTimeField(auto_now=True)
+~~ snapshot = models.BooleanField(default=False, help_text="Include in snapshot task (if enabled)")
+~~ connection = models.CharField(blank=True, null=True, max_length=128,
+ help_text="Name of DB connection (as specified in settings) to use for this query. Will use EXPLORER_DEFAULT_CONNECTION if left blank")
+
+ def __init__(self, *args, **kwargs):
+ self.params = kwargs.get('params')
+ kwargs.pop('params', None)
+ super(Query, self).__init__(*args, **kwargs)
+
+ class Meta:
+ ordering = ['title']
+ verbose_name_plural = 'Queries'
+
+ def __str__(self):
+ return six.text_type(self.title)
+
+ def get_run_count(self):
+ return self.querylog_set.count()
+
+ def avg_duration(self):
+ return self.querylog_set.aggregate(models.Avg('duration'))['duration__avg']
+
+ def passes_blacklist(self):
+ return passes_blacklist(self.final_sql())
+
+ def final_sql(self):
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ return ql
+
+ @property
+ def shared(self):
+ return self.id in set(sum(app_settings.EXPLORER_GET_USER_QUERY_VIEWS().values(), []))
+
+ @property
+ def snapshots(self):
+ if app_settings.ENABLE_TASKS:
+ b = get_s3_bucket()
+ keys = b.list(prefix='query-%s/snap-' % self.id)
+ keys_s = sorted(keys, key=lambda k: k.last_modified)
+ return [SnapShot(k.generate_url(expires_in=0, query_auth=False),
+ k.last_modified) for k in keys_s]
+
+
+class SnapShot(object):
+
+ def __init__(self, url, last_modified):
+ self.url = url
+ self.last_modified = last_modified
+
+
+class QueryLog(models.Model):
+
+~~ sql = models.TextField(null=True, blank=True)
+~~ query = models.ForeignKey(Query, null=True, blank=True, on_delete=models.SET_NULL)
+~~ run_by_user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
+~~ run_at = models.DateTimeField(auto_now_add=True)
+~~ duration = models.FloatField(blank=True, null=True) # milliseconds
+~~ connection = models.CharField(blank=True, null=True, max_length=128)
+
+ @property
+ def is_playground(self):
+ return self.query_id is None
+
+ class Meta:
+ ordering = ['-run_at']
+
+
+class QueryResult(object):
+
+ def __init__(self, sql, connection):
+
+ self.sql = sql
+ self.connection = connection
+
+ cursor, duration = self.execute_query()
+
+ self._description = cursor.description or []
+ self._data = [list(r) for r in cursor.fetchall()]
+ self.duration = duration
+
+ cursor.close()
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 21 from django-tables2
+[django-tables2](https://github.com/jieter/django-tables2)
+([projection documentation](https://django-tables2.readthedocs.io/en/latest/)
+and
+[PyPI page](https://pypi.org/project/django-tables2/))
+is a code library for [Django](/django.html) that simplifies creating and
+displaying tables in [Django templates](/django-templates.html),
+especially with more advanced features such as pagination and sorting.
+The project and its code are
+[available as open source](https://github.com/jieter/django-tables2/blob/master/LICENSE).
+
+[**django-tables2 / django_tables2 / utils.py**](https://github.com/jieter/django-tables2/blob/master/django_tables2/./utils.py)
+
+```python
+# utils.py
+import inspect
+import warnings
+from collections import OrderedDict
+from functools import total_ordering
+from itertools import chain
+
+from django.core.exceptions import FieldDoesNotExist
+~~from django.db import models
+from django.utils.html import format_html_join
+
+
+class Sequence(list):
+
+ def expand(self, columns):
+ ellipses = self.count("...")
+ if ellipses > 1:
+ raise ValueError("'...' must be used at most once in a sequence.")
+ elif ellipses == 0:
+ self.append("...")
+
+ columns = list(columns) # take a copy and exhaust the generator
+ head = []
+ tail = []
+ target = head # start by adding things to the head
+ for name in self:
+ if name == "...":
+ target = tail
+ continue
+ target.append(name)
+ if name in columns:
+ columns.pop(columns.index(name))
+ self[:] = chain(head, columns, tail)
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+ return instance
+
+ def resolve(self, context, safe=True, quiet=False):
+ if isinstance(context, dict) and self in context:
+ return context[self]
+
+ try:
+ current = context
+ for bit in self.bits:
+ try: # dictionary lookup
+ current = current[bit]
+ except (TypeError, AttributeError, KeyError):
+ try: # attribute lookup
+ current = getattr(current, bit)
+ except (TypeError, AttributeError):
+ try: # list-index lookup
+ current = current[int(bit)]
+ except (
+ IndexError, # list index out of range
+ ValueError, # invalid literal for int()
+ KeyError, # dict without `int(bit)` key
+ TypeError, # unsubscriptable object
+ ):
+ current_context = (
+~~ type(current) if isinstance(current, models.Model) else current
+ )
+
+ raise ValueError(
+ self.LOOKUP_ERROR_FMT.format(
+ key=bit, context=current_context, accessor=self
+ )
+ )
+ if callable(current):
+ if safe and getattr(current, "alters_data", False):
+ raise ValueError(self.ALTERS_DATA_ERROR_FMT.format(method=repr(current)))
+ if not getattr(current, "do_not_call_in_templates", False):
+ current = current()
+ if current is None:
+ break
+ return current
+ except Exception:
+ if not quiet:
+ raise
+
+ @property
+ def bits(self):
+ if self == "":
+ return ()
+ return self.split(self.SEPARATOR)
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 22 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / models.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/./models.py)
+
+```python
+# models.py
+from django.contrib.contenttypes.fields import GenericForeignKey
+from django.contrib.contenttypes.models import ContentType
+~~from django.db import IntegrityError, models, router, transaction
+from django.utils.text import slugify
+from django.utils.translation import gettext, gettext_lazy as _
+
+try:
+ from unidecode import unidecode
+except ImportError:
+
+ def unidecode(tag):
+ return tag
+
+
+class TagBase(models.Model):
+~~ name = models.CharField(verbose_name=_("name"), unique=True, max_length=100)
+~~ slug = models.SlugField(verbose_name=_("slug"), unique=True, max_length=100)
+
+ def __str__(self):
+ return self.name
+
+ def __gt__(self, other):
+ return self.name.lower() > other.name.lower()
+
+ def __lt__(self, other):
+ return self.name.lower() < other.name.lower()
+
+ class Meta:
+ abstract = True
+
+ def save(self, *args, **kwargs):
+ if self._state.adding and not self.slug:
+ self.slug = self.slugify(self.name)
+ using = kwargs.get("using") or router.db_for_write(
+ type(self), instance=self
+ )
+ kwargs["using"] = using
+ try:
+ with transaction.atomic(using=using):
+ res = super().save(*args, **kwargs)
+ return res
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ @classmethod
+ def tag_model(cls):
+ field = cls._meta.get_field("tag")
+ return field.remote_field.model
+
+ @classmethod
+ def tag_relname(cls):
+ field = cls._meta.get_field("tag")
+ return field.remote_field.related_name
+
+ @classmethod
+ def lookup_kwargs(cls, instance):
+ return {"content_object": instance}
+
+ @classmethod
+ def tags_for(cls, model, instance=None, **extra_filters):
+ kwargs = extra_filters or {}
+ if instance is not None:
+ kwargs.update({"%s__content_object" % cls.tag_relname(): instance})
+ return cls.tag_model().objects.filter(**kwargs)
+ kwargs.update({"%s__content_object__isnull" % cls.tag_relname(): False})
+ return cls.tag_model().objects.filter(**kwargs).distinct()
+
+
+class TaggedItemBase(ItemBase):
+~~ tag = models.ForeignKey(
+~~ Tag, related_name="%(app_label)s_%(class)s_items", on_delete=models.CASCADE
+ )
+
+ class Meta:
+ abstract = True
+
+
+class CommonGenericTaggedItemBase(ItemBase):
+~~ content_type = models.ForeignKey(
+ ContentType,
+~~ on_delete=models.CASCADE,
+ verbose_name=_("content type"),
+ related_name="%(app_label)s_%(class)s_tagged_items",
+ )
+ content_object = GenericForeignKey()
+
+ class Meta:
+ abstract = True
+
+ @classmethod
+ def lookup_kwargs(cls, instance):
+ return {
+ "object_id": instance.pk,
+ "content_type": ContentType.objects.get_for_model(instance),
+ }
+
+ @classmethod
+ def tags_for(cls, model, instance=None, **extra_filters):
+ tag_relname = cls.tag_relname()
+ model = model._meta.concrete_model
+ kwargs = {
+ "%s__content_type__app_label" % tag_relname: model._meta.app_label,
+ "%s__content_type__model" % tag_relname: model._meta.model_name,
+ }
+ if instance is not None:
+ kwargs["%s__object_id" % tag_relname] = instance.pk
+ if extra_filters:
+ kwargs.update(extra_filters)
+ return cls.tag_model().objects.filter(**kwargs).distinct()
+
+
+class GenericTaggedItemBase(CommonGenericTaggedItemBase):
+~~ object_id = models.IntegerField(verbose_name=_("object ID"), db_index=True)
+
+ class Meta:
+ abstract = True
+
+
+class GenericUUIDTaggedItemBase(CommonGenericTaggedItemBase):
+~~ object_id = models.UUIDField(verbose_name=_("object ID"), db_index=True)
+
+ class Meta:
+ abstract = True
+
+
+class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
+ class Meta:
+ verbose_name = _("tagged item")
+ verbose_name_plural = _("tagged items")
+ app_label = "taggit"
+ index_together = [["content_type", "object_id"]]
+ unique_together = [["content_type", "object_id", "tag"]]
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 23 from django-user-visit
+[django-user-visit](https://github.com/yunojuno/django-user-visit)
+([PyPI package information](https://pypi.org/project/django-user-visit/))
+is a [Django](/django.html) app and
+[middleware](https://docs.djangoproject.com/en/stable/topics/http/middleware/)
+for tracking daily user visits to your web application. The goal
+is to record per user per day instead of for every request a user
+sends to the application. The project is provided as open source
+under the
+[MIT license](https://github.com/yunojuno/django-user-visit/blob/master/LICENSE).
+
+[**django-user-visit / user_visit / models.py**](https://github.com/yunojuno/django-user-visit/blob/master/user_visit/./models.py)
+
+```python
+# models.py
+from __future__ import annotations
+
+import datetime
+import hashlib
+import uuid
+from typing import Any
+
+import user_agents
+from django.conf import settings
+~~from django.db import models
+from django.http import HttpRequest
+from django.utils import timezone
+
+
+def parse_remote_addr(request: HttpRequest) -> str:
+ x_forwarded_for = request.headers.get("X-Forwarded-For", "")
+ if x_forwarded_for:
+ return x_forwarded_for.split(",")[0]
+ return request.META.get("REMOTE_ADDR", "")
+
+
+def parse_ua_string(request: HttpRequest) -> str:
+ return request.headers.get("User-Agent", "")
+
+
+class UserVisitManager(models.Manager):
+
+ def build(self, request: HttpRequest, timestamp: datetime.datetime) -> UserVisit:
+ uv = UserVisit(
+ user=request.user,
+ timestamp=timestamp,
+ session_key=request.session.session_key,
+ remote_addr=parse_remote_addr(request),
+ ua_string=parse_ua_string(request),
+ )
+ uv.hash = uv.md5().hexdigest()
+ return uv
+
+
+class UserVisit(models.Model):
+
+~~ user = models.ForeignKey(
+~~ settings.AUTH_USER_MODEL, related_name="user_visits", on_delete=models.CASCADE
+ )
+~~ timestamp = models.DateTimeField(
+ help_text="The time at which the first visit of the day was recorded",
+ default=timezone.now,
+ )
+~~ session_key = models.CharField(help_text="Django session identifier", max_length=40)
+~~ remote_addr = models.CharField(
+ help_text=(
+ "Client IP address (from X-Forwarded-For HTTP header, "
+ "or REMOTE_ADDR request property)"
+ ),
+ max_length=100,
+ blank=True,
+ )
+~~ ua_string = models.TextField(
+ "User agent (raw)", help_text="Client User-Agent HTTP header", blank=True,
+ )
+~~ uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
+~~ hash = models.CharField(
+ max_length=32,
+ help_text="MD5 hash generated from request properties",
+ unique=True,
+ )
+~~ created_at = models.DateTimeField(
+ help_text="The time at which the database record was created (!=timestamp)",
+ auto_now_add=True,
+ )
+
+ objects = UserVisitManager()
+
+ class Meta:
+ get_latest_by = "timestamp"
+
+ def __str__(self) -> str:
+ return f"{self.user} visited the site on {self.timestamp}"
+
+ def __repr__(self) -> str:
+ return f""
+
+ def save(self, *args: Any, **kwargs: Any) -> None:
+ self.hash = self.md5().hexdigest()
+ super().save(*args, **kwargs)
+
+ @property
+ def user_agent(self) -> user_agents.parsers.UserAgent:
+ return user_agents.parsers.parse(self.ua_string)
+
+ @property
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 24 from django-webshell
+[django-webshell](https://github.com/onrik/django-webshell) is an extension
+for executing arbitrary code in the
+[Django admin](https://docs.djangoproject.com/en/stable/ref/contrib/admin/),
+similar to how you can run code by using the `django manage.py shell`
+command from the terminal.
+
+The django-webshell project is provided as open source under the
+[MIT license](https://github.com/onrik/django-webshell/blob/master/LICENSE).
+
+[**django-webshell / webshell / models.py**](https://github.com/onrik/django-webshell/blob/master/webshell/./models.py)
+
+```python
+# models.py
+~~from django.db import models
+from django.utils.translation import ugettext_lazy as _
+
+
+class Script(models.Model):
+~~ name = models.CharField(_('Name'), max_length=100)
+~~ source = models.TextField(_('Source'))
+
+ class Meta:
+ verbose_name = _('Script')
+ verbose_name_plural = _('Scripts')
+
+ def __unicode__(self):
+ return self.name
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 25 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / migrations / 0003_mptt_upgrade.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/migrations/0003_mptt_upgrade.py)
+
+```python
+# 0003_mptt_upgrade.py
+from django.db import migrations
+~~from django.db import models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("wiki", "0002_urlpath_moved_to"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="urlpath",
+ name="level",
+~~ field=models.PositiveIntegerField(editable=False),
+ ),
+ migrations.AlterField(
+ model_name="urlpath",
+ name="lft",
+~~ field=models.PositiveIntegerField(editable=False),
+ ),
+ migrations.AlterField(
+ model_name="urlpath",
+ name="rght",
+~~ field=models.PositiveIntegerField(editable=False),
+ ),
+ ]
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 26 from dmd-interpreter
+[dmd-interpreter](https://github.com/mitchalexbailey/dmd-interpreter)
+([running web app](http://www.dmd.nl/DOVE))
+is a Python tool to aggregate clinically relevant information related
+to variants in the DMD gene and display that [data](/data.html) to a user
+with a [Django](/django.html) web application.
+
+[**dmd-interpreter / interpreter / models.py**](https://github.com/mitchalexbailey/dmd-interpreter/blob/master/interpreter/./models.py)
+
+```python
+# models.py
+~~from django.db import models
+
+class Question(models.Model):
+~~ question_text = models.CharField(max_length=200)
+~~ pub_date = models.DateTimeField("Date Published")
+ def __unicode__(self):
+ return self.question_text
+
+class Choice(models.Model):
+~~ question = models.ForeignKey(Question)
+~~ choice_text = models.CharField(max_length=200)
+~~ votes = models.IntegerField(default=0)
+ def __unicode__(self):
+ return self.choice_text
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 27 from drf-action-serializer
+[drf-action-serializer](https://github.com/gregschmit/drf-action-serializer)
+([PyPI page](https://pypi.org/project/drf-action-serializer/))
+that makes it easier to configure specific serializers to use based on the
+client's request action. For example, a list view should have one serializer
+whereas the detail view would have a different serializer.
+
+The project is open source under the
+[MIT license](https://github.com/gregschmit/drf-action-serializer/blob/master/LICENSE).
+
+[**drf-action-serializer / action_serializer / tests.py**](https://github.com/gregschmit/drf-action-serializer/blob/master/action_serializer/./tests.py)
+
+```python
+# tests.py
+
+import os
+
+~~from django.db import models
+from django.test import TestCase
+
+from .serializers import ModelActionSerializer
+
+
+def dedent(blocktext):
+ return "\n".join([line[12:] for line in blocktext.splitlines()[1:-1]])
+
+
+class RegularFieldsModel(models.Model):
+
+~~ auto_field = models.AutoField(primary_key=True)
+~~ boolean_field = models.BooleanField(default=False)
+~~ char_field = models.CharField(max_length=100)
+~~ date_field = models.DateField()
+~~ decimal_field = models.DecimalField(max_digits=3, decimal_places=1)
+~~ email_field = models.EmailField(max_length=100)
+~~ float_field = models.FloatField()
+~~ integer_field = models.IntegerField()
+
+
+class ModelActionSerializerTestCase(TestCase):
+
+ def test_action_fields(self):
+
+ class TestSerializer(ModelActionSerializer):
+ class Meta:
+ model = RegularFieldsModel
+ fields = "char_field"
+ action_fields = {"list": {"fields": ("auto_field", "char_field")}}
+
+ expected = dedent(
+ )
+ context = {"view": type("ActionView", (object,), {"action": "list"})}
+ self.assertEqual(repr(TestSerializer(context=context)), expected)
+
+ def test_action_fields_different_action(self):
+
+ class TestSerializer(ModelActionSerializer):
+ class Meta:
+ model = RegularFieldsModel
+ fields = ("char_field",)
+ action_fields = {"list": {"fields": ("auto_field", "char_field")}}
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ )
+ context = {"view": type("ActionView", (object,), {"action": "create"})}
+ self.assertEqual(repr(TestSerializer(context=context)), expected)
+
+ def test_action_extra_kwargs(self):
+
+ class TestSerializer(ModelActionSerializer):
+ class Meta:
+ model = RegularFieldsModel
+ fields = "char_field"
+ action_fields = {
+ "list": {
+ "fields": ("auto_field", "char_field"),
+ "extra_kwargs": {
+ "auto_field": {"required": False, "read_only": False}
+ },
+ }
+ }
+
+ expected = dedent(
+ )
+ context = {"view": type("ActionView", (object,), {"action": "list"})}
+ self.assertEqual(repr(TestSerializer(context=context)), expected)
+
+ def test_action_exclude(self):
+~~ auto_field = models.AutoField(primary_key=True)
+~~ boolean_field = models.BooleanField(default=False)
+~~ char_field = models.CharField(max_length=100)
+~~ date_field = models.DateField()
+~~ decimal_field = models.DecimalField(max_digits=3, decimal_places=1)
+~~ email_field = models.EmailField(max_length=100)
+~~ float_field = models.FloatField()
+~~ integer_field = models.IntegerField()
+
+ class TestSerializer(ModelActionSerializer):
+ class Meta:
+ model = RegularFieldsModel
+ fields = "char_field"
+ action_fields = {
+ "list": {
+ "exclude": (
+ "boolean_field",
+ "date_field",
+ "float_field",
+ "integer_field",
+ )
+ }
+ }
+
+ expected = dedent(
+ )
+ context = {"view": type("ActionView", (object,), {"action": "list"})}
+ self.assertEqual(repr(TestSerializer(context=context)), expected)
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 28 from elasticsearch-django
+[elasticsearch-django](https://github.com/yunojuno/elasticsearch-django)
+([PyPI package information](https://pypi.org/project/elasticsearch-django/))
+is a [Django](/django.html) app for managing
+[ElasticSearch](https://github.com/elastic/elasticsearch) indexes
+populated by [Django ORM](/django-orm.html) models. The project is
+available as open source under the
+[MIT license](https://github.com/yunojuno/elasticsearch-django/blob/master/LICENSE).
+
+[**elasticsearch-django / elasticsearch_django / models.py**](https://github.com/yunojuno/elasticsearch-django/blob/master/elasticsearch_django/./models.py)
+
+```python
+# models.py
+from __future__ import annotations
+
+import logging
+import time
+import warnings
+from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
+
+from django.conf import settings
+from django.contrib.postgres.fields import JSONField
+from django.core.cache import cache
+from django.core.serializers.json import DjangoJSONEncoder
+~~from django.db import models
+from django.db.models.expressions import RawSQL
+from django.db.models.fields import CharField
+from django.db.models.query import QuerySet
+from django.utils.timezone import now as tz_now
+from elasticsearch_dsl import Search
+
+from .settings import (
+ get_client,
+ get_model_index_properties,
+ get_model_indexes,
+ get_setting,
+)
+
+if TYPE_CHECKING:
+ from django.contrib.auth.models import AbstractBaseUser
+
+logger = logging.getLogger(__name__)
+
+UPDATE_STRATEGY_FULL = "full"
+UPDATE_STRATEGY_PARTIAL = "partial"
+UPDATE_STRATEGY = get_setting("update_strategy", UPDATE_STRATEGY_FULL)
+
+
+class SearchDocumentManagerMixin(models.Manager):
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ return
+
+ get_client().update(
+ index=index,
+ doc_type=self.search_doc_type,
+ body={"doc": doc},
+ id=self.pk, # type: ignore
+ )
+
+ def delete_search_document(self, *, index: str) -> None:
+ cache.delete(self.search_document_cache_key)
+ get_client().delete(
+ index=index, doc_type=self.search_doc_type, id=self.pk # type: ignore
+ )
+
+
+class SearchQuery(models.Model):
+
+ QUERY_TYPE_SEARCH = "SEARCH"
+ QUERY_TYPE_COUNT = "COUNT"
+ QUERY_TYPE_CHOICES = (
+ (QUERY_TYPE_SEARCH, "Search results"),
+ (QUERY_TYPE_COUNT, "Count only"),
+ )
+
+~~ user = models.ForeignKey(
+ settings.AUTH_USER_MODEL,
+ related_name="search_queries",
+ blank=True,
+ null=True,
+ help_text="The user who made the search query (nullable).",
+~~ on_delete=models.SET_NULL,
+ )
+~~ index = models.CharField(
+ max_length=100,
+ default="_all",
+ help_text="The name of the ElasticSearch index(es) being queried.",
+ )
+~~ search_terms = models.CharField(
+ max_length=400,
+ default="",
+ blank=True,
+ help_text=(
+ "Free text search terms used in the query, stored for easy reference."
+ ),
+ )
+ query = JSONField(
+ help_text="The raw ElasticSearch DSL query.", encoder=DjangoJSONEncoder
+ )
+ query_type = CharField(
+ help_text="Does this query return results, or just the hit count?",
+ choices=QUERY_TYPE_CHOICES,
+ default=QUERY_TYPE_SEARCH,
+ max_length=10,
+ )
+ hits = JSONField(
+ help_text="The list of meta info for each of the query matches returned.",
+ encoder=DjangoJSONEncoder,
+ )
+~~ total_hits = models.IntegerField(
+ default=0,
+ help_text="Total number of matches found for the query (!= the hits returned).",
+ )
+~~ reference = models.CharField(
+ max_length=100,
+ default="",
+ blank=True,
+ help_text="Custom reference used to identify and group related searches.",
+ )
+~~ executed_at = models.DateTimeField(
+ help_text="When the search was executed - set via execute() method."
+ )
+~~ duration = models.FloatField(
+ help_text="Time taken to execute the search itself, in seconds."
+ )
+
+ class Meta:
+ app_label = "elasticsearch_django"
+ verbose_name = "Search query"
+ verbose_name_plural = "Search queries"
+
+ def __str__(self) -> str:
+ return f"Query (id={self.pk}) run against index '{self.index}'"
+
+ def __repr__(self) -> str:
+ return (
+ f""
+ )
+
+ def save(self, *args: Any, **kwargs: Any) -> SearchQuery:
+ if self.search_terms is None:
+ self.search_terms = ""
+ super().save(**kwargs)
+ return self
+
+ def _extract_set(self, _property: str) -> List[Union[str, int]]:
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 29 from gadget-board
+[gadget-board](https://github.com/mik4el/gadget-board) is a
+[Django](/django.html),
+[Django REST Framework (DRF)](/django-rest-framework-drf.html) and
+[Angular](/angular.html) web application that is open source under the
+[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE).
+
+[**gadget-board / web / authentication / models.py**](https://github.com/mik4el/gadget-board/blob/master/web/authentication/models.py)
+
+```python
+# models.py
+from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
+~~from django.db import models
+from django.contrib.auth.models import BaseUserManager
+
+
+class AccountManager(BaseUserManager):
+ def create_account(self, username, password, **kwargs):
+ if not username:
+ raise ValueError('Users must have a valid username')
+
+ if not password:
+ raise ValueError('Users must have a valid password.')
+
+ if not kwargs.get('email'):
+ raise ValueError('Users must have a valid email.')
+
+ account = self.model(
+ username=username, email=self.normalize_email(kwargs.get('email'))
+ )
+
+ account.is_active = True
+
+ account.set_password(password)
+ account.save()
+
+ return account
+
+ def create_superuser(self, username, password, **kwargs):
+ account = self.create_account(username, password, **kwargs)
+
+ account.is_superuser = True
+
+ account.save()
+
+ return account
+
+
+class Account(AbstractBaseUser, PermissionsMixin):
+~~ username = models.CharField(max_length=40, unique=True)
+~~ email = models.EmailField() # users can share email
+
+~~ is_gadget = models.BooleanField(default=False)
+~~ is_active = models.BooleanField(default=True)
+
+~~ created_at = models.DateTimeField(auto_now_add=True)
+~~ updated_at = models.DateTimeField(auto_now=True)
+
+ objects = AccountManager()
+
+ USERNAME_FIELD = 'username'
+ REQUIRED_FIELDS = ['email']
+
+ def __str__(self):
+ return self.username
+
+ def get_full_name(self):
+ return self.username
+
+ def get_short_name(self):
+ return self.username
+
+ @property
+ def is_staff(self):
+ "Is the user a member of staff?"
+ return self.is_superuser
+
+
+
+## ... source file continues with no further models examples...
+
+```
+
+
+## Example 30 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / models.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/models.py)
+
+```python
+# models.py
+import json
+import logging
+from collections import defaultdict
+from io import StringIO
+from urllib.parse import urlparse
+
+from django.conf import settings
+from django.contrib.auth.models import Group, Permission
+from django.contrib.contenttypes.models import ContentType
+from django.core import checks
+from django.core.cache import cache
+from django.core.exceptions import ValidationError
+from django.core.handlers.base import BaseHandler
+from django.core.handlers.wsgi import WSGIRequest
+~~from django.db import models, transaction
+from django.db.models import Case, Q, Value, When
+from django.db.models.functions import Concat, Lower, Substr
+from django.http import Http404
+from django.http.request import split_domain_port
+from django.template.response import TemplateResponse
+from django.urls import NoReverseMatch, reverse
+from django.utils import timezone
+from django.utils.cache import patch_cache_control
+from django.utils.functional import cached_property
+from django.utils.text import capfirst, slugify
+from django.utils.translation import gettext_lazy as _
+from modelcluster.models import (
+ ClusterableModel, get_all_child_m2m_relations, get_all_child_relations)
+from treebeard.mp_tree import MP_Node
+
+from wagtail.core.query import PageQuerySet, TreeQuerySet
+from wagtail.core.signals import page_published, page_unpublished, post_page_move, pre_page_move
+from wagtail.core.sites import get_site_for_hostname
+from wagtail.core.url_routing import RouteResult
+from wagtail.core.utils import WAGTAIL_APPEND_SLASH, camelcase_to_underscore, resolve_model_string
+from wagtail.search import index
+
+
+logger = logging.getLogger('wagtail.core')
+
+PAGE_TEMPLATE_VAR = 'page'
+
+
+class SiteManager(models.Manager):
+ def get_queryset(self):
+ return super(SiteManager, self).get_queryset().order_by(Lower("hostname"))
+
+ def get_by_natural_key(self, hostname, port):
+ return self.get(hostname=hostname, port=port)
+
+
+class Site(models.Model):
+~~ hostname = models.CharField(verbose_name=_('hostname'), max_length=255, db_index=True)
+~~ port = models.IntegerField(
+ verbose_name=_('port'),
+ default=80,
+ help_text=_(
+ "Set this to something other than 80 if you need a specific port number to appear in URLs"
+ " (e.g. development on port 8000). Does not affect request handling (so port forwarding still works)."
+ )
+ )
+~~ site_name = models.CharField(
+ verbose_name=_('site name'),
+ max_length=255,
+ blank=True,
+ help_text=_("Human-readable name for the site.")
+ )
+~~ root_page = models.ForeignKey('Page', verbose_name=_('root page'), related_name='sites_rooted_here', on_delete=models.CASCADE)
+~~ is_default_site = models.BooleanField(
+ verbose_name=_('is default site'),
+ default=False,
+ help_text=_(
+ "If true, this site will handle requests for all other hostnames that do not have a site entry of their own"
+ )
+ )
+
+ objects = SiteManager()
+
+ class Meta:
+ unique_together = ('hostname', 'port')
+ verbose_name = _('site')
+ verbose_name_plural = _('sites')
+
+ def natural_key(self):
+ return (self.hostname, self.port)
+
+ def __str__(self):
+ default_suffix = " [{}]".format(_("default"))
+ if self.site_name:
+ return(
+ self.site_name
+ + (default_suffix if self.is_default_site else "")
+ )
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+ if 'template' not in dct:
+ cls.template = "%s/%s.html" % (cls._meta.app_label, camelcase_to_underscore(name))
+
+ if 'ajax_template' not in dct:
+ cls.ajax_template = None
+
+ cls._clean_subpage_models = None # to be filled in on first call to cls.clean_subpage_models
+ cls._clean_parent_page_models = None # to be filled in on first call to cls.clean_parent_page_models
+
+ if 'is_creatable' not in dct:
+ cls.is_creatable = not cls._meta.abstract
+
+ if not cls._meta.abstract:
+ PAGE_MODEL_CLASSES.append(cls)
+
+
+class AbstractPage(MP_Node):
+ objects = PageManager()
+
+ class Meta:
+ abstract = True
+
+
+class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
+~~ title = models.CharField(
+ verbose_name=_('title'),
+ max_length=255,
+ help_text=_("The page title as you'd like it to be seen by the public")
+ )
+~~ draft_title = models.CharField(
+ max_length=255,
+ editable=False
+ )
+~~ slug = models.SlugField(
+ verbose_name=_('slug'),
+ allow_unicode=True,
+ max_length=255,
+ help_text=_("The name of the page as it will appear in URLs e.g http://domain.com/blog/[my-slug]/")
+ )
+~~ content_type = models.ForeignKey(
+ ContentType,
+ verbose_name=_('content type'),
+ related_name='pages',
+~~ on_delete=models.SET(get_default_page_content_type)
+ )
+~~ live = models.BooleanField(verbose_name=_('live'), default=True, editable=False)
+~~ has_unpublished_changes = models.BooleanField(
+ verbose_name=_('has unpublished changes'),
+ default=False,
+ editable=False
+ )
+~~ url_path = models.TextField(verbose_name=_('URL path'), blank=True, editable=False)
+~~ owner = models.ForeignKey(
+ settings.AUTH_USER_MODEL,
+ verbose_name=_('owner'),
+ null=True,
+ blank=True,
+ editable=True,
+~~ on_delete=models.SET_NULL,
+ related_name='owned_pages'
+ )
+
+~~ seo_title = models.CharField(
+ verbose_name=_("page title"),
+ max_length=255,
+ blank=True,
+ help_text=_("Optional. 'Search Engine Friendly' title. This will appear at the top of the browser window.")
+ )
+
+ show_in_menus_default = False
+~~ show_in_menus = models.BooleanField(
+ verbose_name=_('show in menus'),
+ default=False,
+ help_text=_("Whether a link to this page will appear in automatically generated menus")
+ )
+~~ search_description = models.TextField(verbose_name=_('search description'), blank=True)
+
+~~ go_live_at = models.DateTimeField(
+ verbose_name=_("go live date/time"),
+ blank=True,
+ null=True
+ )
+~~ expire_at = models.DateTimeField(
+ verbose_name=_("expiry date/time"),
+ blank=True,
+ null=True
+ )
+~~ expired = models.BooleanField(verbose_name=_('expired'), default=False, editable=False)
+
+~~ locked = models.BooleanField(verbose_name=_('locked'), default=False, editable=False)
+~~ locked_at = models.DateTimeField(verbose_name=_('locked at'), null=True, editable=False)
+~~ locked_by = models.ForeignKey(
+ settings.AUTH_USER_MODEL,
+ verbose_name=_('locked by'),
+ null=True,
+ blank=True,
+ editable=False,
+~~ on_delete=models.SET_NULL,
+ related_name='locked_pages'
+ )
+
+~~ first_published_at = models.DateTimeField(
+ verbose_name=_('first published at'),
+ blank=True,
+ null=True,
+ db_index=True
+ )
+~~ last_published_at = models.DateTimeField(
+ verbose_name=_('last published at'),
+ null=True,
+ editable=False
+ )
+~~ latest_revision_created_at = models.DateTimeField(
+ verbose_name=_('latest revision created at'),
+ null=True,
+ editable=False
+ )
+~~ live_revision = models.ForeignKey(
+ 'PageRevision',
+ related_name='+',
+ verbose_name='live revision',
+~~ on_delete=models.SET_NULL,
+ null=True,
+ blank=True,
+ editable=False
+ )
+
+ search_fields = [
+ index.SearchField('title', partial_match=True, boost=2),
+ index.AutocompleteField('title'),
+ index.FilterField('title'),
+ index.FilterField('id'),
+ index.FilterField('live'),
+ index.FilterField('owner'),
+ index.FilterField('content_type'),
+ index.FilterField('path'),
+ index.FilterField('depth'),
+ index.FilterField('locked'),
+ index.FilterField('show_in_menus'),
+ index.FilterField('first_published_at'),
+ index.FilterField('last_published_at'),
+ index.FilterField('latest_revision_created_at'),
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ self.title,
+ self.id,
+ cls._meta.app_label,
+ cls.__name__,
+ self.url_path
+ )
+
+ return result
+
+ def delete(self, *args, **kwargs):
+ if type(self) is Page:
+ return super().delete(*args, **kwargs)
+ else:
+ return Page.objects.get(id=self.id).delete(*args, **kwargs)
+
+ @classmethod
+ def check(cls, **kwargs):
+ errors = super(Page, cls).check(**kwargs)
+
+
+ field_exceptions = [field.name
+ for model in [cls] + list(cls._meta.get_parent_list())
+ for field in model._meta.parents.values() if field]
+
+ for field in cls._meta.fields:
+~~ if isinstance(field, models.ForeignKey) and field.name not in field_exceptions:
+~~ if field.remote_field.on_delete == models.CASCADE:
+ errors.append(
+ checks.Warning(
+ "Field hasn't specified on_delete action",
+~~ hint="Set on_delete=models.SET_NULL and make sure the field is nullable or set on_delete=models.PROTECT. Wagtail does not allow simple database CASCADE because it will corrupt its tree storage.",
+ obj=field,
+ id='wagtailcore.W001',
+ )
+ )
+
+ if not isinstance(cls.objects, PageManager):
+ errors.append(
+ checks.Error(
+ "Manager does not inherit from PageManager",
+ hint="Ensure that custom Page managers inherit from wagtail.core.models.PageManager",
+ obj=cls,
+ id='wagtailcore.E002',
+ )
+ )
+
+ try:
+ cls.clean_subpage_models()
+ except (ValueError, LookupError) as e:
+ errors.append(
+ checks.Error(
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ instance=new_self,
+ parent_page_before=parent_before,
+ parent_page_after=parent_after,
+ url_path_before=old_url_path,
+ url_path_after=new_url_path,
+ )
+
+ logger.info("Page moved: \"%s\" id=%d path=%s", self.title, self.id, new_url_path)
+
+ def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True, keep_live=True, user=None, process_child_object=None, exclude_fields=None):
+ specific_self = self.specific
+ default_exclude_fields = ['id', 'path', 'depth', 'numchild', 'url_path', 'path', 'index_entries']
+ exclude_fields = default_exclude_fields + specific_self.exclude_fields_in_copy + (exclude_fields or [])
+ specific_dict = {}
+
+ for field in specific_self._meta.get_fields():
+ if field.name in exclude_fields:
+ continue
+
+ if field.auto_created:
+ continue
+
+ if field.many_to_many:
+ continue
+
+~~ if isinstance(field, models.OneToOneField) and field.remote_field.parent_link:
+ continue
+
+ specific_dict[field.name] = getattr(specific_self, field.name)
+
+ for related_field in get_all_child_m2m_relations(specific_self):
+ if related_field.name in exclude_fields:
+ continue
+
+ field = getattr(specific_self, related_field.name)
+ if field and hasattr(field, 'all'):
+ values = field.all()
+ if values:
+ specific_dict[related_field.name] = values
+
+ page_copy = self.specific_class(**specific_dict)
+
+ if not keep_live:
+ page_copy.live = False
+ page_copy.has_unpublished_changes = True
+ page_copy.live_revision = None
+ page_copy.first_published_at = None
+ page_copy.last_published_at = None
+
+ if user:
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+ obj.path = self.path
+ obj.depth = self.depth
+ obj.numchild = self.numchild
+
+ obj.set_url_path(self.get_parent())
+
+ obj.draft_title = self.draft_title
+ obj.live = self.live
+ obj.has_unpublished_changes = self.has_unpublished_changes
+ obj.owner = self.owner
+ obj.locked = self.locked
+ obj.locked_by = self.locked_by
+ obj.locked_at = self.locked_at
+ obj.latest_revision_created_at = self.latest_revision_created_at
+ obj.first_published_at = self.first_published_at
+
+ return obj
+
+ class Meta:
+ verbose_name = _('page')
+ verbose_name_plural = _('pages')
+
+
+class Orderable(models.Model):
+~~ sort_order = models.IntegerField(null=True, blank=True, editable=False)
+ sort_order_field = 'sort_order'
+
+ class Meta:
+ abstract = True
+ ordering = ['sort_order']
+
+
+class SubmittedRevisionsManager(models.Manager):
+ def get_queryset(self):
+ return super().get_queryset().filter(submitted_for_moderation=True)
+
+
+class PageRevision(models.Model):
+~~ page = models.ForeignKey('Page', verbose_name=_('page'), related_name='revisions', on_delete=models.CASCADE)
+~~ submitted_for_moderation = models.BooleanField(
+ verbose_name=_('submitted for moderation'),
+ default=False,
+ db_index=True
+ )
+~~ created_at = models.DateTimeField(db_index=True, verbose_name=_('created at'))
+~~ user = models.ForeignKey(
+ settings.AUTH_USER_MODEL, verbose_name=_('user'), null=True, blank=True,
+~~ on_delete=models.SET_NULL
+ )
+~~ content_json = models.TextField(verbose_name=_('content JSON'))
+~~ approved_go_live_at = models.DateTimeField(
+ verbose_name=_('approved go live at'),
+ null=True,
+ blank=True,
+ db_index=True
+ )
+
+~~ objects = models.Manager()
+ submitted_revisions = SubmittedRevisionsManager()
+
+ def save(self, *args, **kwargs):
+ if self.created_at is None:
+ self.created_at = timezone.now()
+
+ super().save(*args, **kwargs)
+ if self.submitted_for_moderation:
+ self.page.revisions.exclude(id=self.id).update(submitted_for_moderation=False)
+
+ def as_page_object(self):
+ return self.page.specific.with_content_json(self.content_json)
+
+ def approve_moderation(self):
+ if self.submitted_for_moderation:
+ logger.info("Page moderation approved: \"%s\" id=%d revision_id=%d", self.page.title, self.page.id, self.id)
+ self.publish()
+
+ def reject_moderation(self):
+ if self.submitted_for_moderation:
+ logger.info("Page moderation rejected: \"%s\" id=%d revision_id=%d", self.page.title, self.page.id, self.id)
+ self.submitted_for_moderation = False
+ self.save(update_fields=['submitted_for_moderation'])
+
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+
+ def __str__(self):
+ return '"' + str(self.page) + '" at ' + str(self.created_at)
+
+ class Meta:
+ verbose_name = _('page revision')
+ verbose_name_plural = _('page revisions')
+
+
+PAGE_PERMISSION_TYPES = [
+ ('add', _("Add"), _("Add/edit pages you own")),
+ ('edit', _("Edit"), _("Edit any page")),
+ ('publish', _("Publish"), _("Publish any page")),
+ ('bulk_delete', _("Bulk delete"), _("Delete pages with children")),
+ ('lock', _("Lock"), _("Lock/unlock pages you've locked")),
+ ('unlock', _("Unlock"), _("Unlock any page")),
+]
+
+PAGE_PERMISSION_TYPE_CHOICES = [
+ (identifier, long_label)
+ for identifier, short_label, long_label in PAGE_PERMISSION_TYPES
+]
+
+
+class GroupPagePermission(models.Model):
+~~ group = models.ForeignKey(Group, verbose_name=_('group'), related_name='page_permissions', on_delete=models.CASCADE)
+~~ page = models.ForeignKey('Page', verbose_name=_('page'), related_name='group_permissions', on_delete=models.CASCADE)
+~~ permission_type = models.CharField(
+ verbose_name=_('permission type'),
+ max_length=20,
+ choices=PAGE_PERMISSION_TYPE_CHOICES
+ )
+
+ class Meta:
+ unique_together = ('group', 'page', 'permission_type')
+ verbose_name = _('group page permission')
+ verbose_name_plural = _('group page permissions')
+
+ def __str__(self):
+ return "Group %d ('%s') has permission '%s' on page %d ('%s')" % (
+ self.group.id, self.group,
+ self.permission_type,
+ self.page.id, self.page
+ )
+
+
+class UserPagePermissionsProxy:
+
+ def __init__(self, user):
+ self.user = user
+
+ if user.is_active and not user.is_superuser:
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ if not destination.specific_class.creatable_subpage_models():
+ return False
+
+ if 'add' not in destination_perms.permissions:
+ return False
+
+ return True
+
+ def can_view_revisions(self):
+ return not self.page_is_root
+
+
+class BaseViewRestriction(models.Model):
+ NONE = 'none'
+ PASSWORD = 'password'
+ GROUPS = 'groups'
+ LOGIN = 'login'
+
+ RESTRICTION_CHOICES = (
+ (NONE, _("Public")),
+ (LOGIN, _("Private, accessible to logged-in users")),
+ (PASSWORD, _("Private, accessible with the following password")),
+ (GROUPS, _("Private, accessible to users in specific groups")),
+ )
+
+~~ restriction_type = models.CharField(
+ max_length=20, choices=RESTRICTION_CHOICES)
+~~ password = models.CharField(verbose_name=_('password'), max_length=255, blank=True)
+~~ groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True)
+
+ def accept_request(self, request):
+ if self.restriction_type == BaseViewRestriction.PASSWORD:
+ passed_restrictions = request.session.get(self.passed_view_restrictions_session_key, [])
+ if self.id not in passed_restrictions:
+ return False
+
+ elif self.restriction_type == BaseViewRestriction.LOGIN:
+ if not request.user.is_authenticated:
+ return False
+
+ elif self.restriction_type == BaseViewRestriction.GROUPS:
+ if not request.user.is_superuser:
+ current_user_groups = request.user.groups.all()
+
+ if not any(group in current_user_groups for group in self.groups.all()):
+ return False
+
+ return True
+
+ def mark_as_passed(self, request):
+ has_existing_session = (settings.SESSION_COOKIE_NAME in request.COOKIES)
+ passed_restrictions = request.session.setdefault(self.passed_view_restrictions_session_key, [])
+ if self.id not in passed_restrictions:
+ passed_restrictions.append(self.id)
+ request.session[self.passed_view_restrictions_session_key] = passed_restrictions
+ if not has_existing_session:
+ request.session.set_expiry(0)
+
+ class Meta:
+ abstract = True
+ verbose_name = _('view restriction')
+ verbose_name_plural = _('view restrictions')
+
+
+class PageViewRestriction(BaseViewRestriction):
+~~ page = models.ForeignKey(
+~~ 'Page', verbose_name=_('page'), related_name='view_restrictions', on_delete=models.CASCADE
+ )
+
+ passed_view_restrictions_session_key = 'passed_page_view_restrictions'
+
+ class Meta:
+ verbose_name = _('page view restriction')
+ verbose_name_plural = _('page view restrictions')
+
+
+class BaseCollectionManager(models.Manager):
+ def get_queryset(self):
+ return TreeQuerySet(self.model).order_by('path')
+
+
+CollectionManager = BaseCollectionManager.from_queryset(TreeQuerySet)
+
+
+class CollectionViewRestriction(BaseViewRestriction):
+~~ collection = models.ForeignKey(
+ 'Collection',
+ verbose_name=_('collection'),
+ related_name='view_restrictions',
+~~ on_delete=models.CASCADE
+ )
+
+ passed_view_restrictions_session_key = 'passed_collection_view_restrictions'
+
+ class Meta:
+ verbose_name = _('collection view restriction')
+ verbose_name_plural = _('collection view restrictions')
+
+
+class Collection(MP_Node):
+~~ name = models.CharField(max_length=255, verbose_name=_('name'))
+
+ objects = CollectionManager()
+
+ def __str__(self):
+ return self.name
+
+ def get_ancestors(self, inclusive=False):
+ return Collection.objects.ancestor_of(self, inclusive)
+
+ def get_descendants(self, inclusive=False):
+ return Collection.objects.descendant_of(self, inclusive)
+
+ def get_siblings(self, inclusive=True):
+ return Collection.objects.sibling_of(self, inclusive)
+
+ def get_next_siblings(self, inclusive=False):
+ return self.get_siblings(inclusive).filter(path__gte=self.path).order_by('path')
+
+ def get_prev_siblings(self, inclusive=False):
+ return self.get_siblings(inclusive).filter(path__lte=self.path).order_by('-path')
+
+ def get_view_restrictions(self):
+ return CollectionViewRestriction.objects.filter(collection__in=self.get_ancestors(inclusive=True))
+
+ @staticmethod
+ def order_for_display(queryset):
+ return queryset.annotate(
+ display_order=Case(
+ When(depth=1, then=Value('')),
+ default='name')
+ ).order_by('display_order')
+
+ class Meta:
+ verbose_name = _('collection')
+ verbose_name_plural = _('collections')
+
+
+def get_root_collection_id():
+ return Collection.get_first_root_node().id
+
+
+class CollectionMember(models.Model):
+~~ collection = models.ForeignKey(
+ Collection,
+ default=get_root_collection_id,
+ verbose_name=_('collection'),
+ related_name='+',
+~~ on_delete=models.CASCADE
+ )
+
+ search_fields = [
+ index.FilterField('collection'),
+ ]
+
+ class Meta:
+ abstract = True
+
+
+class GroupCollectionPermission(models.Model):
+~~ group = models.ForeignKey(
+ Group,
+ verbose_name=_('group'),
+ related_name='collection_permissions',
+~~ on_delete=models.CASCADE
+ )
+~~ collection = models.ForeignKey(
+ Collection,
+ verbose_name=_('collection'),
+ related_name='group_permissions',
+~~ on_delete=models.CASCADE
+ )
+~~ permission = models.ForeignKey(
+ Permission,
+ verbose_name=_('permission'),
+~~ on_delete=models.CASCADE
+ )
+
+ def __str__(self):
+ return "Group %d ('%s') has permission '%s' on collection %d ('%s')" % (
+ self.group.id, self.group,
+ self.permission,
+ self.collection.id, self.collection
+ )
+
+ class Meta:
+ unique_together = ('group', 'collection', 'permission')
+ verbose_name = _('group collection permission')
+ verbose_name_plural = _('group collection permissions')
+
+ ]
+
+ is_creatable = False
+
+
+
+## ... source file abbreviated to get to models examples ...
+
+
+ "Invalid subpage_types setting for %s" % cls,
+ hint=str(e),
+ id='wagtailcore.E002'
+ )
+
+
+## ... source file continues with no further models examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-programmingerror.markdown b/content/pages/examples/django/django-db-programmingerror.markdown
new file mode 100644
index 000000000..6b3037811
--- /dev/null
+++ b/content/pages/examples/django/django-db-programmingerror.markdown
@@ -0,0 +1,232 @@
+title: django.db ProgrammingError Example Code
+category: page
+slug: django-db-programmingerror-examples
+sortorder: 500011163
+toc: False
+sidebartitle: django.db ProgrammingError
+meta: Python example code for the ProgrammingError class from the django.db module of the Django project.
+
+
+ProgrammingError is a class within the django.db module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / appresolver.py**](https://github.com/divio/django-cms/blob/develop/cms/./appresolver.py)
+
+```python
+# appresolver.py
+from collections import OrderedDict
+from importlib import import_module
+
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db import OperationalError, ProgrammingError
+from django.utils.translation import get_language, override
+from django.urls import Resolver404, reverse
+
+from six import string_types
+
+from cms.apphook_pool import apphook_pool
+from cms.models.pagemodel import Page
+from cms.utils import get_current_site
+from cms.utils.compat import DJANGO_1_11
+from cms.utils.compat.dj import RegexPattern, URLPattern, URLResolver
+from cms.utils.i18n import get_language_list
+from cms.utils.moderator import use_draft
+
+
+APP_RESOLVERS = []
+
+
+def clear_app_resolvers():
+ global APP_RESOLVERS
+ APP_RESOLVERS = []
+
+
+def applications_page_check(request, current_page=None, path=None):
+ if current_page:
+
+
+## ... source file abbreviated to get to ProgrammingError examples ...
+
+
+ if not hasattr(mod, 'urlpatterns'):
+ raise ImproperlyConfigured(
+ "URLConf `%s` has no urlpatterns attribute" % urlconf)
+ yield getattr(mod, 'urlpatterns')
+ elif isinstance(urlconf, (list, tuple)):
+ yield urlconf
+ else:
+ yield [urlconf]
+
+
+def get_patterns_for_title(path, title):
+ app = apphook_pool.get_apphook(title.page.application_urls)
+ url_patterns = []
+ for pattern_list in get_app_urls(app.get_urls(title.page, title.language)):
+ if path and not path.endswith('/'):
+ path += '/'
+ page_id = title.page.id
+ url_patterns += recurse_patterns(path, pattern_list, page_id)
+ return url_patterns
+
+
+def get_app_patterns():
+ try:
+ site = get_current_site()
+ return _get_app_patterns(site)
+~~ except (OperationalError, ProgrammingError):
+ return []
+
+
+def _get_app_patterns(site):
+ from cms.models import Title
+
+ included = []
+
+ title_qs = Title.objects.public().filter(page__node__site=site)
+
+ hooked_applications = OrderedDict()
+
+ titles = (title_qs.exclude(page__application_urls=None)
+ .exclude(page__application_urls='')
+ .order_by('-page__node__path').select_related())
+ for title in titles:
+ path = title.path
+ mix_id = "%s:%s:%s" % (
+ path + "/", title.page.application_urls, title.language)
+ if mix_id in included:
+ continue
+ if not settings.APPEND_SLASH:
+ path += '/'
+ app = apphook_pool.get_apphook(title.page.application_urls)
+
+
+## ... source file continues with no further ProgrammingError examples...
+
+```
+
+
+## Example 2 from django-migration-linter
+[django-migration-linter](https://github.com/3YOURMIND/django-migration-linter)
+([PyPI package information](https://pypi.org/project/django-migration-linter/))
+checks for backwards-incompatible changes in [Django ORM](/django-orm.html)
+schema migrations and warns you about them. The purpose of the project is
+to save time in older and larger projects by detecting field migrations
+that will be a problem so you do not run into issues later, and make it
+easier to enable continuous [deployment](/deployment.html) configurations
+with database changes. There is a
+[blog post on keeping Django database migrations backward compatible](https://medium.com/3yourmind/keeping-django-database-migrations-backward-compatible-727820260dbb)
+that goes into further detail on the tool.
+
+The django-migration-linter project is open sourced under the
+[Apache 2.0 license](https://github.com/3YOURMIND/django-migration-linter/blob/master/LICENSE).
+
+[**django-migration-linter / django_migration_linter / migration_linter.py**](https://github.com/3YOURMIND/django-migration-linter/blob/master/django_migration_linter/./migration_linter.py)
+
+```python
+# migration_linter.py
+from __future__ import print_function
+
+import hashlib
+import inspect
+import logging
+import os
+import re
+from subprocess import Popen, PIPE
+
+from django.conf import settings
+from django.core.management import call_command
+~~from django.db import DEFAULT_DB_ALIAS, connections, ProgrammingError
+from django.db.migrations import RunPython
+from enum import Enum, unique
+from six import PY2
+
+from .cache import Cache
+from .constants import (
+ DEFAULT_CACHE_PATH,
+ EXPECTED_DATA_MIGRATION_ARGS,
+ DJANGO_APPS_WITH_MIGRATIONS,
+)
+from .utils import clean_bytes_to_str, get_migration_abspath, split_migration_path
+from .operations import IgnoreMigration
+from .sql_analyser import analyse_sql_statements
+
+logger = logging.getLogger(__name__)
+
+
+@unique
+class MessageType(Enum):
+ OK = "ok"
+ IGNORE = "ignore"
+ WARNING = "warning"
+ ERROR = "error"
+
+
+
+## ... source file abbreviated to get to ProgrammingError examples ...
+
+
+ def print_summary(self):
+ print()
+ print("*** Summary ***")
+ print("Valid migrations: {}/{}".format(self.nb_valid, self.nb_total))
+ print("Erroneous migrations: {}/{}".format(self.nb_erroneous, self.nb_total))
+ print("Migrations with warnings: {}/{}".format(self.nb_warnings, self.nb_total))
+ print("Ignored migrations: {}/{}".format(self.nb_ignored, self.nb_total))
+
+ @property
+ def has_errors(self):
+ return self.nb_erroneous > 0
+
+ def get_sql(self, app_label, migration_name):
+ logger.info(
+ "Calling sqlmigrate command {} {}".format(app_label, migration_name)
+ )
+ dev_null = open(os.devnull, "w")
+ try:
+ sql_statement = call_command(
+ "sqlmigrate",
+ app_label,
+ migration_name,
+ database=self.database,
+ stdout=dev_null,
+ )
+~~ except (ValueError, ProgrammingError):
+ logger.warning(
+ (
+ "Error while executing sqlmigrate on (%s, %s). "
+ "Continuing execution with empty SQL."
+ ),
+ app_label,
+ migration_name,
+ )
+ sql_statement = ""
+ return sql_statement.splitlines()
+
+ @staticmethod
+ def is_migration_file(filename):
+ from django.db.migrations.loader import MIGRATIONS_MODULE_NAME
+
+ return (
+ re.search(r"/{0}/.*\.py".format(MIGRATIONS_MODULE_NAME), filename)
+ and "__init__" not in filename
+ )
+
+ @classmethod
+ def read_migrations_list(cls, migrations_file_path):
+ if not migrations_file_path:
+ return None
+
+
+## ... source file continues with no further ProgrammingError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-router.markdown b/content/pages/examples/django/django-db-router.markdown
new file mode 100644
index 000000000..7987041a7
--- /dev/null
+++ b/content/pages/examples/django/django-db-router.markdown
@@ -0,0 +1,522 @@
+title: django.db router Example Code
+category: page
+slug: django-db-router-examples
+sortorder: 500011168
+toc: False
+sidebartitle: django.db router
+meta: Python example code for the router callable from the django.db module of the Django project.
+
+
+router is a callable within the django.db module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / admin / placeholderadmin.py**](https://github.com/divio/django-cms/blob/develop/cms/admin/placeholderadmin.py)
+
+```python
+# placeholderadmin.py
+import uuid
+import warnings
+
+from django.conf.urls import url
+from django.contrib.admin.helpers import AdminForm
+from django.contrib.admin.utils import get_deleted_objects
+from django.core.exceptions import PermissionDenied
+~~from django.db import router, transaction
+from django.http import (
+ HttpResponse,
+ HttpResponseBadRequest,
+ HttpResponseForbidden,
+ HttpResponseNotFound,
+ HttpResponseRedirect,
+)
+from django.shortcuts import get_list_or_404, get_object_or_404, render
+from django.template.response import TemplateResponse
+from django.utils.decorators import method_decorator
+from django.utils.encoding import force_text
+from django.utils import translation
+from django.utils.translation import ugettext as _
+from django.views.decorators.clickjacking import xframe_options_sameorigin
+from django.views.decorators.http import require_POST
+
+from six.moves.urllib.parse import parse_qsl, urlparse
+
+from six import get_unbound_function, get_method_function
+
+from cms import operations
+from cms.admin.forms import PluginAddValidationForm
+from cms.constants import SLUG_REGEXP
+from cms.exceptions import PluginLimitReached
+
+
+## ... source file abbreviated to get to router examples ...
+
+
+ source_placeholder.mark_as_dirty(target_language, clear_cache=False)
+
+ self._send_post_placeholder_operation(
+ request,
+ operation=operations.CUT_PLUGIN,
+ token=action_token,
+ plugin=updated_plugin.get_bound_plugin(),
+ clipboard=target_placeholder,
+ clipboard_language=target_language,
+ source_language=source_language,
+ source_placeholder=source_placeholder,
+ source_parent_id=plugin.parent_id,
+ source_order=new_source_order,
+ )
+ return updated_plugin
+
+ @xframe_options_sameorigin
+ def delete_plugin(self, request, plugin_id):
+ plugin = self._get_plugin_from_id(plugin_id)
+
+ if not self.has_delete_plugin_permission(request, plugin):
+ return HttpResponseForbidden(force_text(
+ _("You do not have permission to delete this plugin")))
+
+ opts = plugin._meta
+~~ using = router.db_for_write(opts.model)
+ if DJANGO_2_0:
+ get_deleted_objects_additional_kwargs = {
+ 'opts': opts,
+ 'using': using,
+ 'user': request.user,
+ }
+ else:
+ get_deleted_objects_additional_kwargs = {'request': request}
+ deleted_objects, __, perms_needed, protected = get_deleted_objects(
+ [plugin], admin_site=self.admin_site,
+ **get_deleted_objects_additional_kwargs
+ )
+
+ if request.POST: # The user has already confirmed the deletion.
+ if perms_needed:
+ raise PermissionDenied(_("You do not have permission to delete this plugin"))
+ obj_display = force_text(plugin)
+ placeholder = plugin.placeholder
+ plugin_tree_order = placeholder.get_plugin_tree_order(
+ language=plugin.language,
+ parent_id=plugin.parent_id,
+ )
+
+ operation_token = self._send_pre_placeholder_operation(
+
+
+## ... source file abbreviated to get to router examples ...
+
+
+ "object": plugin,
+ "deleted_objects": deleted_objects,
+ "perms_lacking": perms_needed,
+ "protected": protected,
+ "opts": opts,
+ "app_label": opts.app_label,
+ }
+ request.current_app = self.admin_site.name
+ return TemplateResponse(
+ request, "admin/cms/page/plugin/delete_confirmation.html", context
+ )
+
+ @xframe_options_sameorigin
+ def clear_placeholder(self, request, placeholder_id):
+ placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
+ language = request.GET.get('language')
+
+ if placeholder.pk == request.toolbar.clipboard.pk:
+ placeholder.clear(language)
+ return HttpResponseRedirect(admin_reverse('index', current_app=self.admin_site.name))
+
+ if not self.has_clear_placeholder_permission(request, placeholder, language):
+ return HttpResponseForbidden(force_text(_("You do not have permission to clear this placeholder")))
+
+ opts = Placeholder._meta
+~~ using = router.db_for_write(Placeholder)
+ plugins = placeholder.get_plugins_list(language)
+
+ if DJANGO_2_0:
+ get_deleted_objects_additional_kwargs = {
+ 'opts': opts,
+ 'using': using,
+ 'user': request.user,
+ }
+ else:
+ get_deleted_objects_additional_kwargs = {'request': request}
+ deleted_objects, __, perms_needed, protected = get_deleted_objects(
+ plugins, admin_site=self.admin_site,
+ **get_deleted_objects_additional_kwargs
+ )
+
+ obj_display = force_text(placeholder)
+
+ if request.POST:
+ if perms_needed:
+ return HttpResponseForbidden(force_text(_("You do not have permission to clear this placeholder")))
+
+ operation_token = self._send_pre_placeholder_operation(
+ request,
+ operation=operations.CLEAR_PLACEHOLDER,
+
+
+## ... source file continues with no further router examples...
+
+```
+
+
+## Example 2 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / admin / folderadmin.py**](https://github.com/divio/django-filer/blob/develop/filer/admin/folderadmin.py)
+
+```python
+# folderadmin.py
+from __future__ import absolute_import, division, unicode_literals
+
+import itertools
+import os
+import re
+from collections import OrderedDict
+
+from django import forms
+from django.conf import settings as django_settings
+from django.conf.urls import url
+from django.contrib import messages
+from django.contrib.admin import helpers
+from django.contrib.admin.utils import capfirst, quote, unquote
+from django.core.exceptions import PermissionDenied, ValidationError
+from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
+~~from django.db import models, router
+from django.http import HttpResponse, HttpResponseRedirect
+from django.shortcuts import get_object_or_404, render
+from django.urls import reverse
+from django.utils.encoding import force_text
+from django.utils.html import escape
+from django.utils.http import urlquote, urlunquote
+from django.utils.safestring import mark_safe
+from django.utils.translation import ugettext as _
+from django.utils.translation import ugettext_lazy, ungettext
+
+from .. import settings
+from ..models import (
+ File, Folder, FolderPermission, FolderRoot, ImagesWithMissingData,
+ UnsortedImages, tools,
+)
+from ..settings import FILER_IMAGE_MODEL, FILER_PAGINATE_BY
+from ..thumbnail_processors import normalize_subject_location
+from ..utils.compatibility import get_delete_permission
+from ..utils.filer_easy_thumbnails import FilerActionThumbnailer
+from ..utils.loader import load_model
+from . import views
+from .forms import CopyFilesAndFoldersForm, RenameFilesForm, ResizeImagesForm
+from .patched.admin_utils import get_deleted_objects
+from .permissions import PrimitivePermissionAwareModelAdmin
+
+
+## ... source file abbreviated to get to router examples ...
+
+
+ return self.files_set_public_or_private(request, False, files_queryset,
+ folders_queryset)
+
+ files_set_private.short_description = ugettext_lazy(
+ "Enable permissions for selected files")
+
+ def files_set_public(self, request, files_queryset, folders_queryset):
+ return self.files_set_public_or_private(request, True, files_queryset,
+ folders_queryset)
+
+ files_set_public.short_description = ugettext_lazy(
+ "Disable permissions for selected files")
+
+ def delete_files_or_folders(self, request, files_queryset, folders_queryset):
+ opts = self.model._meta
+ app_label = opts.app_label
+
+ if not self.has_delete_permission(request):
+ raise PermissionDenied
+
+ current_folder = self._get_current_action_folder(
+ request, files_queryset, folders_queryset)
+
+ all_protected = []
+
+~~ using = router.db_for_write(self.model)
+ deletable_files, model_count_files, perms_needed_files, protected_files = get_deleted_objects(files_queryset, files_queryset.model._meta, request.user, self.admin_site, using)
+ deletable_folders, model_count_folder, perms_needed_folders, protected_folders = get_deleted_objects(folders_queryset, folders_queryset.model._meta, request.user, self.admin_site, using)
+ all_protected.extend(protected_files)
+ all_protected.extend(protected_folders)
+
+ all_deletable_objects = [deletable_files, deletable_folders]
+ all_perms_needed = perms_needed_files.union(perms_needed_folders)
+
+ if request.POST.get('post'):
+ if all_perms_needed:
+ raise PermissionDenied
+ n = files_queryset.count() + folders_queryset.count()
+ if n:
+ for f in files_queryset:
+ self.log_deletion(request, f, force_text(f))
+ f.delete()
+ folder_ids = set()
+ for folder in folders_queryset:
+ folder_ids.add(folder.id)
+ folder_ids.update(
+ folder.get_descendants().values_list('id', flat=True))
+ for f in File.objects.filter(folder__in=folder_ids):
+ self.log_deletion(request, f, force_text(f))
+ f.delete()
+
+
+## ... source file continues with no further router examples...
+
+```
+
+
+## Example 3 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / management / __init__.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/management/__init__.py)
+
+```python
+# __init__.py
+from django.contrib.auth import get_user_model
+from django.db.models import signals
+from django.utils.module_loading import import_string
+~~from django.db import router
+from guardian.conf import settings as guardian_settings
+
+
+def get_init_anonymous_user(User):
+ kwargs = {
+ User.USERNAME_FIELD: guardian_settings.ANONYMOUS_USER_NAME
+ }
+ user = User(**kwargs)
+ user.set_unusable_password()
+ return user
+
+
+def create_anonymous_user(sender, **kwargs):
+ User = get_user_model()
+~~ if not router.allow_migrate_model(kwargs['using'], User):
+ return
+ try:
+ lookup = {User.USERNAME_FIELD: guardian_settings.ANONYMOUS_USER_NAME}
+ User.objects.using(kwargs['using']).get(**lookup)
+ except User.DoesNotExist:
+ retrieve_anonymous_function = import_string(
+ guardian_settings.GET_INIT_ANONYMOUS_USER)
+ user = retrieve_anonymous_function(User)
+ user.save(using=kwargs['using'])
+
+if guardian_settings.ANONYMOUS_USER_NAME is not None:
+ from django.apps import apps
+ guardian_app = apps.get_app_config('guardian')
+ signals.post_migrate.connect(create_anonymous_user, sender=guardian_app,
+ dispatch_uid="guardian.management.create_anonymous_user")
+
+
+
+## ... source file continues with no further router examples...
+
+```
+
+
+## Example 4 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / models.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./models.py)
+
+```python
+# models.py
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db import models, transaction, router
+from django.db.models.signals import post_save, pre_save
+from django.utils.translation import gettext_lazy as _
+
+from model_utils.fields import (
+ AutoCreatedField,
+ AutoLastModifiedField,
+ StatusField,
+ MonitorField,
+ UUIDField,
+)
+from model_utils.managers import (
+ QueryManager,
+ SoftDeletableManager,
+)
+
+from django.db.models.functions import Now
+now = Now()
+
+
+class TimeStampedModel(models.Model):
+ created = AutoCreatedField(_('created'))
+ modified = AutoLastModifiedField(_('modified'))
+
+ def save(self, *args, **kwargs):
+
+
+## ... source file abbreviated to get to router examples ...
+
+
+
+
+class UUIDModel(models.Model):
+ id = UUIDField(
+ primary_key=True,
+ version=4,
+ editable=False,
+ )
+
+ class Meta:
+ abstract = True
+
+
+class SaveSignalHandlingModel(models.Model):
+ class Meta:
+ abstract = True
+
+ def save(self, signals_to_disable=None, *args, **kwargs):
+
+ self.signals_to_disable = signals_to_disable or []
+
+ super().save(*args, **kwargs)
+
+ def save_base(self, raw=False, force_insert=False,
+ force_update=False, using=None, update_fields=None):
+~~ using = using or router.db_for_write(self.__class__, instance=self)
+ assert not (force_insert and (force_update or update_fields))
+ assert update_fields is None or len(update_fields) > 0
+ cls = origin = self.__class__
+
+ if cls._meta.proxy:
+ cls = cls._meta.concrete_model
+ meta = cls._meta
+ if not meta.auto_created and 'pre_save' not in self.signals_to_disable:
+ pre_save.send(
+ sender=origin, instance=self, raw=raw, using=using,
+ update_fields=update_fields,
+ )
+ with transaction.atomic(using=using, savepoint=False):
+ if not raw:
+ self._save_parents(cls, using, update_fields)
+ updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
+
+ self._state.db = using
+ self._state.adding = False
+
+ if not meta.auto_created and 'post_save' not in self.signals_to_disable:
+ post_save.send(
+ sender=origin, instance=self, created=(not updated),
+ update_fields=update_fields, raw=raw, using=using,
+
+
+## ... source file continues with no further router examples...
+
+```
+
+
+## Example 5 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / models.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/./models.py)
+
+```python
+# models.py
+from django.contrib.contenttypes.fields import GenericForeignKey
+from django.contrib.contenttypes.models import ContentType
+~~from django.db import IntegrityError, models, router, transaction
+from django.utils.text import slugify
+from django.utils.translation import gettext, gettext_lazy as _
+
+try:
+ from unidecode import unidecode
+except ImportError:
+
+ def unidecode(tag):
+ return tag
+
+
+class TagBase(models.Model):
+ name = models.CharField(verbose_name=_("name"), unique=True, max_length=100)
+ slug = models.SlugField(verbose_name=_("slug"), unique=True, max_length=100)
+
+ def __str__(self):
+ return self.name
+
+ def __gt__(self, other):
+ return self.name.lower() > other.name.lower()
+
+ def __lt__(self, other):
+ return self.name.lower() < other.name.lower()
+
+ class Meta:
+ abstract = True
+
+ def save(self, *args, **kwargs):
+ if self._state.adding and not self.slug:
+ self.slug = self.slugify(self.name)
+~~ using = kwargs.get("using") or router.db_for_write(
+ type(self), instance=self
+ )
+ kwargs["using"] = using
+ try:
+ with transaction.atomic(using=using):
+ res = super().save(*args, **kwargs)
+ return res
+ except IntegrityError:
+ pass
+ slugs = set(
+ type(self)
+ ._default_manager.filter(slug__startswith=self.slug)
+ .values_list("slug", flat=True)
+ )
+ i = 1
+ while True:
+ slug = self.slugify(self.name, i)
+ if slug not in slugs:
+ self.slug = slug
+ return super().save(*args, **kwargs)
+ i += 1
+ else:
+ return super().save(*args, **kwargs)
+
+
+
+## ... source file continues with no further router examples...
+
+```
+
diff --git a/content/pages/examples/django/django-db-transaction.markdown b/content/pages/examples/django/django-db-transaction.markdown
new file mode 100644
index 000000000..163c44043
--- /dev/null
+++ b/content/pages/examples/django/django-db-transaction.markdown
@@ -0,0 +1,707 @@
+title: django.db transaction Example Code
+category: page
+slug: django-db-transaction-examples
+sortorder: 500011169
+toc: False
+sidebartitle: django.db transaction
+meta: Python example code for the transaction callable from the django.db module of the Django project.
+
+
+transaction is a callable within the django.db module of the Django project.
+
+
+## Example 1 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / account / models.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/account/models.py)
+
+```python
+# models.py
+from __future__ import unicode_literals
+
+import datetime
+
+from django.core import signing
+~~from django.db import models, transaction
+from django.utils import timezone
+from django.utils.crypto import get_random_string
+from django.utils.translation import gettext_lazy as _
+
+from .. import app_settings as allauth_app_settings
+from . import app_settings, signals
+from .adapter import get_adapter
+from .managers import EmailAddressManager, EmailConfirmationManager
+from .utils import user_email
+
+
+class EmailAddress(models.Model):
+
+ user = models.ForeignKey(allauth_app_settings.USER_MODEL,
+ verbose_name=_('user'),
+ on_delete=models.CASCADE)
+ email = models.EmailField(unique=app_settings.UNIQUE_EMAIL,
+ max_length=app_settings.EMAIL_MAX_LENGTH,
+ verbose_name=_('e-mail address'))
+ verified = models.BooleanField(verbose_name=_('verified'), default=False)
+ primary = models.BooleanField(verbose_name=_('primary'), default=False)
+
+ objects = EmailAddressManager()
+
+
+
+## ... source file abbreviated to get to transaction examples ...
+
+
+ def __str__(self):
+ return self.email
+
+ def set_as_primary(self, conditional=False):
+ old_primary = EmailAddress.objects.get_primary(self.user)
+ if old_primary:
+ if conditional:
+ return False
+ old_primary.primary = False
+ old_primary.save()
+ self.primary = True
+ self.save()
+ user_email(self.user, self.email)
+ self.user.save()
+ return True
+
+ def send_confirmation(self, request=None, signup=False):
+ if app_settings.EMAIL_CONFIRMATION_HMAC:
+ confirmation = EmailConfirmationHMAC(self)
+ else:
+ confirmation = EmailConfirmation.create(self)
+ confirmation.send(request, signup=signup)
+ return confirmation
+
+ def change(self, request, new_email, confirm=True):
+~~ with transaction.atomic():
+ user_email(self.user, new_email)
+ self.user.save()
+ self.email = new_email
+ self.verified = False
+ self.save()
+ if confirm:
+ self.send_confirmation(request)
+
+
+class EmailConfirmation(models.Model):
+
+ email_address = models.ForeignKey(EmailAddress,
+ verbose_name=_('e-mail address'),
+ on_delete=models.CASCADE)
+ created = models.DateTimeField(verbose_name=_('created'),
+ default=timezone.now)
+ sent = models.DateTimeField(verbose_name=_('sent'), null=True)
+ key = models.CharField(verbose_name=_('key'), max_length=64, unique=True)
+
+ objects = EmailConfirmationManager()
+
+ class Meta:
+ verbose_name = _("email confirmation")
+ verbose_name_plural = _("email confirmations")
+
+
+## ... source file continues with no further transaction examples...
+
+```
+
+
+## Example 2 from django-import-export
+[django-import-export](https://github.com/django-import-export/django-import-export)
+([documentation](https://django-import-export.readthedocs.io/en/latest/)
+and [PyPI page](https://pypi.org/project/django-import-export/))
+is a [Django](/django.html) code library for importing and exporting data
+from the Django Admin. The tool supports many export and import formats
+such as CSV, JSON and YAML. django-import-export is open source under the
+[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
+
+[**django-import-export / import_export / utils.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./utils.py)
+
+```python
+# utils.py
+~~from django.db import transaction
+
+
+class atomic_if_using_transaction:
+ def __init__(self, using_transactions):
+ self.using_transactions = using_transactions
+ if using_transactions:
+~~ self.context_manager = transaction.atomic()
+
+ def __enter__(self):
+ if self.using_transactions:
+ self.context_manager.__enter__()
+
+ def __exit__(self, *args):
+ if self.using_transactions:
+ self.context_manager.__exit__(*args)
+
+
+
+## ... source file continues with no further transaction examples...
+
+```
+
+
+## Example 3 from django-model-utils
+[django-model-utils](https://github.com/jazzband/django-model-utils)
+([project documentation](https://django-model-utils.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-model-utils/))
+provides useful mixins and utilities for working with
+[Django ORM](/django-orm.html) models in your projects.
+
+The django-model-utils project is open sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/jazzband/django-model-utils/blob/master/LICENSE.txt).
+
+[**django-model-utils / model_utils / models.py**](https://github.com/jazzband/django-model-utils/blob/master/model_utils/./models.py)
+
+```python
+# models.py
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db import models, transaction, router
+from django.db.models.signals import post_save, pre_save
+from django.utils.translation import gettext_lazy as _
+
+from model_utils.fields import (
+ AutoCreatedField,
+ AutoLastModifiedField,
+ StatusField,
+ MonitorField,
+ UUIDField,
+)
+from model_utils.managers import (
+ QueryManager,
+ SoftDeletableManager,
+)
+
+from django.db.models.functions import Now
+now = Now()
+
+
+class TimeStampedModel(models.Model):
+ created = AutoCreatedField(_('created'))
+ modified = AutoLastModifiedField(_('modified'))
+
+ def save(self, *args, **kwargs):
+
+
+## ... source file abbreviated to get to transaction examples ...
+
+
+class SaveSignalHandlingModel(models.Model):
+ class Meta:
+ abstract = True
+
+ def save(self, signals_to_disable=None, *args, **kwargs):
+
+ self.signals_to_disable = signals_to_disable or []
+
+ super().save(*args, **kwargs)
+
+ def save_base(self, raw=False, force_insert=False,
+ force_update=False, using=None, update_fields=None):
+ using = using or router.db_for_write(self.__class__, instance=self)
+ assert not (force_insert and (force_update or update_fields))
+ assert update_fields is None or len(update_fields) > 0
+ cls = origin = self.__class__
+
+ if cls._meta.proxy:
+ cls = cls._meta.concrete_model
+ meta = cls._meta
+ if not meta.auto_created and 'pre_save' not in self.signals_to_disable:
+ pre_save.send(
+ sender=origin, instance=self, raw=raw, using=using,
+ update_fields=update_fields,
+ )
+~~ with transaction.atomic(using=using, savepoint=False):
+ if not raw:
+ self._save_parents(cls, using, update_fields)
+ updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
+
+ self._state.db = using
+ self._state.adding = False
+
+ if not meta.auto_created and 'post_save' not in self.signals_to_disable:
+ post_save.send(
+ sender=origin, instance=self, created=(not updated),
+ update_fields=update_fields, raw=raw, using=using,
+ )
+
+ self.signals_to_disable = []
+
+
+
+## ... source file continues with no further transaction examples...
+
+```
+
+
+## Example 4 from django-oauth-toolkit
+[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit)
+([project website](http://dot.evonove.it/) and
+[PyPI package information](https://pypi.org/project/django-oauth-toolkit/1.2.0/))
+is a code library for adding and handling [OAuth2](https://oauth.net/)
+flows within your [Django](/django.html) web application and
+[API](/application-programming-interfaces.html).
+
+The django-oauth-toolkit project is open sourced under the
+[FreeBSD license](https://github.com/jazzband/django-oauth-toolkit/blob/master/LICENSE)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-oauth-toolkit / oauth2_provider / models.py**](https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/./models.py)
+
+```python
+# models.py
+import logging
+from datetime import timedelta
+from urllib.parse import parse_qsl, urlparse
+
+from django.apps import apps
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+~~from django.db import models, transaction
+from django.urls import reverse
+from django.utils import timezone
+from django.utils.translation import gettext_lazy as _
+
+from .generators import generate_client_id, generate_client_secret
+from .scopes import get_scopes_backend
+from .settings import oauth2_settings
+from .validators import RedirectURIValidator, WildcardSet
+
+
+logger = logging.getLogger(__name__)
+
+
+class AbstractApplication(models.Model):
+ CLIENT_CONFIDENTIAL = "confidential"
+ CLIENT_PUBLIC = "public"
+ CLIENT_TYPES = (
+ (CLIENT_CONFIDENTIAL, _("Confidential")),
+ (CLIENT_PUBLIC, _("Public")),
+ )
+
+ GRANT_AUTHORIZATION_CODE = "authorization-code"
+ GRANT_IMPLICIT = "implicit"
+ GRANT_PASSWORD = "password"
+
+
+## ... source file abbreviated to get to transaction examples ...
+
+
+ class Meta(AbstractAccessToken.Meta):
+ swappable = "OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL"
+
+
+class AbstractRefreshToken(models.Model):
+ id = models.BigAutoField(primary_key=True)
+ user = models.ForeignKey(
+ settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
+ related_name="%(app_label)s_%(class)s"
+ )
+ token = models.CharField(max_length=255)
+ application = models.ForeignKey(
+ oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE)
+ access_token = models.OneToOneField(
+ oauth2_settings.ACCESS_TOKEN_MODEL, on_delete=models.SET_NULL, blank=True, null=True,
+ related_name="refresh_token"
+ )
+
+ created = models.DateTimeField(auto_now_add=True)
+ updated = models.DateTimeField(auto_now=True)
+ revoked = models.DateTimeField(null=True)
+
+ def revoke(self):
+ access_token_model = get_access_token_model()
+ refresh_token_model = get_refresh_token_model()
+~~ with transaction.atomic():
+ self = refresh_token_model.objects.filter(
+ pk=self.pk, revoked__isnull=True
+ ).select_for_update().first()
+ if not self:
+ return
+
+ try:
+ access_token_model.objects.get(id=self.access_token_id).revoke()
+ except access_token_model.DoesNotExist:
+ pass
+ self.access_token = None
+ self.revoked = timezone.now()
+ self.save()
+
+ def __str__(self):
+ return self.token
+
+ class Meta:
+ abstract = True
+ unique_together = ("token", "revoked",)
+
+
+class RefreshToken(AbstractRefreshToken):
+ class Meta(AbstractRefreshToken.Meta):
+
+
+## ... source file abbreviated to get to transaction examples ...
+
+
+
+def get_access_token_model():
+ return apps.get_model(oauth2_settings.ACCESS_TOKEN_MODEL)
+
+
+def get_refresh_token_model():
+ return apps.get_model(oauth2_settings.REFRESH_TOKEN_MODEL)
+
+
+def clear_expired():
+ now = timezone.now()
+ refresh_expire_at = None
+ access_token_model = get_access_token_model()
+ refresh_token_model = get_refresh_token_model()
+ grant_model = get_grant_model()
+ REFRESH_TOKEN_EXPIRE_SECONDS = oauth2_settings.REFRESH_TOKEN_EXPIRE_SECONDS
+ if REFRESH_TOKEN_EXPIRE_SECONDS:
+ if not isinstance(REFRESH_TOKEN_EXPIRE_SECONDS, timedelta):
+ try:
+ REFRESH_TOKEN_EXPIRE_SECONDS = timedelta(seconds=REFRESH_TOKEN_EXPIRE_SECONDS)
+ except TypeError:
+ e = "REFRESH_TOKEN_EXPIRE_SECONDS must be either a timedelta or seconds"
+ raise ImproperlyConfigured(e)
+ refresh_expire_at = now - REFRESH_TOKEN_EXPIRE_SECONDS
+
+~~ with transaction.atomic():
+ if refresh_expire_at:
+ revoked = refresh_token_model.objects.filter(
+ revoked__lt=refresh_expire_at,
+ )
+ expired = refresh_token_model.objects.filter(
+ access_token__expires__lt=refresh_expire_at,
+ )
+
+ logger.info("%s Revoked refresh tokens to be deleted", revoked.count())
+ logger.info("%s Expired refresh tokens to be deleted", expired.count())
+
+ revoked.delete()
+ expired.delete()
+ else:
+ logger.info("refresh_expire_at is %s. No refresh tokens deleted.",
+ refresh_expire_at)
+
+ access_tokens = access_token_model.objects.filter(
+ refresh_token__isnull=True,
+ expires__lt=now
+ )
+ grants = grant_model.objects.filter(expires__lt=now)
+
+ logger.info("%s Expired access tokens to be deleted", access_tokens.count())
+
+
+## ... source file continues with no further transaction examples...
+
+```
+
+
+## Example 5 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / views.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./views.py)
+
+```python
+# views.py
+from django.conf import settings
+from django.core.exceptions import PermissionDenied
+~~from django.db import connection, models, transaction
+from django.http import Http404
+from django.http.response import HttpResponseBase
+from django.utils.cache import cc_delim_re, patch_vary_headers
+from django.utils.encoding import smart_str
+from django.views.decorators.csrf import csrf_exempt
+from django.views.generic import View
+
+from rest_framework import exceptions, status
+from rest_framework.request import Request
+from rest_framework.response import Response
+from rest_framework.schemas import DefaultSchema
+from rest_framework.settings import api_settings
+from rest_framework.utils import formatting
+
+
+def get_view_name(view):
+ name = getattr(view, 'name', None)
+ if name is not None:
+ return name
+
+ name = view.__class__.__name__
+ name = formatting.remove_trailing_string(name, 'View')
+ name = formatting.remove_trailing_string(name, 'ViewSet')
+ name = formatting.camelcase_to_spaces(name)
+
+ suffix = getattr(view, 'suffix', None)
+ if suffix:
+ name += ' ' + suffix
+
+ return name
+
+
+def get_view_description(view, html=False):
+ description = getattr(view, 'description', None)
+ if description is None:
+ description = view.__class__.__doc__ or ''
+
+ description = formatting.dedent(smart_str(description))
+ if html:
+ return formatting.markup_description(description)
+ return description
+
+
+def set_rollback():
+ atomic_requests = connection.settings_dict.get('ATOMIC_REQUESTS', False)
+ if atomic_requests and connection.in_atomic_block:
+~~ transaction.set_rollback(True)
+
+
+def exception_handler(exc, context):
+ if isinstance(exc, Http404):
+ exc = exceptions.NotFound()
+ elif isinstance(exc, PermissionDenied):
+ exc = exceptions.PermissionDenied()
+
+ if isinstance(exc, exceptions.APIException):
+ headers = {}
+ if getattr(exc, 'auth_header', None):
+ headers['WWW-Authenticate'] = exc.auth_header
+ if getattr(exc, 'wait', None):
+ headers['Retry-After'] = '%d' % exc.wait
+
+ if isinstance(exc.detail, (list, dict)):
+ data = exc.detail
+ else:
+ data = {'detail': exc.detail}
+
+ set_rollback()
+ return Response(data, status=exc.status_code, headers=headers)
+
+ return None
+
+
+## ... source file continues with no further transaction examples...
+
+```
+
+
+## Example 6 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / models.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/./models.py)
+
+```python
+# models.py
+from django.contrib.contenttypes.fields import GenericForeignKey
+from django.contrib.contenttypes.models import ContentType
+~~from django.db import IntegrityError, models, router, transaction
+from django.utils.text import slugify
+from django.utils.translation import gettext, gettext_lazy as _
+
+try:
+ from unidecode import unidecode
+except ImportError:
+
+ def unidecode(tag):
+ return tag
+
+
+class TagBase(models.Model):
+ name = models.CharField(verbose_name=_("name"), unique=True, max_length=100)
+ slug = models.SlugField(verbose_name=_("slug"), unique=True, max_length=100)
+
+ def __str__(self):
+ return self.name
+
+ def __gt__(self, other):
+ return self.name.lower() > other.name.lower()
+
+ def __lt__(self, other):
+ return self.name.lower() < other.name.lower()
+
+ class Meta:
+ abstract = True
+
+ def save(self, *args, **kwargs):
+ if self._state.adding and not self.slug:
+ self.slug = self.slugify(self.name)
+ using = kwargs.get("using") or router.db_for_write(
+ type(self), instance=self
+ )
+ kwargs["using"] = using
+ try:
+~~ with transaction.atomic(using=using):
+ res = super().save(*args, **kwargs)
+ return res
+ except IntegrityError:
+ pass
+ slugs = set(
+ type(self)
+ ._default_manager.filter(slug__startswith=self.slug)
+ .values_list("slug", flat=True)
+ )
+ i = 1
+ while True:
+ slug = self.slugify(self.name, i)
+ if slug not in slugs:
+ self.slug = slug
+ return super().save(*args, **kwargs)
+ i += 1
+ else:
+ return super().save(*args, **kwargs)
+
+ def slugify(self, tag, i=None):
+ slug = slugify(unidecode(tag))
+ if i is not None:
+ slug += "_%d" % i
+ return slug
+
+
+## ... source file continues with no further transaction examples...
+
+```
+
+
+## Example 7 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / models.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/models.py)
+
+```python
+# models.py
+import json
+import logging
+from collections import defaultdict
+from io import StringIO
+from urllib.parse import urlparse
+
+from django.conf import settings
+from django.contrib.auth.models import Group, Permission
+from django.contrib.contenttypes.models import ContentType
+from django.core import checks
+from django.core.cache import cache
+from django.core.exceptions import ValidationError
+from django.core.handlers.base import BaseHandler
+from django.core.handlers.wsgi import WSGIRequest
+~~from django.db import models, transaction
+from django.db.models import Case, Q, Value, When
+from django.db.models.functions import Concat, Lower, Substr
+from django.http import Http404
+from django.http.request import split_domain_port
+from django.template.response import TemplateResponse
+from django.urls import NoReverseMatch, reverse
+from django.utils import timezone
+from django.utils.cache import patch_cache_control
+from django.utils.functional import cached_property
+from django.utils.text import capfirst, slugify
+from django.utils.translation import gettext_lazy as _
+from modelcluster.models import (
+ ClusterableModel, get_all_child_m2m_relations, get_all_child_relations)
+from treebeard.mp_tree import MP_Node
+
+from wagtail.core.query import PageQuerySet, TreeQuerySet
+from wagtail.core.signals import page_published, page_unpublished, post_page_move, pre_page_move
+from wagtail.core.sites import get_site_for_hostname
+from wagtail.core.url_routing import RouteResult
+from wagtail.core.utils import WAGTAIL_APPEND_SLASH, camelcase_to_underscore, resolve_model_string
+from wagtail.search import index
+
+
+logger = logging.getLogger('wagtail.core')
+
+
+## ... source file abbreviated to get to transaction examples ...
+
+
+ return self.revisions.exclude(approved_go_live_at__isnull=True).exists()
+
+ def has_unpublished_subtree(self):
+ return (not self.live) and (not self.get_descendants().filter(live=True).exists())
+
+ def move(self, target, pos=None):
+ parent_before = self.get_parent()
+ if pos in ('first-child', 'last-child', 'sorted-child'):
+ parent_after = target
+ else:
+ parent_after = target.get_parent()
+
+ old_self = Page.objects.get(id=self.id)
+ old_url_path = old_self.url_path
+ new_url_path = old_self.set_url_path(parent=parent_after)
+
+ pre_page_move.send(
+ sender=self.specific_class or self.__class__,
+ instance=self,
+ parent_page_before=parent_before,
+ parent_page_after=parent_after,
+ url_path_before=old_url_path,
+ url_path_after=new_url_path,
+ )
+
+~~ with transaction.atomic():
+ super().move(target, pos=pos)
+
+ new_self = Page.objects.get(id=self.id)
+ new_self.url_path = new_url_path
+ new_self.save()
+
+ if old_url_path != new_url_path:
+ new_self._update_descendant_url_paths(old_url_path, new_url_path)
+
+ post_page_move.send(
+ sender=self.specific_class or self.__class__,
+ instance=new_self,
+ parent_page_before=parent_before,
+ parent_page_after=parent_after,
+ url_path_before=old_url_path,
+ url_path_after=new_url_path,
+ )
+
+ logger.info("Page moved: \"%s\" id=%d path=%s", self.title, self.id, new_url_path)
+
+ def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True, keep_live=True, user=None, process_child_object=None, exclude_fields=None):
+ specific_self = self.specific
+ default_exclude_fields = ['id', 'path', 'depth', 'numchild', 'url_path', 'path', 'index_entries']
+ exclude_fields = default_exclude_fields + specific_self.exclude_fields_in_copy + (exclude_fields or [])
+
+
+## ... source file continues with no further transaction examples...
+
+```
+
diff --git a/content/pages/examples/django/django-extensions-plug-ins.markdown b/content/pages/examples/django/django-extensions-plug-ins.markdown
index eb7e5fbce..632cde7da 100644
--- a/content/pages/examples/django/django-extensions-plug-ins.markdown
+++ b/content/pages/examples/django/django-extensions-plug-ins.markdown
@@ -75,19 +75,31 @@ Code from django-angular is shown on:
* [django.utils.html format_html](/django-utils-html-format-html-examples.html)
* [django.urls.exceptions NoReverseMatch](/django-urls-exceptions-noreversematch-examples.html)
+### django-appmail
+[Django-Appmail](https://github.com/yunojuno/django-appmail)
+([PyPI package information](https://pypi.org/project/django-appmail/))
+is a [Django](/django.html) app for handling transactional email templates.
+While the project began development as a way to work with the Mandrill
+transactional [API](/application-programming-interfaces.html), it is
+not exclusive to that API. The project simply provides a way to store
+and render email content. The library does not send or receive emails.
+
+Django-Appmail is open sourced under the
+[MIT license](https://github.com/yunojuno/django-appmail/blob/master/LICENSE).
+
### django-axes
[django-axes](https://github.com/jazzband/django-axes/)
([project documentation](https://django-axes.readthedocs.io/en/latest/)
and
-[PyPI package information](https://pypi.org/project/django-axes/)
+[PyPI package information](https://pypi.org/project/django-axes/))
is a code library for [Django](/django.html) projects to track failed
login attempts against a web application. The goal of the project is
to make it easier for you to stop people and scripts from hacking your
Django-powered website.
The code for django-axes is
-[open source under the MIT liense](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
and maintained by the group of developers known as
[Jazzband](https://jazzband.co/).
@@ -321,6 +333,18 @@ The django-jsonfield project is open source under the
[MIT license](https://github.com/dmkoch/django-jsonfield/blob/master/LICENSE).
+### django-linear-migrations
+[django-linear-migrations](https://github.com/adamchainz/django-linear-migrations)
+([PyPI package information](https://pypi.org/project/django-linear-migrations/))
+is a [Django](/django.html) code library to mitigate conflicting database
+migrations, which can cause non-deterministic behavior in different
+environments. The
+[introductory blog post by the package author](https://adamj.eu/tech/2020/12/10/introducing-django-linear-migrations/)
+does a good job of explaining the problem and how this library prevents
+the issue. This library is open sourced under the
+[MIT license](https://github.com/adamchainz/django-linear-migrations/blob/master/LICENSE).
+
+
### django-loginas
[django-loginas](https://github.com/skorokithakis/django-loginas)
([PyPI package information](https://pypi.org/project/django-loginas/))
@@ -331,6 +355,15 @@ django-loginas is open source under the
[BSD 3-Clause "New" or "Revised" License](https://github.com/skorokithakis/django-loginas/blob/master/LICENSE).
+### django-markdown-view
+[django-markdown-view](https://github.com/rgs258/django-markdown-view)
+([PyPI package information](https://pypi.org/project/django-markdown-view/))
+is a Django extension for serving [Markdown](/markdown.html) files as
+[Django templates](/django-templates.html). The project is open
+sourced under the
+[BSD 3-Clause "New" or "Revised" license](https://github.com/rgs258/django-markdown-view/blob/master/LICENSE).
+
+
### django-migration-linter
[django-migration-linter](https://github.com/3YOURMIND/django-migration-linter)
([PyPI package information](https://pypi.org/project/django-migration-linter/))
@@ -461,6 +494,29 @@ The project is open sourced under the
[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+### Django Request Token
+[Django Request Token](https://github.com/yunojuno/django-request-token)
+([PyPI package information](https://pypi.org/project/django-request-token/0.13/))
+encapsulates the logic for issuing expiring and one-time tokens
+with a [Django](/django.html) web application to use with protected URLs.
+Note that [PostgreSQL](/postgresql.html) as your backend
+[database](/databases.html) is a dependency for using this project.
+
+The Django Request Token project is open sourced under the
+[MIT license](https://github.com/yunojuno/django-request-token/blob/master/LICENSE).
+
+
+### django-rq
+[django-rq](https://github.com/rq/django-rq)
+([PyPI package information](https://pypi.org/project/django-rq/))
+is an [RQ](/redis-queue-rq.html)-based [task queue](/task-queues.html)
+that integrates with [Django](/django.html) as an app. This project
+is useful when you need a lightweight task queue and do not want
+to go through configuring [Celery](/celery.html) in your project.
+django-rq is open sourced under the
+[MIT license](https://github.com/rq/django-rq/blob/master/LICENSE.txt).
+
+
### django-simple-task
[django-simple-task](https://github.com/ericls/django-simple-task)
([project documentation](https://django-simple-task.readthedocs.io/)
@@ -536,6 +592,35 @@ and maintained by the collaborative developer community group
[Jazzband](https://jazzband.co/).
+### django-user-visit
+[django-user-visit](https://github.com/yunojuno/django-user-visit)
+([PyPI package information](https://pypi.org/project/django-user-visit/))
+is a [Django](/django.html) app and
+[middleware](https://docs.djangoproject.com/en/stable/topics/http/middleware/)
+for tracking daily user visits to your web application. The goal
+is to record per user per day instead of for every request a user
+sends to the application. The project is provided as open source
+under the
+[MIT license](https://github.com/yunojuno/django-user-visit/blob/master/LICENSE).
+
+
+### django-version-checks
+[django-version-checks](https://github.com/adamchainz/django-version-checks)
+([PyPI package](https://pypi.org/project/django-version-checks/))
+is a code library to ensure external system dependencies match
+desired versions. For example, a specific version of
+[PostgreSQL](/postgresql.html) or [MySQL](/mysql.html) as your database
+backend. This is different from using `pip` and a `requirements.txt` file,
+because those are Python dependencies, rather than system-wide software.
+The
+[introductory blog post](https://adamj.eu/tech/2020/12/14/introducing-django-version-checks/)
+for the project has some good reasons why these external dependencies
+can cause problems if they vary from the expected versions.
+
+django-version-checks is provided as open source under the
+[MIT license](https://github.com/adamchainz/django-version-checks/blob/master/LICENSE).
+
+
### django-webshell
[django-webshell](https://github.com/onrik/django-webshell) is an extension
for executing arbitrary code in the
@@ -575,6 +660,16 @@ The code for django-wiki is provided as open source under the
[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+### elasticsearch-django
+[elasticsearch-django](https://github.com/yunojuno/elasticsearch-django)
+([PyPI package information](https://pypi.org/project/elasticsearch-django/))
+is a [Django](/django.html) app for managing
+[ElasticSearch](https://github.com/elastic/elasticsearch) indexes
+populated by [Django ORM](/django-orm.html) models. The project is
+available as open source under the
+[MIT license](https://github.com/yunojuno/elasticsearch-django/blob/master/LICENSE).
+
+
### pytest-django
[pytest-django](https://github.com/pytest-dev/pytest-django)
([project documentation](https://pytest-django.readthedocs.io/en/latest/)
diff --git a/content/pages/examples/django/django-forms-baseform.markdown b/content/pages/examples/django/django-forms-baseform.markdown
new file mode 100644
index 000000000..c7e06434c
--- /dev/null
+++ b/content/pages/examples/django/django-forms-baseform.markdown
@@ -0,0 +1,125 @@
+title: django.forms BaseForm Example Code
+category: page
+slug: django-forms-baseform-examples
+sortorder: 500011255
+toc: False
+sidebartitle: django.forms BaseForm
+meta: Python example code for the BaseForm class from the django.forms module of the Django project.
+
+
+BaseForm is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / templatetags / wiki_tags.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/templatetags/wiki_tags.py)
+
+```python
+# wiki_tags.py
+import re
+
+from django import template
+from django.apps import apps
+from django.conf import settings as django_settings
+from django.contrib.contenttypes.models import ContentType
+from django.db.models import Model
+~~from django.forms import BaseForm
+from django.template.defaultfilters import striptags
+from django.utils.http import urlquote
+from django.utils.safestring import mark_safe
+from wiki import models
+from wiki.conf import settings
+from wiki.core.plugins import registry as plugin_registry
+
+register = template.Library()
+
+
+_cache = {}
+
+
+@register.simple_tag(takes_context=True)
+def article_for_object(context, obj):
+ if not isinstance(obj, Model):
+ raise TypeError(
+ "A Wiki article can only be associated to a Django Model "
+ "instance, not %s" % type(obj)
+ )
+
+ content_type = ContentType.objects.get_for_model(obj)
+
+ if True or obj not in _cache:
+
+
+## ... source file abbreviated to get to BaseForm examples ...
+
+
+@register.inclusion_tag("wiki/includes/render.html", takes_context=True)
+def wiki_render(context, article, preview_content=None):
+
+ if preview_content:
+ content = article.render(preview_content=preview_content)
+ elif article.current_revision:
+ content = article.get_cached_content(user=context.get("user"))
+ else:
+ content = None
+
+ context.update(
+ {
+ "article": article,
+ "content": content,
+ "preview": preview_content is not None,
+ "plugins": plugin_registry.get_plugins(),
+ "STATIC_URL": django_settings.STATIC_URL,
+ "CACHE_TIMEOUT": settings.CACHE_TIMEOUT,
+ }
+ )
+ return context
+
+
+@register.inclusion_tag("wiki/includes/form.html", takes_context=True)
+def wiki_form(context, form_obj):
+~~ if not isinstance(form_obj, BaseForm):
+ raise TypeError(
+ "Error including form, it's not a form, it's a %s" % type(form_obj)
+ )
+ context.update({"form": form_obj})
+ return context
+
+
+@register.inclusion_tag("wiki/includes/messages.html", takes_context=True)
+def wiki_messages(context):
+
+ messages = context.get("messages", [])
+ for message in messages:
+ message.css_class = settings.MESSAGE_TAG_CSS_CLASS[message.level]
+ context.update({"messages": messages})
+ return context
+
+
+@register.filter
+def get_content_snippet(content, keyword, max_words=30):
+
+ def clean_text(content):
+
+ content = striptags(content)
+ words = content.split()
+
+
+## ... source file continues with no further BaseForm examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-checkboxinput.markdown b/content/pages/examples/django/django-forms-checkboxinput.markdown
new file mode 100644
index 000000000..9b80d9c35
--- /dev/null
+++ b/content/pages/examples/django/django-forms-checkboxinput.markdown
@@ -0,0 +1,115 @@
+title: django.forms CheckboxInput Example Code
+category: page
+slug: django-forms-checkboxinput-examples
+sortorder: 500011258
+toc: False
+sidebartitle: django.forms CheckboxInput
+meta: Python example code for the CheckboxInput class from the django.forms module of the Django project.
+
+
+CheckboxInput is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / templatetags / jet_tags.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/templatetags/jet_tags.py)
+
+```python
+# jet_tags.py
+from __future__ import unicode_literals
+import json
+import os
+from django import template
+try:
+ from django.core.urlresolvers import reverse
+except ImportError: # Django 1.11
+ from django.urls import reverse
+
+~~from django.forms import CheckboxInput, ModelChoiceField, Select, ModelMultipleChoiceField, SelectMultiple
+from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
+from django.utils.formats import get_format
+from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_text
+from jet import settings, VERSION
+from jet.models import Bookmark
+from jet.utils import get_model_instance_label, get_model_queryset, get_possible_language_codes, \
+ get_admin_site, get_menu_items
+
+try:
+ from urllib.parse import parse_qsl
+except ImportError:
+ from urlparse import parse_qsl
+
+
+register = template.Library()
+assignment_tag = register.assignment_tag if hasattr(register, 'assignment_tag') else register.simple_tag
+
+
+@assignment_tag
+def jet_get_date_format():
+ return get_format('DATE_INPUT_FORMATS')[0]
+
+
+@assignment_tag
+def jet_get_time_format():
+ return get_format('TIME_INPUT_FORMATS')[0]
+
+
+@assignment_tag
+def jet_get_datetime_format():
+ return get_format('DATETIME_INPUT_FORMATS')[0]
+
+
+@assignment_tag(takes_context=True)
+def jet_get_menu(context):
+ return get_menu_items(context)
+
+
+@assignment_tag
+def jet_get_bookmarks(user):
+ if user is None:
+ return None
+ return Bookmark.objects.filter(user=user.pk)
+
+
+@register.filter
+def jet_is_checkbox(field):
+~~ return field.field.widget.__class__.__name__ == CheckboxInput().__class__.__name__
+
+
+@register.filter
+def jet_select2_lookups(field):
+ if hasattr(field, 'field') and \
+ (isinstance(field.field, ModelChoiceField) or isinstance(field.field, ModelMultipleChoiceField)):
+ qs = field.field.queryset
+ model = qs.model
+
+ if getattr(model, 'autocomplete_search_fields', None) and getattr(field.field, 'autocomplete', True):
+ choices = []
+ app_label = model._meta.app_label
+ model_name = model._meta.object_name
+
+ attrs = {
+ 'class': 'ajax',
+ 'data-app-label': app_label,
+ 'data-model': model_name,
+ 'data-ajax--url': reverse('jet:model_lookup')
+ }
+
+ initial_value = field.value()
+
+ if hasattr(field, 'field') and isinstance(field.field, ModelMultipleChoiceField):
+
+
+## ... source file continues with no further CheckboxInput examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-checkboxselectmultiple.markdown b/content/pages/examples/django/django-forms-checkboxselectmultiple.markdown
new file mode 100644
index 000000000..e424a6977
--- /dev/null
+++ b/content/pages/examples/django/django-forms-checkboxselectmultiple.markdown
@@ -0,0 +1,61 @@
+title: django.forms CheckboxSelectMultiple Example Code
+category: page
+slug: django-forms-checkboxselectmultiple-examples
+sortorder: 500011259
+toc: False
+sidebartitle: django.forms CheckboxSelectMultiple
+meta: Python example code for the CheckboxSelectMultiple class from the django.forms module of the Django project.
+
+
+CheckboxSelectMultiple is a class within the django.forms module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / gears / widgets.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/gears/widgets.py)
+
+```python
+# widgets.py
+~~from django.forms import FileInput, CheckboxSelectMultiple, Select
+
+
+class CustomFileInput(FileInput):
+ template_name = 'gears/widgets/file_input.html'
+ accept = ''
+ show_file_name = True
+
+
+~~class CustomCheckboxSelectMultiple(CheckboxSelectMultiple):
+ template_name = 'gears/widgets/checkbox_multiple_select.html'
+ hide_label = False
+ hide_apply_btn = False
+
+ class Media:
+ js = ('gears/js/checkbox_multiple_select.js',)
+
+ def __init__(self, *args, **kwargs):
+ self.hide_label = kwargs.pop('hide_label', False)
+ self.hide_apply_btn = kwargs.pop('hide_apply_btn', False)
+ super().__init__(*args, **kwargs)
+
+ def get_context(self, name, value, attrs):
+ context = super().get_context(name, value, attrs)
+ context['widget'].update({
+ 'hide_label': self.hide_label,
+ 'hide_apply_btn': self.hide_apply_btn,
+ })
+ return context
+
+
+class DropdownSelectSubmit(Select):
+ template_name = 'gears/widgets/dropdown_select_submit.html'
+ empty_label = 'Not selected'
+
+
+## ... source file continues with no further CheckboxSelectMultiple examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-datefield.markdown b/content/pages/examples/django/django-forms-datefield.markdown
index 0268e90d0..9d97fbfa3 100644
--- a/content/pages/examples/django/django-forms-datefield.markdown
+++ b/content/pages/examples/django/django-forms-datefield.markdown
@@ -311,7 +311,7 @@ except ImportError:
## Example 4 from register
[register](https://github.com/ORGAN-IZE/register) is a [Django](/django.html),
-[Bootstrap](/bootstrap.html), [PostgreSQL](/postgresql.html) project that is
+[Bootstrap](/bootstrap-css.html), [PostgreSQL](/postgresql.html) project that is
open source under the
[GNU General Public License v3.0](https://github.com/ORGAN-IZE/register/blob/master/LICENSE).
This web application makes it easier for people to register as organ donors.
diff --git a/content/pages/examples/django/django-forms-dateinput.markdown b/content/pages/examples/django/django-forms-dateinput.markdown
new file mode 100644
index 000000000..bedd29ab9
--- /dev/null
+++ b/content/pages/examples/django/django-forms-dateinput.markdown
@@ -0,0 +1,120 @@
+title: django.forms DateInput Example Code
+category: page
+slug: django-forms-dateinput-examples
+sortorder: 500011262
+toc: False
+sidebartitle: django.forms DateInput
+meta: Python example code for the DateInput class from the django.forms module of the Django project.
+
+
+DateInput is a class within the django.forms module of the Django project.
+
+
+## Example 1 from register
+[register](https://github.com/ORGAN-IZE/register) is a [Django](/django.html),
+[Bootstrap](/bootstrap-css.html), [PostgreSQL](/postgresql.html) project that is
+open source under the
+[GNU General Public License v3.0](https://github.com/ORGAN-IZE/register/blob/master/LICENSE).
+This web application makes it easier for people to register as organ donors.
+You can see the application live at
+[https://register.organize.org/](https://register.organize.org/).
+
+[**register / registration / forms.py**](https://github.com/ORGAN-IZE/register/blob/master/registration/./forms.py)
+
+```python
+# forms.py
+from __future__ import unicode_literals
+
+import logging
+import re
+import collections
+import datetime
+
+~~import django.forms
+~~import django.forms.utils
+~~import django.forms.widgets
+import django.core.validators
+import django.core.exceptions
+from django.conf import settings
+from django.utils.translation import ugettext_lazy as _
+from django.utils.safestring import mark_safe
+
+import form_utils.forms
+import requests
+import dateutil.parser
+import validate_email
+
+logger = logging.getLogger(__name__)
+
+
+REGISTRATION_CONFIGURATION_NAME = 'registration_configuration'
+
+RE_NON_DECIMAL = re.compile(r'[^\d]+')
+RE_NON_ALPHA = re.compile('[\W]+')
+RE_POSTAL_CODE = re.compile(r'^[0-9]{5}$')
+validate_postal_code = django.core.validators.RegexValidator(
+ RE_POSTAL_CODE, _("Enter a valid postal code consisting 5 numbers."), 'invalid')
+
+
+CHOICES_GENDER = (
+
+
+## ... source file abbreviated to get to DateInput examples ...
+
+
+ 'clean_ssn': register_form_clean_ssn,
+ 'clean_license_id': register_form_clean_license_id,
+ 'api_errors': {},
+ 'skip_api_error_validation': False,
+ 'validate_organ_tissue_selection': conf.get('validate_organ_tissue_selection', None),
+ })
+ return cls
+
+
+class RevokeForm(django.forms.Form):
+ email = django.forms.EmailField(label=_('Email'))
+ first_name = django.forms.CharField(
+ label=_('First Name'), max_length=150, min_length=1)
+ middle_name = django.forms.CharField(
+ label=_('Middle Name'), max_length=150, min_length=0, required=False)
+ last_name = django.forms.CharField(
+ label=_('Last Name'), max_length=150, min_length=1)
+ postal_code = django.forms.CharField(
+ label=_('Postal Code'),
+ max_length=5, min_length=5, validators=[validate_postal_code])
+ gender = django.forms.ChoiceField(
+ label=_('Gender'), choices=CHOICES_GENDER,
+ widget=django.forms.RadioSelect)
+ birthdate = django.forms.DateField(
+ label=_('Birthdate'),
+~~ widget=django.forms.DateInput(
+ attrs={'placeholder': '__/__/____', 'class': 'date',}))
+ agree_to_tos = django.forms.BooleanField(label='', widget=django.forms.widgets.CheckboxInput(attrs={'required': 'required', }))
+
+ def clean_email(self):
+ email = self.cleaned_data['email']
+ if settings.DISABLE_EMAIL_VALIDATION:
+ logger.warning(
+ 'Email validation disabled: DISABLE_EMAIL_VALIDATION is set')
+ return email
+ if not hasattr(settings, 'MAILGUN_PUBLIC_API_KEY'):
+ logger.warning(
+ 'Cannot validate email: MAILGUN_PUBLIC_API_KEY not set')
+ return email
+ r = requests.get(
+ 'https://api.mailgun.net/v2/address/validate',
+ data={'address': email, },
+ auth=('api', settings.MAILGUN_PUBLIC_API_KEY))
+ if r.status_code == 200:
+ if r.json()['is_valid']:
+ return email
+ logger.warning('Cannot validate email: {}'.format(r.text))
+ raise django.forms.ValidationError(_('Enter a valid email.'))
+
+
+
+
+## ... source file continues with no further DateInput examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-datetimefield.markdown b/content/pages/examples/django/django-forms-datetimefield.markdown
new file mode 100644
index 000000000..bf9a7f20a
--- /dev/null
+++ b/content/pages/examples/django/django-forms-datetimefield.markdown
@@ -0,0 +1,498 @@
+title: django.forms DateTimeField Code Examples
+category: page
+slug: django-forms-datetimefield-examples
+sortorder: 500013119
+toc: False
+sidebartitle: django.forms DateTimeField
+meta: Python code examples that show how to use DateTimeField from the forms module of the Django project.
+
+
+The [DateTimeField](https://github.com/django/django/blob/master/django/forms/fields.py)
+([documentation](https://docs.djangoproject.com/en/stable/ref/forms/fields/#datetimefield))
+class in the `django.forms` module in the [Django](/django.html)
+[web framework](/web-frameworks.html) provides a mechanism for safely handling
+dates and times as input from HTTP POST requests. The requests are
+usually generated by an [HTML](/hypertext-markup-language-html.html) form
+created from a Django [web application](/web-development.html).
+
+
+## Example 1 from django-filter
+[django-filter](https://github.com/carltongibson/django-filter)
+([project documentation](https://django-filter.readthedocs.io/en/master/)
+and
+[PyPI page](https://pypi.org/project/django-filter/2.2.0/))
+makes it easier to filter down querysets from the
+[Django ORM](/django-orm.html) by providing common bits of boilerplate
+code. django-filter is provided as
+[open source](https://github.com/carltongibson/django-filter/blob/master/LICENSE).
+
+[**django-filter / django_filters / fields.py**](https://github.com/carltongibson/django-filter/blob/master/django_filters/./fields.py)
+
+```python
+from collections import namedtuple
+from datetime import datetime, time
+
+~~from django import forms
+from django.utils.dateparse import parse_datetime
+from django.utils.encoding import force_str
+from django.utils.translation import gettext_lazy as _
+
+from .conf import settings
+from .constants import EMPTY_VALUES
+from .utils import handle_timezone
+from .widgets import (
+ BaseCSVWidget,
+ CSVWidget,
+ DateRangeWidget,
+ LookupChoiceWidget,
+ RangeWidget
+)
+
+
+class RangeField(forms.MultiValueField):
+ widget = RangeWidget
+
+ def __init__(self, fields=None, *args, **kwargs):
+ if fields is None:
+ fields = (
+ forms.DecimalField(),
+ forms.DecimalField())
+ super().__init__(fields, *args, **kwargs)
+
+ def compress(self, data_list):
+ if data_list:
+ return slice(*data_list)
+ return None
+
+
+class DateRangeField(RangeField):
+ widget = DateRangeWidget
+
+ def __init__(self, *args, **kwargs):
+ fields = (
+ forms.DateField(),
+ forms.DateField())
+ super().__init__(fields, *args, **kwargs)
+
+ def compress(self, data_list):
+ if data_list:
+ start_date, stop_date = data_list
+ if start_date:
+ start_date = handle_timezone(
+ datetime.combine(start_date, time.min),
+ False
+ )
+ if stop_date:
+ stop_date = handle_timezone(
+ datetime.combine(stop_date, time.max),
+ False
+ )
+ return slice(start_date, stop_date)
+ return None
+
+
+class DateTimeRangeField(RangeField):
+ widget = DateRangeWidget
+
+ def __init__(self, *args, **kwargs):
+~~ fields = (
+~~ forms.DateTimeField(),
+~~ forms.DateTimeField())
+~~ super().__init__(fields, *args, **kwargs)
+
+
+class IsoDateTimeRangeField(RangeField):
+ widget = DateRangeWidget
+
+ def __init__(self, *args, **kwargs):
+ fields = (
+ IsoDateTimeField(),
+ IsoDateTimeField())
+ super().__init__(fields, *args, **kwargs)
+
+
+class TimeRangeField(RangeField):
+ widget = DateRangeWidget
+
+ def __init__(self, *args, **kwargs):
+ fields = (
+ forms.TimeField(),
+ forms.TimeField())
+ super().__init__(fields, *args, **kwargs)
+
+
+class Lookup(namedtuple('Lookup', ('value', 'lookup_expr'))):
+ def __new__(cls, value, lookup_expr):
+ if value in EMPTY_VALUES or lookup_expr in EMPTY_VALUES:
+ raise ValueError(
+ "Empty values ([], (), {}, '', None) are not "
+ "valid Lookup arguments. Return None instead."
+ )
+
+ return super().__new__(cls, value, lookup_expr)
+
+
+class LookupChoiceField(forms.MultiValueField):
+ default_error_messages = {
+ 'lookup_required': _('Select a lookup.'),
+ }
+
+ def __init__(self, field, lookup_choices, *args, **kwargs):
+ empty_label = kwargs.pop('empty_label', settings.EMPTY_CHOICE_LABEL)
+ fields = (field, ChoiceField(choices=lookup_choices, empty_label=empty_label))
+ widget = LookupChoiceWidget(widgets=[f.widget for f in fields])
+ kwargs['widget'] = widget
+ kwargs['help_text'] = field.help_text
+ super().__init__(fields, *args, **kwargs)
+
+ def compress(self, data_list):
+ if len(data_list) == 2:
+ value, lookup_expr = data_list
+ if value not in EMPTY_VALUES:
+ if lookup_expr not in EMPTY_VALUES:
+ return Lookup(value=value, lookup_expr=lookup_expr)
+ else:
+ raise forms.ValidationError(
+ self.error_messages['lookup_required'],
+ code='lookup_required')
+ return None
+
+
+~~class IsoDateTimeField(forms.DateTimeField):
+~~ """
+~~ Supports 'iso-8601' date format too which is out the scope of
+~~ the ``datetime.strptime`` standard library
+
+~~ # ISO 8601: ``http://www.w3.org/TR/NOTE-datetime``
+
+~~ Based on Gist example by David Medina https://gist.github.com/copitux/5773821
+~~ """
+~~ ISO_8601 = 'iso-8601'
+~~ input_formats = [ISO_8601]
+
+~~ def strptime(self, value, format):
+~~ value = force_str(value)
+
+~~ if format == self.ISO_8601:
+~~ parsed = parse_datetime(value)
+~~ if parsed is None: # Continue with other formats if doesn't match
+~~ raise ValueError
+~~ return handle_timezone(parsed)
+~~ return super().strptime(value, format)
+
+
+## ... source file continues with no further DateTimeField examples ...
+
+```
+
+
+## Example 2 from django-floppyforms
+[django-floppyforms](https://github.com/jazzband/django-floppyforms)
+([project documentation](https://django-floppyforms.readthedocs.io/en/latest/)
+and
+[PyPI page](https://pypi.org/project/django-floppyforms/))
+is a [Django](/django.html) code library for better control
+over rendering HTML forms in your [templates](/template-engines.html).
+
+The django-floppyforms code is provided as
+[open source](https://github.com/jazzband/django-floppyforms/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-floppyforms / floppyforms / fields.py**](https://github.com/jazzband/django-floppyforms/blob/master/floppyforms/./fields.py)
+
+```python
+import django
+~~from django import forms
+import decimal
+
+from .widgets import (TextInput, HiddenInput, CheckboxInput, Select,
+ ClearableFileInput, SelectMultiple, DateInput,
+ DateTimeInput, TimeInput, URLInput, NumberInput,
+ EmailInput, NullBooleanSelect, SlugInput, IPAddressInput,
+ SplitDateTimeWidget, SplitHiddenDateTimeWidget,
+ MultipleHiddenInput)
+
+__all__ = (
+ 'Field', 'CharField', 'IntegerField', 'DateField', 'TimeField',
+ 'DateTimeField', 'EmailField', 'FileField', 'ImageField', 'URLField',
+ 'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
+ 'FloatField', 'DecimalField', 'SlugField', 'RegexField',
+ 'GenericIPAddressField', 'TypedChoiceField', 'FilePathField',
+ 'TypedMultipleChoiceField', 'ComboField', 'MultiValueField',
+ 'SplitDateTimeField',
+)
+if django.VERSION < (1, 9):
+ __all__ += ('IPAddressField',)
+
+
+class Field(forms.Field):
+ widget = TextInput
+ hidden_widget = HiddenInput
+
+
+class CharField(Field, forms.CharField):
+ widget = TextInput
+
+ def widget_attrs(self, widget):
+ attrs = super(CharField, self).widget_attrs(widget)
+ if attrs is None:
+ attrs = {}
+ if self.max_length is not None and isinstance(widget, (TextInput, HiddenInput)):
+ # The HTML attribute is maxlength, not max_length.
+ attrs.update({'maxlength': str(self.max_length)})
+ return attrs
+
+
+class BooleanField(Field, forms.BooleanField):
+ widget = CheckboxInput
+
+
+class NullBooleanField(Field, forms.NullBooleanField):
+ widget = NullBooleanSelect
+
+
+class ChoiceField(Field, forms.ChoiceField):
+ widget = Select
+
+
+class TypedChoiceField(ChoiceField, forms.TypedChoiceField):
+ widget = Select
+
+
+class FilePathField(ChoiceField, forms.FilePathField):
+ widget = Select
+
+
+class FileField(Field, forms.FileField):
+ widget = ClearableFileInput
+
+
+class ImageField(Field, forms.ImageField):
+ widget = ClearableFileInput
+
+
+class MultipleChoiceField(Field, forms.MultipleChoiceField):
+ widget = SelectMultiple
+ hidden_widget = MultipleHiddenInput
+
+
+class TypedMultipleChoiceField(MultipleChoiceField,
+ forms.TypedMultipleChoiceField):
+ pass
+
+
+class DateField(Field, forms.DateField):
+ widget = DateInput
+
+
+~~class DateTimeField(Field, forms.DateTimeField):
+~~ widget = DateTimeInput
+
+
+class TimeField(Field, forms.TimeField):
+ widget = TimeInput
+
+
+class FloatField(Field, forms.FloatField):
+ widget = NumberInput
+
+ def widget_attrs(self, widget):
+ attrs = super(FloatField, self).widget_attrs(widget) or {}
+ if self.min_value is not None:
+ attrs['min'] = self.min_value
+ if self.max_value is not None:
+ attrs['max'] = self.max_value
+ if 'step' not in widget.attrs:
+ attrs.setdefault('step', 'any')
+ return attrs
+
+
+class IntegerField(Field, forms.IntegerField):
+ widget = NumberInput
+
+ def __init__(self, *args, **kwargs):
+ kwargs.setdefault('widget', NumberInput if not kwargs.get('localize') else self.widget)
+ super(IntegerField, self).__init__(*args, **kwargs)
+
+ def widget_attrs(self, widget):
+ attrs = super(IntegerField, self).widget_attrs(widget) or {}
+ if self.min_value is not None:
+ attrs['min'] = self.min_value
+ if self.max_value is not None:
+ attrs['max'] = self.max_value
+ return attrs
+
+
+class DecimalField(Field, forms.DecimalField):
+ widget = NumberInput
+
+ def __init__(self, *args, **kwargs):
+ kwargs.setdefault('widget', NumberInput if not kwargs.get('localize') else self.widget)
+ super(DecimalField, self).__init__(*args, **kwargs)
+
+ def widget_attrs(self, widget):
+ attrs = super(DecimalField, self).widget_attrs(widget) or {}
+ if self.min_value is not None:
+ attrs['min'] = self.min_value
+ if self.max_value is not None:
+ attrs['max'] = self.max_value
+ if self.decimal_places is not None:
+ attrs['step'] = decimal.Decimal('0.1') ** self.decimal_places
+ return attrs
+
+
+class EmailField(Field, forms.EmailField):
+ widget = EmailInput
+
+
+class URLField(Field, forms.URLField):
+ widget = URLInput
+
+
+class SlugField(Field, forms.SlugField):
+ widget = SlugInput
+
+
+class RegexField(Field, forms.RegexField):
+ widget = TextInput
+
+ def __init__(self, regex, js_regex=None, max_length=None, min_length=None,
+ error_message=None, *args, **kwargs):
+ self.js_regex = js_regex
+ super(RegexField, self).__init__(regex, max_length, min_length,
+ *args, **kwargs)
+
+ def widget_attrs(self, widget):
+ attrs = super(RegexField, self).widget_attrs(widget) or {}
+ if self.js_regex is not None:
+ attrs['pattern'] = self.js_regex
+ return attrs
+
+
+if django.VERSION < (1, 9):
+ class IPAddressField(Field, forms.IPAddressField):
+ widget = IPAddressInput
+
+
+class GenericIPAddressField(Field, forms.GenericIPAddressField):
+ pass
+
+
+class ComboField(Field, forms.ComboField):
+ pass
+
+
+class MultiValueField(Field, forms.MultiValueField):
+ pass
+
+
+class SplitDateTimeField(forms.SplitDateTimeField):
+ widget = SplitDateTimeWidget
+ hidden_widget = SplitHiddenDateTimeWidget
+
+ def __init__(self, *args, **kwargs):
+ super(SplitDateTimeField, self).__init__(*args, **kwargs)
+ for widget in self.widget.widgets:
+ widget.is_required = self.required
+
+```
+
+
+## Example 3 from django-mongonaut
+[django-mongonaut](https://github.com/jazzband/django-mongonaut)
+([project documentation](https://django-mongonaut.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-mongonaut/))
+provides an introspective interface for working with
+[MongoDB](/mongodb.html) via mongoengine. The project has its own new code
+to map MongoDB to the [Django](/django.html) Admin interface.
+
+django-mongonaut's highlighted features include automatic introspection of
+mongoengine documents, the ability to constrain who sees what and what
+they can do and full control for adding, editing and deleting documents.
+
+The django-mongonaut project is open sourced under the
+[MIT License](https://github.com/jazzband/django-mongonaut/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-mongonaut / mongonaut / forms / widgets.py**](https://github.com/jazzband/django-mongonaut/blob/master/mongonaut/forms/widgets.py)
+
+```python
+# -*- coding: utf-8 -*-
+
+""" Widgets for mongonaut forms"""
+
+~~from django import forms
+
+from mongoengine.base import ObjectIdField
+from mongoengine.fields import BooleanField
+from mongoengine.fields import DateTimeField
+from mongoengine.fields import EmbeddedDocumentField
+from mongoengine.fields import ListField
+from mongoengine.fields import ReferenceField
+from mongoengine.fields import FloatField
+from mongoengine.fields import EmailField
+from mongoengine.fields import DecimalField
+from mongoengine.fields import URLField
+from mongoengine.fields import IntField
+from mongoengine.fields import StringField
+from mongoengine.fields import GeoPointField
+
+
+def get_widget(model_field, disabled=False):
+ """Choose which widget to display for a field."""
+
+ attrs = get_attrs(model_field, disabled)
+
+ if hasattr(model_field, "max_length") and not model_field.max_length:
+ return forms.Textarea(attrs=attrs)
+
+ elif isinstance(model_field, DateTimeField):
+ return forms.DateTimeInput(attrs=attrs)
+
+ elif isinstance(model_field, BooleanField):
+ return forms.CheckboxInput(attrs=attrs)
+
+ elif isinstance(model_field, ReferenceField) or model_field.choices:
+ return forms.Select(attrs=attrs)
+
+ elif (isinstance(model_field, ListField) or
+ isinstance(model_field, EmbeddedDocumentField) or
+ isinstance(model_field, GeoPointField)):
+ return None
+
+ else:
+ return forms.TextInput(attrs=attrs)
+
+
+def get_attrs(model_field, disabled=False):
+ """Set attributes on the display widget."""
+ attrs = {}
+ attrs['class'] = 'span6 xlarge'
+ if disabled or isinstance(model_field, ObjectIdField):
+ attrs['class'] += ' disabled'
+ attrs['readonly'] = 'readonly'
+ return attrs
+
+
+def get_form_field_class(model_field):
+ """Gets the default form field for a mongoenigne field."""
+
+ FIELD_MAPPING = {
+ IntField: forms.IntegerField,
+ StringField: forms.CharField,
+ FloatField: forms.FloatField,
+ BooleanField: forms.BooleanField,
+~~ DateTimeField: forms.DateTimeField,
+ DecimalField: forms.DecimalField,
+ URLField: forms.URLField,
+ EmailField: forms.EmailField
+ }
+
+ return FIELD_MAPPING.get(model_field.__class__, forms.CharField)
+
+```
+
+
diff --git a/content/pages/examples/django/django-forms-emailfield.markdown b/content/pages/examples/django/django-forms-emailfield.markdown
index 2155cafce..3cdcafb49 100644
--- a/content/pages/examples/django/django-forms-emailfield.markdown
+++ b/content/pages/examples/django/django-forms-emailfield.markdown
@@ -1,7 +1,7 @@
title: django.forms EmailField Python Code Examples
category: page
slug: django-forms-emailfield-examples
-sortorder: 500013120
+sortorder: 500013124
toc: False
sidebartitle: django.forms EmailField
meta: View Python code examples that show how to use the EmailField class within the forms module of the Django open source project.
diff --git a/content/pages/examples/django/django-forms-field.markdown b/content/pages/examples/django/django-forms-field.markdown
new file mode 100644
index 000000000..6d38d49e3
--- /dev/null
+++ b/content/pages/examples/django/django-forms-field.markdown
@@ -0,0 +1,66 @@
+title: django.forms Field Example Code
+category: page
+slug: django-forms-field-examples
+sortorder: 500011264
+toc: False
+sidebartitle: django.forms Field
+meta: Python example code for the Field class from the django.forms module of the Django project.
+
+
+Field is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / forms.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./forms.py)
+
+```python
+# forms.py
+from django.db import DatabaseError
+~~from django.forms import ModelForm, Field, ValidationError, BooleanField, CharField
+from django.forms.widgets import CheckboxInput, Select
+
+from explorer.app_settings import EXPLORER_DEFAULT_CONNECTION, EXPLORER_CONNECTIONS
+from explorer.models import Query, MSG_FAILED_BLACKLIST
+
+
+~~class SqlField(Field):
+
+ def validate(self, value):
+
+ query = Query(sql=value)
+
+ passes_blacklist, failing_words = query.passes_blacklist()
+
+ error = MSG_FAILED_BLACKLIST % ', '.join(failing_words) if not passes_blacklist else None
+
+ if error:
+ raise ValidationError(
+ error,
+ code="InvalidSql"
+ )
+
+
+class QueryForm(ModelForm):
+
+ sql = SqlField()
+ snapshot = BooleanField(widget=CheckboxInput, required=False)
+ connection = CharField(widget=Select, required=False)
+
+ def __init__(self, *args, **kwargs):
+ super(QueryForm, self).__init__(*args, **kwargs)
+
+
+## ... source file continues with no further Field examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-fileinput.markdown b/content/pages/examples/django/django-forms-fileinput.markdown
new file mode 100644
index 000000000..be0a370d1
--- /dev/null
+++ b/content/pages/examples/django/django-forms-fileinput.markdown
@@ -0,0 +1,55 @@
+title: django.forms FileInput Example Code
+category: page
+slug: django-forms-fileinput-examples
+sortorder: 500011265
+toc: False
+sidebartitle: django.forms FileInput
+meta: Python example code for the FileInput class from the django.forms module of the Django project.
+
+
+FileInput is a class within the django.forms module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / gears / widgets.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/gears/widgets.py)
+
+```python
+# widgets.py
+~~from django.forms import FileInput, CheckboxSelectMultiple, Select
+
+
+~~class CustomFileInput(FileInput):
+ template_name = 'gears/widgets/file_input.html'
+ accept = ''
+ show_file_name = True
+
+
+class CustomCheckboxSelectMultiple(CheckboxSelectMultiple):
+ template_name = 'gears/widgets/checkbox_multiple_select.html'
+ hide_label = False
+ hide_apply_btn = False
+
+ class Media:
+ js = ('gears/js/checkbox_multiple_select.js',)
+
+ def __init__(self, *args, **kwargs):
+ self.hide_label = kwargs.pop('hide_label', False)
+ self.hide_apply_btn = kwargs.pop('hide_apply_btn', False)
+ super().__init__(*args, **kwargs)
+
+ def get_context(self, name, value, attrs):
+ context = super().get_context(name, value, attrs)
+ context['widget'].update({
+ 'hide_label': self.hide_label,
+ 'hide_apply_btn': self.hide_apply_btn,
+ })
+
+
+## ... source file continues with no further FileInput examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-filepathfield.markdown b/content/pages/examples/django/django-forms-filepathfield.markdown
new file mode 100644
index 000000000..b56800554
--- /dev/null
+++ b/content/pages/examples/django/django-forms-filepathfield.markdown
@@ -0,0 +1,136 @@
+title: django.forms FilePathField Example Code
+category: page
+slug: django-forms-filepathfield-examples
+sortorder: 500011266
+toc: False
+sidebartitle: django.forms FilePathField
+meta: Python example code for the FilePathField class from the django.forms module of the Django project.
+
+
+FilePathField is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / fields.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./fields.py)
+
+```python
+# fields.py
+import copy
+import datetime
+import decimal
+import functools
+import inspect
+import re
+import uuid
+import warnings
+from collections import OrderedDict
+from collections.abc import Mapping
+
+from django.conf import settings
+from django.core.exceptions import ObjectDoesNotExist
+from django.core.exceptions import ValidationError as DjangoValidationError
+from django.core.validators import (
+ EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
+ MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
+ URLValidator, ip_address_validators
+)
+~~from django.forms import FilePathField as DjangoFilePathField
+from django.forms import ImageField as DjangoImageField
+from django.utils import timezone
+from django.utils.dateparse import (
+ parse_date, parse_datetime, parse_duration, parse_time
+)
+from django.utils.duration import duration_string
+from django.utils.encoding import is_protected_type, smart_str
+from django.utils.formats import localize_input, sanitize_separators
+from django.utils.ipv6 import clean_ipv6_address
+from django.utils.timezone import utc
+from django.utils.translation import gettext_lazy as _
+from pytz.exceptions import InvalidTimeError
+
+from rest_framework import (
+ ISO_8601, RemovedInDRF313Warning, RemovedInDRF314Warning
+)
+from rest_framework.exceptions import ErrorDetail, ValidationError
+from rest_framework.settings import api_settings
+from rest_framework.utils import html, humanize_datetime, json, representation
+from rest_framework.utils.formatting import lazy_format
+from rest_framework.validators import ProhibitSurrogateCharactersValidator
+
+
+class empty:
+
+
+## ... source file abbreviated to get to FilePathField examples ...
+
+
+ def get_value(self, dictionary):
+ if self.field_name not in dictionary:
+ if getattr(self.root, 'partial', False):
+ return empty
+ if html.is_html_input(dictionary):
+ return dictionary.getlist(self.field_name)
+ return dictionary.get(self.field_name, empty)
+
+ def to_internal_value(self, data):
+ if isinstance(data, str) or not hasattr(data, '__iter__'):
+ self.fail('not_a_list', input_type=type(data).__name__)
+ if not self.allow_empty and len(data) == 0:
+ self.fail('empty')
+
+ return {
+ super(MultipleChoiceField, self).to_internal_value(item)
+ for item in data
+ }
+
+ def to_representation(self, value):
+ return {
+ self.choice_strings_to_values.get(str(item), item) for item in value
+ }
+
+
+~~class FilePathField(ChoiceField):
+ default_error_messages = {
+ 'invalid_choice': _('"{input}" is not a valid path choice.')
+ }
+
+ def __init__(self, path, match=None, recursive=False, allow_files=True,
+ allow_folders=False, required=None, **kwargs):
+ field = DjangoFilePathField(
+ path, match=match, recursive=recursive, allow_files=allow_files,
+ allow_folders=allow_folders, required=required
+ )
+ kwargs['choices'] = field.choices
+ super().__init__(**kwargs)
+
+
+
+class FileField(Field):
+ default_error_messages = {
+ 'required': _('No file was submitted.'),
+ 'invalid': _('The submitted data was not a file. Check the encoding type on the form.'),
+ 'no_name': _('No filename could be determined.'),
+ 'empty': _('The submitted file is empty.'),
+ 'max_length': _('Ensure this filename has at most {max_length} characters (it has {length}).'),
+ }
+
+
+
+## ... source file continues with no further FilePathField examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-form.markdown b/content/pages/examples/django/django-forms-form.markdown
new file mode 100644
index 000000000..b7b244c07
--- /dev/null
+++ b/content/pages/examples/django/django-forms-form.markdown
@@ -0,0 +1,313 @@
+title: django.forms Form Example Code
+category: page
+slug: django-forms-form-examples
+sortorder: 500011267
+toc: False
+sidebartitle: django.forms Form
+meta: Python example code for the Form class from the django.forms module of the Django project.
+
+
+Form is a class within the django.forms module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / chair / forms.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/chair/forms.py)
+
+```python
+# forms.py
+from functools import reduce
+
+from django import forms
+from django.contrib.auth import get_user_model
+from django.db import models
+from django.db.models import Q, F, Count, Max, Subquery, OuterRef, Value
+from django.db.models.functions import Concat
+~~from django.forms import MultipleChoiceField, ChoiceField, Form
+from django.urls import reverse
+from django.utils.translation import ugettext_lazy as _
+
+from django_countries import countries
+
+from conferences.models import Conference, ArtifactDescriptor
+from gears.widgets import CustomCheckboxSelectMultiple, CustomFileInput
+from review.models import Reviewer, Review, ReviewStats
+from review.utilities import get_average_score
+from submissions.models import Submission, Attachment
+from users.models import Profile
+
+User = get_user_model()
+
+
+def clean_data_to_int(iterable, empty=None):
+ return [int(x) if x != '' else None for x in iterable]
+
+
+def q_or(disjuncts, default=True):
+ if disjuncts:
+ return reduce(lambda acc, d: acc | d, disjuncts)
+ return Q(pk__isnull=(not default)) # otherwise, check whether PK is null
+
+
+
+## ... source file abbreviated to get to Form examples ...
+
+
+ available_reviewers = Reviewer.objects.exclude(
+ Q(pk__in=assigned_reviewers) | Q(user__in=authors_users)
+ )
+ profiles = {
+ rev: rev.user.profile for rev in available_reviewers
+ }
+ reviewers = list(available_reviewers)
+ reviewers.sort(key=lambda r: r.reviews.count())
+ self.fields['reviewer'].choices = (
+ (rev.pk,
+ f'{profiles[rev].get_full_name()} ({rev.reviews.count()}) - '
+ f'{profiles[rev].affiliation}, '
+ f'{profiles[rev].get_country_display()}')
+ for rev in reviewers
+ )
+
+ def save(self):
+ reviewer = Reviewer.objects.get(pk=self.cleaned_data['reviewer'])
+ review = Review.objects.create(
+ reviewer=reviewer, stage=self.review_stage)
+ return review
+
+
+
+
+~~class ExportSubmissionsForm(Form):
+ ORDER_COLUMN = '#'
+ ID_COLUMN = 'ID'
+ AUTHORS_COLUMN = 'AUTHORS'
+ TITLE_COLUMN = 'TITLE'
+ COUNTRY_COLUMN = 'COUNTRY'
+ STYPE_COLUMN = 'TYPE'
+ REVIEW_PAPER_COLUMN = 'REVIEW_MANUSCRIPT'
+ REVIEW_SCORE_COLUMN = 'REVIEW_SCORE'
+ STATUS_COLUMN = 'STATUS'
+ TOPICS_COLUMN = 'TOPICS'
+
+ COLUMNS = (
+ (ORDER_COLUMN, ORDER_COLUMN),
+ (ID_COLUMN, ID_COLUMN),
+ (TITLE_COLUMN, TITLE_COLUMN),
+ (AUTHORS_COLUMN, AUTHORS_COLUMN),
+ (COUNTRY_COLUMN, COUNTRY_COLUMN),
+ (STYPE_COLUMN, STYPE_COLUMN),
+ (REVIEW_PAPER_COLUMN, REVIEW_PAPER_COLUMN),
+ (REVIEW_SCORE_COLUMN, REVIEW_SCORE_COLUMN),
+ (STATUS_COLUMN, STATUS_COLUMN),
+ (TOPICS_COLUMN, TOPICS_COLUMN),
+ )
+
+
+
+## ... source file continues with no further Form examples...
+
+```
+
+
+## Example 2 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / wizards / views.py**](https://github.com/divio/django-cms/blob/develop/cms/wizards/views.py)
+
+```python
+# views.py
+
+import os
+
+from django.conf import settings
+from django.core.exceptions import PermissionDenied
+from django.core.files.storage import FileSystemStorage
+~~from django.forms import Form
+from django.template.response import SimpleTemplateResponse
+from django.urls import NoReverseMatch
+
+from formtools.wizard.views import SessionWizardView
+
+from cms.models import Page
+from cms.utils import get_current_site
+from cms.utils.i18n import get_site_language_from_request
+
+from .wizard_pool import wizard_pool
+from .forms import (
+ WizardStep1Form,
+ WizardStep2BaseForm,
+ step2_form_factory,
+)
+
+
+class WizardCreateView(SessionWizardView):
+ template_name = 'cms/wizards/start.html'
+ file_storage = FileSystemStorage(
+ location=os.path.join(settings.MEDIA_ROOT, 'wizard_tmp_files'))
+
+ form_list = [
+ ('0', WizardStep1Form),
+
+
+## ... source file continues with no further Form examples...
+
+```
+
+
+## Example 3 from django-mongonaut
+[django-mongonaut](https://github.com/jazzband/django-mongonaut)
+([project documentation](https://django-mongonaut.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-mongonaut/))
+provides an introspective interface for working with
+[MongoDB](/mongodb.html) via mongoengine. The project has its own new code
+to map MongoDB to the [Django](/django.html) Admin interface.
+
+django-mongonaut's highlighted features include automatic introspection of
+mongoengine documents, the ability to constrain who sees what and what
+they can do and full control for adding, editing and deleting documents.
+
+The django-mongonaut project is open sourced under the
+[MIT License](https://github.com/jazzband/django-mongonaut/blob/master/LICENSE.txt)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-mongonaut / mongonaut / views.py**](https://github.com/jazzband/django-mongonaut/blob/master/mongonaut/./views.py)
+
+```python
+# views.py
+import math
+
+from django.contrib import messages
+from django.urls import reverse
+~~from django.forms import Form
+from django.http import HttpResponseForbidden
+from django.http import Http404
+from django.utils.functional import cached_property
+from django.views.generic.edit import DeletionMixin
+from django.views.generic import ListView
+from django.views.generic import TemplateView
+from django.views.generic.edit import FormView
+from mongoengine.fields import EmbeddedDocumentField, ListField
+
+from mongonaut.forms import MongoModelForm
+from mongonaut.mixins import MongonautFormViewMixin
+from mongonaut.mixins import MongonautViewMixin
+from mongonaut.utils import is_valid_object_id
+
+
+class IndexView(MongonautViewMixin, ListView):
+
+ template_name = "mongonaut/index.html"
+ queryset = []
+ permission = 'has_view_permission'
+
+ def get_queryset(self):
+ return self.get_mongoadmins()
+
+
+
+## ... source file abbreviated to get to Form examples ...
+
+
+ self.ident = self.kwargs.get('id')
+ self.document = self.document_type.objects.get(pk=self.ident)
+
+ context['document'] = self.document
+ context['app_label'] = self.app_label
+ context['document_name'] = self.document_name
+ context['form_action'] = reverse('document_detail_edit_form', args=[self.kwargs.get('app_label'),
+ self.kwargs.get('document_name'),
+ self.kwargs.get('id')])
+
+ return context
+
+ def get_form(self): #get_form(self, Form) leads to "get_form() missing 1 required positional argument: 'Form'" error."
+ self.set_mongoadmin()
+ context = self.set_permissions_in_context({})
+
+ if not context['has_edit_permission']:
+ return HttpResponseForbidden("You do not have permissions to edit this content.")
+
+ self.document_type = getattr(self.models, self.document_name)
+ self.ident = self.kwargs.get('id')
+ try:
+ self.document = self.document_type.objects.get(pk=self.ident)
+ except self.document_type.DoesNotExist:
+ raise Http404
+~~ self.form = Form()
+
+ if self.request.method == 'POST':
+ self.form = self.process_post_form('Your changes have been saved.')
+ else:
+ self.form = MongoModelForm(model=self.document_type, instance=self.document).get_form()
+ return self.form
+
+
+class DocumentAddFormView(MongonautViewMixin, FormView, MongonautFormViewMixin):
+
+ template_name = "mongonaut/document_add_form.html"
+ form_class = Form
+ success_url = '/'
+ permission = 'has_add_permission'
+
+ def get_success_url(self):
+ self.set_mongonaut_base()
+ return reverse('document_detail', kwargs={'app_label': self.app_label, 'document_name': self.document_name, 'id': str(self.new_document.id)})
+
+ def get_context_data(self, **kwargs):
+ context = super(DocumentAddFormView, self).get_context_data(**kwargs)
+ self.set_mongoadmin()
+ context = self.set_permissions_in_context(context)
+ self.document_type = getattr(self.models, self.document_name)
+
+ context['app_label'] = self.app_label
+ context['document_name'] = self.document_name
+ context['form_action'] = reverse('document_detail_add_form', args=[self.kwargs.get('app_label'),
+ self.kwargs.get('document_name')])
+
+ return context
+
+ def get_form(self):
+ self.set_mongonaut_base()
+ self.document_type = getattr(self.models, self.document_name)
+~~ self.form = Form()
+
+ if self.request.method == 'POST':
+ self.form = self.process_post_form('Your new document has been added and saved.')
+ else:
+ self.form = MongoModelForm(model=self.document_type).get_form()
+ return self.form
+
+
+class DocumentDeleteView(DeletionMixin, MongonautViewMixin, TemplateView):
+
+ success_url = "/"
+ template_name = "mongonaut/document_delete.html"
+
+ def get_success_url(self):
+ self.set_mongonaut_base()
+ messages.add_message(self.request, messages.INFO, 'Your document has been deleted.')
+ return reverse('document_list', kwargs={'app_label': self.app_label, 'document_name': self.document_name})
+
+ def get_object(self):
+ self.set_mongoadmin()
+ self.document_type = getattr(self.models, self.document_name)
+ self.ident = self.kwargs.get('id')
+ self.document = self.document_type.objects.get(pk=self.ident)
+ return self.document
+
+
+## ... source file continues with no further Form examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-hiddeninput.markdown b/content/pages/examples/django/django-forms-hiddeninput.markdown
new file mode 100644
index 000000000..a1a870cd7
--- /dev/null
+++ b/content/pages/examples/django/django-forms-hiddeninput.markdown
@@ -0,0 +1,213 @@
+title: django.forms HiddenInput Example Code
+category: page
+slug: django-forms-hiddeninput-examples
+sortorder: 500011268
+toc: False
+sidebartitle: django.forms HiddenInput
+meta: Python example code for the HiddenInput class from the django.forms module of the Django project.
+
+
+HiddenInput is a class within the django.forms module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / review / forms.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/review/forms.py)
+
+```python
+# forms.py
+from django import forms
+~~from django.forms import Form, HiddenInput, CharField, ChoiceField, ModelForm
+
+from conferences.models import ProceedingType, ProceedingVolume
+from review.models import Review, check_review_details, ReviewDecision
+
+
+class EditReviewForm(forms.ModelForm):
+ class Meta:
+ model = Review
+ fields = [
+ 'technical_merit', 'relevance', 'originality', 'clarity',
+ 'details', 'submitted'
+ ]
+
+ submitted = forms.BooleanField(required=False)
+ technical_merit = forms.ChoiceField(choices=Review.SCORE_CHOICES, required=False)
+ relevance = forms.ChoiceField(choices=Review.SCORE_CHOICES, required=False)
+ originality = forms.ChoiceField(choices=Review.SCORE_CHOICES, required=False)
+ details = forms.CharField(widget=forms.Textarea(attrs={'rows': '5'}), required=False)
+
+ def clean(self):
+ cleaned_data = super().clean()
+ if cleaned_data['submitted']:
+ is_incomplete = False
+ for score_field in self.instance.score_fields().keys():
+ if not cleaned_data[score_field]:
+ self.add_error(score_field, 'Must select a score')
+ is_incomplete = True
+ stype = self.instance.paper.stype
+ if not check_review_details(cleaned_data['details'], stype):
+ self.add_error(
+ 'details',
+ f'Review details must have at least '
+ f'{stype.min_num_words_in_review} words'
+ )
+ is_incomplete = True
+ if is_incomplete:
+ self.cleaned_data['submitted'] = False
+ raise forms.ValidationError('Review is incomplete')
+ return cleaned_data
+
+
+class UpdateReviewDecisionForm(ModelForm):
+ class Meta:
+ model = ReviewDecision
+ fields = ['decision_type']
+
+
+class UpdateVolumeForm(Form):
+~~ volume = CharField(widget=HiddenInput(), required=False)
+
+ def __init__(self, *args, instance=None, **kwargs):
+ if not instance:
+ raise ValueError('Decision instance is required')
+ self.instance = instance
+ kwargs.update({
+ 'initial': {
+ 'volume': str(instance.volume.pk) if instance.volume else '',
+ }
+ })
+ super().__init__(*args, **kwargs)
+ self.volume = None
+
+ def clean_volume(self):
+ try:
+ pk = int(self.cleaned_data['volume'])
+ volumes = ProceedingVolume.objects.filter(pk=pk)
+ self.volume = volumes.first() if volumes.count() else None
+ except ValueError:
+ self.volume = None
+ return self.cleaned_data['volume']
+
+ def save(self, commit=True):
+ self.instance.volume = self.volume
+
+
+## ... source file continues with no further HiddenInput examples...
+
+```
+
+
+## Example 2 from django-flexible-subscriptions
+[django-flexible-subscriptions](https://github.com/studybuffalo/django-flexible-subscriptions)
+([project documentation](https://django-flexible-subscriptions.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-flexible-subscriptions/))
+provides boilerplate code for adding subscription and recurrent billing
+to [Django](/django.html) web applications. Various payment providers
+can be added on the back end to run the transactions.
+
+The django-flexible-subscriptions project is open sourced under the
+[GNU General Public License v3.0](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/LICENSE).
+
+[**django-flexible-subscriptions / subscriptions / views.py**](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/subscriptions/./views.py)
+
+```python
+# views.py
+from copy import copy
+
+from django.contrib import messages
+from django.contrib.auth import get_user_model
+from django.contrib.auth.mixins import (
+ LoginRequiredMixin, PermissionRequiredMixin
+)
+from django.contrib.messages.views import SuccessMessageMixin
+~~from django.forms import HiddenInput
+from django.forms.models import inlineformset_factory
+from django.http import HttpResponseRedirect
+from django.http.response import HttpResponseNotAllowed, HttpResponseNotFound
+from django.shortcuts import get_object_or_404
+from django.template.response import TemplateResponse
+from django.urls import reverse_lazy
+from django.utils import timezone
+
+from subscriptions import models, forms, abstract
+
+
+class DashboardView(PermissionRequiredMixin, abstract.TemplateView):
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ template_name = 'subscriptions/dashboard.html'
+
+
+class TagListView(PermissionRequiredMixin, abstract.ListView):
+ model = models.PlanTag
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ context_object_name = 'tags'
+ template_name = 'subscriptions/tag_list.html'
+
+
+
+## ... source file abbreviated to get to HiddenInput examples ...
+
+
+ payment_transaction = self.process_payment(
+ payment_form=payment_form,
+ plan_cost_form=plan_cost_form,
+ )
+
+ if payment_transaction:
+ subscription = self.setup_subscription(
+ request.user, plan_cost_form.cleaned_data['plan_cost']
+ )
+
+ transaction = self.record_transaction(
+ subscription,
+ self.retrieve_transaction_date(payment_transaction)
+ )
+
+ return HttpResponseRedirect(
+ self.get_success_url(transaction_id=transaction.id)
+ )
+
+ messages.error(request, 'Error processing payment')
+
+ return self.render_confirmation(request, **kwargs)
+
+ def hide_form(self, form):
+ for _, field in form.fields.items():
+~~ field.widget = HiddenInput()
+
+ return form
+
+ def process_payment(self, *args, **kwargs): # pylint: disable=unused-argument
+ return True
+
+ def setup_subscription(self, request_user, plan_cost):
+ current_date = timezone.now()
+
+ subscription = models.UserSubscription.objects.create(
+ user=request_user,
+ subscription=plan_cost,
+ date_billing_start=current_date,
+ date_billing_end=None,
+ date_billing_last=current_date,
+ date_billing_next=plan_cost.next_billing_datetime(current_date),
+ active=True,
+ cancelled=False,
+ )
+
+ try:
+ group = self.subscription_plan.group
+ group.user_set.add(request_user)
+ except AttributeError:
+
+
+## ... source file continues with no further HiddenInput examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-imagefield.markdown b/content/pages/examples/django/django-forms-imagefield.markdown
new file mode 100644
index 000000000..fe8f3b7aa
--- /dev/null
+++ b/content/pages/examples/django/django-forms-imagefield.markdown
@@ -0,0 +1,137 @@
+title: django.forms ImageField Example Code
+category: page
+slug: django-forms-imagefield-examples
+sortorder: 500011269
+toc: False
+sidebartitle: django.forms ImageField
+meta: Python example code for the ImageField class from the django.forms module of the Django project.
+
+
+ImageField is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / fields.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./fields.py)
+
+```python
+# fields.py
+import copy
+import datetime
+import decimal
+import functools
+import inspect
+import re
+import uuid
+import warnings
+from collections import OrderedDict
+from collections.abc import Mapping
+
+from django.conf import settings
+from django.core.exceptions import ObjectDoesNotExist
+from django.core.exceptions import ValidationError as DjangoValidationError
+from django.core.validators import (
+ EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
+ MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
+ URLValidator, ip_address_validators
+)
+from django.forms import FilePathField as DjangoFilePathField
+~~from django.forms import ImageField as DjangoImageField
+from django.utils import timezone
+from django.utils.dateparse import (
+ parse_date, parse_datetime, parse_duration, parse_time
+)
+from django.utils.duration import duration_string
+from django.utils.encoding import is_protected_type, smart_str
+from django.utils.formats import localize_input, sanitize_separators
+from django.utils.ipv6 import clean_ipv6_address
+from django.utils.timezone import utc
+from django.utils.translation import gettext_lazy as _
+from pytz.exceptions import InvalidTimeError
+
+from rest_framework import (
+ ISO_8601, RemovedInDRF313Warning, RemovedInDRF314Warning
+)
+from rest_framework.exceptions import ErrorDetail, ValidationError
+from rest_framework.settings import api_settings
+from rest_framework.utils import html, humanize_datetime, json, representation
+from rest_framework.utils.formatting import lazy_format
+from rest_framework.validators import ProhibitSurrogateCharactersValidator
+
+
+class empty:
+ pass
+
+
+## ... source file abbreviated to get to ImageField examples ...
+
+
+ if not self.allow_empty_file and not file_size:
+ self.fail('empty')
+ if self.max_length and len(file_name) > self.max_length:
+ self.fail('max_length', max_length=self.max_length, length=len(file_name))
+
+ return data
+
+ def to_representation(self, value):
+ if not value:
+ return None
+
+ use_url = getattr(self, 'use_url', api_settings.UPLOADED_FILES_USE_URL)
+ if use_url:
+ try:
+ url = value.url
+ except AttributeError:
+ return None
+ request = self.context.get('request', None)
+ if request is not None:
+ return request.build_absolute_uri(url)
+ return url
+
+ return value.name
+
+
+~~class ImageField(FileField):
+ default_error_messages = {
+ 'invalid_image': _(
+ 'Upload a valid image. The file you uploaded was either not an image or a corrupted image.'
+ ),
+ }
+
+ def __init__(self, *args, **kwargs):
+ self._DjangoImageField = kwargs.pop('_DjangoImageField', DjangoImageField)
+ super().__init__(*args, **kwargs)
+
+ def to_internal_value(self, data):
+ file_object = super().to_internal_value(data)
+ django_field = self._DjangoImageField()
+ django_field.error_messages = self.error_messages
+ return django_field.clean(file_object)
+
+
+
+class _UnvalidatedField(Field):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.allow_blank = True
+ self.allow_null = True
+
+
+
+## ... source file continues with no further ImageField examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-integerfield.markdown b/content/pages/examples/django/django-forms-integerfield.markdown
index e5cd5198e..c754b66ce 100644
--- a/content/pages/examples/django/django-forms-integerfield.markdown
+++ b/content/pages/examples/django/django-forms-integerfield.markdown
@@ -1,7 +1,7 @@
title: django.forms IntegerField Python Code Examples
category: page
slug: django-forms-integerfield-examples
-sortorder: 500013120
+sortorder: 500013125
toc: False
sidebartitle: django.forms IntegerField
meta: View Python code examples that show how to use the IntegerField class within the forms module of the Django open source project.
diff --git a/content/pages/examples/django/django-forms-media.markdown b/content/pages/examples/django/django-forms-media.markdown
new file mode 100644
index 000000000..22f4fab39
--- /dev/null
+++ b/content/pages/examples/django/django-forms-media.markdown
@@ -0,0 +1,109 @@
+title: django.forms Media Example Code
+category: page
+slug: django-forms-media-examples
+sortorder: 500011270
+toc: False
+sidebartitle: django.forms Media
+meta: Python example code for the Media class from the django.forms module of the Django project.
+
+
+Media is a class within the django.forms module of the Django project.
+
+
+## Example 1 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / admin / menu.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/admin/menu.py)
+
+```python
+# menu.py
+~~from django.forms import Media, MediaDefiningClass
+from django.forms.utils import flatatt
+from django.template.loader import render_to_string
+from django.utils.safestring import mark_safe
+from django.utils.text import slugify
+
+from wagtail.core import hooks
+
+
+class MenuItem(metaclass=MediaDefiningClass):
+ template = 'wagtailadmin/shared/menu_item.html'
+
+ def __init__(self, label, url, name=None, classnames='', icon_name='', attrs=None, order=1000):
+ self.label = label
+ self.url = url
+ self.classnames = classnames
+ self.icon_name = icon_name
+ self.name = (name or slugify(str(label)))
+ self.order = order
+
+ if attrs:
+ self.attr_string = flatatt(attrs)
+ else:
+ self.attr_string = ""
+
+
+
+## ... source file abbreviated to get to Media examples ...
+
+
+ def render_html(self, request):
+ context = self.get_context(request)
+ return render_to_string(self.template, context, request=request)
+
+
+class Menu:
+ def __init__(self, register_hook_name, construct_hook_name=None):
+ self.register_hook_name = register_hook_name
+ self.construct_hook_name = construct_hook_name
+ self._registered_menu_items = None
+
+ @property
+ def registered_menu_items(self):
+ if self._registered_menu_items is None:
+ self._registered_menu_items = [fn() for fn in hooks.get_hooks(self.register_hook_name)]
+ return self._registered_menu_items
+
+ def menu_items_for_request(self, request):
+ return [item for item in self.registered_menu_items if item.is_shown(request)]
+
+ def active_menu_items(self, request):
+ return [item for item in self.menu_items_for_request(request) if item.is_active(request)]
+
+ @property
+ def media(self):
+~~ media = Media()
+ for item in self.registered_menu_items:
+ media += item.media
+ return media
+
+ def render_html(self, request):
+ menu_items = self.menu_items_for_request(request)
+
+ if self.construct_hook_name:
+ for fn in hooks.get_hooks(self.construct_hook_name):
+ fn(request, menu_items)
+
+ rendered_menu_items = []
+ for item in sorted(menu_items, key=lambda i: i.order):
+ rendered_menu_items.append(item.render_html(request))
+ return mark_safe(''.join(rendered_menu_items))
+
+
+class SubmenuMenuItem(MenuItem):
+ template = 'wagtailadmin/shared/menu_submenu_item.html'
+
+ def __init__(self, label, menu, **kwargs):
+ self.menu = menu
+ super().__init__(label, '#', **kwargs)
+
+
+
+## ... source file continues with no further Media examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-mediadefiningclass.markdown b/content/pages/examples/django/django-forms-mediadefiningclass.markdown
new file mode 100644
index 000000000..f27021174
--- /dev/null
+++ b/content/pages/examples/django/django-forms-mediadefiningclass.markdown
@@ -0,0 +1,107 @@
+title: django.forms MediaDefiningClass Example Code
+category: page
+slug: django-forms-mediadefiningclass-examples
+sortorder: 500011271
+toc: False
+sidebartitle: django.forms MediaDefiningClass
+meta: Python example code for the MediaDefiningClass class from the django.forms module of the Django project.
+
+
+MediaDefiningClass is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / toolbar_base.py**](https://github.com/divio/django-cms/blob/develop/cms/./toolbar_base.py)
+
+```python
+# toolbar_base.py
+~~from django.forms import MediaDefiningClass
+
+from six import with_metaclass
+
+from cms.exceptions import LanguageError
+from cms.utils import get_current_site, get_language_from_request
+from cms.utils.i18n import get_language_object
+
+
+~~class CMSToolbar(with_metaclass(MediaDefiningClass)):
+ supported_apps = None
+
+ def __init__(self, request, toolbar, is_current_app, app_path):
+ self.request = request
+ self.toolbar = toolbar
+ self.is_current_app = is_current_app
+ self.app_path = app_path
+ self.current_site = get_current_site()
+ try:
+ self.current_lang = get_language_object(get_language_from_request(self.request), self.current_site.pk)['code']
+ except LanguageError:
+ self.current_lang = None
+
+ def populate(self):
+ pass
+
+ def post_template_populate(self):
+ pass
+
+ @classmethod
+ def check_current_app(cls, key, app_name):
+ if cls.supported_apps is None:
+ local_apps = ".".join(key.split(".")[:-2]),
+ else:
+
+
+## ... source file continues with no further MediaDefiningClass examples...
+
+```
+
+
+## Example 2 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / admin / menu.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/admin/menu.py)
+
+```python
+# menu.py
+~~from django.forms import Media, MediaDefiningClass
+from django.forms.utils import flatatt
+from django.template.loader import render_to_string
+from django.utils.safestring import mark_safe
+from django.utils.text import slugify
+
+from wagtail.core import hooks
+
+
+class MenuItem(metaclass=MediaDefiningClass):
+ template = 'wagtailadmin/shared/menu_item.html'
+
+ def __init__(self, label, url, name=None, classnames='', icon_name='', attrs=None, order=1000):
+ self.label = label
+ self.url = url
+ self.classnames = classnames
+ self.icon_name = icon_name
+ self.name = (name or slugify(str(label)))
+ self.order = order
+
+ if attrs:
+ self.attr_string = flatatt(attrs)
+ else:
+ self.attr_string = ""
+
+
+
+## ... source file continues with no further MediaDefiningClass examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-modelchoicefield.markdown b/content/pages/examples/django/django-forms-modelchoicefield.markdown
new file mode 100644
index 000000000..a9900a6a7
--- /dev/null
+++ b/content/pages/examples/django/django-forms-modelchoicefield.markdown
@@ -0,0 +1,121 @@
+title: django.forms ModelChoiceField Example Code
+category: page
+slug: django-forms-modelchoicefield-examples
+sortorder: 500011272
+toc: False
+sidebartitle: django.forms ModelChoiceField
+meta: Python example code for the ModelChoiceField class from the django.forms module of the Django project.
+
+
+ModelChoiceField is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / templatetags / jet_tags.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/templatetags/jet_tags.py)
+
+```python
+# jet_tags.py
+from __future__ import unicode_literals
+import json
+import os
+from django import template
+try:
+ from django.core.urlresolvers import reverse
+except ImportError: # Django 1.11
+ from django.urls import reverse
+
+~~from django.forms import CheckboxInput, ModelChoiceField, Select, ModelMultipleChoiceField, SelectMultiple
+from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
+from django.utils.formats import get_format
+from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_text
+from jet import settings, VERSION
+from jet.models import Bookmark
+from jet.utils import get_model_instance_label, get_model_queryset, get_possible_language_codes, \
+ get_admin_site, get_menu_items
+
+try:
+ from urllib.parse import parse_qsl
+except ImportError:
+ from urlparse import parse_qsl
+
+
+register = template.Library()
+assignment_tag = register.assignment_tag if hasattr(register, 'assignment_tag') else register.simple_tag
+
+
+@assignment_tag
+def jet_get_date_format():
+ return get_format('DATE_INPUT_FORMATS')[0]
+
+
+
+
+## ... source file abbreviated to get to ModelChoiceField examples ...
+
+
+ app_label = model._meta.app_label
+ model_name = model._meta.object_name
+
+ attrs = {
+ 'class': 'ajax',
+ 'data-app-label': app_label,
+ 'data-model': model_name,
+ 'data-ajax--url': reverse('jet:model_lookup')
+ }
+
+ initial_value = field.value()
+
+ if hasattr(field, 'field') and isinstance(field.field, ModelMultipleChoiceField):
+ if initial_value:
+ initial_objects = model.objects.filter(pk__in=initial_value)
+ choices.extend(
+ [(initial_object.pk, get_model_instance_label(initial_object))
+ for initial_object in initial_objects]
+ )
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+ field.field.widget.widget = SelectMultiple(attrs)
+ else:
+ field.field.widget = SelectMultiple(attrs)
+ field.field.choices = choices
+~~ elif hasattr(field, 'field') and isinstance(field.field, ModelChoiceField):
+ if initial_value:
+ try:
+ initial_object = model.objects.get(pk=initial_value)
+ attrs['data-object-id'] = initial_value
+ choices.append((initial_object.pk, get_model_instance_label(initial_object)))
+ except model.DoesNotExist:
+ pass
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+ field.field.widget.widget = Select(attrs)
+ else:
+ field.field.widget = Select(attrs)
+ field.field.choices = choices
+
+ return field
+
+
+@assignment_tag(takes_context=True)
+def jet_get_current_theme(context):
+ if 'request' in context and 'JET_THEME' in context['request'].COOKIES:
+ theme = context['request'].COOKIES['JET_THEME']
+ if isinstance(settings.JET_THEMES, list) and len(settings.JET_THEMES) > 0:
+ for conf_theme in settings.JET_THEMES:
+ if isinstance(conf_theme, dict) and conf_theme.get('theme') == theme:
+
+
+## ... source file continues with no further ModelChoiceField examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-modelform.markdown b/content/pages/examples/django/django-forms-modelform.markdown
new file mode 100644
index 000000000..e48ce14a2
--- /dev/null
+++ b/content/pages/examples/django/django-forms-modelform.markdown
@@ -0,0 +1,202 @@
+title: django.forms ModelForm Example Code
+category: page
+slug: django-forms-modelform-examples
+sortorder: 500011273
+toc: False
+sidebartitle: django.forms ModelForm
+meta: Python example code for the ModelForm class from the django.forms module of the Django project.
+
+
+ModelForm is a class within the django.forms module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / proceedings / forms.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/proceedings/forms.py)
+
+```python
+# forms.py
+~~from django.forms import ModelForm
+
+from gears.widgets import DropdownSelectSubmit
+from proceedings.models import CameraReady
+
+
+EMPTY_VOLUME_LABEL = '(no volume)'
+
+
+~~class UpdateVolumeForm(ModelForm):
+
+ class Meta:
+ model = CameraReady
+ fields = ['volume']
+ widgets = {
+ 'volume': DropdownSelectSubmit(
+ empty_label=EMPTY_VOLUME_LABEL,
+ label_class='font-weight-normal dccn-text-small',
+ empty_label_class='text-warning-18',
+ nonempty_label_class='text-success-18',
+ )
+ }
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.fields['volume'].queryset = self.instance.proc_type.volumes.all()
+ self.fields['volume'].empty_label = EMPTY_VOLUME_LABEL
+
+
+
+## ... source file continues with no further ModelForm examples...
+
+```
+
+
+## Example 2 from django-flexible-subscriptions
+[django-flexible-subscriptions](https://github.com/studybuffalo/django-flexible-subscriptions)
+([project documentation](https://django-flexible-subscriptions.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-flexible-subscriptions/))
+provides boilerplate code for adding subscription and recurrent billing
+to [Django](/django.html) web applications. Various payment providers
+can be added on the back end to run the transactions.
+
+The django-flexible-subscriptions project is open sourced under the
+[GNU General Public License v3.0](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/LICENSE).
+
+[**django-flexible-subscriptions / subscriptions / forms.py**](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/subscriptions/./forms.py)
+
+```python
+# forms.py
+from django import forms
+from django.core import validators
+~~from django.forms import ModelForm
+from django.utils import timezone
+
+from subscriptions.conf import SETTINGS
+from subscriptions.models import SubscriptionPlan, PlanCost
+
+
+def assemble_cc_years():
+ cc_years = []
+ now = timezone.now()
+
+ for year in range(now.year, now.year + 60):
+ cc_years.append((year, year))
+
+ return cc_years
+
+
+~~class SubscriptionPlanForm(ModelForm):
+ class Meta:
+ model = SubscriptionPlan
+ fields = [
+ 'plan_name', 'plan_description', 'group', 'tags', 'grace_period',
+ ]
+
+
+~~class PlanCostForm(ModelForm):
+ class Meta:
+ model = PlanCost
+ fields = ['recurrence_period', 'recurrence_unit', 'cost']
+
+
+class PaymentForm(forms.Form):
+ CC_MONTHS = (
+ ('1', '01 - January'),
+ ('2', '02 - February'),
+ ('3', '03 - March'),
+ ('4', '04 - April'),
+ ('5', '05 - May'),
+ ('6', '06 - June'),
+ ('7', '07 - July'),
+ ('8', '08 - August'),
+ ('9', '09 - September'),
+ ('10', '10 - October'),
+ ('11', '11 - November'),
+ ('12', '12 - December'),
+ )
+ CC_YEARS = assemble_cc_years()
+
+ cardholder_name = forms.CharField(
+ label='Cardholder name',
+
+
+## ... source file continues with no further ModelForm examples...
+
+```
+
+
+## Example 3 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / forms.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./forms.py)
+
+```python
+# forms.py
+from django.db import DatabaseError
+~~from django.forms import ModelForm, Field, ValidationError, BooleanField, CharField
+from django.forms.widgets import CheckboxInput, Select
+
+from explorer.app_settings import EXPLORER_DEFAULT_CONNECTION, EXPLORER_CONNECTIONS
+from explorer.models import Query, MSG_FAILED_BLACKLIST
+
+
+class SqlField(Field):
+
+ def validate(self, value):
+
+ query = Query(sql=value)
+
+ passes_blacklist, failing_words = query.passes_blacklist()
+
+ error = MSG_FAILED_BLACKLIST % ', '.join(failing_words) if not passes_blacklist else None
+
+ if error:
+ raise ValidationError(
+ error,
+ code="InvalidSql"
+ )
+
+
+~~class QueryForm(ModelForm):
+
+ sql = SqlField()
+ snapshot = BooleanField(widget=CheckboxInput, required=False)
+ connection = CharField(widget=Select, required=False)
+
+ def __init__(self, *args, **kwargs):
+ super(QueryForm, self).__init__(*args, **kwargs)
+ self.fields['connection'].widget.choices = self.connections
+ if not self.instance.connection:
+ self.initial['connection'] = EXPLORER_DEFAULT_CONNECTION
+ self.fields['connection'].widget.attrs['class'] = 'form-control'
+
+ def clean(self):
+ if self.instance and self.data.get('created_by_user', None):
+ self.cleaned_data['created_by_user'] = self.instance.created_by_user
+ return super(QueryForm, self).clean()
+
+ @property
+ def created_by_user_email(self):
+ return self.instance.created_by_user.email if self.instance.created_by_user else '--'
+
+ @property
+ def created_at_time(self):
+ return self.instance.created_at.strftime('%Y-%m-%d')
+
+
+## ... source file continues with no further ModelForm examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-modelmultiplechoicefield.markdown b/content/pages/examples/django/django-forms-modelmultiplechoicefield.markdown
new file mode 100644
index 000000000..6680b5dfc
--- /dev/null
+++ b/content/pages/examples/django/django-forms-modelmultiplechoicefield.markdown
@@ -0,0 +1,121 @@
+title: django.forms ModelMultipleChoiceField Example Code
+category: page
+slug: django-forms-modelmultiplechoicefield-examples
+sortorder: 500011274
+toc: False
+sidebartitle: django.forms ModelMultipleChoiceField
+meta: Python example code for the ModelMultipleChoiceField class from the django.forms module of the Django project.
+
+
+ModelMultipleChoiceField is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / templatetags / jet_tags.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/templatetags/jet_tags.py)
+
+```python
+# jet_tags.py
+from __future__ import unicode_literals
+import json
+import os
+from django import template
+try:
+ from django.core.urlresolvers import reverse
+except ImportError: # Django 1.11
+ from django.urls import reverse
+
+~~from django.forms import CheckboxInput, ModelChoiceField, Select, ModelMultipleChoiceField, SelectMultiple
+from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
+from django.utils.formats import get_format
+from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_text
+from jet import settings, VERSION
+from jet.models import Bookmark
+from jet.utils import get_model_instance_label, get_model_queryset, get_possible_language_codes, \
+ get_admin_site, get_menu_items
+
+try:
+ from urllib.parse import parse_qsl
+except ImportError:
+ from urlparse import parse_qsl
+
+
+register = template.Library()
+assignment_tag = register.assignment_tag if hasattr(register, 'assignment_tag') else register.simple_tag
+
+
+@assignment_tag
+def jet_get_date_format():
+ return get_format('DATE_INPUT_FORMATS')[0]
+
+
+
+
+## ... source file abbreviated to get to ModelMultipleChoiceField examples ...
+
+
+def jet_is_checkbox(field):
+ return field.field.widget.__class__.__name__ == CheckboxInput().__class__.__name__
+
+
+@register.filter
+def jet_select2_lookups(field):
+ if hasattr(field, 'field') and \
+ (isinstance(field.field, ModelChoiceField) or isinstance(field.field, ModelMultipleChoiceField)):
+ qs = field.field.queryset
+ model = qs.model
+
+ if getattr(model, 'autocomplete_search_fields', None) and getattr(field.field, 'autocomplete', True):
+ choices = []
+ app_label = model._meta.app_label
+ model_name = model._meta.object_name
+
+ attrs = {
+ 'class': 'ajax',
+ 'data-app-label': app_label,
+ 'data-model': model_name,
+ 'data-ajax--url': reverse('jet:model_lookup')
+ }
+
+ initial_value = field.value()
+
+~~ if hasattr(field, 'field') and isinstance(field.field, ModelMultipleChoiceField):
+ if initial_value:
+ initial_objects = model.objects.filter(pk__in=initial_value)
+ choices.extend(
+ [(initial_object.pk, get_model_instance_label(initial_object))
+ for initial_object in initial_objects]
+ )
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+ field.field.widget.widget = SelectMultiple(attrs)
+ else:
+ field.field.widget = SelectMultiple(attrs)
+ field.field.choices = choices
+ elif hasattr(field, 'field') and isinstance(field.field, ModelChoiceField):
+ if initial_value:
+ try:
+ initial_object = model.objects.get(pk=initial_value)
+ attrs['data-object-id'] = initial_value
+ choices.append((initial_object.pk, get_model_instance_label(initial_object)))
+ except model.DoesNotExist:
+ pass
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+ field.field.widget.widget = Select(attrs)
+ else:
+
+
+## ... source file continues with no further ModelMultipleChoiceField examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-multiplechoicefield.markdown b/content/pages/examples/django/django-forms-multiplechoicefield.markdown
new file mode 100644
index 000000000..2faa61d74
--- /dev/null
+++ b/content/pages/examples/django/django-forms-multiplechoicefield.markdown
@@ -0,0 +1,269 @@
+title: django.forms MultipleChoiceField Example Code
+category: page
+slug: django-forms-multiplechoicefield-examples
+sortorder: 500011275
+toc: False
+sidebartitle: django.forms MultipleChoiceField
+meta: Python example code for the MultipleChoiceField class from the django.forms module of the Django project.
+
+
+MultipleChoiceField is a class within the django.forms module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / chair / forms.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/chair/forms.py)
+
+```python
+# forms.py
+from functools import reduce
+
+from django import forms
+from django.contrib.auth import get_user_model
+from django.db import models
+from django.db.models import Q, F, Count, Max, Subquery, OuterRef, Value
+from django.db.models.functions import Concat
+~~from django.forms import MultipleChoiceField, ChoiceField, Form
+from django.urls import reverse
+from django.utils.translation import ugettext_lazy as _
+
+from django_countries import countries
+
+from conferences.models import Conference, ArtifactDescriptor
+from gears.widgets import CustomCheckboxSelectMultiple, CustomFileInput
+from review.models import Reviewer, Review, ReviewStats
+from review.utilities import get_average_score
+from submissions.models import Submission, Attachment
+from users.models import Profile
+
+User = get_user_model()
+
+
+def clean_data_to_int(iterable, empty=None):
+ return [int(x) if x != '' else None for x in iterable]
+
+
+def q_or(disjuncts, default=True):
+ if disjuncts:
+ return reduce(lambda acc, d: acc | d, disjuncts)
+ return Q(pk__isnull=(not default)) # otherwise, check whether PK is null
+
+
+
+## ... source file abbreviated to get to MultipleChoiceField examples ...
+
+
+ )
+
+ Q1 = 'Q1'
+ Q2 = 'Q2'
+ Q3 = 'Q3'
+ Q4 = 'Q4'
+ QUARTILE_CHOICES = ((Q1, 'Q1'), (Q2, 'Q2'), (Q3, 'Q3'), (Q4, 'Q4'))
+
+ ORDER_BY_PK = 'PK'
+ ORDER_BY_TITLE = 'TITLE'
+ ORDER_BY_SCORE = 'SCORE'
+ ORDER_CHOICES = (
+ (ORDER_BY_PK, 'Order by ID'),
+ (ORDER_BY_SCORE, 'Order by score'),
+ (ORDER_BY_TITLE, 'Order by title'),
+ )
+
+ DIRECTION_CHOICES = (('ASC', 'Ascending'), ('DESC', 'Descending'))
+
+ class Meta:
+ model = Conference
+ fields = []
+
+ term = forms.CharField(required=False)
+
+~~ completion = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple(attrs={
+ 'btn_class': 'btn btn-link dccn-link dccn-text-small',
+ 'label_class': 'dccn-text-0',
+ }), required=False, choices=COMPLETION_CHOICES)
+
+~~ types = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False)
+
+~~ topics = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False)
+
+~~ status = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ choices=Submission.STATUS_CHOICE)
+
+~~ countries = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False)
+
+~~ affiliations = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False)
+
+~~ proc_types = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple(
+ attrs={'label': 'Proceedings'}), required=False)
+
+~~ volumes = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False)
+
+~~ quartiles = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ choices=QUARTILE_CHOICES)
+
+~~ artifacts = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False)
+
+ order = ChoiceField(required=False, choices=ORDER_CHOICES)
+ direction = ChoiceField(required=False, choices=DIRECTION_CHOICES)
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ assert isinstance(self.instance, Conference)
+ self.fields['types'].choices = [
+ (x.pk, x.name) for x in self.instance.submissiontype_set.all()]
+ self.fields['topics'].choices = [
+ (x.pk, x.name) for x in self.instance.topic_set.all()]
+ self.fields['proc_types'].choices = [('', 'Not defined')] + [
+ (x.pk, x.name) for x in self.instance.proceedingtype_set.all()]
+ self.fields['volumes'].choices = [('', 'Not defined')] + [
+ (vol_pk, vol_name) for (vol_pk, vol_name) in
+ self.instance.proceedingtype_set.values_list(
+ 'volumes__pk', 'volumes__name').distinct()]
+ self.fields['artifacts'].choices = [
+ (x.pk, f'{x.name} ({x.proc_type.name}') for x in
+ ArtifactDescriptor.objects.filter(
+ proc_type__conference=self.instance)]
+
+ profiles_data = Profile.objects.filter(
+
+
+## ... source file abbreviated to get to MultipleChoiceField examples ...
+
+
+ term = forms.CharField(required=False)
+
+ authorship = forms.MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ choices=AUTHORSHIP_CHOICES,
+ )
+
+ countries = forms.MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ )
+
+ affiliations = forms.MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ )
+
+ graduation = forms.MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ choices=GRADUATION_CHOICES,
+ )
+
+ membership = forms.MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ choices=MEMBERSHIP_CHOICES,
+ )
+
+~~ reviewer = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple, required=False,
+ choices=REVIEWER_CHOICES)
+
+ order = ChoiceField(required=False, choices=ORDER_CHOICES)
+ direction = ChoiceField(required=False, choices=DIRECTION_CHOICES)
+
+ columns = forms.MultipleChoiceField(
+ required=False, choices=COLUMNS,
+ widget=CustomCheckboxSelectMultiple
+ )
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ assert isinstance(self.instance, Conference)
+ countries_dict = dict(countries)
+
+ self.fields['countries'].choices = [
+ (code, countries_dict[code]) for code in
+ Profile.objects.filter(country__isnull=False).values_list(
+ 'country', flat=True).order_by('country').distinct()]
+
+ self.fields['affiliations'].choices = [
+ (aff, aff) for aff in
+ Profile.objects.values_list('affiliation', flat=True).order_by(
+
+
+## ... source file abbreviated to get to MultipleChoiceField examples ...
+
+
+class ExportSubmissionsForm(Form):
+ ORDER_COLUMN = '#'
+ ID_COLUMN = 'ID'
+ AUTHORS_COLUMN = 'AUTHORS'
+ TITLE_COLUMN = 'TITLE'
+ COUNTRY_COLUMN = 'COUNTRY'
+ STYPE_COLUMN = 'TYPE'
+ REVIEW_PAPER_COLUMN = 'REVIEW_MANUSCRIPT'
+ REVIEW_SCORE_COLUMN = 'REVIEW_SCORE'
+ STATUS_COLUMN = 'STATUS'
+ TOPICS_COLUMN = 'TOPICS'
+
+ COLUMNS = (
+ (ORDER_COLUMN, ORDER_COLUMN),
+ (ID_COLUMN, ID_COLUMN),
+ (TITLE_COLUMN, TITLE_COLUMN),
+ (AUTHORS_COLUMN, AUTHORS_COLUMN),
+ (COUNTRY_COLUMN, COUNTRY_COLUMN),
+ (STYPE_COLUMN, STYPE_COLUMN),
+ (REVIEW_PAPER_COLUMN, REVIEW_PAPER_COLUMN),
+ (REVIEW_SCORE_COLUMN, REVIEW_SCORE_COLUMN),
+ (STATUS_COLUMN, STATUS_COLUMN),
+ (TOPICS_COLUMN, TOPICS_COLUMN),
+ )
+
+~~ columns = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple(hide_apply_btn=True),
+ required=False, choices=COLUMNS)
+
+~~ status = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple(hide_apply_btn=True),
+ required=False, choices=Submission.STATUS_CHOICE)
+
+~~ countries = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple(hide_apply_btn=True),
+ required=False)
+
+~~ topics = MultipleChoiceField(
+ widget=CustomCheckboxSelectMultiple(hide_apply_btn=True),
+ required=False)
+
+ def __init__(self, *args, conference=None, **kwargs):
+ super().__init__(*args, **kwargs)
+ if conference is None:
+ raise ValueError('conference must be provided')
+ self.conference = conference
+ self.fields['columns'].initial = [
+ self.ORDER_COLUMN, self.ID_COLUMN, self.TITLE_COLUMN,
+ self.AUTHORS_COLUMN, self.STATUS_COLUMN]
+ countries_list = list(
+ set(p.country for p in Profile.objects.all() if p.country))
+ countries_list.sort(key=lambda cnt: cnt.name)
+ self.fields['countries'].choices = [
+ (cnt.code, cnt.name) for cnt in countries_list]
+ self.fields['topics'].choices = [
+ (t.pk, t.name) for t in self.conference.topic_set.all()]
+
+ def apply(self, request):
+ submissions = Submission.objects.filter(conference=self.conference)
+ if self.cleaned_data['status']:
+ submissions = submissions.filter(
+ status__in=self.cleaned_data['status'])
+
+
+## ... source file continues with no further MultipleChoiceField examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-select.markdown b/content/pages/examples/django/django-forms-select.markdown
new file mode 100644
index 000000000..9a7f7ca44
--- /dev/null
+++ b/content/pages/examples/django/django-forms-select.markdown
@@ -0,0 +1,195 @@
+title: django.forms Select Example Code
+category: page
+slug: django-forms-select-examples
+sortorder: 500011276
+toc: False
+sidebartitle: django.forms Select
+meta: Python example code for the Select class from the django.forms module of the Django project.
+
+
+Select is a class within the django.forms module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / gears / widgets.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/gears/widgets.py)
+
+```python
+# widgets.py
+~~from django.forms import FileInput, CheckboxSelectMultiple, Select
+
+
+class CustomFileInput(FileInput):
+ template_name = 'gears/widgets/file_input.html'
+ accept = ''
+ show_file_name = True
+
+
+class CustomCheckboxSelectMultiple(CheckboxSelectMultiple):
+ template_name = 'gears/widgets/checkbox_multiple_select.html'
+ hide_label = False
+ hide_apply_btn = False
+
+ class Media:
+ js = ('gears/js/checkbox_multiple_select.js',)
+
+ def __init__(self, *args, **kwargs):
+ self.hide_label = kwargs.pop('hide_label', False)
+ self.hide_apply_btn = kwargs.pop('hide_apply_btn', False)
+ super().__init__(*args, **kwargs)
+
+ def get_context(self, name, value, attrs):
+ context = super().get_context(name, value, attrs)
+ context['widget'].update({
+ 'hide_label': self.hide_label,
+ 'hide_apply_btn': self.hide_apply_btn,
+ })
+ return context
+
+
+~~class DropdownSelectSubmit(Select):
+ template_name = 'gears/widgets/dropdown_select_submit.html'
+ empty_label = 'Not selected'
+ label_class = ''
+ empty_label_class = 'text-warning'
+ nonempty_label_class = 'text-success'
+
+ class Media:
+ js = ('gears/js/dropdown_select_submit.js',)
+
+ def __init__(self, *args, **kwargs):
+ self.empty_label = kwargs.pop('empty_label', 'Not selected')
+ self.label_class = kwargs.pop('label_class', '')
+ self.empty_label_class = kwargs.pop('empty_label_class', 'text-warning')
+ self.nonempty_label_class = kwargs.pop(
+ 'nonempty_label_class', 'text-success')
+ super().__init__(*args, **kwargs)
+
+ def get_context(self, name, value, attrs):
+ context = super().get_context(name, value, attrs)
+ widget = context['widget']
+ widget['label_class'] = self.label_class
+ widget['empty_label_class'] = self.empty_label_class
+ widget['nonempty_label_class'] = self.nonempty_label_class
+ widget['label'] = self.empty_label
+
+
+## ... source file continues with no further Select examples...
+
+```
+
+
+## Example 2 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / templatetags / jet_tags.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/templatetags/jet_tags.py)
+
+```python
+# jet_tags.py
+from __future__ import unicode_literals
+import json
+import os
+from django import template
+try:
+ from django.core.urlresolvers import reverse
+except ImportError: # Django 1.11
+ from django.urls import reverse
+
+~~from django.forms import CheckboxInput, ModelChoiceField, Select, ModelMultipleChoiceField, SelectMultiple
+from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
+from django.utils.formats import get_format
+from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_text
+from jet import settings, VERSION
+from jet.models import Bookmark
+from jet.utils import get_model_instance_label, get_model_queryset, get_possible_language_codes, \
+ get_admin_site, get_menu_items
+
+try:
+ from urllib.parse import parse_qsl
+except ImportError:
+ from urlparse import parse_qsl
+
+
+register = template.Library()
+assignment_tag = register.assignment_tag if hasattr(register, 'assignment_tag') else register.simple_tag
+
+
+@assignment_tag
+def jet_get_date_format():
+ return get_format('DATE_INPUT_FORMATS')[0]
+
+
+
+
+## ... source file abbreviated to get to Select examples ...
+
+
+ initial_value = field.value()
+
+ if hasattr(field, 'field') and isinstance(field.field, ModelMultipleChoiceField):
+ if initial_value:
+ initial_objects = model.objects.filter(pk__in=initial_value)
+ choices.extend(
+ [(initial_object.pk, get_model_instance_label(initial_object))
+ for initial_object in initial_objects]
+ )
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+ field.field.widget.widget = SelectMultiple(attrs)
+ else:
+ field.field.widget = SelectMultiple(attrs)
+ field.field.choices = choices
+ elif hasattr(field, 'field') and isinstance(field.field, ModelChoiceField):
+ if initial_value:
+ try:
+ initial_object = model.objects.get(pk=initial_value)
+ attrs['data-object-id'] = initial_value
+ choices.append((initial_object.pk, get_model_instance_label(initial_object)))
+ except model.DoesNotExist:
+ pass
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+~~ field.field.widget.widget = Select(attrs)
+ else:
+~~ field.field.widget = Select(attrs)
+ field.field.choices = choices
+
+ return field
+
+
+@assignment_tag(takes_context=True)
+def jet_get_current_theme(context):
+ if 'request' in context and 'JET_THEME' in context['request'].COOKIES:
+ theme = context['request'].COOKIES['JET_THEME']
+ if isinstance(settings.JET_THEMES, list) and len(settings.JET_THEMES) > 0:
+ for conf_theme in settings.JET_THEMES:
+ if isinstance(conf_theme, dict) and conf_theme.get('theme') == theme:
+ return theme
+ return settings.JET_DEFAULT_THEME
+
+
+@assignment_tag
+def jet_get_themes():
+ return settings.JET_THEMES
+
+
+@assignment_tag
+def jet_get_current_version():
+ return VERSION
+
+
+## ... source file continues with no further Select examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-selectmultiple.markdown b/content/pages/examples/django/django-forms-selectmultiple.markdown
new file mode 100644
index 000000000..571f1b1ce
--- /dev/null
+++ b/content/pages/examples/django/django-forms-selectmultiple.markdown
@@ -0,0 +1,123 @@
+title: django.forms SelectMultiple Example Code
+category: page
+slug: django-forms-selectmultiple-examples
+sortorder: 500011277
+toc: False
+sidebartitle: django.forms SelectMultiple
+meta: Python example code for the SelectMultiple class from the django.forms module of the Django project.
+
+
+SelectMultiple is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-jet
+[django-jet](https://github.com/geex-arts/django-jet)
+([project documentation](https://jet.readthedocs.io/en/latest/),
+[PyPI project page](https://pypi.org/project/django-jet/) and
+[more information](http://jet.geex-arts.com/))
+is a fancy [Django](/django.html) Admin panel replacement.
+
+The django-jet project is open source under the
+[GNU Affero General Public License v3.0](https://github.com/geex-arts/django-jet/blob/dev/LICENSE).
+
+[**django-jet / jet / templatetags / jet_tags.py**](https://github.com/geex-arts/django-jet/blob/dev/jet/templatetags/jet_tags.py)
+
+```python
+# jet_tags.py
+from __future__ import unicode_literals
+import json
+import os
+from django import template
+try:
+ from django.core.urlresolvers import reverse
+except ImportError: # Django 1.11
+ from django.urls import reverse
+
+~~from django.forms import CheckboxInput, ModelChoiceField, Select, ModelMultipleChoiceField, SelectMultiple
+from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
+from django.utils.formats import get_format
+from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_text
+from jet import settings, VERSION
+from jet.models import Bookmark
+from jet.utils import get_model_instance_label, get_model_queryset, get_possible_language_codes, \
+ get_admin_site, get_menu_items
+
+try:
+ from urllib.parse import parse_qsl
+except ImportError:
+ from urlparse import parse_qsl
+
+
+register = template.Library()
+assignment_tag = register.assignment_tag if hasattr(register, 'assignment_tag') else register.simple_tag
+
+
+@assignment_tag
+def jet_get_date_format():
+ return get_format('DATE_INPUT_FORMATS')[0]
+
+
+
+
+## ... source file abbreviated to get to SelectMultiple examples ...
+
+
+ model = qs.model
+
+ if getattr(model, 'autocomplete_search_fields', None) and getattr(field.field, 'autocomplete', True):
+ choices = []
+ app_label = model._meta.app_label
+ model_name = model._meta.object_name
+
+ attrs = {
+ 'class': 'ajax',
+ 'data-app-label': app_label,
+ 'data-model': model_name,
+ 'data-ajax--url': reverse('jet:model_lookup')
+ }
+
+ initial_value = field.value()
+
+ if hasattr(field, 'field') and isinstance(field.field, ModelMultipleChoiceField):
+ if initial_value:
+ initial_objects = model.objects.filter(pk__in=initial_value)
+ choices.extend(
+ [(initial_object.pk, get_model_instance_label(initial_object))
+ for initial_object in initial_objects]
+ )
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+~~ field.field.widget.widget = SelectMultiple(attrs)
+ else:
+~~ field.field.widget = SelectMultiple(attrs)
+ field.field.choices = choices
+ elif hasattr(field, 'field') and isinstance(field.field, ModelChoiceField):
+ if initial_value:
+ try:
+ initial_object = model.objects.get(pk=initial_value)
+ attrs['data-object-id'] = initial_value
+ choices.append((initial_object.pk, get_model_instance_label(initial_object)))
+ except model.DoesNotExist:
+ pass
+
+ if isinstance(field.field.widget, RelatedFieldWidgetWrapper):
+ field.field.widget.widget = Select(attrs)
+ else:
+ field.field.widget = Select(attrs)
+ field.field.choices = choices
+
+ return field
+
+
+@assignment_tag(takes_context=True)
+def jet_get_current_theme(context):
+ if 'request' in context and 'JET_THEME' in context['request'].COOKIES:
+ theme = context['request'].COOKIES['JET_THEME']
+ if isinstance(settings.JET_THEMES, list) and len(settings.JET_THEMES) > 0:
+
+
+## ... source file continues with no further SelectMultiple examples...
+
+```
+
diff --git a/content/pages/examples/django/django-forms-typedchoicefield.markdown b/content/pages/examples/django/django-forms-typedchoicefield.markdown
index 23eab0e9f..10fb85a86 100644
--- a/content/pages/examples/django/django-forms-typedchoicefield.markdown
+++ b/content/pages/examples/django/django-forms-typedchoicefield.markdown
@@ -1,7 +1,7 @@
title: django.forms TypedChoiceField Python Code Examples
category: page
slug: django-forms-typedchoicefield-examples
-sortorder: 500013125
+sortorder: 500013129
toc: False
sidebartitle: django.forms TypedChoiceField
meta: View code examples that show how to use the TypedChoiceField class within the forms module of Django.
diff --git a/content/pages/examples/django/django-forms-validationerror.markdown b/content/pages/examples/django/django-forms-validationerror.markdown
new file mode 100644
index 000000000..59a18ace7
--- /dev/null
+++ b/content/pages/examples/django/django-forms-validationerror.markdown
@@ -0,0 +1,502 @@
+title: django.forms ValidationError Example Code
+category: page
+slug: django-forms-validationerror-examples
+sortorder: 500011278
+toc: False
+sidebartitle: django.forms ValidationError
+meta: Python example code for the ValidationError class from the django.forms module of the Django project.
+
+
+ValidationError is a class within the django.forms module of the Django project.
+
+
+## Example 1 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / socialaccount / helpers.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/helpers.py)
+
+```python
+# helpers.py
+from django.contrib import messages
+~~from django.forms import ValidationError
+from django.http import HttpResponseRedirect
+from django.shortcuts import render
+from django.urls import reverse
+
+from allauth.account import app_settings as account_settings
+from allauth.account.adapter import get_adapter as get_account_adapter
+from allauth.account.utils import complete_signup, perform_login, user_username
+from allauth.exceptions import ImmediateHttpResponse
+
+from . import app_settings, signals
+from .adapter import get_adapter
+from .models import SocialLogin
+from .providers.base import AuthError, AuthProcess
+
+
+def _process_signup(request, sociallogin):
+ auto_signup = get_adapter(request).is_auto_signup_allowed(
+ request,
+ sociallogin)
+ if not auto_signup:
+ request.session['socialaccount_sociallogin'] = sociallogin.serialize()
+ url = reverse('socialaccount_signup')
+ ret = HttpResponseRedirect(url)
+ else:
+ if account_settings.USER_MODEL_USERNAME_FIELD:
+ username = user_username(sociallogin.user)
+ try:
+ get_account_adapter(request).clean_username(username)
+~~ except ValidationError:
+ user_username(sociallogin.user, '')
+ if not get_adapter(request).is_open_for_signup(
+ request,
+ sociallogin):
+ return render(
+ request,
+ "account/signup_closed." +
+ account_settings.TEMPLATE_EXTENSION)
+ get_adapter(request).save_user(request, sociallogin, form=None)
+ ret = complete_social_signup(request, sociallogin)
+ return ret
+
+
+def _login_social_account(request, sociallogin):
+ return perform_login(request, sociallogin.user,
+ email_verification=app_settings.EMAIL_VERIFICATION,
+ redirect_url=sociallogin.get_redirect_url(request),
+ signal_kwargs={"sociallogin": sociallogin})
+
+
+def render_authentication_error(request,
+ provider_id,
+ error=AuthError.UNKNOWN,
+ exception=None,
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 2 from django-jsonfield
+[django-jsonfield](https://github.com/dmkoch/django-jsonfield)
+([jsonfield on PyPi](https://pypi.org/project/jsonfield/)) is a
+[Django](/django.html) code library that makes it easier to store validated
+JSON in a [Django object-relational mapper (ORM)](/django-orm.html) database
+model.
+
+The django-jsonfield project is open source under the
+[MIT license](https://github.com/dmkoch/django-jsonfield/blob/master/LICENSE).
+
+[**django-jsonfield / src/jsonfield / fields.py**](https://github.com/dmkoch/django-jsonfield/blob/master/src/jsonfield/./fields.py)
+
+```python
+# fields.py
+import copy
+import json
+import warnings
+
+from django.db import models
+~~from django.forms import ValidationError
+from django.utils.translation import gettext_lazy as _
+
+from . import forms
+from .encoder import JSONEncoder
+from .json import JSONString, checked_loads
+
+DEFAULT_DUMP_KWARGS = {
+ 'cls': JSONEncoder,
+}
+
+DEFAULT_LOAD_KWARGS = {}
+
+INVALID_JSON_WARNING = (
+ '{0!s} failed to load invalid json ({1}) from the database. The value has '
+ 'been returned as a string instead.'
+)
+
+
+class JSONFieldMixin(models.Field):
+ form_class = forms.JSONField
+
+ def __init__(self, *args, dump_kwargs=None, load_kwargs=None, **kwargs):
+ self.dump_kwargs = DEFAULT_DUMP_KWARGS if dump_kwargs is None else dump_kwargs
+ self.load_kwargs = DEFAULT_LOAD_KWARGS if load_kwargs is None else load_kwargs
+
+ super().__init__(*args, **kwargs)
+
+ def deconstruct(self):
+ name, path, args, kwargs = super().deconstruct()
+
+ if self.dump_kwargs != DEFAULT_DUMP_KWARGS:
+ kwargs['dump_kwargs'] = self.dump_kwargs
+ if self.load_kwargs != DEFAULT_LOAD_KWARGS:
+ kwargs['load_kwargs'] = self.load_kwargs
+
+ return name, path, args, kwargs
+
+ def to_python(self, value):
+ try:
+ return checked_loads(value, **self.load_kwargs)
+ except ValueError:
+~~ raise ValidationError(_("Enter valid JSON."))
+
+ def from_db_value(self, value, expression, connection):
+ if value is None:
+ return None
+
+ try:
+ return checked_loads(value, **self.load_kwargs)
+ except json.JSONDecodeError:
+ warnings.warn(INVALID_JSON_WARNING.format(self, value), RuntimeWarning)
+ return JSONString(value)
+
+ def get_prep_value(self, value):
+ if self.null and value is None:
+ return None
+ return json.dumps(value, **self.dump_kwargs)
+
+ def value_to_string(self, obj):
+ value = self.value_from_object(obj)
+ return json.dumps(value, **self.dump_kwargs)
+
+ def formfield(self, **kwargs):
+ kwargs.setdefault('form_class', self.form_class)
+ if issubclass(kwargs['form_class'], forms.JSONField):
+ kwargs.setdefault('dump_kwargs', self.dump_kwargs)
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 3 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / forms.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./forms.py)
+
+```python
+# forms.py
+from django.db import DatabaseError
+~~from django.forms import ModelForm, Field, ValidationError, BooleanField, CharField
+from django.forms.widgets import CheckboxInput, Select
+
+from explorer.app_settings import EXPLORER_DEFAULT_CONNECTION, EXPLORER_CONNECTIONS
+from explorer.models import Query, MSG_FAILED_BLACKLIST
+
+
+class SqlField(Field):
+
+ def validate(self, value):
+
+ query = Query(sql=value)
+
+ passes_blacklist, failing_words = query.passes_blacklist()
+
+ error = MSG_FAILED_BLACKLIST % ', '.join(failing_words) if not passes_blacklist else None
+
+ if error:
+~~ raise ValidationError(
+ error,
+ code="InvalidSql"
+ )
+
+
+class QueryForm(ModelForm):
+
+ sql = SqlField()
+ snapshot = BooleanField(widget=CheckboxInput, required=False)
+ connection = CharField(widget=Select, required=False)
+
+ def __init__(self, *args, **kwargs):
+ super(QueryForm, self).__init__(*args, **kwargs)
+ self.fields['connection'].widget.choices = self.connections
+ if not self.instance.connection:
+ self.initial['connection'] = EXPLORER_DEFAULT_CONNECTION
+ self.fields['connection'].widget.attrs['class'] = 'form-control'
+
+ def clean(self):
+ if self.instance and self.data.get('created_by_user', None):
+ self.cleaned_data['created_by_user'] = self.instance.created_by_user
+ return super(QueryForm, self).clean()
+
+ @property
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
+
+## Example 4 from register
+[register](https://github.com/ORGAN-IZE/register) is a [Django](/django.html),
+[Bootstrap](/bootstrap-css.html), [PostgreSQL](/postgresql.html) project that is
+open source under the
+[GNU General Public License v3.0](https://github.com/ORGAN-IZE/register/blob/master/LICENSE).
+This web application makes it easier for people to register as organ donors.
+You can see the application live at
+[https://register.organize.org/](https://register.organize.org/).
+
+[**register / registration / forms.py**](https://github.com/ORGAN-IZE/register/blob/master/registration/./forms.py)
+
+```python
+# forms.py
+from __future__ import unicode_literals
+
+import logging
+import re
+import collections
+import datetime
+
+~~import django.forms
+~~import django.forms.utils
+~~import django.forms.widgets
+import django.core.validators
+import django.core.exceptions
+from django.conf import settings
+from django.utils.translation import ugettext_lazy as _
+from django.utils.safestring import mark_safe
+
+import form_utils.forms
+import requests
+import dateutil.parser
+import validate_email
+
+logger = logging.getLogger(__name__)
+
+
+REGISTRATION_CONFIGURATION_NAME = 'registration_configuration'
+
+RE_NON_DECIMAL = re.compile(r'[^\d]+')
+RE_NON_ALPHA = re.compile('[\W]+')
+RE_POSTAL_CODE = re.compile(r'^[0-9]{5}$')
+validate_postal_code = django.core.validators.RegexValidator(
+ RE_POSTAL_CODE, _("Enter a valid postal code consisting 5 numbers."), 'invalid')
+
+
+CHOICES_GENDER = (
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+class StateLookupForm(django.forms.Form):
+ email = django.forms.EmailField(label=_('Email'), help_text=_('so we can send you confirmation of your registration'))
+ postal_code = django.forms.CharField(
+ label=_('Postal Code'),
+ max_length=5, min_length=5, validators=[validate_postal_code],
+ help_text=_('to determine which series of state-based questions we will ask next'))
+
+ def clean_email(self):
+ email = self.cleaned_data['email']
+ if settings.DISABLE_EMAIL_VALIDATION:
+ logger.warning('Email validation disabled: DISABLE_EMAIL_VALIDATION '
+ 'is set')
+ return email
+ if not hasattr(settings, 'MAILGUN_PUBLIC_API_KEY'):
+ logger.warning(
+ 'Cannot validate email: MAILGUN_PUBLIC_API_KEY not set')
+ return email
+ r = requests.get(
+ 'https://api.mailgun.net/v2/address/validate',
+ data={'address': email, },
+ auth=('api', settings.MAILGUN_PUBLIC_API_KEY))
+ if r.status_code == 200:
+ if r.json()['is_valid']:
+ return email
+ logger.warning('Cannot validate email: {}'.format(r.text))
+~~ raise django.forms.ValidationError(_('Enter a valid email.'))
+
+
+class UPENNStateLookupForm(StateLookupForm):
+ error_css_class = 'invalid-data-error'
+ email = django.forms.EmailField(label='')
+ postal_code = django.forms.CharField(label='', max_length=5, min_length=5, validators=[validate_postal_code])
+
+ email.widget.attrs['placeholder'] = 'EMAIL'
+ email.widget.attrs['class'] = 'upenn-text-field'
+
+ postal_code.widget.attrs['placeholder'] = 'POSTAL CODE'
+ postal_code.widget.attrs['class'] = 'upenn-text-field'
+
+
+def register_form_clean(self):
+ cleaned_data = super(self.__class__, self).clean()
+
+ if self.validate_organ_tissue_selection:
+ organ_choices = [value for key, value in cleaned_data.items() if key.startswith('include')]
+ if organ_choices:
+ if not any(organ_choices):
+~~ raise django.forms.ValidationError(_('At least one organ/tissue needs to be selected'))
+
+ if self.api_errors and not self.skip_api_error_validation:
+ for k, v in self.api_errors.items():
+ if k in self.fields:
+ self.add_error(k, v)
+
+ return cleaned_data
+
+
+def register_form_clean_license_id(self):
+ license_id = self.cleaned_data['license_id']
+ if self.fields['license_id'].required and is_license_id_not_applicable(license_id):
+~~ raise django.forms.ValidationError(_('License ID is required.'))
+ return license_id
+
+
+def is_license_id_not_applicable(license_id):
+ not_applicable_list = ['na', 'n/a', ]
+ if license_id.lower() in not_applicable_list:
+ return True
+ return False
+
+
+def register_form_clean_birthdate(self):
+ date = self.cleaned_data['birthdate']
+ if not date:
+ return date
+ if date >= datetime.date.today():
+~~ raise django.forms.ValidationError(_('Enter an accurate birthdate.'))
+ return date
+
+
+def register_form_clean_phone_number(self):
+ phone_number = self.cleaned_data['phone_number']
+ if not phone_number:
+ return phone_number
+ phone_number = RE_NON_DECIMAL.sub('', phone_number)
+ if phone_number.startswith('1'):
+ phone_number = phone_number[1:]
+ if len(phone_number) != 10:
+~~ raise django.forms.ValidationError(
+ _('Enter an accurate phone number including area code.'))
+ return phone_number
+
+
+def register_form_clean_ssn(self):
+ ssn = self.cleaned_data['ssn']
+ if not ssn:
+ return ssn
+ if len(ssn) != 4:
+~~ raise django.forms.ValidationError(
+ _('Enter the last 4 digits of your social security number.'))
+ try:
+ int(ssn)
+ except ValueError:
+~~ raise django.forms.ValidationError(_('Enter only digits.'))
+ return ssn
+
+
+def validate_date_generator(min_value):
+ min_value = dateutil.parser.parse(min_value).date()
+
+ def validate_date(date):
+ if date < min_value:
+~~ raise django.forms.ValidationError(
+ _('Date must be later than %(date)s.') %
+ {'date': min_value.strftime('%m/%d/%Y'), },
+ code='minimum')
+
+ return validate_date
+
+
+def register_form_generator(conf):
+ fieldsets = []
+ fields = collections.OrderedDict()
+ for index, fieldset_def in enumerate(conf['fieldsets']):
+ fieldset_title = _(fieldset_def['title'])
+ fieldset_fields = fieldset_def['fields']
+
+ if not fieldset_fields:
+ continue
+ fieldset = (unicode(index), {'legend': fieldset_title, 'fields': []}, )
+
+ has_booleans = False
+
+ for field_def in fieldset_def['fields']:
+ field_name = field_def['field_name']
+ field_type = field_def.get('type')
+ label = _(field_def['human_name']) or ''
+
+
+## ... source file abbreviated to get to ValidationError examples ...
+
+
+ widget=django.forms.RadioSelect)
+ birthdate = django.forms.DateField(
+ label=_('Birthdate'),
+ widget=django.forms.DateInput(
+ attrs={'placeholder': '__/__/____', 'class': 'date',}))
+ agree_to_tos = django.forms.BooleanField(label='', widget=django.forms.widgets.CheckboxInput(attrs={'required': 'required', }))
+
+ def clean_email(self):
+ email = self.cleaned_data['email']
+ if settings.DISABLE_EMAIL_VALIDATION:
+ logger.warning(
+ 'Email validation disabled: DISABLE_EMAIL_VALIDATION is set')
+ return email
+ if not hasattr(settings, 'MAILGUN_PUBLIC_API_KEY'):
+ logger.warning(
+ 'Cannot validate email: MAILGUN_PUBLIC_API_KEY not set')
+ return email
+ r = requests.get(
+ 'https://api.mailgun.net/v2/address/validate',
+ data={'address': email, },
+ auth=('api', settings.MAILGUN_PUBLIC_API_KEY))
+ if r.status_code == 200:
+ if r.json()['is_valid']:
+ return email
+ logger.warning('Cannot validate email: {}'.format(r.text))
+~~ raise django.forms.ValidationError(_('Enter a valid email.'))
+
+
+class EmailNextOfKinForm(django.forms.Form):
+ to = MultiEmailField(label=_('To'), max_length=300, help_text=_('Enter one or more emails separated by commas.'))
+ subject = django.forms.CharField(label=_('Subject'), max_length=250)
+ body = django.forms.CharField(label=_('Body'), widget=django.forms.widgets.Textarea())
+
+ def clean_to(self):
+ emails = self.cleaned_data['to']
+ if settings.DISABLE_EMAIL_VALIDATION:
+ logger.warning('Email validation disabled: DISABLE_EMAIL_VALIDATION is set')
+ return emails
+ if not hasattr(settings, 'MAILGUN_PUBLIC_API_KEY'):
+ logger.warning('Cannot validate email: MAILGUN_PUBLIC_API_KEY not set')
+ return emails
+ valid_emails = []
+ invalid_emails = []
+ for email in emails:
+ r = requests.get('https://api.mailgun.net/v2/address/validate',
+ data={'address': email, },
+ auth=('api', settings.MAILGUN_PUBLIC_API_KEY))
+ if r.status_code == 200 and r.json()['is_valid']:
+ valid_emails.append(email)
+ else:
+ logger.warning('Cannot validate email: {}'.format(r.text))
+ invalid_emails.append(email)
+ if invalid_emails:
+~~ raise django.forms.ValidationError(_('Enter valid email addresses.'))
+ else:
+ return valid_emails
+
+
+
+## ... source file continues with no further ValidationError examples...
+
+```
+
diff --git a/content/pages/examples/django/django-shortcuts-get-list-or-404.markdown b/content/pages/examples/django/django-shortcuts-get-list-or-404.markdown
new file mode 100644
index 000000000..70a07e08a
--- /dev/null
+++ b/content/pages/examples/django/django-shortcuts-get-list-or-404.markdown
@@ -0,0 +1,125 @@
+title: django.shortcuts get_list_or_404 Example Code
+category: page
+slug: django-shortcuts-get-list-or-404-examples
+sortorder: 500011345
+toc: False
+sidebartitle: django.shortcuts get_list_or_404
+meta: Python example code for the get_list_or_404 callable from the django.shortcuts module of the Django project.
+
+
+get_list_or_404 is a callable within the django.shortcuts module of the Django project.
+
+
+## Example 1 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / admin / placeholderadmin.py**](https://github.com/divio/django-cms/blob/develop/cms/admin/placeholderadmin.py)
+
+```python
+# placeholderadmin.py
+import uuid
+import warnings
+
+from django.conf.urls import url
+from django.contrib.admin.helpers import AdminForm
+from django.contrib.admin.utils import get_deleted_objects
+from django.core.exceptions import PermissionDenied
+from django.db import router, transaction
+from django.http import (
+ HttpResponse,
+ HttpResponseBadRequest,
+ HttpResponseForbidden,
+ HttpResponseNotFound,
+ HttpResponseRedirect,
+)
+~~from django.shortcuts import get_list_or_404, get_object_or_404, render
+from django.template.response import TemplateResponse
+from django.utils.decorators import method_decorator
+from django.utils.encoding import force_text
+from django.utils import translation
+from django.utils.translation import ugettext as _
+from django.views.decorators.clickjacking import xframe_options_sameorigin
+from django.views.decorators.http import require_POST
+
+from six.moves.urllib.parse import parse_qsl, urlparse
+
+from six import get_unbound_function, get_method_function
+
+from cms import operations
+from cms.admin.forms import PluginAddValidationForm
+from cms.constants import SLUG_REGEXP
+from cms.exceptions import PluginLimitReached
+from cms.models.placeholdermodel import Placeholder
+from cms.models.placeholderpluginmodel import PlaceholderReference
+from cms.models.pluginmodel import CMSPlugin
+from cms.plugin_pool import plugin_pool
+from cms.signals import pre_placeholder_operation, post_placeholder_operation
+from cms.toolbar.utils import get_plugin_tree_as_json
+from cms.utils import copy_plugins, get_current_site
+from cms.utils.compat import DJANGO_2_0
+
+
+## ... source file abbreviated to get to get_list_or_404 examples ...
+
+
+ operation=operation,
+ request=request,
+ language=self._get_operation_language(request),
+ token=token,
+ origin=self._get_operation_origin(request),
+ **kwargs
+ )
+ return token
+
+ def _send_post_placeholder_operation(self, request, operation, token, **kwargs):
+ if not request.GET.get('cms_path'):
+ return
+
+ post_placeholder_operation.send(
+ sender=self.__class__,
+ operation=operation,
+ request=request,
+ language=self._get_operation_language(request),
+ token=token,
+ origin=self._get_operation_origin(request),
+ **kwargs
+ )
+
+ def _get_plugin_from_id(self, plugin_id):
+ queryset = CMSPlugin.objects.values_list('plugin_type', flat=True)
+~~ plugin_type = get_list_or_404(queryset, pk=plugin_id)[0]
+ plugin_class = plugin_pool.get_plugin(plugin_type)
+ real_queryset = plugin_class.get_render_queryset().select_related('parent', 'placeholder')
+ return get_object_or_404(real_queryset, pk=plugin_id)
+
+ def get_urls(self):
+ info = "%s_%s" % (self.model._meta.app_label, self.model._meta.model_name)
+ pat = lambda regex, fn: url(regex, self.admin_site.admin_view(fn), name='%s_%s' % (info, fn.__name__))
+ url_patterns = [
+ pat(r'copy-plugins/$', self.copy_plugins),
+ pat(r'add-plugin/$', self.add_plugin),
+ pat(r'edit-plugin/(%s)/$' % SLUG_REGEXP, self.edit_plugin),
+ pat(r'delete-plugin/(%s)/$' % SLUG_REGEXP, self.delete_plugin),
+ pat(r'clear-placeholder/(%s)/$' % SLUG_REGEXP, self.clear_placeholder),
+ pat(r'move-plugin/$', self.move_plugin),
+ ]
+ return url_patterns + super(PlaceholderAdminMixin, self).get_urls()
+
+ def has_add_plugin_permission(self, request, placeholder, plugin_type):
+ return placeholder.has_add_plugin_permission(request.user, plugin_type)
+
+ def has_change_plugin_permission(self, request, plugin):
+ placeholder = plugin.placeholder
+ return placeholder.has_change_plugin_permission(request.user, plugin)
+
+
+
+## ... source file continues with no further get_list_or_404 examples...
+
+```
+
diff --git a/content/pages/examples/django/django-shortcuts-get-object-or-404.markdown b/content/pages/examples/django/django-shortcuts-get-object-or-404.markdown
new file mode 100644
index 000000000..529114790
--- /dev/null
+++ b/content/pages/examples/django/django-shortcuts-get-object-or-404.markdown
@@ -0,0 +1,1530 @@
+title: django.shortcuts get_object_or_404 Example Code
+category: page
+slug: django-shortcuts-get-object-or-404-examples
+sortorder: 500011346
+toc: False
+sidebartitle: django.shortcuts get_object_or_404
+meta: Python example code for the get_object_or_404 callable from the django.shortcuts module of the Django project.
+
+
+get_object_or_404 is a callable within the django.shortcuts module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / proceedings / views.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/proceedings/views.py)
+
+```python
+# views.py
+from django.http import JsonResponse
+~~from django.shortcuts import render, get_object_or_404
+from django.views.decorators.http import require_POST
+
+from conferences.utilities import validate_chair_access
+from proceedings.forms import UpdateVolumeForm
+from proceedings.models import CameraReady
+
+
+@require_POST
+def update_volume(request, camera_id):
+~~ camera = get_object_or_404(CameraReady, id=camera_id)
+ validate_chair_access(request.user, camera.submission.conference)
+ form = UpdateVolumeForm(request.POST, instance=camera)
+ if form.is_valid():
+ form.save()
+ return JsonResponse(status=200, data={})
+ return JsonResponse(status=500, data={})
+
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 2 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / admin / placeholderadmin.py**](https://github.com/divio/django-cms/blob/develop/cms/admin/placeholderadmin.py)
+
+```python
+# placeholderadmin.py
+import uuid
+import warnings
+
+from django.conf.urls import url
+from django.contrib.admin.helpers import AdminForm
+from django.contrib.admin.utils import get_deleted_objects
+from django.core.exceptions import PermissionDenied
+from django.db import router, transaction
+from django.http import (
+ HttpResponse,
+ HttpResponseBadRequest,
+ HttpResponseForbidden,
+ HttpResponseNotFound,
+ HttpResponseRedirect,
+)
+~~from django.shortcuts import get_list_or_404, get_object_or_404, render
+from django.template.response import TemplateResponse
+from django.utils.decorators import method_decorator
+from django.utils.encoding import force_text
+from django.utils import translation
+from django.utils.translation import ugettext as _
+from django.views.decorators.clickjacking import xframe_options_sameorigin
+from django.views.decorators.http import require_POST
+
+from six.moves.urllib.parse import parse_qsl, urlparse
+
+from six import get_unbound_function, get_method_function
+
+from cms import operations
+from cms.admin.forms import PluginAddValidationForm
+from cms.constants import SLUG_REGEXP
+from cms.exceptions import PluginLimitReached
+from cms.models.placeholdermodel import Placeholder
+from cms.models.placeholderpluginmodel import PlaceholderReference
+from cms.models.pluginmodel import CMSPlugin
+from cms.plugin_pool import plugin_pool
+from cms.signals import pre_placeholder_operation, post_placeholder_operation
+from cms.toolbar.utils import get_plugin_tree_as_json
+from cms.utils import copy_plugins, get_current_site
+from cms.utils.compat import DJANGO_2_0
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ token=token,
+ origin=self._get_operation_origin(request),
+ **kwargs
+ )
+ return token
+
+ def _send_post_placeholder_operation(self, request, operation, token, **kwargs):
+ if not request.GET.get('cms_path'):
+ return
+
+ post_placeholder_operation.send(
+ sender=self.__class__,
+ operation=operation,
+ request=request,
+ language=self._get_operation_language(request),
+ token=token,
+ origin=self._get_operation_origin(request),
+ **kwargs
+ )
+
+ def _get_plugin_from_id(self, plugin_id):
+ queryset = CMSPlugin.objects.values_list('plugin_type', flat=True)
+ plugin_type = get_list_or_404(queryset, pk=plugin_id)[0]
+ plugin_class = plugin_pool.get_plugin(plugin_type)
+ real_queryset = plugin_class.get_render_queryset().select_related('parent', 'placeholder')
+~~ return get_object_or_404(real_queryset, pk=plugin_id)
+
+ def get_urls(self):
+ info = "%s_%s" % (self.model._meta.app_label, self.model._meta.model_name)
+ pat = lambda regex, fn: url(regex, self.admin_site.admin_view(fn), name='%s_%s' % (info, fn.__name__))
+ url_patterns = [
+ pat(r'copy-plugins/$', self.copy_plugins),
+ pat(r'add-plugin/$', self.add_plugin),
+ pat(r'edit-plugin/(%s)/$' % SLUG_REGEXP, self.edit_plugin),
+ pat(r'delete-plugin/(%s)/$' % SLUG_REGEXP, self.delete_plugin),
+ pat(r'clear-placeholder/(%s)/$' % SLUG_REGEXP, self.clear_placeholder),
+ pat(r'move-plugin/$', self.move_plugin),
+ ]
+ return url_patterns + super(PlaceholderAdminMixin, self).get_urls()
+
+ def has_add_plugin_permission(self, request, placeholder, plugin_type):
+ return placeholder.has_add_plugin_permission(request.user, plugin_type)
+
+ def has_change_plugin_permission(self, request, plugin):
+ placeholder = plugin.placeholder
+ return placeholder.has_change_plugin_permission(request.user, plugin)
+
+ def has_delete_plugin_permission(self, request, plugin):
+ placeholder = plugin.placeholder
+ return placeholder.has_delete_plugin_permission(request.user, plugin)
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+
+ plugin = getattr(plugin_instance, 'saved_object', None)
+
+ if plugin:
+ plugin.placeholder.mark_as_dirty(plugin.language, clear_cache=False)
+
+ if plugin_instance._operation_token:
+ tree_order = placeholder.get_plugin_tree_order(plugin.parent_id)
+ self._send_post_placeholder_operation(
+ request,
+ operation=operations.ADD_PLUGIN,
+ token=plugin_instance._operation_token,
+ plugin=plugin,
+ placeholder=plugin.placeholder,
+ tree_order=tree_order,
+ )
+ return response
+
+ @method_decorator(require_POST)
+ @xframe_options_sameorigin
+ @transaction.atomic
+ def copy_plugins(self, request):
+ source_placeholder_id = request.POST['source_placeholder_id']
+ target_language = request.POST['target_language']
+ target_placeholder_id = request.POST['target_placeholder_id']
+~~ source_placeholder = get_object_or_404(Placeholder, pk=source_placeholder_id)
+~~ target_placeholder = get_object_or_404(Placeholder, pk=target_placeholder_id)
+
+ if not target_language or not target_language in get_language_list():
+ return HttpResponseBadRequest(force_text(_("Language must be set to a supported language!")))
+
+ copy_to_clipboard = target_placeholder.pk == request.toolbar.clipboard.pk
+ source_plugin_id = request.POST.get('source_plugin_id', None)
+
+ if copy_to_clipboard and source_plugin_id:
+ new_plugin = self._copy_plugin_to_clipboard(
+ request,
+ source_placeholder,
+ target_placeholder,
+ )
+ new_plugins = [new_plugin]
+ elif copy_to_clipboard:
+ new_plugin = self._copy_placeholder_to_clipboard(
+ request,
+ source_placeholder,
+ target_placeholder,
+ )
+ new_plugins = [new_plugin]
+ else:
+ new_plugins = self._add_plugins_from_placeholder(
+ request,
+ source_placeholder,
+ target_placeholder,
+ )
+ data = get_plugin_tree_as_json(request, new_plugins)
+ return HttpResponse(data, content_type='application/json')
+
+ def _copy_plugin_to_clipboard(self, request, source_placeholder, target_placeholder):
+ source_language = request.POST['source_language']
+ source_plugin_id = request.POST.get('source_plugin_id')
+ target_language = request.POST['target_language']
+
+~~ source_plugin = get_object_or_404(
+ CMSPlugin,
+ pk=source_plugin_id,
+ language=source_language,
+ )
+
+ old_plugins = (
+ CMSPlugin
+ .get_tree(parent=source_plugin)
+ .filter(placeholder=source_placeholder)
+ .order_by('path')
+ )
+
+ if not self.has_copy_plugins_permission(request, old_plugins):
+ message = _('You do not have permission to copy these plugins.')
+ raise PermissionDenied(force_text(message))
+
+ target_placeholder.clear()
+
+ plugin_pairs = copy_plugins.copy_plugins_to(
+ old_plugins,
+ to_placeholder=target_placeholder,
+ to_language=target_language,
+ )
+ return plugin_pairs[0][0]
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+
+ if placeholder != source_placeholder:
+ try:
+ template = self.get_placeholder_template(request, placeholder)
+ has_reached_plugin_limit(placeholder, plugin.plugin_type,
+ target_language, template=template)
+ except PluginLimitReached as er:
+ return HttpResponseBadRequest(er)
+
+ exclude_from_order_check = ['__COPY__', str(plugin.pk)]
+ ordered_plugin_ids = [int(pk) for pk in order if pk not in exclude_from_order_check]
+ plugins_in_tree_count = (
+ placeholder
+ .get_plugins(target_language)
+ .filter(parent=parent_id, pk__in=ordered_plugin_ids)
+ .count()
+ )
+
+ if len(ordered_plugin_ids) != plugins_in_tree_count:
+ message = _('order parameter references plugins in different trees')
+ return HttpResponseBadRequest(force_text(message))
+
+ move_a_plugin = not move_a_copy and not move_to_clipboard
+
+ if parent_id and plugin.parent_id != parent_id:
+~~ target_parent = get_object_or_404(CMSPlugin, pk=parent_id)
+
+ if move_a_plugin and target_parent.placeholder_id != placeholder.pk:
+ return HttpResponseBadRequest(force_text(
+ _('parent must be in the same placeholder')))
+
+ if move_a_plugin and target_parent.language != target_language:
+ return HttpResponseBadRequest(force_text(
+ _('parent must be in the same language as '
+ 'plugin_language')))
+ elif parent_id:
+ target_parent = plugin.parent
+ else:
+ target_parent = None
+
+ new_plugin = None
+ fetch_tree = False
+
+ if move_a_copy and plugin.plugin_type == "PlaceholderPlugin":
+ new_plugins = self._paste_placeholder(
+ request,
+ plugin=plugin,
+ target_language=target_language,
+ target_placeholder=placeholder,
+ tree_order=order,
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ return HttpResponseRedirect(admin_reverse('index', current_app=self.admin_site.name))
+
+ plugin_name = force_text(plugin.get_plugin_class().name)
+
+ if perms_needed or protected:
+ title = _("Cannot delete %(name)s") % {"name": plugin_name}
+ else:
+ title = _("Are you sure?")
+ context = {
+ "title": title,
+ "object_name": plugin_name,
+ "object": plugin,
+ "deleted_objects": deleted_objects,
+ "perms_lacking": perms_needed,
+ "protected": protected,
+ "opts": opts,
+ "app_label": opts.app_label,
+ }
+ request.current_app = self.admin_site.name
+ return TemplateResponse(
+ request, "admin/cms/page/plugin/delete_confirmation.html", context
+ )
+
+ @xframe_options_sameorigin
+ def clear_placeholder(self, request, placeholder_id):
+~~ placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
+ language = request.GET.get('language')
+
+ if placeholder.pk == request.toolbar.clipboard.pk:
+ placeholder.clear(language)
+ return HttpResponseRedirect(admin_reverse('index', current_app=self.admin_site.name))
+
+ if not self.has_clear_placeholder_permission(request, placeholder, language):
+ return HttpResponseForbidden(force_text(_("You do not have permission to clear this placeholder")))
+
+ opts = Placeholder._meta
+ using = router.db_for_write(Placeholder)
+ plugins = placeholder.get_plugins_list(language)
+
+ if DJANGO_2_0:
+ get_deleted_objects_additional_kwargs = {
+ 'opts': opts,
+ 'using': using,
+ 'user': request.user,
+ }
+ else:
+ get_deleted_objects_additional_kwargs = {'request': request}
+ deleted_objects, __, perms_needed, protected = get_deleted_objects(
+ plugins, admin_site=self.admin_site,
+ **get_deleted_objects_additional_kwargs
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 3 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / views.py**](https://github.com/divio/django-filer/blob/develop/filer/./views.py)
+
+```python
+# views.py
+from __future__ import absolute_import, unicode_literals
+
+from django.http import Http404
+~~from django.shortcuts import get_object_or_404, redirect
+
+from .models import File
+
+
+def canonical(request, uploaded_at, file_id):
+~~ filer_file = get_object_or_404(File, pk=file_id, is_public=True)
+ if (not filer_file.file or int(uploaded_at) != filer_file.canonical_time):
+ raise Http404('No %s matches the given query.' % File._meta.object_name)
+ return redirect(filer_file.url)
+
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 4 from django-flexible-subscriptions
+[django-flexible-subscriptions](https://github.com/studybuffalo/django-flexible-subscriptions)
+([project documentation](https://django-flexible-subscriptions.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-flexible-subscriptions/))
+provides boilerplate code for adding subscription and recurrent billing
+to [Django](/django.html) web applications. Various payment providers
+can be added on the back end to run the transactions.
+
+The django-flexible-subscriptions project is open sourced under the
+[GNU General Public License v3.0](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/LICENSE).
+
+[**django-flexible-subscriptions / subscriptions / views.py**](https://github.com/studybuffalo/django-flexible-subscriptions/blob/master/subscriptions/./views.py)
+
+```python
+# views.py
+from copy import copy
+
+from django.contrib import messages
+from django.contrib.auth import get_user_model
+from django.contrib.auth.mixins import (
+ LoginRequiredMixin, PermissionRequiredMixin
+)
+from django.contrib.messages.views import SuccessMessageMixin
+from django.forms import HiddenInput
+from django.forms.models import inlineformset_factory
+from django.http import HttpResponseRedirect
+from django.http.response import HttpResponseNotAllowed, HttpResponseNotFound
+~~from django.shortcuts import get_object_or_404
+from django.template.response import TemplateResponse
+from django.urls import reverse_lazy
+from django.utils import timezone
+
+from subscriptions import models, forms, abstract
+
+
+class DashboardView(PermissionRequiredMixin, abstract.TemplateView):
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ template_name = 'subscriptions/dashboard.html'
+
+
+class TagListView(PermissionRequiredMixin, abstract.ListView):
+ model = models.PlanTag
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ context_object_name = 'tags'
+ template_name = 'subscriptions/tag_list.html'
+
+
+class TagCreateView(
+ PermissionRequiredMixin, SuccessMessageMixin, abstract.CreateView
+):
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+
+class PlanListDetailListView(PermissionRequiredMixin, abstract.DetailView):
+ model = models.PlanList
+ pk_url_kwarg = 'plan_list_id'
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ context_object_name = 'plan_list'
+ template_name = 'subscriptions/plan_list_detail_list.html'
+
+
+class PlanListDetailCreateView(
+ PermissionRequiredMixin, SuccessMessageMixin, abstract.CreateView
+):
+ model = models.PlanListDetail
+ fields = [
+ 'plan', 'plan_list', 'html_content', 'subscribe_button_text', 'order'
+ ]
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ success_message = 'Subscription plan successfully added to plan list'
+ template_name = 'subscriptions/plan_list_detail_create.html'
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+
+~~ context['plan_list'] = get_object_or_404(
+ models.PlanList, id=self.kwargs.get('plan_list_id', None)
+ )
+
+ return context
+
+ def get_success_url(self):
+ return reverse_lazy(
+ 'dfs_plan_list_detail_list',
+ kwargs={'plan_list_id': self.kwargs['plan_list_id']},
+ )
+
+
+class PlanListDetailUpdateView(
+ PermissionRequiredMixin, SuccessMessageMixin, abstract.UpdateView
+):
+ model = models.PlanListDetail
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ fields = [
+ 'plan', 'plan_list', 'html_content', 'subscribe_button_text', 'order'
+ ]
+ success_message = 'Plan list details successfully updated'
+ pk_url_kwarg = 'plan_list_detail_id'
+ template_name = 'subscriptions/plan_list_detail_update.html'
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+
+~~ context['plan_list'] = get_object_or_404(
+ models.PlanList, id=self.kwargs.get('plan_list_id', None)
+ )
+
+ return context
+
+ def get_success_url(self):
+ return reverse_lazy(
+ 'dfs_plan_list_detail_list',
+ kwargs={'plan_list_id': self.kwargs['plan_list_id']},
+ )
+
+
+class PlanListDetailDeleteView(PermissionRequiredMixin, abstract.DeleteView):
+ model = models.PlanListDetail
+ permission_required = 'subscriptions.subscriptions'
+ raise_exception = True
+ context_object_name = 'plan_list_detail'
+ pk_url_kwarg = 'plan_list_detail_id'
+ success_message = 'Subscription plan successfully removed from plan list'
+ template_name = 'subscriptions/plan_list_detail_delete.html'
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+
+~~ context['plan_list'] = get_object_or_404(
+ models.PlanList, id=self.kwargs.get('plan_list_id', None)
+ )
+
+ return context
+
+ def delete(self, request, *args, **kwargs):
+ messages.success(self.request, self.success_message)
+ return super(PlanListDetailDeleteView, self).delete(
+ request, *args, **kwargs
+ )
+
+ def get_success_url(self):
+ return reverse_lazy(
+ 'dfs_plan_list_detail_list',
+ kwargs={'plan_list_id': self.kwargs['plan_list_id']},
+ )
+
+
+class SubscribeList(abstract.TemplateView):
+ context_object_name = 'plan_list'
+ template_name = 'subscriptions/subscribe_list.html'
+
+ def get(self, request, *args, **kwargs):
+ plan_list = models.PlanList.objects.filter(active=True).first()
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ self.get_context_data(plan_list=plan_list, details=details)
+ )
+
+ return response
+
+ return HttpResponseNotFound('No subscription plans are available')
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+
+ context['plan_list'] = kwargs['plan_list']
+ context['details'] = kwargs['details']
+
+ return context
+
+
+class SubscribeView(LoginRequiredMixin, abstract.TemplateView):
+ confirmation = False
+ payment_form = forms.PaymentForm
+ subscription_plan = None
+ success_url = 'dfs_subscribe_thank_you'
+ template_preview = 'subscriptions/subscribe_preview.html'
+ template_confirmation = 'subscriptions/subscribe_confirmation.html'
+
+ def get_object(self):
+~~ return get_object_or_404(
+ models.SubscriptionPlan, id=self.request.POST.get('plan_id', None)
+ )
+
+ def get_context_data(self, **kwargs):
+ context = super(SubscribeView, self).get_context_data(**kwargs)
+
+ context['confirmation'] = self.confirmation
+
+ context['plan'] = self.subscription_plan
+
+ return context
+
+ def get_template_names(self):
+ conf_templates = [self.template_confirmation]
+ prev_templates = [self.template_preview]
+
+ return conf_templates if self.confirmation else prev_templates
+
+ def get_success_url(self, **kwargs):
+ return reverse_lazy(self.success_url, kwargs=kwargs)
+
+ def get(self, request, *args, **kwargs):
+ return HttpResponseNotAllowed(['POST'])
+
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ try:
+ return models.SubscriptionTransaction.objects.get(
+ id=self.kwargs['transaction_id'],
+ user=self.request.user,
+ )
+ except models.SubscriptionTransaction.DoesNotExist:
+ return None
+
+ def get_context_data(self, **kwargs):
+ context = super(SubscribeThankYouView, self).get_context_data(**kwargs)
+
+ context[self.context_object_name] = self.get_object()
+
+ return context
+
+
+class SubscribeCancelView(LoginRequiredMixin, abstract.DetailView):
+ model = models.UserSubscription
+ context_object_name = 'subscription'
+ pk_url_kwarg = 'subscription_id'
+ success_message = 'Subscription successfully cancelled'
+ success_url = 'dfs_subscribe_user_list'
+ template_name = 'subscriptions/subscribe_cancel.html'
+
+ def get_object(self, queryset=None):
+~~ return get_object_or_404(
+ self.model,
+ user=self.request.user,
+ id=self.kwargs['subscription_id'],
+ )
+
+ def get_success_url(self):
+ return reverse_lazy(self.success_url)
+
+ def post(self, request, *args, **kwargs): # pylint: disable=unused-argument
+ subscription = self.get_object()
+ subscription.date_billing_end = copy(subscription.date_billing_next)
+ subscription.date_billing_next = None
+ subscription.cancelled = True
+ subscription.save()
+
+ messages.success(self.request, self.success_message)
+
+ return HttpResponseRedirect(self.get_success_url())
+
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 5 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / admin.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./admin.py)
+
+```python
+# admin.py
+from collections import OrderedDict
+
+from django import forms
+from django.conf import settings
+from django.contrib import admin, messages
+from django.contrib.admin.widgets import FilteredSelectMultiple
+from django.contrib.auth import get_user_model
+~~from django.shortcuts import get_object_or_404, redirect, render
+from django.urls import reverse, path
+from django.utils.translation import gettext_lazy as _
+from django.utils.translation import gettext
+from guardian.forms import GroupObjectPermissionsForm, UserObjectPermissionsForm
+from django.contrib.auth.models import Group
+from guardian.shortcuts import (get_group_perms, get_groups_with_perms, get_perms_for_model, get_user_perms,
+ get_users_with_perms)
+
+
+class AdminUserObjectPermissionsForm(UserObjectPermissionsForm):
+
+ def get_obj_perms_field_widget(self):
+ return FilteredSelectMultiple(_("Permissions"), False)
+
+
+class AdminGroupObjectPermissionsForm(GroupObjectPermissionsForm):
+
+ def get_obj_perms_field_widget(self):
+ return FilteredSelectMultiple(_("Permissions"), False)
+
+
+class GuardedModelAdminMixin:
+ change_form_template = \
+ 'admin/guardian/model/change_form.html'
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ ]
+ urls = myurls + urls
+ return urls
+
+ def get_obj_perms_base_context(self, request, obj):
+ context = self.admin_site.each_context(request)
+ context.update({
+ 'adminform': {'model_admin': self},
+ 'media': self.media,
+ 'object': obj,
+ 'app_label': self.model._meta.app_label,
+ 'opts': self.model._meta,
+ 'original': str(obj),
+ 'has_change_permission': self.has_change_permission(request, obj),
+ 'model_perms': get_perms_for_model(obj),
+ 'title': _("Object permissions"),
+ })
+ return context
+
+ def obj_perms_manage_view(self, request, object_pk):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+ return redirect(post_url)
+
+ from django.contrib.admin.utils import unquote
+~~ obj = get_object_or_404(self.get_queryset(
+ request), pk=unquote(object_pk))
+ users_perms = OrderedDict(
+ sorted(
+ get_users_with_perms(obj, attach_perms=True,
+ with_group_users=False).items(),
+ key=lambda user: getattr(
+ user[0], get_user_model().USERNAME_FIELD)
+ )
+ )
+
+ groups_perms = OrderedDict(
+ sorted(
+ get_groups_with_perms(obj, attach_perms=True).items(),
+ key=lambda group: group[0].name
+ )
+ )
+
+ if request.method == 'POST' and 'submit_manage_user' in request.POST:
+ user_form = self.get_obj_perms_user_select_form(
+ request)(request.POST)
+ group_form = self.get_obj_perms_group_select_form(
+ request)(request.POST)
+ info = (
+ self.admin_site.name,
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ return redirect(url)
+ else:
+ user_form = self.get_obj_perms_user_select_form(request)()
+ group_form = self.get_obj_perms_group_select_form(request)()
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['users_perms'] = users_perms
+ context['groups_perms'] = groups_perms
+ context['user_form'] = user_form
+ context['group_form'] = group_form
+
+ request.current_app = self.admin_site.name
+
+ return render(request, self.get_obj_perms_manage_template(), context)
+
+ def get_obj_perms_manage_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage.html'
+ return self.obj_perms_manage_template
+
+ def obj_perms_manage_user_view(self, request, object_pk, user_id):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+ return redirect(post_url)
+
+~~ user = get_object_or_404(get_user_model(), pk=user_id)
+~~ obj = get_object_or_404(self.get_queryset(request), pk=object_pk)
+ form_class = self.get_obj_perms_manage_user_form(request)
+ form = form_class(user, obj, request.POST or None)
+
+ if request.method == 'POST' and form.is_valid():
+ form.save_obj_perms()
+ msg = gettext("Permissions saved.")
+ messages.success(request, msg)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ url = reverse(
+ '%s:%s_%s_permissions_manage_user' % info,
+ args=[obj.pk, user.pk]
+ )
+ return redirect(url)
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['user_obj'] = user
+ context['user_perms'] = get_user_perms(user, obj)
+ context['form'] = form
+
+ request.current_app = self.admin_site.name
+
+ return render(request, self.get_obj_perms_manage_user_template(), context)
+
+ def get_obj_perms_manage_user_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage_user.html'
+ return self.obj_perms_manage_user_template
+
+ def get_obj_perms_user_select_form(self, request):
+ return UserManage
+
+ def get_obj_perms_group_select_form(self, request):
+ return GroupManage
+
+ def get_obj_perms_manage_user_form(self, request):
+ return AdminUserObjectPermissionsForm
+
+ def obj_perms_manage_group_view(self, request, object_pk, group_id):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+ return redirect(post_url)
+
+~~ group = get_object_or_404(Group, id=group_id)
+~~ obj = get_object_or_404(self.get_queryset(request), pk=object_pk)
+ form_class = self.get_obj_perms_manage_group_form(request)
+ form = form_class(group, obj, request.POST or None)
+
+ if request.method == 'POST' and form.is_valid():
+ form.save_obj_perms()
+ msg = gettext("Permissions saved.")
+ messages.success(request, msg)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ url = reverse(
+ '%s:%s_%s_permissions_manage_group' % info,
+ args=[obj.pk, group.id]
+ )
+ return redirect(url)
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['group_obj'] = group
+ context['group_perms'] = get_group_perms(group, obj)
+ context['form'] = form
+
+ request.current_app = self.admin_site.name
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 6 from django-rest-framework
+[Django REST Framework](https://github.com/encode/django-rest-framework)
+([project homepage and documentation](https://www.django-rest-framework.org/),
+[PyPI package information](https://pypi.org/project/djangorestframework/)
+and [more resources on Full Stack Python](/django-rest-framework-drf.html)),
+often abbreviated as "DRF", is a popular [Django](/django.html) extension
+for building [web APIs](/application-programming-interfaces.html).
+The project has fantastic documentation and a wonderful
+[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/)
+that serve as examples of how to make it easier for newcomers
+to get started.
+
+The project is open sourced under the
+[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md).
+
+[**django-rest-framework / rest_framework / generics.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./generics.py)
+
+```python
+# generics.py
+from django.core.exceptions import ValidationError
+from django.db.models.query import QuerySet
+from django.http import Http404
+~~from django.shortcuts import get_object_or_404 as _get_object_or_404
+
+from rest_framework import mixins, views
+from rest_framework.settings import api_settings
+
+
+~~def get_object_or_404(queryset, *filter_args, **filter_kwargs):
+ try:
+ return _get_object_or_404(queryset, *filter_args, **filter_kwargs)
+ except (TypeError, ValueError, ValidationError):
+ raise Http404
+
+
+class GenericAPIView(views.APIView):
+ queryset = None
+ serializer_class = None
+
+ lookup_field = 'pk'
+ lookup_url_kwarg = None
+
+ filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
+
+ pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
+
+ def get_queryset(self):
+ assert self.queryset is not None, (
+ "'%s' should either include a `queryset` attribute, "
+ "or override the `get_queryset()` method."
+ % self.__class__.__name__
+ )
+
+ queryset = self.queryset
+ if isinstance(queryset, QuerySet):
+ queryset = queryset.all()
+ return queryset
+
+ def get_object(self):
+ queryset = self.filter_queryset(self.get_queryset())
+
+ lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
+
+ assert lookup_url_kwarg in self.kwargs, (
+ 'Expected view %s to be called with a URL keyword argument '
+ 'named "%s". Fix your URL conf, or set the `.lookup_field` '
+ 'attribute on the view correctly.' %
+ (self.__class__.__name__, lookup_url_kwarg)
+ )
+
+ filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
+~~ obj = get_object_or_404(queryset, **filter_kwargs)
+
+ self.check_object_permissions(self.request, obj)
+
+ return obj
+
+ def get_serializer(self, *args, **kwargs):
+ serializer_class = self.get_serializer_class()
+ kwargs.setdefault('context', self.get_serializer_context())
+ return serializer_class(*args, **kwargs)
+
+ def get_serializer_class(self):
+ assert self.serializer_class is not None, (
+ "'%s' should either include a `serializer_class` attribute, "
+ "or override the `get_serializer_class()` method."
+ % self.__class__.__name__
+ )
+
+ return self.serializer_class
+
+ def get_serializer_context(self):
+ return {
+ 'request': self.request,
+ 'format': self.format_kwarg,
+ 'view': self
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 7 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / views.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./views.py)
+
+```python
+# views.py
+import re
+import six
+from collections import Counter
+
+try:
+ from django.urls import reverse_lazy
+except ImportError:
+ from django.core.urlresolvers import reverse_lazy
+
+import django
+from django.db import DatabaseError
+from django.db.models import Count
+from django.forms.models import model_to_dict
+from django.http import HttpResponse, JsonResponse, HttpResponseRedirect, Http404
+~~from django.shortcuts import get_object_or_404, render
+from django.views.decorators.http import require_POST
+from django.utils.decorators import method_decorator
+from django.views.generic import ListView
+from django.views.generic.base import View
+from django.views.generic.edit import CreateView, DeleteView
+from django.views.decorators.clickjacking import xframe_options_sameorigin
+from django.core.exceptions import ImproperlyConfigured
+from django.contrib.auth import REDIRECT_FIELD_NAME
+from django.contrib.auth.views import LoginView
+
+from explorer import app_settings
+from explorer.connections import connections
+from explorer.exporters import get_exporter_class
+from explorer.forms import QueryForm
+from explorer.models import Query, QueryLog, MSG_FAILED_BLACKLIST
+from explorer.tasks import execute_query
+from explorer.utils import (
+ url_get_rows,
+ url_get_query_id,
+ url_get_log_id,
+ url_get_params,
+ safe_login_prompt,
+ fmt_sql,
+ allowed_query_pks,
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+
+def _export(request, query, download=True):
+ format = request.GET.get('format', 'csv')
+ exporter_class = get_exporter_class(format)
+ query.params = url_get_params(request)
+ delim = request.GET.get('delim')
+ exporter = exporter_class(query)
+ try:
+ output = exporter.get_output(delim=delim)
+ except DatabaseError as e:
+ msg = "Error executing query %s: %s" % (query.title, e)
+ return HttpResponse(msg, status=500)
+ response = HttpResponse(output, content_type=exporter.content_type)
+ if download:
+ response['Content-Disposition'] = 'attachment; filename="%s"' % (
+ exporter.get_filename()
+ )
+ return response
+
+
+class DownloadQueryView(PermissionRequiredMixin, View):
+
+ permission_required = 'view_permission'
+
+ def get(self, request, query_id, *args, **kwargs):
+~~ query = get_object_or_404(Query, pk=query_id)
+ return _export(request, query)
+
+
+class DownloadFromSqlView(PermissionRequiredMixin, View):
+
+ permission_required = 'view_permission'
+
+ def post(self, request, *args, **kwargs):
+ sql = request.POST.get('sql')
+ connection = request.POST.get('connection')
+ query = Query(sql=sql, connection=connection, title='')
+ ql = query.log(request.user)
+ query.title = 'Playground - %s' % ql.id
+ return _export(request, query)
+
+
+class StreamQueryView(PermissionRequiredMixin, View):
+
+ permission_required = 'view_permission'
+
+ def get(self, request, query_id, *args, **kwargs):
+~~ query = get_object_or_404(Query, pk=query_id)
+ return _export(request, query, download=False)
+
+
+class EmailCsvQueryView(PermissionRequiredMixin, View):
+
+ permission_required = 'view_permission'
+
+ def post(self, request, query_id, *args, **kwargs):
+ if request.is_ajax():
+ email = request.POST.get('email', None)
+ if email:
+ execute_query.delay(query_id, email)
+ return JsonResponse({'message': 'message was sent successfully'})
+ return JsonResponse({}, status=403)
+
+
+class SchemaView(PermissionRequiredMixin, View):
+ permission_required = 'change_permission'
+
+ @method_decorator(xframe_options_sameorigin)
+ def dispatch(self, *args, **kwargs):
+ return super(SchemaView, self).dispatch(*args, **kwargs)
+
+ def get(self, request, *args, **kwargs):
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+class CreateQueryView(PermissionRequiredMixin, ExplorerContextMixin, CreateView):
+
+ permission_required = 'change_permission'
+
+ def form_valid(self, form):
+ form.instance.created_by_user = self.request.user
+ return super(CreateQueryView, self).form_valid(form)
+
+ form_class = QueryForm
+ template_name = 'explorer/query.html'
+
+
+class DeleteQueryView(PermissionRequiredMixin, ExplorerContextMixin, DeleteView):
+
+ permission_required = 'change_permission'
+ model = Query
+ success_url = reverse_lazy("explorer_index")
+
+
+class PlayQueryView(PermissionRequiredMixin, ExplorerContextMixin, View):
+
+ permission_required = 'change_permission'
+
+ def get(self, request):
+ if url_get_query_id(request):
+~~ query = get_object_or_404(Query, pk=url_get_query_id(request))
+ return self.render_with_sql(request, query, run_query=False)
+
+ if url_get_log_id(request):
+~~ log = get_object_or_404(QueryLog, pk=url_get_log_id(request))
+ query = Query(sql=log.sql, title="Playground", connection=log.connection)
+ return self.render_with_sql(request, query)
+
+ return self.render()
+
+ def post(self, request):
+ sql = request.POST.get('sql')
+ show = url_get_show(request)
+ query = Query(sql=sql, title="Playground", connection=request.POST.get('connection'))
+ passes_blacklist, failing_words = query.passes_blacklist()
+ error = MSG_FAILED_BLACKLIST % ', '.join(failing_words) if not passes_blacklist else None
+ run_query = not bool(error) if show else False
+ return self.render_with_sql(request, query, run_query=run_query, error=error)
+
+ def render(self):
+ return self.render_template('explorer/play.html', {'title': 'Playground', 'form': QueryForm()})
+
+ def render_with_sql(self, request, query, run_query=True, error=None):
+ rows = url_get_rows(request)
+ fullscreen = url_get_fullscreen(request)
+ template = 'fullscreen' if fullscreen else 'play'
+ form = QueryForm(request.POST if len(request.POST) else None, instance=query)
+ return self.render_template('explorer/%s.html' % template, query_viewmodel(request.user,
+ query,
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ show = url_get_show(request)
+ rows = url_get_rows(request)
+ vm = query_viewmodel(request.user, query, form=form, run_query=show, rows=rows)
+ fullscreen = url_get_fullscreen(request)
+ template = 'fullscreen' if fullscreen else 'query'
+ return self.render_template('explorer/%s.html' % template, vm)
+
+ def post(self, request, query_id):
+ if not app_settings.EXPLORER_PERMISSION_CHANGE(request.user):
+ return HttpResponseRedirect(
+ reverse_lazy('query_detail', kwargs={'query_id': query_id})
+ )
+ show = url_get_show(request)
+ query, form = QueryView.get_instance_and_form(request, query_id)
+ success = form.is_valid() and form.save()
+ vm = query_viewmodel(request.user,
+ query,
+ form=form,
+ run_query=show,
+ rows=url_get_rows(request),
+ message="Query saved." if success else None)
+ return self.render_template('explorer/query.html', vm)
+
+ @staticmethod
+ def get_instance_and_form(request, query_id):
+~~ query = get_object_or_404(Query, pk=query_id)
+ query.params = url_get_params(request)
+ form = QueryForm(request.POST if len(request.POST) else None, instance=query)
+ return query, form
+
+
+def query_viewmodel(user, query, title=None, form=None, message=None, run_query=True, error=None, rows=app_settings.EXPLORER_DEFAULT_ROWS):
+ res = None
+ ql = None
+ if run_query:
+ try:
+ res, ql = query.execute_with_logging(user)
+ except DatabaseError as e:
+ error = str(e)
+ has_valid_results = not error and res and run_query
+ ret = {
+ 'tasks_enabled': app_settings.ENABLE_TASKS,
+ 'params': query.available_params(),
+ 'title': title,
+ 'shared': query.shared,
+ 'query': query,
+ 'form': form,
+ 'message': message,
+ 'error': error,
+ 'rows': rows,
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 8 from django-taggit
+[django-taggit](https://github.com/jazzband/django-taggit/)
+([PyPI page](https://pypi.org/project/django-taggit/)) provides a way
+to create, store, manage and use tags in a [Django](/django.html) project.
+The code for django-taggit is
+[open source](https://github.com/jazzband/django-taggit/blob/master/LICENSE)
+and maintained by the collaborative developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-taggit / taggit / views.py**](https://github.com/jazzband/django-taggit/blob/master/taggit/./views.py)
+
+```python
+# views.py
+from django.contrib.contenttypes.models import ContentType
+~~from django.shortcuts import get_object_or_404
+from django.views.generic.list import ListView
+
+from taggit.models import Tag, TaggedItem
+
+
+def tagged_object_list(request, slug, queryset, **kwargs):
+ if callable(queryset):
+ queryset = queryset()
+ kwargs["slug"] = slug
+ tag_list_view = type(
+ "TagListView",
+ (TagListMixin, ListView),
+ {"model": queryset.model, "queryset": queryset},
+ )
+ return tag_list_view.as_view()(request, **kwargs)
+
+
+class TagListMixin:
+ tag_suffix = "_tag"
+
+ def dispatch(self, request, *args, **kwargs):
+ slug = kwargs.pop("slug")
+~~ self.tag = get_object_or_404(Tag, slug=slug)
+ return super().dispatch(request, *args, **kwargs)
+
+ def get_queryset(self, **kwargs):
+ qs = super().get_queryset(**kwargs)
+ return qs.filter(
+ pk__in=TaggedItem.objects.filter(
+ tag=self.tag, content_type=ContentType.objects.get_for_model(qs.model)
+ ).values_list("object_id", flat=True)
+ )
+
+ def get_template_names(self):
+ if self.tag_suffix:
+ self.template_name_suffix = self.tag_suffix + self.template_name_suffix
+ return super().get_template_names()
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+ if "extra_context" not in context:
+ context["extra_context"] = {}
+ context["extra_context"]["tag"] = self.tag
+ return context
+
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 9 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / forms.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./forms.py)
+
+```python
+# forms.py
+__all__ = [
+ "UserCreationForm",
+ "UserUpdateForm",
+ "WikiSlugField",
+ "SpamProtectionMixin",
+ "CreateRootForm",
+ "MoveForm",
+ "EditForm",
+ "SelectWidgetBootstrap",
+ "TextInputPrepend",
+ "CreateForm",
+ "DeleteForm",
+ "PermissionsForm",
+ "DirFilterForm",
+ "SearchForm",
+]
+
+from datetime import timedelta
+
+from django import forms
+from django.apps import apps
+from django.contrib.auth import get_user_model
+from django.core import validators
+from django.core.validators import RegexValidator
+from django.forms.widgets import HiddenInput
+~~from django.shortcuts import get_object_or_404
+from django.urls import Resolver404, resolve
+from django.utils import timezone
+from django.utils.safestring import mark_safe
+from django.utils.translation import gettext, gettext_lazy as _, pgettext_lazy
+from wiki import models
+from wiki.conf import settings
+from wiki.core import permissions
+from wiki.core.diff import simple_merge
+from wiki.core.plugins.base import PluginSettingsFormMixin
+from wiki.editors import getEditor
+
+from .forms_account_handling import UserCreationForm, UserUpdateForm
+
+validate_slug_numbers = RegexValidator(
+ r"^[0-9]+$",
+ _("A 'slug' cannot consist solely of numbers."),
+ "invalid",
+ inverse_match=True,
+)
+
+
+class WikiSlugField(forms.CharField):
+
+ default_validators = [validators.validate_slug, validate_slug_numbers]
+
+
+## ... source file abbreviated to get to get_object_or_404 examples ...
+
+
+ ),
+ )
+ content = forms.CharField(
+ label=_("Type in some contents"),
+ help_text=_(
+ "This is just the initial contents of your article. After creating it, you can use more complex features like adding plugins, meta data, related articles etc..."
+ ),
+ required=False,
+ widget=getEditor().get_widget(),
+ ) # @UndefinedVariable
+
+
+class MoveForm(forms.Form):
+
+ destination = forms.CharField(label=_("Destination"))
+ slug = WikiSlugField(max_length=models.URLPath.SLUG_MAX_LENGTH)
+ redirect = forms.BooleanField(
+ label=_("Redirect pages"),
+ help_text=_("Create a redirect page for every moved article?"),
+ required=False,
+ )
+
+ def clean(self):
+ cd = super().clean()
+ if cd.get("slug"):
+~~ dest_path = get_object_or_404(
+ models.URLPath, pk=self.cleaned_data["destination"]
+ )
+ cd["slug"] = _clean_slug(cd["slug"], dest_path)
+ return cd
+
+
+class EditForm(forms.Form, SpamProtectionMixin):
+
+ title = forms.CharField(label=_("Title"),)
+ content = forms.CharField(
+ label=_("Contents"), required=False, widget=getEditor().get_widget()
+ ) # @UndefinedVariable
+
+ summary = forms.CharField(
+ label=pgettext_lazy("Revision comment", "Summary"),
+ help_text=_(
+ "Give a short reason for your edit, which will be stated in the revision log."
+ ),
+ required=False,
+ )
+
+ current_revision = forms.IntegerField(required=False, widget=forms.HiddenInput())
+
+ def __init__(self, request, current_revision, *args, **kwargs):
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 10 from gadget-board
+[gadget-board](https://github.com/mik4el/gadget-board) is a
+[Django](/django.html),
+[Django REST Framework (DRF)](/django-rest-framework-drf.html) and
+[Angular](/angular.html) web application that is open source under the
+[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE).
+
+[**gadget-board / web / gadgets / permissions.py**](https://github.com/mik4el/gadget-board/blob/master/web/gadgets/permissions.py)
+
+```python
+# permissions.py
+from rest_framework import permissions
+~~from django.shortcuts import get_object_or_404
+from .models import Gadget
+
+
+class CanUserAddGadgetData(permissions.BasePermission):
+ def has_permission(self, request, view):
+ if request.method in permissions.SAFE_METHODS:
+ return True
+ if request.method == "POST":
+ if request.user.is_authenticated:
+~~ gadget = get_object_or_404(Gadget, slug=view.kwargs['gadget_slug'])
+ if request.user in gadget.users_can_upload.all():
+ return True
+ return False
+
+ def has_object_permission(self, request, view, obj):
+ return False
+
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
+
+## Example 11 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / views.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/views.py)
+
+```python
+# views.py
+from django.http import Http404, HttpResponse
+~~from django.shortcuts import get_object_or_404, redirect
+from django.urls import reverse
+
+from wagtail.core import hooks
+from wagtail.core.forms import PasswordViewRestrictionForm
+from wagtail.core.models import Page, PageViewRestriction, Site
+
+
+def serve(request, path):
+ site = Site.find_for_request(request)
+ if not site:
+ raise Http404
+
+ path_components = [component for component in path.split('/') if component]
+ page, args, kwargs = site.root_page.specific.route(request, path_components)
+
+ for fn in hooks.get_hooks('before_serve_page'):
+ result = fn(page, request, args, kwargs)
+ if isinstance(result, HttpResponse):
+ return result
+
+ return page.serve(request, *args, **kwargs)
+
+
+def authenticate_with_password(request, page_view_restriction_id, page_id):
+~~ restriction = get_object_or_404(PageViewRestriction, id=page_view_restriction_id)
+~~ page = get_object_or_404(Page, id=page_id).specific
+
+ if request.method == 'POST':
+ form = PasswordViewRestrictionForm(request.POST, instance=restriction)
+ if form.is_valid():
+ restriction.mark_as_passed(request)
+
+ return redirect(form.cleaned_data['return_url'])
+ else:
+ form = PasswordViewRestrictionForm(instance=restriction)
+
+ action_url = reverse('wagtailcore_authenticate_with_password', args=[restriction.id, page.id])
+ return page.serve_password_required_response(request, form, action_url)
+
+
+
+## ... source file continues with no further get_object_or_404 examples...
+
+```
+
diff --git a/content/pages/examples/django/django-shortcuts-redirect.markdown b/content/pages/examples/django/django-shortcuts-redirect.markdown
new file mode 100644
index 000000000..31d3af0c3
--- /dev/null
+++ b/content/pages/examples/django/django-shortcuts-redirect.markdown
@@ -0,0 +1,1115 @@
+title: django.shortcuts redirect Example Code
+category: page
+slug: django-shortcuts-redirect-examples
+sortorder: 500011347
+toc: False
+sidebartitle: django.shortcuts redirect
+meta: Python example code for the redirect callable from the django.shortcuts module of the Django project.
+
+
+redirect is a callable within the django.shortcuts module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / registration / views.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/registration/views.py)
+
+```python
+# views.py
+from django.contrib.auth import get_user_model
+from django.contrib.auth.decorators import login_required
+~~from django.shortcuts import render, redirect
+
+from users.models import generate_avatar
+from users.forms import PersonalForm, ProfessionalForm, SubscriptionsForm
+
+User = get_user_model()
+
+
+@login_required
+def personal(request):
+ profile = request.user.profile
+ if request.method == 'POST':
+ form = PersonalForm(request.POST, instance=profile)
+ if form.is_valid():
+ form.save()
+ profile.avatar = generate_avatar(profile)
+ profile.save()
+~~ return redirect('register-professional')
+ else:
+ form = PersonalForm(instance=profile)
+ return render(request, 'registration/personal.html', {
+ 'form': form
+ })
+
+@login_required
+def professional(request):
+ profile = request.user.profile
+ if request.method == 'POST':
+ form = ProfessionalForm(request.POST, instance=profile)
+ if form.is_valid():
+ form.save()
+~~ return redirect('register-subscriptions')
+ else:
+ form = ProfessionalForm(instance=profile)
+ return render(request, 'registration/professional.html', {
+ 'form': form
+ })
+
+
+@login_required
+def subscriptions(request):
+ subscriptions = request.user.subscriptions
+ if request.method == 'POST':
+ form = SubscriptionsForm(request.POST, instance=subscriptions)
+ if form.is_valid():
+ form.save()
+ request.user.has_finished_registration = True
+ request.user.save()
+~~ return redirect('home')
+ else:
+ form = SubscriptionsForm(instance=subscriptions)
+ return render(request, 'registration/subscriptions.html', {
+ 'form': form
+ })
+
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 2 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / account / views.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py)
+
+```python
+# views.py
+from django.contrib import messages
+from django.contrib.auth.decorators import login_required
+from django.contrib.sites.shortcuts import get_current_site
+from django.http import (
+ Http404,
+ HttpResponsePermanentRedirect,
+ HttpResponseRedirect,
+)
+~~from django.shortcuts import redirect
+from django.urls import reverse, reverse_lazy
+from django.utils.decorators import method_decorator
+from django.views.decorators.debug import sensitive_post_parameters
+from django.views.generic.base import TemplateResponseMixin, TemplateView, View
+from django.views.generic.edit import FormView
+
+from ..exceptions import ImmediateHttpResponse
+from ..utils import get_form_class, get_request_param
+from . import app_settings, signals
+from .adapter import get_adapter
+from .forms import (
+ AddEmailForm,
+ ChangePasswordForm,
+ LoginForm,
+ ResetPasswordForm,
+ ResetPasswordKeyForm,
+ SetPasswordForm,
+ SignupForm,
+ UserTokenForm,
+)
+from .models import EmailAddress, EmailConfirmation, EmailConfirmationHMAC
+from .utils import (
+ complete_signup,
+ get_login_redirect_url,
+
+
+## ... source file abbreviated to get to redirect examples ...
+
+
+ try:
+ self.object = self.get_object()
+ if app_settings.CONFIRM_EMAIL_ON_GET:
+ return self.post(*args, **kwargs)
+ except Http404:
+ self.object = None
+ ctx = self.get_context_data()
+ return self.render_to_response(ctx)
+
+ def post(self, *args, **kwargs):
+ self.object = confirmation = self.get_object()
+ confirmation.confirm(self.request)
+ get_adapter(self.request).add_message(
+ self.request,
+ messages.SUCCESS,
+ 'account/messages/email_confirmed.txt',
+ {'email': confirmation.email_address.email})
+ if app_settings.LOGIN_ON_EMAIL_CONFIRMATION:
+ resp = self.login_on_confirm(confirmation)
+ if resp is not None:
+ return resp
+ redirect_url = self.get_redirect_url()
+ if not redirect_url:
+ ctx = self.get_context_data()
+ return self.render_to_response(ctx)
+~~ return redirect(redirect_url)
+
+ def login_on_confirm(self, confirmation):
+ user_pk = None
+ user_pk_str = get_adapter(self.request).unstash_user(self.request)
+ if user_pk_str:
+ user_pk = url_str_to_user_pk(user_pk_str)
+ user = confirmation.email_address.user
+ if user_pk == user.pk and self.request.user.is_anonymous:
+ return perform_login(self.request,
+ user,
+ app_settings.EmailVerificationMethod.NONE,
+ redirect_url=self.get_redirect_url)
+
+ return None
+
+ def get_object(self, queryset=None):
+ key = self.kwargs['key']
+ emailconfirmation = EmailConfirmationHMAC.from_key(key)
+ if not emailconfirmation:
+ if queryset is None:
+ queryset = self.get_queryset()
+ try:
+ emailconfirmation = queryset.get(key=key.lower())
+ except EmailConfirmation.DoesNotExist:
+
+
+## ... source file abbreviated to get to redirect examples ...
+
+
+ return get_form_class(app_settings.FORMS,
+ 'reset_password_from_key',
+ self.form_class)
+
+ def dispatch(self, request, uidb36, key, **kwargs):
+ self.request = request
+ self.key = key
+
+ if self.key == INTERNAL_RESET_URL_KEY:
+ self.key = self.request.session.get(INTERNAL_RESET_SESSION_KEY, '')
+ token_form = UserTokenForm(
+ data={'uidb36': uidb36, 'key': self.key})
+ if token_form.is_valid():
+ self.reset_user = token_form.reset_user
+ return super(PasswordResetFromKeyView, self).dispatch(request,
+ uidb36,
+ self.key,
+ **kwargs)
+ else:
+ token_form = UserTokenForm(
+ data={'uidb36': uidb36, 'key': self.key})
+ if token_form.is_valid():
+ self.request.session[INTERNAL_RESET_SESSION_KEY] = self.key
+ redirect_url = self.request.path.replace(
+ self.key, INTERNAL_RESET_URL_KEY)
+~~ return redirect(redirect_url)
+
+ self.reset_user = None
+ response = self.render_to_response(
+ self.get_context_data(token_fail=True)
+ )
+ return _ajax_response(self.request, response, form=token_form)
+
+ def get_context_data(self, **kwargs):
+ ret = super(PasswordResetFromKeyView, self).get_context_data(**kwargs)
+ ret['action_url'] = reverse(
+ 'account_reset_password_from_key',
+ kwargs={'uidb36': self.kwargs['uidb36'],
+ 'key': self.kwargs['key']})
+ return ret
+
+ def get_form_kwargs(self):
+ kwargs = super(PasswordResetFromKeyView, self).get_form_kwargs()
+ kwargs["user"] = self.reset_user
+ kwargs["temp_key"] = self.key
+ return kwargs
+
+ def form_valid(self, form):
+ form.save()
+ get_adapter(self.request).add_message(
+
+
+## ... source file abbreviated to get to redirect examples ...
+
+
+
+ return super(PasswordResetFromKeyView, self).form_valid(form)
+
+
+password_reset_from_key = PasswordResetFromKeyView.as_view()
+
+
+class PasswordResetFromKeyDoneView(TemplateView):
+ template_name = (
+ "account/password_reset_from_key_done." +
+ app_settings.TEMPLATE_EXTENSION)
+
+
+password_reset_from_key_done = PasswordResetFromKeyDoneView.as_view()
+
+
+class LogoutView(TemplateResponseMixin, View):
+
+ template_name = "account/logout." + app_settings.TEMPLATE_EXTENSION
+ redirect_field_name = "next"
+
+ def get(self, *args, **kwargs):
+ if app_settings.LOGOUT_ON_GET:
+ return self.post(*args, **kwargs)
+ if not self.request.user.is_authenticated:
+~~ response = redirect(self.get_redirect_url())
+ return _ajax_response(self.request, response)
+ ctx = self.get_context_data()
+ response = self.render_to_response(ctx)
+ return _ajax_response(self.request, response)
+
+ def post(self, *args, **kwargs):
+ url = self.get_redirect_url()
+ if self.request.user.is_authenticated:
+ self.logout()
+~~ response = redirect(url)
+ return _ajax_response(self.request, response)
+
+ def logout(self):
+ adapter = get_adapter(self.request)
+ adapter.add_message(
+ self.request,
+ messages.SUCCESS,
+ 'account/messages/logged_out.txt')
+ adapter.logout(self.request)
+
+ def get_context_data(self, **kwargs):
+ ctx = kwargs
+ redirect_field_value = get_request_param(self.request,
+ self.redirect_field_name)
+ ctx.update({
+ "redirect_field_name": self.redirect_field_name,
+ "redirect_field_value": redirect_field_value})
+ return ctx
+
+ def get_redirect_url(self):
+ return (
+ get_next_redirect_url(
+ self.request,
+ self.redirect_field_name) or get_adapter(
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 3 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / helpers.py**](https://github.com/jazzband/django-axes/blob/master/axes/./helpers.py)
+
+```python
+# helpers.py
+from datetime import timedelta
+from hashlib import md5
+from logging import getLogger
+from string import Template
+from typing import Callable, Optional, Type, Union
+from urllib.parse import urlencode
+
+from django.core.cache import caches, BaseCache
+from django.http import HttpRequest, HttpResponse, JsonResponse, QueryDict
+~~from django.shortcuts import render, redirect
+from django.utils.module_loading import import_string
+
+import ipware.ip
+
+from axes.conf import settings
+from axes.models import AccessBase
+
+log = getLogger(__name__)
+
+
+def get_cache() -> BaseCache:
+
+ return caches[getattr(settings, "AXES_CACHE", "default")]
+
+
+def get_cache_timeout() -> Optional[int]:
+
+ cool_off = get_cool_off()
+ if cool_off is None:
+ return None
+ return int(cool_off.total_seconds())
+
+
+def get_cool_off() -> Optional[timedelta]:
+
+
+## ... source file abbreviated to get to redirect examples ...
+
+
+ "failure_limit": get_failure_limit(request, credentials),
+ "username": get_client_username(request, credentials) or "",
+ }
+
+ cool_off = get_cool_off()
+ if cool_off:
+ context.update(
+ {
+ "cooloff_time": get_cool_off_iso8601(
+ cool_off
+ ), # differing old name is kept for backwards compatibility
+ "cooloff_timedelta": cool_off,
+ }
+ )
+
+ if request.is_ajax():
+ return JsonResponse(context, status=status)
+
+ if settings.AXES_LOCKOUT_TEMPLATE:
+ return render(request, settings.AXES_LOCKOUT_TEMPLATE, context, status=status)
+
+ if settings.AXES_LOCKOUT_URL:
+ lockout_url = settings.AXES_LOCKOUT_URL
+ query_string = urlencode({"username": context["username"]})
+ url = "{}?{}".format(lockout_url, query_string)
+~~ return redirect(url)
+
+ return HttpResponse(get_lockout_message(), status=status)
+
+
+def is_ip_address_in_whitelist(ip_address: str) -> bool:
+ if not settings.AXES_IP_WHITELIST:
+ return False
+
+ return ip_address in settings.AXES_IP_WHITELIST
+
+
+def is_ip_address_in_blacklist(ip_address: str) -> bool:
+ if not settings.AXES_IP_BLACKLIST:
+ return False
+
+ return ip_address in settings.AXES_IP_BLACKLIST
+
+
+def is_client_ip_address_whitelisted(request):
+
+ if settings.AXES_NEVER_LOCKOUT_WHITELIST and is_ip_address_in_whitelist(
+ request.axes_ip_address
+ ):
+ return True
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 4 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / views.py**](https://github.com/divio/django-filer/blob/develop/filer/./views.py)
+
+```python
+# views.py
+from __future__ import absolute_import, unicode_literals
+
+from django.http import Http404
+~~from django.shortcuts import get_object_or_404, redirect
+
+from .models import File
+
+
+def canonical(request, uploaded_at, file_id):
+ filer_file = get_object_or_404(File, pk=file_id, is_public=True)
+ if (not filer_file.file or int(uploaded_at) != filer_file.canonical_time):
+ raise Http404('No %s matches the given query.' % File._meta.object_name)
+~~ return redirect(filer_file.url)
+
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 5 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / admin.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./admin.py)
+
+```python
+# admin.py
+from collections import OrderedDict
+
+from django import forms
+from django.conf import settings
+from django.contrib import admin, messages
+from django.contrib.admin.widgets import FilteredSelectMultiple
+from django.contrib.auth import get_user_model
+~~from django.shortcuts import get_object_or_404, redirect, render
+from django.urls import reverse, path
+from django.utils.translation import gettext_lazy as _
+from django.utils.translation import gettext
+from guardian.forms import GroupObjectPermissionsForm, UserObjectPermissionsForm
+from django.contrib.auth.models import Group
+from guardian.shortcuts import (get_group_perms, get_groups_with_perms, get_perms_for_model, get_user_perms,
+ get_users_with_perms)
+
+
+class AdminUserObjectPermissionsForm(UserObjectPermissionsForm):
+
+ def get_obj_perms_field_widget(self):
+ return FilteredSelectMultiple(_("Permissions"), False)
+
+
+class AdminGroupObjectPermissionsForm(GroupObjectPermissionsForm):
+
+ def get_obj_perms_field_widget(self):
+ return FilteredSelectMultiple(_("Permissions"), False)
+
+
+class GuardedModelAdminMixin:
+ change_form_template = \
+ 'admin/guardian/model/change_form.html'
+
+
+## ... source file abbreviated to get to redirect examples ...
+
+
+ view=self.admin_site.admin_view(
+ self.obj_perms_manage_group_view),
+ name='%s_%s_permissions_manage_group' % info),
+ ]
+ urls = myurls + urls
+ return urls
+
+ def get_obj_perms_base_context(self, request, obj):
+ context = self.admin_site.each_context(request)
+ context.update({
+ 'adminform': {'model_admin': self},
+ 'media': self.media,
+ 'object': obj,
+ 'app_label': self.model._meta.app_label,
+ 'opts': self.model._meta,
+ 'original': str(obj),
+ 'has_change_permission': self.has_change_permission(request, obj),
+ 'model_perms': get_perms_for_model(obj),
+ 'title': _("Object permissions"),
+ })
+ return context
+
+ def obj_perms_manage_view(self, request, object_pk):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+~~ return redirect(post_url)
+
+ from django.contrib.admin.utils import unquote
+ obj = get_object_or_404(self.get_queryset(
+ request), pk=unquote(object_pk))
+ users_perms = OrderedDict(
+ sorted(
+ get_users_with_perms(obj, attach_perms=True,
+ with_group_users=False).items(),
+ key=lambda user: getattr(
+ user[0], get_user_model().USERNAME_FIELD)
+ )
+ )
+
+ groups_perms = OrderedDict(
+ sorted(
+ get_groups_with_perms(obj, attach_perms=True).items(),
+ key=lambda group: group[0].name
+ )
+ )
+
+ if request.method == 'POST' and 'submit_manage_user' in request.POST:
+ user_form = self.get_obj_perms_user_select_form(
+ request)(request.POST)
+ group_form = self.get_obj_perms_group_select_form(
+ request)(request.POST)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ if user_form.is_valid():
+ user_id = user_form.cleaned_data['user'].pk
+ url = reverse(
+ '%s:%s_%s_permissions_manage_user' % info,
+ args=[obj.pk, user_id]
+ )
+~~ return redirect(url)
+ elif request.method == 'POST' and 'submit_manage_group' in request.POST:
+ user_form = self.get_obj_perms_user_select_form(
+ request)(request.POST)
+ group_form = self.get_obj_perms_group_select_form(
+ request)(request.POST)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ if group_form.is_valid():
+ group_id = group_form.cleaned_data['group'].id
+ url = reverse(
+ '%s:%s_%s_permissions_manage_group' % info,
+ args=[obj.pk, group_id]
+ )
+~~ return redirect(url)
+ else:
+ user_form = self.get_obj_perms_user_select_form(request)()
+ group_form = self.get_obj_perms_group_select_form(request)()
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['users_perms'] = users_perms
+ context['groups_perms'] = groups_perms
+ context['user_form'] = user_form
+ context['group_form'] = group_form
+
+ request.current_app = self.admin_site.name
+
+ return render(request, self.get_obj_perms_manage_template(), context)
+
+ def get_obj_perms_manage_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage.html'
+ return self.obj_perms_manage_template
+
+ def obj_perms_manage_user_view(self, request, object_pk, user_id):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+~~ return redirect(post_url)
+
+ user = get_object_or_404(get_user_model(), pk=user_id)
+ obj = get_object_or_404(self.get_queryset(request), pk=object_pk)
+ form_class = self.get_obj_perms_manage_user_form(request)
+ form = form_class(user, obj, request.POST or None)
+
+ if request.method == 'POST' and form.is_valid():
+ form.save_obj_perms()
+ msg = gettext("Permissions saved.")
+ messages.success(request, msg)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ url = reverse(
+ '%s:%s_%s_permissions_manage_user' % info,
+ args=[obj.pk, user.pk]
+ )
+~~ return redirect(url)
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['user_obj'] = user
+ context['user_perms'] = get_user_perms(user, obj)
+ context['form'] = form
+
+ request.current_app = self.admin_site.name
+
+ return render(request, self.get_obj_perms_manage_user_template(), context)
+
+ def get_obj_perms_manage_user_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage_user.html'
+ return self.obj_perms_manage_user_template
+
+ def get_obj_perms_user_select_form(self, request):
+ return UserManage
+
+ def get_obj_perms_group_select_form(self, request):
+ return GroupManage
+
+ def get_obj_perms_manage_user_form(self, request):
+ return AdminUserObjectPermissionsForm
+
+ def obj_perms_manage_group_view(self, request, object_pk, group_id):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+~~ return redirect(post_url)
+
+ group = get_object_or_404(Group, id=group_id)
+ obj = get_object_or_404(self.get_queryset(request), pk=object_pk)
+ form_class = self.get_obj_perms_manage_group_form(request)
+ form = form_class(group, obj, request.POST or None)
+
+ if request.method == 'POST' and form.is_valid():
+ form.save_obj_perms()
+ msg = gettext("Permissions saved.")
+ messages.success(request, msg)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ url = reverse(
+ '%s:%s_%s_permissions_manage_group' % info,
+ args=[obj.pk, group.id]
+ )
+~~ return redirect(url)
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['group_obj'] = group
+ context['group_perms'] = get_group_perms(group, obj)
+ context['form'] = form
+
+ request.current_app = self.admin_site.name
+
+ return render(request, self.get_obj_perms_manage_group_template(), context)
+
+ def get_obj_perms_manage_group_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage_group.html'
+ return self.obj_perms_manage_group_template
+
+ def get_obj_perms_manage_group_form(self, request):
+ return AdminGroupObjectPermissionsForm
+
+
+class GuardedModelAdmin(GuardedModelAdminMixin, admin.ModelAdmin):
+
+
+class UserManage(forms.Form):
+ user = forms.CharField(label=_("User identification"),
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 6 from django-inline-actions
+[django-inline-actions](https://github.com/escaped/django-inline-actions)
+([PyPI package information](https://pypi.org/project/django-inline-actions/))
+is an extension that adds actions to the [Django](/django.html)
+Admin InlineModelAdmin and ModelAdmin changelists. The project is open
+sourced under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/escaped/django-inline-actions/blob/master/LICENSE).
+
+[**django-inline-actions / inline_actions / actions.py**](https://github.com/escaped/django-inline-actions/blob/master/inline_actions/./actions.py)
+
+```python
+# actions.py
+from django.contrib import messages
+~~from django.shortcuts import redirect
+from django.urls import reverse
+from django.utils.translation import ugettext_lazy as _
+
+
+class ViewAction:
+ inline_actions = ['view_action']
+
+ def view_action(self, request, obj, parent_obj=None):
+ url = reverse(
+ 'admin:{}_{}_change'.format(
+ obj._meta.app_label,
+ obj._meta.model_name,
+ ),
+ args=(obj.pk,)
+ )
+~~ return redirect(url)
+ view_action.short_description = _("View")
+
+
+class DeleteAction:
+ def get_inline_actions(self, request, obj=None):
+ actions = super().get_inline_actions(request, obj)
+ if self.has_delete_permission(request, obj):
+ actions.append('delete_action')
+ return actions
+
+ def delete_action(self, request, obj, parent_obj=None):
+ if self.has_delete_permission(request):
+ obj.delete()
+ messages.info(request, "`{}` deleted.".format(obj))
+ delete_action.short_description = _("Delete")
+
+
+class DefaultActionsMixin(ViewAction,
+ DeleteAction):
+ inline_actions = []
+
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 7 from django-loginas
+[django-loginas](https://github.com/skorokithakis/django-loginas)
+([PyPI package information](https://pypi.org/project/django-loginas/))
+is [Django](/django.html) code library for admins to log into an application
+as another user, typically for debugging purposes.
+
+django-loginas is open source under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/skorokithakis/django-loginas/blob/master/LICENSE).
+
+[**django-loginas / loginas / views.py**](https://github.com/skorokithakis/django-loginas/blob/master/loginas/./views.py)
+
+```python
+# views.py
+from django.contrib import messages
+from django.contrib.admin.utils import unquote
+from django.core.exceptions import ImproperlyConfigured, PermissionDenied
+~~from django.shortcuts import redirect
+from django.utils.translation import gettext_lazy as _
+from django.views.decorators.csrf import csrf_protect
+from django.views.decorators.http import require_POST
+
+from . import settings as la_settings
+from .utils import login_as, restore_original_login
+
+try:
+ from importlib import import_module
+except ImportError:
+ from django.utils.importlib import import_module # type: ignore
+
+
+try:
+ from django.contrib.auth import get_user_model
+
+ User = get_user_model()
+except ImportError:
+ from django.contrib.auth.models import User # type: ignore
+
+
+def _load_module(path):
+
+ i = path.rfind(".")
+
+
+## ... source file abbreviated to get to redirect examples ...
+
+
+ can_login_as = getattr(mod, attr)
+ except AttributeError:
+ raise ImproperlyConfigured("Module {0} does not define a {1} " "function.".format(module, attr))
+ return can_login_as
+
+
+@csrf_protect
+@require_POST
+def user_login(request, user_id):
+ user = User.objects.get(pk=unquote(user_id))
+
+ if isinstance(la_settings.CAN_LOGIN_AS, str):
+ can_login_as = _load_module(la_settings.CAN_LOGIN_AS)
+ elif hasattr(la_settings.CAN_LOGIN_AS, "__call__"):
+ can_login_as = la_settings.CAN_LOGIN_AS
+ else:
+ raise ImproperlyConfigured("The CAN_LOGIN_AS setting is neither a valid module nor callable.")
+ no_permission_error = None
+ try:
+ if not can_login_as(request, user):
+ no_permission_error = _("You do not have permission to do that.")
+ except PermissionDenied as e:
+ no_permission_error = str(e)
+ if no_permission_error is not None:
+ messages.error(request, no_permission_error, extra_tags=la_settings.MESSAGE_EXTRA_TAGS, fail_silently=True)
+~~ return redirect(request.META.get("HTTP_REFERER", "/"))
+
+ try:
+ login_as(user, request)
+ except ImproperlyConfigured as e:
+ messages.error(request, str(e), extra_tags=la_settings.MESSAGE_EXTRA_TAGS, fail_silently=True)
+~~ return redirect(request.META.get("HTTP_REFERER", "/"))
+
+~~ return redirect(la_settings.LOGIN_REDIRECT)
+
+
+def user_logout(request):
+ restore_original_login(request)
+
+~~ return redirect(la_settings.LOGOUT_REDIRECT)
+
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 8 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / decorators.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/./decorators.py)
+
+```python
+# decorators.py
+from functools import wraps
+
+from django.http import HttpResponseForbidden
+from django.http import HttpResponseNotFound
+from django.http import HttpResponseRedirect
+from django.shortcuts import get_object_or_404
+~~from django.shortcuts import redirect
+from django.template.loader import render_to_string
+from django.urls import reverse
+from django.utils.http import urlquote
+from wiki.conf import settings
+from wiki.core.exceptions import NoRootURL
+
+
+def response_forbidden(request, article, urlpath, read_denied=False):
+ if request.user.is_anonymous:
+ qs = request.META.get("QUERY_STRING", "")
+ if qs:
+ qs = urlquote("?" + qs)
+ else:
+ qs = ""
+~~ return redirect(settings.LOGIN_URL + "?next=" + request.path + qs)
+ else:
+ return HttpResponseForbidden(
+ render_to_string(
+ "wiki/permission_denied.html",
+ context={
+ "article": article,
+ "urlpath": urlpath,
+ "read_denied": read_denied,
+ },
+ request=request,
+ )
+ )
+
+
+def get_article( # noqa: max-complexity=23
+ func=None,
+ can_read=True,
+ can_write=False,
+ deleted_contents=False,
+ not_locked=False,
+ can_delete=False,
+ can_moderate=False,
+ can_create=False,
+):
+
+ def wrapper(request, *args, **kwargs):
+ from . import models
+
+ path = kwargs.pop("path", None)
+ article_id = kwargs.pop("article_id", None)
+
+ if path is not None:
+ try:
+ urlpath = models.URLPath.get_by_path(path, select_related=True)
+ except NoRootURL:
+~~ return redirect("wiki:root_create")
+ except models.URLPath.DoesNotExist:
+ try:
+ pathlist = list(filter(lambda x: x != "", path.split("/"),))
+ path = "/".join(pathlist[:-1])
+ parent = models.URLPath.get_by_path(path)
+ return HttpResponseRedirect(
+ reverse("wiki:create", kwargs={"path": parent.path})
+ + "?slug=%s" % pathlist[-1].lower()
+ )
+ except models.URLPath.DoesNotExist:
+ return HttpResponseNotFound(
+ render_to_string(
+ "wiki/error.html",
+ context={"error_type": "ancestors_missing"},
+ request=request,
+ )
+ )
+ if urlpath.article:
+ article = urlpath.article
+ else:
+ return_url = reverse("wiki:get", kwargs={"path": urlpath.parent.path})
+ urlpath.delete()
+ return HttpResponseRedirect(return_url)
+
+ elif article_id:
+ articles = models.Article.objects
+
+ article = get_object_or_404(articles, id=article_id)
+ try:
+ urlpath = models.URLPath.objects.get(articles__article=article)
+ except (
+ models.URLPath.DoesNotExist,
+ models.URLPath.MultipleObjectsReturned,
+ ):
+ urlpath = None
+
+ else:
+ raise TypeError("You should specify either article_id or path")
+
+ if not deleted_contents:
+ if urlpath:
+ if urlpath.is_deleted(): # This also checks all ancestors
+~~ return redirect("wiki:deleted", path=urlpath.path)
+ else:
+ if article.current_revision and article.current_revision.deleted:
+~~ return redirect("wiki:deleted", article_id=article.id)
+
+ if article.current_revision.locked and not_locked:
+ return response_forbidden(request, article, urlpath)
+
+ if can_read and not article.can_read(request.user):
+ return response_forbidden(request, article, urlpath, read_denied=True)
+
+ if (can_write or can_create) and not article.can_write(request.user):
+ return response_forbidden(request, article, urlpath)
+
+ if can_create and not (
+ request.user.is_authenticated or settings.ANONYMOUS_CREATE
+ ):
+ return response_forbidden(request, article, urlpath)
+
+ if can_delete and not article.can_delete(request.user):
+ return response_forbidden(request, article, urlpath)
+
+ if can_moderate and not article.can_moderate(request.user):
+ return response_forbidden(request, article, urlpath)
+
+ kwargs["urlpath"] = urlpath
+
+ return func(request, article, *args, **kwargs)
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 9 from register
+[register](https://github.com/ORGAN-IZE/register) is a [Django](/django.html),
+[Bootstrap](/bootstrap-css.html), [PostgreSQL](/postgresql.html) project that is
+open source under the
+[GNU General Public License v3.0](https://github.com/ORGAN-IZE/register/blob/master/LICENSE).
+This web application makes it easier for people to register as organ donors.
+You can see the application live at
+[https://register.organize.org/](https://register.organize.org/).
+
+[**register / registration / middleware.py**](https://github.com/ORGAN-IZE/register/blob/master/registration/./middleware.py)
+
+```python
+# middleware.py
+import django.middleware.locale
+~~import django.shortcuts
+from django.utils import translation
+from django.utils.deprecation import MiddlewareMixin
+
+
+class RequestLocaleMiddleware(MiddlewareMixin):
+ def process_view(self, request, view_func, view_args, view_kwargs):
+ if request.method == 'GET':
+ language = request.GET.get('lang')
+ if language:
+ translation.activate(language)
+ request.session[translation.LANGUAGE_SESSION_KEY] = translation.get_language()
+ query = request.GET.copy()
+ del query['lang']
+ path = '?'.join([request.path, query.urlencode()])
+~~ return django.shortcuts.redirect(path)
+
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
+
+## Example 10 from wagtail
+[wagtail](https://github.com/wagtail/wagtail)
+([project website](https://wagtail.io/)) is a fantastic
+[Django](/django.html)-based CMS with code that is open source
+under the
+[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
+
+[**wagtail / wagtail / core / views.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/views.py)
+
+```python
+# views.py
+from django.http import Http404, HttpResponse
+~~from django.shortcuts import get_object_or_404, redirect
+from django.urls import reverse
+
+from wagtail.core import hooks
+from wagtail.core.forms import PasswordViewRestrictionForm
+from wagtail.core.models import Page, PageViewRestriction, Site
+
+
+def serve(request, path):
+ site = Site.find_for_request(request)
+ if not site:
+ raise Http404
+
+ path_components = [component for component in path.split('/') if component]
+ page, args, kwargs = site.root_page.specific.route(request, path_components)
+
+ for fn in hooks.get_hooks('before_serve_page'):
+ result = fn(page, request, args, kwargs)
+ if isinstance(result, HttpResponse):
+ return result
+
+ return page.serve(request, *args, **kwargs)
+
+
+def authenticate_with_password(request, page_view_restriction_id, page_id):
+ restriction = get_object_or_404(PageViewRestriction, id=page_view_restriction_id)
+ page = get_object_or_404(Page, id=page_id).specific
+
+ if request.method == 'POST':
+ form = PasswordViewRestrictionForm(request.POST, instance=restriction)
+ if form.is_valid():
+ restriction.mark_as_passed(request)
+
+~~ return redirect(form.cleaned_data['return_url'])
+ else:
+ form = PasswordViewRestrictionForm(instance=restriction)
+
+ action_url = reverse('wagtailcore_authenticate_with_password', args=[restriction.id, page.id])
+ return page.serve_password_required_response(request, form, action_url)
+
+
+
+## ... source file continues with no further redirect examples...
+
+```
+
diff --git a/content/pages/examples/django/django-shortcuts-render.markdown b/content/pages/examples/django/django-shortcuts-render.markdown
new file mode 100644
index 000000000..7b5d2d193
--- /dev/null
+++ b/content/pages/examples/django/django-shortcuts-render.markdown
@@ -0,0 +1,1883 @@
+title: django.shortcuts render Example Code
+category: page
+slug: django-shortcuts-render-examples
+sortorder: 500011348
+toc: False
+sidebartitle: django.shortcuts render
+meta: Python example code for the render callable from the django.shortcuts module of the Django project.
+
+
+render is a callable within the django.shortcuts module of the Django project.
+
+
+## Example 1 from dccnsys
+[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
+system built with [Django](/django.html). The code is open source under the
+[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
+
+[**dccnsys / wwwdccn / registration / views.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/registration/views.py)
+
+```python
+# views.py
+from django.contrib.auth import get_user_model
+from django.contrib.auth.decorators import login_required
+~~from django.shortcuts import render, redirect
+
+from users.models import generate_avatar
+from users.forms import PersonalForm, ProfessionalForm, SubscriptionsForm
+
+User = get_user_model()
+
+
+@login_required
+def personal(request):
+ profile = request.user.profile
+ if request.method == 'POST':
+ form = PersonalForm(request.POST, instance=profile)
+ if form.is_valid():
+ form.save()
+ profile.avatar = generate_avatar(profile)
+ profile.save()
+ return redirect('register-professional')
+ else:
+ form = PersonalForm(instance=profile)
+~~ return render(request, 'registration/personal.html', {
+ 'form': form
+ })
+
+@login_required
+def professional(request):
+ profile = request.user.profile
+ if request.method == 'POST':
+ form = ProfessionalForm(request.POST, instance=profile)
+ if form.is_valid():
+ form.save()
+ return redirect('register-subscriptions')
+ else:
+ form = ProfessionalForm(instance=profile)
+~~ return render(request, 'registration/professional.html', {
+ 'form': form
+ })
+
+
+@login_required
+def subscriptions(request):
+ subscriptions = request.user.subscriptions
+ if request.method == 'POST':
+ form = SubscriptionsForm(request.POST, instance=subscriptions)
+ if form.is_valid():
+ form.save()
+ request.user.has_finished_registration = True
+ request.user.save()
+ return redirect('home')
+ else:
+ form = SubscriptionsForm(instance=subscriptions)
+~~ return render(request, 'registration/subscriptions.html', {
+ 'form': form
+ })
+
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 2 from django-allauth
+[django-allauth](https://github.com/pennersr/django-allauth)
+([project website](https://www.intenct.nl/projects/django-allauth/)) is a
+[Django](/django.html) library for easily adding local and social authentication
+flows to Django projects. It is open source under the
+[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE).
+
+
+[**django-allauth / allauth / socialaccount / helpers.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/helpers.py)
+
+```python
+# helpers.py
+from django.contrib import messages
+from django.forms import ValidationError
+from django.http import HttpResponseRedirect
+~~from django.shortcuts import render
+from django.urls import reverse
+
+from allauth.account import app_settings as account_settings
+from allauth.account.adapter import get_adapter as get_account_adapter
+from allauth.account.utils import complete_signup, perform_login, user_username
+from allauth.exceptions import ImmediateHttpResponse
+
+from . import app_settings, signals
+from .adapter import get_adapter
+from .models import SocialLogin
+from .providers.base import AuthError, AuthProcess
+
+
+def _process_signup(request, sociallogin):
+ auto_signup = get_adapter(request).is_auto_signup_allowed(
+ request,
+ sociallogin)
+ if not auto_signup:
+ request.session['socialaccount_sociallogin'] = sociallogin.serialize()
+ url = reverse('socialaccount_signup')
+ ret = HttpResponseRedirect(url)
+ else:
+ if account_settings.USER_MODEL_USERNAME_FIELD:
+ username = user_username(sociallogin.user)
+ try:
+ get_account_adapter(request).clean_username(username)
+ except ValidationError:
+ user_username(sociallogin.user, '')
+ if not get_adapter(request).is_open_for_signup(
+ request,
+ sociallogin):
+~~ return render(
+ request,
+ "account/signup_closed." +
+ account_settings.TEMPLATE_EXTENSION)
+ get_adapter(request).save_user(request, sociallogin, form=None)
+ ret = complete_social_signup(request, sociallogin)
+ return ret
+
+
+def _login_social_account(request, sociallogin):
+ return perform_login(request, sociallogin.user,
+ email_verification=app_settings.EMAIL_VERIFICATION,
+ redirect_url=sociallogin.get_redirect_url(request),
+ signal_kwargs={"sociallogin": sociallogin})
+
+
+def render_authentication_error(request,
+ provider_id,
+ error=AuthError.UNKNOWN,
+ exception=None,
+ extra_context=None):
+ try:
+ if extra_context is None:
+ extra_context = {}
+ get_adapter(request).authentication_error(
+ request,
+ provider_id,
+ error=error,
+ exception=exception,
+ extra_context=extra_context)
+ except ImmediateHttpResponse as e:
+ return e.response
+ if error == AuthError.CANCELLED:
+ return HttpResponseRedirect(reverse('socialaccount_login_cancelled'))
+ context = {
+ 'auth_error': {
+ 'provider': provider_id,
+ 'code': error,
+ 'exception': exception
+ }
+ }
+ context.update(extra_context)
+~~ return render(
+ request,
+ "socialaccount/authentication_error." +
+ account_settings.TEMPLATE_EXTENSION,
+ context
+ )
+
+
+def _add_social_account(request, sociallogin):
+ if request.user.is_anonymous:
+ return HttpResponseRedirect(reverse('socialaccount_connections'))
+ level = messages.INFO
+ message = 'socialaccount/messages/account_connected.txt'
+ action = None
+ if sociallogin.is_existing:
+ if sociallogin.user != request.user:
+ level = messages.ERROR
+ message = 'socialaccount/messages/account_connected_other.txt'
+ else:
+ action = 'updated'
+ message = 'socialaccount/messages/account_connected_updated.txt'
+ signals.social_account_updated.send(
+ sender=SocialLogin,
+ request=request,
+ sociallogin=sociallogin)
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 3 from django-axes
+[django-axes](https://github.com/jazzband/django-axes/)
+([project documentation](https://django-axes.readthedocs.io/en/latest/)
+and
+[PyPI package information](https://pypi.org/project/django-axes/)
+is a code library for [Django](/django.html) projects to track failed
+login attempts against a web application. The goal of the project is
+to make it easier for you to stop people and scripts from hacking your
+Django-powered website.
+
+The code for django-axes is
+[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
+and maintained by the group of developers known as
+[Jazzband](https://jazzband.co/).
+
+[**django-axes / axes / helpers.py**](https://github.com/jazzband/django-axes/blob/master/axes/./helpers.py)
+
+```python
+# helpers.py
+from datetime import timedelta
+from hashlib import md5
+from logging import getLogger
+from string import Template
+from typing import Callable, Optional, Type, Union
+from urllib.parse import urlencode
+
+from django.core.cache import caches, BaseCache
+from django.http import HttpRequest, HttpResponse, JsonResponse, QueryDict
+~~from django.shortcuts import render, redirect
+from django.utils.module_loading import import_string
+
+import ipware.ip
+
+from axes.conf import settings
+from axes.models import AccessBase
+
+log = getLogger(__name__)
+
+
+def get_cache() -> BaseCache:
+
+ return caches[getattr(settings, "AXES_CACHE", "default")]
+
+
+def get_cache_timeout() -> Optional[int]:
+
+ cool_off = get_cool_off()
+ if cool_off is None:
+ return None
+ return int(cool_off.total_seconds())
+
+
+def get_cool_off() -> Optional[timedelta]:
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ raise TypeError(
+ "settings.AXES_LOCKOUT_CALLABLE needs to be a string, callable, or None."
+ )
+
+ status = 403
+ context = {
+ "failure_limit": get_failure_limit(request, credentials),
+ "username": get_client_username(request, credentials) or "",
+ }
+
+ cool_off = get_cool_off()
+ if cool_off:
+ context.update(
+ {
+ "cooloff_time": get_cool_off_iso8601(
+ cool_off
+ ), # differing old name is kept for backwards compatibility
+ "cooloff_timedelta": cool_off,
+ }
+ )
+
+ if request.is_ajax():
+ return JsonResponse(context, status=status)
+
+ if settings.AXES_LOCKOUT_TEMPLATE:
+~~ return render(request, settings.AXES_LOCKOUT_TEMPLATE, context, status=status)
+
+ if settings.AXES_LOCKOUT_URL:
+ lockout_url = settings.AXES_LOCKOUT_URL
+ query_string = urlencode({"username": context["username"]})
+ url = "{}?{}".format(lockout_url, query_string)
+ return redirect(url)
+
+ return HttpResponse(get_lockout_message(), status=status)
+
+
+def is_ip_address_in_whitelist(ip_address: str) -> bool:
+ if not settings.AXES_IP_WHITELIST:
+ return False
+
+ return ip_address in settings.AXES_IP_WHITELIST
+
+
+def is_ip_address_in_blacklist(ip_address: str) -> bool:
+ if not settings.AXES_IP_BLACKLIST:
+ return False
+
+ return ip_address in settings.AXES_IP_BLACKLIST
+
+
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 4 from django-cms
+[django-cms](https://github.com/divio/django-cms)
+([project website](https://www.django-cms.org/en/)) is a Python-based
+content management system (CMS) [library](https://pypi.org/project/django-cms/)
+for use with Django web apps that is open sourced under the
+[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
+license.
+
+[**django-cms / cms / page_rendering.py**](https://github.com/divio/django-cms/blob/develop/cms/./page_rendering.py)
+
+```python
+# page_rendering.py
+from django.conf import settings
+from django.http import Http404
+~~from django.shortcuts import render
+from django.template.response import TemplateResponse
+from django.urls import Resolver404, resolve, reverse
+
+from cms import __version__
+from cms.cache.page import set_page_cache
+from cms.models import Page
+from cms.utils.conf import get_cms_setting
+from cms.utils.page import get_page_template_from_request
+from cms.utils.page_permissions import user_can_change_page, user_can_view_page
+
+
+def render_page(request, page, current_language, slug):
+ context = {}
+ context['lang'] = current_language
+ context['current_page'] = page
+ context['has_change_permissions'] = user_can_change_page(request.user, page)
+ context['has_view_permissions'] = user_can_view_page(request.user, page)
+
+ if not context['has_view_permissions']:
+ return _handle_no_page(request)
+
+ template = get_page_template_from_request(request)
+ response = TemplateResponse(request, template, context)
+ response.add_post_render_callback(set_page_cache)
+
+ xframe_options = page.get_xframe_options()
+ if xframe_options == Page.X_FRAME_OPTIONS_INHERIT or xframe_options is None:
+ return response
+
+ response.xframe_options_exempt = True
+
+ if xframe_options == Page.X_FRAME_OPTIONS_ALLOW:
+ return response
+ elif xframe_options == Page.X_FRAME_OPTIONS_SAMEORIGIN:
+ response['X-Frame-Options'] = 'SAMEORIGIN'
+ elif xframe_options == Page.X_FRAME_OPTIONS_DENY:
+ response['X-Frame-Options'] = 'DENY'
+ return response
+
+
+def render_object_structure(request, obj):
+ context = {
+ 'object': obj,
+ 'cms_toolbar': request.toolbar,
+ }
+~~ return render(request, 'cms/toolbar/structure.html', context)
+
+
+def _handle_no_page(request):
+ try:
+ resolve('%s$' % request.path)
+ except Resolver404 as e:
+ exc = Http404(dict(path=request.path, tried=e.args[0]['tried']))
+ raise exc
+ raise Http404('CMS Page not found: %s' % request.path)
+
+
+def _render_welcome_page(request):
+ context = {
+ 'cms_version': __version__,
+ 'cms_edit_on': get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'),
+ 'django_debug': settings.DEBUG,
+ 'next_url': reverse('pages-root'),
+ }
+ return TemplateResponse(request, "cms/welcome.html", context)
+
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 5 from django-filer
+[django-filer](https://github.com/divio/django-filer)
+([project documentation](https://django-filer.readthedocs.io/en/latest/))
+is a file management library for uploading and organizing files and images
+in Django's admin interface. The project's code is available under the
+[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
+
+[**django-filer / filer / admin / folderadmin.py**](https://github.com/divio/django-filer/blob/develop/filer/admin/folderadmin.py)
+
+```python
+# folderadmin.py
+from __future__ import absolute_import, division, unicode_literals
+
+import itertools
+import os
+import re
+from collections import OrderedDict
+
+from django import forms
+from django.conf import settings as django_settings
+from django.conf.urls import url
+from django.contrib import messages
+from django.contrib.admin import helpers
+from django.contrib.admin.utils import capfirst, quote, unquote
+from django.core.exceptions import PermissionDenied, ValidationError
+from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
+from django.db import models, router
+from django.http import HttpResponse, HttpResponseRedirect
+~~from django.shortcuts import get_object_or_404, render
+from django.urls import reverse
+from django.utils.encoding import force_text
+from django.utils.html import escape
+from django.utils.http import urlquote, urlunquote
+from django.utils.safestring import mark_safe
+from django.utils.translation import ugettext as _
+from django.utils.translation import ugettext_lazy, ungettext
+
+from .. import settings
+from ..models import (
+ File, Folder, FolderPermission, FolderRoot, ImagesWithMissingData,
+ UnsortedImages, tools,
+)
+from ..settings import FILER_IMAGE_MODEL, FILER_PAGINATE_BY
+from ..thumbnail_processors import normalize_subject_location
+from ..utils.compatibility import get_delete_permission
+from ..utils.filer_easy_thumbnails import FilerActionThumbnailer
+from ..utils.loader import load_model
+from . import views
+from .forms import CopyFilesAndFoldersForm, RenameFilesForm, ResizeImagesForm
+from .patched.admin_utils import get_deleted_objects
+from .permissions import PrimitivePermissionAwareModelAdmin
+from .tools import (
+ AdminContext, admin_url_params_encoded, check_files_edit_permissions,
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ 'virtual_items': virtual_items,
+ 'uploader_connections': settings.FILER_UPLOADER_CONNECTIONS,
+ 'permissions': permissions,
+ 'permstest': userperms_for_request(folder, request),
+ 'current_url': request.path,
+ 'title': _('Directory listing for %(folder_name)s') % {'folder_name': folder.name},
+ 'search_string': ' '.join(search_terms),
+ 'q': urlquote(q),
+ 'show_result_count': show_result_count,
+ 'folder_children': folder_children,
+ 'folder_files': folder_files,
+ 'limit_search_to_folder': limit_search_to_folder,
+ 'is_popup': popup_status(request),
+ 'filer_admin_context': AdminContext(request),
+ 'root_path': reverse('admin:index'),
+ 'action_form': action_form,
+ 'actions_on_top': self.actions_on_top,
+ 'actions_on_bottom': self.actions_on_bottom,
+ 'actions_selection_counter': self.actions_selection_counter,
+ 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(paginated_items.object_list)},
+ 'selection_note_all': selection_note_all % {'total_count': paginator.count},
+ 'media': self.media,
+ 'enable_permissions': settings.FILER_ENABLE_PERMISSIONS,
+ 'can_make_folder': request.user.is_superuser or (folder.is_root and settings.FILER_ALLOW_REGULAR_USERS_TO_ADD_ROOT_FOLDERS) or permissions.get("has_add_children_permission"),
+ })
+~~ return render(request, self.directory_listing_template, context)
+
+ def filter_folder(self, qs, terms=()):
+ def construct_search(field_name):
+ if field_name.startswith('^'):
+ return "%s__istartswith" % field_name[1:]
+ elif field_name.startswith('='):
+ return "%s__iexact" % field_name[1:]
+ elif field_name.startswith('@'):
+ return "%s__search" % field_name[1:]
+ else:
+ return "%s__icontains" % field_name
+
+ for term in terms:
+ filters = models.Q()
+ for filter_ in self.search_fields:
+ filters |= models.Q(**{construct_search(filter_): term})
+ for filter_ in self.get_owner_filter_lookups():
+ filters |= models.Q(**{filter_: term})
+ qs = qs.filter(filters)
+ return qs
+
+ def filter_file(self, qs, terms=()):
+ for term in terms:
+ filters = (models.Q(name__icontains=term)
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ return None
+
+ if all_perms_needed or all_protected:
+ title = _("Cannot delete files and/or folders")
+ else:
+ title = _("Are you sure?")
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": title,
+ "instance": current_folder,
+ "breadcrumbs_action": _("Delete files and/or folders"),
+ "deletable_objects": all_deletable_objects,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": all_perms_needed,
+ "protected": all_protected,
+ "opts": opts,
+ 'is_popup': popup_status(request),
+ 'filer_admin_context': AdminContext(request),
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+~~ return render(
+ request,
+ "admin/filer/delete_selected_files_confirmation.html",
+ context
+ )
+
+ delete_files_or_folders.short_description = ugettext_lazy(
+ "Delete selected files and/or folders")
+
+ def _format_callback(self, obj, user, admin_site, perms_needed):
+ has_admin = obj.__class__ in admin_site._registry
+ opts = obj._meta
+ if has_admin:
+ admin_url = reverse('%s:%s_%s_change'
+ % (admin_site.name,
+ opts.app_label,
+ opts.object_name.lower()),
+ None, (quote(obj._get_pk_val()),))
+ p = get_delete_permission(opts)
+ if not user.has_perm(p):
+ perms_needed.add(opts.verbose_name)
+ return mark_safe('%s: %s' %
+ (escape(capfirst(opts.verbose_name)),
+ admin_url,
+ escape(obj)))
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ "destination") % ", ".join(conflicting_names))
+ elif n:
+ self._move_files_and_folders_impl(files_queryset, folders_queryset, destination)
+ self.message_user(request, _("Successfully moved %(count)d files and/or folders to folder '%(destination)s'.") % {
+ "count": n,
+ "destination": destination,
+ })
+ return None
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Move files and/or folders"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Move files and/or folders"),
+ "to_move": to_move,
+ "destination_folders": folders,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+~~ return render(request, "admin/filer/folder/choose_move_destination.html", context)
+
+ move_files_and_folders.short_description = ugettext_lazy("Move selected files and/or folders")
+
+ def _rename_file(self, file_obj, form_data, counter, global_counter):
+ original_basename, original_extension = os.path.splitext(file_obj.original_filename)
+ if file_obj.name:
+ current_basename, current_extension = os.path.splitext(file_obj.name)
+ else:
+ current_basename = ""
+ current_extension = ""
+ file_obj.name = form_data['rename_format'] % {
+ 'original_filename': file_obj.original_filename,
+ 'original_basename': original_basename,
+ 'original_extension': original_extension,
+ 'current_filename': file_obj.name or "",
+ 'current_basename': current_basename,
+ 'current_extension': current_extension,
+ 'current_folder': getattr(file_obj.folder, 'name', ''),
+ 'counter': counter + 1, # 1-based
+ 'global_counter': global_counter + 1, # 1-based
+ }
+ file_obj.save()
+
+ def _rename_files(self, files, form_data, global_counter):
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ if files_queryset.count() + folders_queryset.count():
+ n = self._rename_files_impl(files_queryset, folders_queryset, form.cleaned_data, 0)
+ self.message_user(request, _("Successfully renamed %(count)d files.") % {
+ "count": n,
+ })
+ return None
+ else:
+ form = RenameFilesForm()
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Rename files"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Rename files"),
+ "to_rename": to_rename,
+ "rename_form": form,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+~~ return render(request, "admin/filer/folder/choose_rename_format.html", context)
+
+ rename_files.short_description = ugettext_lazy("Rename files")
+
+ def _generate_new_filename(self, filename, suffix):
+ basename, extension = os.path.splitext(filename)
+ return basename + suffix + extension
+
+ def _copy_file(self, file_obj, destination, suffix, overwrite):
+ if overwrite:
+ raise NotImplementedError
+
+
+ filename = self._generate_new_filename(file_obj.file.name, suffix)
+
+ file_obj.pk = None
+ file_obj.id = None
+ file_obj.save()
+ file_obj.folder = destination
+ file_obj._file_data_changed_hint = False # no need to update size, sha1, etc.
+ file_obj.file = file_obj._copy_file(filename)
+ file_obj.original_filename = self._generate_new_filename(file_obj.original_filename, suffix)
+ file_obj.save()
+
+ def _copy_files(self, files, destination, suffix, overwrite):
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ selected_destination_folder = int(request.POST.get('destination', 0))
+ except ValueError:
+ if current_folder:
+ selected_destination_folder = current_folder.pk
+ else:
+ selected_destination_folder = 0
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Copy files and/or folders"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Copy files and/or folders"),
+ "to_copy": to_copy,
+ "destination_folders": folders,
+ "selected_destination_folder": selected_destination_folder,
+ "copy_form": form,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+~~ return render(request, "admin/filer/folder/choose_copy_destination.html", context)
+
+ copy_files_and_folders.short_description = ugettext_lazy("Copy selected files and/or folders")
+
+ def _check_resize_perms(self, request, files_queryset, folders_queryset):
+ try:
+ check_files_read_permissions(request, files_queryset)
+ check_folder_read_permissions(request, folders_queryset)
+ check_files_edit_permissions(request, files_queryset)
+ except PermissionDenied:
+ return True
+ return False
+
+ def _list_folders_to_resize(self, request, folders):
+ for fo in folders:
+ children = list(self._list_folders_to_resize(request, fo.children.all()))
+ children.extend([self._format_callback(f, request.user, self.admin_site, set()) for f in sorted(fo.files) if isinstance(f, Image)])
+ if children:
+ yield self._format_callback(fo, request.user, self.admin_site, set())
+ yield children
+
+ def _list_all_to_resize(self, request, files_queryset, folders_queryset):
+ to_resize = list(self._list_folders_to_resize(request, folders_queryset))
+ to_resize.extend([self._format_callback(f, request.user, self.admin_site, set()) for f in sorted(files_queryset) if isinstance(f, Image)])
+ return to_resize
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ form.cleaned_data['upscale'] = form.cleaned_data['thumbnail_option'].upscale
+ if files_queryset.count() + folders_queryset.count():
+ n = self._resize_images_impl(files_queryset, folders_queryset, form.cleaned_data)
+ self.message_user(request, _("Successfully resized %(count)d images.") % {"count": n, })
+ return None
+ else:
+ form = ResizeImagesForm()
+
+ context = self.admin_site.each_context(request)
+ context.update({
+ "title": _("Resize images"),
+ "instance": current_folder,
+ "breadcrumbs_action": _("Resize images"),
+ "to_resize": to_resize,
+ "resize_form": form,
+ "cmsplugin_enabled": 'cmsplugin_filer_image' in django_settings.INSTALLED_APPS,
+ "files_queryset": files_queryset,
+ "folders_queryset": folders_queryset,
+ "perms_lacking": perms_needed,
+ "opts": opts,
+ "root_path": reverse('admin:index'),
+ "app_label": app_label,
+ "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
+ })
+
+~~ return render(request, "admin/filer/folder/choose_images_resize_options.html", context)
+
+ resize_images.short_description = ugettext_lazy("Resize selected images")
+
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 6 from django-guardian
+[django-guardian](https://github.com/django-guardian/django-guardian)
+([project documentation](https://django-guardian.readthedocs.io/en/stable/)
+and
+[PyPI page](https://pypi.org/project/django-guardian/))
+provides per-object permissions in [Django](/django.html) projects
+by enhancing the existing authentication backend. The project's code
+is open source under the
+[MIT license](https://github.com/django-guardian/django-guardian/blob/devel/LICENSE).
+
+[**django-guardian / guardian / admin.py**](https://github.com/django-guardian/django-guardian/blob/devel/guardian/./admin.py)
+
+```python
+# admin.py
+from collections import OrderedDict
+
+from django import forms
+from django.conf import settings
+from django.contrib import admin, messages
+from django.contrib.admin.widgets import FilteredSelectMultiple
+from django.contrib.auth import get_user_model
+~~from django.shortcuts import get_object_or_404, redirect, render
+from django.urls import reverse, path
+from django.utils.translation import gettext_lazy as _
+from django.utils.translation import gettext
+from guardian.forms import GroupObjectPermissionsForm, UserObjectPermissionsForm
+from django.contrib.auth.models import Group
+from guardian.shortcuts import (get_group_perms, get_groups_with_perms, get_perms_for_model, get_user_perms,
+ get_users_with_perms)
+
+
+class AdminUserObjectPermissionsForm(UserObjectPermissionsForm):
+
+ def get_obj_perms_field_widget(self):
+ return FilteredSelectMultiple(_("Permissions"), False)
+
+
+class AdminGroupObjectPermissionsForm(GroupObjectPermissionsForm):
+
+ def get_obj_perms_field_widget(self):
+ return FilteredSelectMultiple(_("Permissions"), False)
+
+
+class GuardedModelAdminMixin:
+ change_form_template = \
+ 'admin/guardian/model/change_form.html'
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ request)(request.POST)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ if group_form.is_valid():
+ group_id = group_form.cleaned_data['group'].id
+ url = reverse(
+ '%s:%s_%s_permissions_manage_group' % info,
+ args=[obj.pk, group_id]
+ )
+ return redirect(url)
+ else:
+ user_form = self.get_obj_perms_user_select_form(request)()
+ group_form = self.get_obj_perms_group_select_form(request)()
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['users_perms'] = users_perms
+ context['groups_perms'] = groups_perms
+ context['user_form'] = user_form
+ context['group_form'] = group_form
+
+ request.current_app = self.admin_site.name
+
+~~ return render(request, self.get_obj_perms_manage_template(), context)
+
+ def get_obj_perms_manage_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage.html'
+ return self.obj_perms_manage_template
+
+ def obj_perms_manage_user_view(self, request, object_pk, user_id):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+ return redirect(post_url)
+
+ user = get_object_or_404(get_user_model(), pk=user_id)
+ obj = get_object_or_404(self.get_queryset(request), pk=object_pk)
+ form_class = self.get_obj_perms_manage_user_form(request)
+ form = form_class(user, obj, request.POST or None)
+
+ if request.method == 'POST' and form.is_valid():
+ form.save_obj_perms()
+ msg = gettext("Permissions saved.")
+ messages.success(request, msg)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ url = reverse(
+ '%s:%s_%s_permissions_manage_user' % info,
+ args=[obj.pk, user.pk]
+ )
+ return redirect(url)
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['user_obj'] = user
+ context['user_perms'] = get_user_perms(user, obj)
+ context['form'] = form
+
+ request.current_app = self.admin_site.name
+
+~~ return render(request, self.get_obj_perms_manage_user_template(), context)
+
+ def get_obj_perms_manage_user_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage_user.html'
+ return self.obj_perms_manage_user_template
+
+ def get_obj_perms_user_select_form(self, request):
+ return UserManage
+
+ def get_obj_perms_group_select_form(self, request):
+ return GroupManage
+
+ def get_obj_perms_manage_user_form(self, request):
+ return AdminUserObjectPermissionsForm
+
+ def obj_perms_manage_group_view(self, request, object_pk, group_id):
+ if not self.has_change_permission(request, None):
+ post_url = reverse('admin:index', current_app=self.admin_site.name)
+ return redirect(post_url)
+
+ group = get_object_or_404(Group, id=group_id)
+ obj = get_object_or_404(self.get_queryset(request), pk=object_pk)
+ form_class = self.get_obj_perms_manage_group_form(request)
+ form = form_class(group, obj, request.POST or None)
+
+ if request.method == 'POST' and form.is_valid():
+ form.save_obj_perms()
+ msg = gettext("Permissions saved.")
+ messages.success(request, msg)
+ info = (
+ self.admin_site.name,
+ self.model._meta.app_label,
+ self.model._meta.model_name,
+ )
+ url = reverse(
+ '%s:%s_%s_permissions_manage_group' % info,
+ args=[obj.pk, group.id]
+ )
+ return redirect(url)
+
+ context = self.get_obj_perms_base_context(request, obj)
+ context['group_obj'] = group
+ context['group_perms'] = get_group_perms(group, obj)
+ context['form'] = form
+
+ request.current_app = self.admin_site.name
+
+~~ return render(request, self.get_obj_perms_manage_group_template(), context)
+
+ def get_obj_perms_manage_group_template(self):
+ if 'grappelli' in settings.INSTALLED_APPS:
+ return 'admin/guardian/contrib/grappelli/obj_perms_manage_group.html'
+ return self.obj_perms_manage_group_template
+
+ def get_obj_perms_manage_group_form(self, request):
+ return AdminGroupObjectPermissionsForm
+
+
+class GuardedModelAdmin(GuardedModelAdminMixin, admin.ModelAdmin):
+
+
+class UserManage(forms.Form):
+ user = forms.CharField(label=_("User identification"),
+ max_length=200,
+ error_messages={'does_not_exist': _(
+ "This user does not exist")},
+ help_text=_(
+ 'Enter a value compatible with User.USERNAME_FIELD')
+ )
+
+ def clean_user(self):
+ identification = self.cleaned_data['user']
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 7 from django-haystack
+[django-haystack](https://github.com/django-haystack/django-haystack)
+([project website](http://haystacksearch.org/) and
+[PyPI page](https://pypi.org/project/django-haystack/))
+is a search abstraction layer that separates the Python search code
+in a [Django](/django.html) web application from the search engine
+implementation that it runs on, such as
+[Apache Solr](http://lucene.apache.org/solr/),
+[Elasticsearch](https://www.elastic.co/)
+or [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html).
+
+The django-haystack project is open source under the
+[BSD license](https://github.com/django-haystack/django-haystack/blob/master/LICENSE).
+
+[**django-haystack / haystack / admin.py**](https://github.com/django-haystack/django-haystack/blob/master/haystack/./admin.py)
+
+```python
+# admin.py
+from django.contrib.admin.options import ModelAdmin, csrf_protect_m
+from django.contrib.admin.views.main import SEARCH_VAR, ChangeList
+from django.core.exceptions import PermissionDenied
+from django.core.paginator import InvalidPage, Paginator
+~~from django.shortcuts import render
+from django.utils.encoding import force_str
+from django.utils.translation import ungettext
+
+from haystack import connections
+from haystack.constants import DEFAULT_ALIAS
+from haystack.query import SearchQuerySet
+from haystack.utils import get_model_ct_tuple
+
+
+class SearchChangeList(ChangeList):
+ def __init__(self, **kwargs):
+ self.haystack_connection = kwargs.pop("haystack_connection", DEFAULT_ALIAS)
+ super(SearchChangeList, self).__init__(**kwargs)
+
+ def get_results(self, request):
+ if SEARCH_VAR not in request.GET:
+ return super(SearchChangeList, self).get_results(request)
+
+ sqs = (
+ SearchQuerySet(self.haystack_connection)
+ .models(self.model)
+ .auto_query(request.GET[SEARCH_VAR])
+ .load_all()
+ )
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ "%(total_count)s selected",
+ "All %(total_count)s selected",
+ changelist.result_count,
+ )
+
+ context = {
+ "module_name": force_str(self.model._meta.verbose_name_plural),
+ "selection_note": selection_note % {"count": len(changelist.result_list)},
+ "selection_note_all": selection_note_all
+ % {"total_count": changelist.result_count},
+ "title": changelist.title,
+ "is_popup": changelist.is_popup,
+ "cl": changelist,
+ "media": media,
+ "has_add_permission": self.has_add_permission(request),
+ "opts": changelist.opts,
+ "app_label": self.model._meta.app_label,
+ "action_form": action_form,
+ "actions_on_top": self.actions_on_top,
+ "actions_on_bottom": self.actions_on_bottom,
+ "actions_selection_counter": getattr(self, "actions_selection_counter", 0),
+ }
+ context.update(extra_context or {})
+ request.current_app = self.admin_site.name
+ app_name, model_name = get_model_ct_tuple(self.model)
+~~ return render(
+ request,
+ self.change_list_template
+ or [
+ "admin/%s/%s/change_list.html" % (app_name, model_name),
+ "admin/%s/change_list.html" % app_name,
+ "admin/change_list.html",
+ ],
+ context,
+ )
+
+
+class SearchModelAdmin(SearchModelAdminMixin, ModelAdmin):
+ pass
+
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 8 from django-oauth-toolkit
+[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit)
+([project website](http://dot.evonove.it/) and
+[PyPI package information](https://pypi.org/project/django-oauth-toolkit/1.2.0/))
+is a code library for adding and handling [OAuth2](https://oauth.net/)
+flows within your [Django](/django.html) web application and
+[API](/application-programming-interfaces.html).
+
+The django-oauth-toolkit project is open sourced under the
+[FreeBSD license](https://github.com/jazzband/django-oauth-toolkit/blob/master/LICENSE)
+and it is maintained by the developer community group
+[Jazzband](https://jazzband.co/).
+
+[**django-oauth-toolkit / oauth2_provider / views / base.py**](https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/views/base.py)
+
+```python
+# base.py
+import json
+import logging
+import urllib.parse
+
+from django.contrib.auth.mixins import LoginRequiredMixin
+from django.http import HttpResponse, JsonResponse
+~~from django.shortcuts import render
+from django.urls import reverse
+from django.utils import timezone
+from django.utils.decorators import method_decorator
+from django.views.decorators.csrf import csrf_exempt
+from django.views.decorators.debug import sensitive_post_parameters
+from django.views.generic import FormView, View
+
+from ..exceptions import OAuthToolkitError
+from ..forms import AllowForm
+from ..http import OAuth2ResponseRedirect
+from ..models import get_access_token_model, get_application_model
+from ..scopes import get_scopes_backend
+from ..settings import oauth2_settings
+from ..signals import app_authorized
+from .mixins import OAuthLibMixin
+
+
+log = logging.getLogger("oauth2_provider")
+
+
+class BaseAuthorizationView(LoginRequiredMixin, OAuthLibMixin, View):
+
+ def dispatch(self, request, *args, **kwargs):
+ self.oauth2_data = {}
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ return self.error_response(error, application)
+
+ return self.render_to_response(self.get_context_data(**kwargs))
+
+ def redirect(self, redirect_to, application, token=None):
+
+ if not redirect_to.startswith("urn:ietf:wg:oauth:2.0:oob"):
+ return super().redirect(redirect_to, application)
+
+ parsed_redirect = urllib.parse.urlparse(redirect_to)
+ code = urllib.parse.parse_qs(parsed_redirect.query)["code"][0]
+
+ if redirect_to.startswith("urn:ietf:wg:oauth:2.0:oob:auto"):
+
+ response = {
+ "access_token": code,
+ "token_uri": redirect_to,
+ "client_id": application.client_id,
+ "client_secret": application.client_secret,
+ "revoke_uri": reverse("oauth2_provider:revoke-token"),
+ }
+
+ return JsonResponse(response)
+
+ else:
+~~ return render(
+ request=self.request,
+ template_name="oauth2_provider/authorized-oob.html",
+ context={
+ "code": code,
+ },
+ )
+
+
+@method_decorator(csrf_exempt, name="dispatch")
+class TokenView(OAuthLibMixin, View):
+ server_class = oauth2_settings.OAUTH2_SERVER_CLASS
+ validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
+ oauthlib_backend_class = oauth2_settings.OAUTH2_BACKEND_CLASS
+
+ @method_decorator(sensitive_post_parameters("password"))
+ def post(self, request, *args, **kwargs):
+ url, headers, body, status = self.create_token_response(request)
+ if status == 200:
+ access_token = json.loads(body).get("access_token")
+ if access_token is not None:
+ token = get_access_token_model().objects.get(
+ token=access_token)
+ app_authorized.send(
+ sender=self, request=request,
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 9 from django-sql-explorer
+[django-sql-explorer](https://github.com/groveco/django-sql-explorer)
+([PyPI page](https://pypi.org/project/django-sql-explorer/0.2/)),
+also referred to as "SQL Explorer",
+is a code library for the [Django](/django.html) Admin that allows
+approved, authenticated users to view and execute direct database SQL
+queries. The tool keeps track of executed queries so users can share them
+with each other, as well as export results to downloadable formats.
+django-sql-explorer is provided as open source under the
+[MIT license](https://github.com/groveco/django-sql-explorer/blob/master/LICENSE).
+
+[**django-sql-explorer / explorer / views.py**](https://github.com/groveco/django-sql-explorer/blob/master/explorer/./views.py)
+
+```python
+# views.py
+import re
+import six
+from collections import Counter
+
+try:
+ from django.urls import reverse_lazy
+except ImportError:
+ from django.core.urlresolvers import reverse_lazy
+
+import django
+from django.db import DatabaseError
+from django.db.models import Count
+from django.forms.models import model_to_dict
+from django.http import HttpResponse, JsonResponse, HttpResponseRedirect, Http404
+~~from django.shortcuts import get_object_or_404, render
+from django.views.decorators.http import require_POST
+from django.utils.decorators import method_decorator
+from django.views.generic import ListView
+from django.views.generic.base import View
+from django.views.generic.edit import CreateView, DeleteView
+from django.views.decorators.clickjacking import xframe_options_sameorigin
+from django.core.exceptions import ImproperlyConfigured
+from django.contrib.auth import REDIRECT_FIELD_NAME
+from django.contrib.auth.views import LoginView
+
+from explorer import app_settings
+from explorer.connections import connections
+from explorer.exporters import get_exporter_class
+from explorer.forms import QueryForm
+from explorer.models import Query, QueryLog, MSG_FAILED_BLACKLIST
+from explorer.tasks import execute_query
+from explorer.utils import (
+ url_get_rows,
+ url_get_query_id,
+ url_get_log_id,
+ url_get_params,
+ safe_login_prompt,
+ fmt_sql,
+ allowed_query_pks,
+ url_get_show,
+ url_get_fullscreen
+)
+
+from explorer.schema import schema_info
+from explorer import permissions
+
+
+class ExplorerContextMixin(object):
+
+ def gen_ctx(self):
+ return {'can_view': app_settings.EXPLORER_PERMISSION_VIEW(self.request.user),
+ 'can_change': app_settings.EXPLORER_PERMISSION_CHANGE(self.request.user)}
+
+ def get_context_data(self, **kwargs):
+ ctx = super(ExplorerContextMixin, self).get_context_data(**kwargs)
+ ctx.update(self.gen_ctx())
+ return ctx
+
+ def render_template(self, template, ctx):
+ ctx.update(self.gen_ctx())
+~~ return render(self.request, template, ctx)
+
+
+class PermissionRequiredMixin(object):
+
+ permission_required = None
+
+ def get_permission_required(self):
+ if self.permission_required is None:
+ raise ImproperlyConfigured(
+ '{0} is missing the permission_required attribute. Define {0}.permission_required, or override '
+ '{0}.get_permission_required().'.format(self.__class__.__name__)
+ )
+ return self.permission_required
+
+ def has_permission(self, request, *args, **kwargs):
+ perms = self.get_permission_required()
+ handler = getattr(permissions, perms) # TODO: fix the case when the perms is
+ return handler(request, *args, **kwargs)
+
+ def handle_no_permission(self, request):
+ return SafeLoginView.as_view(
+ extra_context={'title': 'Log in', REDIRECT_FIELD_NAME: request.get_full_path()})(request)
+
+ def dispatch(self, request, *args, **kwargs):
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+
+ permission_required = 'view_permission'
+
+ def post(self, request, query_id, *args, **kwargs):
+ if request.is_ajax():
+ email = request.POST.get('email', None)
+ if email:
+ execute_query.delay(query_id, email)
+ return JsonResponse({'message': 'message was sent successfully'})
+ return JsonResponse({}, status=403)
+
+
+class SchemaView(PermissionRequiredMixin, View):
+ permission_required = 'change_permission'
+
+ @method_decorator(xframe_options_sameorigin)
+ def dispatch(self, *args, **kwargs):
+ return super(SchemaView, self).dispatch(*args, **kwargs)
+
+ def get(self, request, *args, **kwargs):
+ connection = kwargs.get('connection')
+ if connection not in connections:
+ raise Http404
+ schema = schema_info(connection)
+ if schema:
+~~ return render(None, 'explorer/schema.html',
+ {'schema': schema_info(connection)})
+ else:
+~~ return render(None, 'explorer/schema_building.html')
+
+
+@require_POST
+def format_sql(request):
+ sql = request.POST.get('sql', '')
+ formatted = fmt_sql(sql)
+ return JsonResponse({"formatted": formatted})
+
+
+class ListQueryView(PermissionRequiredMixin, ExplorerContextMixin, ListView):
+
+ permission_required = 'view_permission_list'
+
+ def recently_viewed(self):
+ qll = QueryLog.objects.filter(run_by_user=self.request.user, query_id__isnull=False).order_by(
+ '-run_at').select_related('query')
+ ret = []
+ tracker = []
+ for ql in qll:
+ if len(ret) == app_settings.EXPLORER_RECENT_QUERY_COUNT:
+ break
+
+ if ql.query_id not in tracker:
+ ret.append(ql)
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+class PlayQueryView(PermissionRequiredMixin, ExplorerContextMixin, View):
+
+ permission_required = 'change_permission'
+
+ def get(self, request):
+ if url_get_query_id(request):
+ query = get_object_or_404(Query, pk=url_get_query_id(request))
+ return self.render_with_sql(request, query, run_query=False)
+
+ if url_get_log_id(request):
+ log = get_object_or_404(QueryLog, pk=url_get_log_id(request))
+ query = Query(sql=log.sql, title="Playground", connection=log.connection)
+ return self.render_with_sql(request, query)
+
+ return self.render()
+
+ def post(self, request):
+ sql = request.POST.get('sql')
+ show = url_get_show(request)
+ query = Query(sql=sql, title="Playground", connection=request.POST.get('connection'))
+ passes_blacklist, failing_words = query.passes_blacklist()
+ error = MSG_FAILED_BLACKLIST % ', '.join(failing_words) if not passes_blacklist else None
+ run_query = not bool(error) if show else False
+ return self.render_with_sql(request, query, run_query=run_query, error=error)
+
+~~ def render(self):
+ return self.render_template('explorer/play.html', {'title': 'Playground', 'form': QueryForm()})
+
+ def render_with_sql(self, request, query, run_query=True, error=None):
+ rows = url_get_rows(request)
+ fullscreen = url_get_fullscreen(request)
+ template = 'fullscreen' if fullscreen else 'play'
+ form = QueryForm(request.POST if len(request.POST) else None, instance=query)
+ return self.render_template('explorer/%s.html' % template, query_viewmodel(request.user,
+ query,
+ title="Playground",
+ run_query=run_query,
+ error=error,
+ rows=rows,
+ form=form))
+
+
+class QueryView(PermissionRequiredMixin, ExplorerContextMixin, View):
+
+ permission_required = 'view_permission'
+
+ def get(self, request, query_id):
+ query, form = QueryView.get_instance_and_form(request, query_id)
+ query.save() # updates the modified date
+ show = url_get_show(request)
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 10 from django-wiki
+[django-wiki](https://github.com/django-wiki/django-wiki)
+([project documentation](https://django-wiki.readthedocs.io/en/master/),
+[demo](https://demo.django-wiki.org/),
+and [PyPI page](https://pypi.org/project/django-wiki/))
+is a wiki system code library for [Django](/django.html)
+projects that makes it easier to create user-editable content.
+The project aims to provide necessary core features and then
+have an easy plugin format for additional features, rather than
+having every exhaustive feature built into the core system.
+django-wiki is a rewrite of an earlier now-defunct project
+named [django-simplewiki](https://code.google.com/p/django-simple-wiki/).
+
+The code for django-wiki is provided as open source under the
+[GNU General Public License 3.0](https://github.com/django-wiki/django-wiki/blob/master/COPYING).
+
+[**django-wiki / src/wiki / views / accounts.py**](https://github.com/django-wiki/django-wiki/blob/master/src/wiki/views/accounts.py)
+
+```python
+# accounts.py
+from django.conf import settings as django_settings
+from django.contrib import messages
+from django.contrib.auth import get_user_model
+from django.contrib.auth import login as auth_login
+from django.contrib.auth import logout as auth_logout
+from django.contrib.auth.forms import AuthenticationForm
+from django.shortcuts import get_object_or_404
+from django.shortcuts import redirect
+~~from django.shortcuts import render
+from django.urls import reverse
+from django.utils.translation import gettext as _
+from django.views.generic import CreateView
+from django.views.generic import FormView
+from django.views.generic import UpdateView
+from django.views.generic import View
+from wiki import forms
+from wiki.conf import settings
+
+User = get_user_model()
+
+
+class Signup(CreateView):
+ model = User
+ form_class = forms.UserCreationForm
+ template_name = "wiki/accounts/signup.html"
+
+ def dispatch(self, request, *args, **kwargs):
+ if not request.user.is_anonymous and not request.user.is_superuser:
+ return redirect("wiki:root")
+ if not settings.ACCOUNT_HANDLING:
+ return redirect(settings.SIGNUP_URL)
+ if not request.user.is_superuser and not settings.ACCOUNT_SIGNUP_ALLOWED:
+ c = {"error_msg": _("Account signup is only allowed for administrators.")}
+~~ return render(request, "wiki/error.html", context=c)
+
+ return super().dispatch(request, *args, **kwargs)
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+ context["honeypot_class"] = context["form"].honeypot_class
+ context["honeypot_jsfunction"] = context["form"].honeypot_jsfunction
+ return context
+
+ def get_success_url(self, *args):
+ messages.success(
+ self.request, _("You are now signed up... and now you can sign in!")
+ )
+ return reverse("wiki:login")
+
+
+class Logout(View):
+ def dispatch(self, request, *args, **kwargs):
+ if not settings.ACCOUNT_HANDLING:
+ return redirect(settings.LOGOUT_URL)
+ return super().dispatch(request, *args, **kwargs)
+
+ def get(self, request, *args, **kwargs):
+ auth_logout(request)
+
+
+## ... source file continues with no further render examples...
+
+```
+
+
+## Example 11 from dmd-interpreter
+[dmd-interpreter](https://github.com/mitchalexbailey/dmd-interpreter)
+([running web app](http://www.dmd.nl/DOVE))
+is a Python tool to aggregate clinically relevant information related
+to variants in the DMD gene and display that [data](/data.html) to a user
+with a [Django](/django.html) web application.
+
+[**dmd-interpreter / interpreter / views.py**](https://github.com/mitchalexbailey/dmd-interpreter/blob/master/interpreter/./views.py)
+
+```python
+# views.py
+~~from django.shortcuts import get_object_or_404, render
+from django.http import HttpResponseRedirect, HttpResponse
+from django.template import RequestContext, loader
+from django.core.urlresolvers import reverse
+from .forms import IndexForm, ACMGForm
+
+from interpreter_functions import *
+import os,sys
+import subprocess
+import csv
+import re
+import pprint
+
+interpreter_dir = os.path.dirname(__file__)
+
+def index(request):
+ if request.method == "POST":
+ user = request.POST.get('user', None)
+~~ return render(request, 'index.html')
+
+
+def results(request):
+ if request.method == 'POST':
+ mut = request.POST.get('mutation', None)
+
+ ucsc_info = [12, 'NM_004006', 'chrX', '-', 31137344, 33229673, 31140035, 33229429, 79, '31137344,31144758,31152218,31164407,31165391,31187559,31190464,31191655,31196048,31196785,31198486,31200854,31222077,31224698,31227614,31241163,31279071,31341714,31366672,31462597,31496222,31497099,31514904,31525397,31645789,31676106,31697491,31747747,31792076,31838091,31854834,31893307,31947712,31950196,31986455,32235032,32305645,32328198,32360216,32361250,32364059,32366522,32380904,32382698,32383136,32398626,32404426,32407617,32408187,32429868,32456357,32459296,32466572,32472778,32481555,32482702,32486614,32490280,32503035,32509393,32519871,32536124,32563275,32583818,32591646,32591861,32613873,32632419,32662248,32663080,32715986,32717228,32827609,32834584,32841411,32862899,32867844,33038255,33229398,', '31140047,31144790,31152311,31164531,31165635,31187718,31190530,31191721,31196087,31196922,31198598,31201021,31222235,31224784,31227816,31241238,31279133,31341775,31366751,31462744,31496491,31497220,31515061,31525570,31645979,31676261,31697703,31747865,31792309,31838200,31854939,31893490,31947862,31950344,31986631,32235180,32305818,32328393,32360399,32361403,32364197,32366645,32381075,32382827,32383316,32398797,32404582,32407791,32408298,32430030,32456507,32459431,32466755,32472949,32481711,32482816,32486827,32490426,32503216,32509635,32519959,32536248,32563451,32583998,32591754,32591963,32613993,32632570,32662430,32663269,32716115,32717410,32827728,32834757,32841504,32862977,32867937,33038317,33229673,', 0, 'DMD', 'cmpl', 'cmpl', '0,1,1,0,2,2,2,2,2,0,2,0,1,2,1,1,2,1,0,0,1,0,2,0,2,0,1,0,1,0,0,0,0,2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,2,0,0,0,0,0,2,0,0,0,1,2,0,0,0,0,1,0,']
+
+
+
+ f = open(os.path.join(interpreter_dir, "rescue_esesites.txt"))
+ ese_sites = []
+ for line in f:
+ temp = ''
+ for char in line:
+ if char != "\n":
+ temp += char
+ temp = ''.join(temp)
+ ese_sites += [temp]
+ f.close()
+
+ f = open(os.path.join(interpreter_dir, "esssites.txt"))
+ ess_sites = []
+ for line in f:
+
+
+## ... source file abbreviated to get to render examples ...
+
+
+ provean = 'Not missense'
+ metasvm = 'Not missense'
+ nsfp_score = ''
+ nsfp_message = ''
+ cv = ''
+ consequence = ''
+ consequence_statement = ''
+ ese_message = ''
+ esefinder = ''
+ rescueese = ''
+ ess_message = ''
+ splice_message = ''
+ splice_message2 = ''
+ motif_message = ''
+ ds = ''
+ splice_result = []
+ long_aa_change = []
+ readthrough_elig = "Not eligible"
+
+ ex_input = exoninput(mut)
+ mutype, search = type_inp(mut, ex_input)
+ num_list, intron_list = parse(mut, mutype, exon_positions, ex_input)
+
+
+ if mutype == "Invalid mutation" and len(num_list) == 0:
+~~ return render(request, 'index.html',{'success':False})
+ else:
+ pass
+
+
+ for num in num_list:
+ if int(num) > 3685*3 and not ex_input: #AA length *3
+ past_max = True
+ elif int(num) > len_ref:
+ past_max = True
+ else:
+ pass
+
+
+ intron_only = intron_count(intron_list, num_list)
+
+
+ if (mutype == "Deletion" or mutype == "Duplication") and "c." not in mut and len(num_list) > 0 and not ex_input and "Nucleotide" not in mut:
+ if max(num_list) < 80 and not ex_input:
+ with open(os.path.join(interpreter_dir, 'templates/triage.html'), 'w') as g:
+ num_list = re.findall("\d+", mut)
+ g.write("""