Create an advanced REST API with Python, Django REST Framework and Docker using Test Driven Development (TDD)
The advanced course on how to Build a Backend REST API using Python, Django (2.0), Django REST Framework (3.9), Docker, Travis CI, Postgres and Test Driven Development!
The original course content was created by Mark Winterbottom.
- User authentication
- Creating objects
- Filtering and sorting objects
- Uploading and viewing images
- Setup a project with Docker and Docker-Compose
- Configure Travis-CI to automatically run linting and unit tests
- Write unit tests using the Django Test Framework
- Apply best practice principles including Test Driven Development
- Handle uploading media files with Django
- Customize the Django admin
- Configure a Postgres database
First-Time Git Setup
The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
Changing the project interpreter in the PyCharm project settings
- In the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter.
- Expand the list of the available interpreters and click the Show All link.
- Select the target interpreter. When PyCharm stops supporting any of the outdated Python versions, the corresponding project interpreter is marked as unsupported.
- The Python interpreter name specified in the Name field, becomes visible in the list of available interpreters. Click OK to apply the changes.
For more info please check here
PyCharm - Choosing Your Testing Framework
- Open the Settings/Preferences dialog, and under the node Tools, click the page Python Integrated Tools.
- On this page, click the Default Test Runner field.
- Choose the desired test runner:
For more info please see Enable Pytest for you project
Setting up Python3 virtual environment on Windows machine
- open CMD
- navigate to project directory, for example:
cd C:\Users\superadmin\Desktop\Python\CodinGame
- run following command:
pip install virtualenv
- run following command:
virtualenv venv --python=python
Manjaro: Install Python3 pip, virtualenv
All python3 packages are prefixed python-, whereas python2 packages are prefixed python2-.
-
The package is called python-pip. First check if it's already installed:
pacman -Qs python-pip
-
If there is no output after running the above command, that means that the package is not installed. In order to install it, run:
sudo pacman -Syu python-pip
-
In order to install
virtualenv
run:pip install virtualenv
-
You also need to run
sudo /usr/bin/easy_install virtualenv
which puts it in/usr/local/bin/
.
Setting up Python3 virtual environment on Linx (Ubuntu) machine
- Install pip first
sudo apt-get install python3-pip
- Then install virtualenv using pip3
sudo pip3 install virtualenv
- Now create a virtual environment
virtualenv venv
you can use any name insted of venv
- You can also use a Python interpreter of your choice:
virtualenv -p /usr/bin/python2.7 venv
- Active your virtual environment:
source venv/bin/activate
- Using fish shell:
source venv/bin/activate.fish
- To deactivate:
deactivate
- Create virtualenv using Python3:
virtualenv -p python3 myenv
- Instead of using virtualenv you can use this command in Python3:
python3 -m venv myenv
Activate Virtual Environment
In a newly created virtualenv there will be a bin/activate shell script. For Windows systems, activation scripts are provided for CMD.exe and Powershell.
- Open Terminal
- Run: \path\to\env\Scripts\activate
Auto generate requirements.txt
Any application typically has a set of dependencies that are required for that application to work. The requirements file is a way to specify and install specific set of package dependencies at once.
Use pip’s freeze command to generate a requirements.txt file for your project:
pip freeze > requirements.txt
If you save this in requirements.txt, you can follow this guide: PyCharm - Manage dependencies using requirements.txt, or you can:
pip install -r requirements.txt
Install Python 3 virtualenv on Ubuntu
Step by step:
# Step 1: Update your repositories
sudo apt-get update
# Step 2: Install pip for Python 3
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
sudo apt install python3-pip
# Step 3: Use pip to install virtualenv
sudo pip3 install virtualenv
# Step 4: Launch your Python 3 virtual environment, here the name of my virtual environment will be `venv`
virtualenv -p python3 venv
# Step 5: Activate your new Python 3 environment. There are two ways to do this
. venv/bin/activate # or source env3/bin/activate which does exactly the same thing
# you can make sure you are now working with Python 3
python -- version
# this command will show you what is going on: the python executable you are using is now located inside your virtualenv repository
which python
# Step 6: code your stuff
# Step 7: done? leave the virtual environment
deactivate
Install Docker Engine on Ubuntu
- Older versions of Docker were called
docker
,docker.io
, ordocker-engine
. If these are installed, uninstall them:
sudo apt-get remove docker docker-engine docker.io containerd runc
- Update the
apt
package index and install packages to allowapt
to use a repository over HTTPS:
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
- Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Verify that you now have the key with the fingerprint
sudo apt-key fingerprint 0EBFCD88
- Use the following command to set up the
stable
repository:
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
(lsb_release -cs) \
stable"
- Update the
apt
package index, and install the latest version of Docker Engine and container:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
- Verify that Docker Engine is installed correctly by running the
hello-world
image:
sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
Install Docker Compose on Linux systems
Step-by-step instructions are included below:
- Run this command to download the current stable release of Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
- Test the installation:
docker-compose --version
error: RPC failed; curl 56 Recv failure: Connection was reset
- Open Git Bash
- Run: "git config --global http.postBuffer 157286400"
How to fix in case .gitignore is ignored by Git
Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore
NOTE:
- First commit your current changes, or you will lose them.
- Then run the following commands from the top folder of your Git repository:
git rm -r --cached .
git add .
git commit -m "fixed untracked files"
How to generate Allure report with history trends (Windows OS)
Step by step:
- Run tests from pytest using following arguments: -v --alluredir=allure-results
- Copy '.\allure-report\history' folder into '.\allure-results\history'
- Run: allure generate .\allure-results\ -o .\allure-report\ --clean
- Following output should appear: Report successfully generated to .\allure-report
- Run: allure open .\allure-report\
Sphinx Documentation Set Up
Step by step:
-
Create docs directory
-
Open cmd > Go to docs directory
-
cmd > Run: sphinx-quickstart. Note: run with default answers
-
Go to docs/conf.py
-
Uncomment following lines:
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
- Update extensions list as following:
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc']
- Update template as following:
html_theme = 'sphinx_rtd_theme'
- Update sys.path.insert as following:
sys.path.insert(0, os.path.abspath('..'))
- Go to docs/index.rst > add modules, see example below:
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
- Open cmd > run:
sphinx-apidoc -o . ..
- cmd > Run: make html
- Install html template:
pip install sphinx_rtd_theme
Auto-Generated Python Documentation with Sphinx
Step by step:
- Open CMD
- Go to docs directory
- Run: make clean
- Run: make html