ikostan / Build_Backend_REST_API_with_Python_and_Django

Create an advanced REST API with Python, Django REST Framework and Docker using Test Driven Development (TDD)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

License: Unlicense GitHub commit activity Build Status Codacy Badge

Create an advanced REST API with Python, Django REST Framework and Docker using Test Driven Development (TDD)

Table of Contents

  1. About

  2. Main Objectives

  3. Tech Topics Covered

  4. Icons Library

  5. Official Web Resources

  6. Additional Resources

  7. Tech Issues and Problem Solving

  8. Django App:

About

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.

The main goal is to built a fully functioning REST API that can handle

  • User authentication
  • Creating objects
  • Filtering and sorting objects
  • Uploading and viewing images

Additional topics covered

  • 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

Official Web Resources

Additional Resources

Tech Issues and Problem Solving

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

Source

Changing the project interpreter in the PyCharm project settings
  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter.
  2. Expand the list of the available interpreters and click the Show All link.
  3. Select the target interpreter. When PyCharm stops supporting any of the outdated Python versions, the corresponding project interpreter is marked as unsupported.
  4. 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
  1. Open the Settings/Preferences dialog, and under the node Tools, click the page Python Integrated Tools.
  2. On this page, click the Default Test Runner field.
  3. Choose the desired test runner:

For more info please see Enable Pytest for you project

Setting up Python3 virtual environment on Windows machine
  1. open CMD
  2. navigate to project directory, for example:
cd C:\Users\superadmin\Desktop\Python\CodinGame
  1. run following command:
pip install virtualenv
  1. run following command:
virtualenv venv --python=python
Manjaro: Install Python3 pip, virtualenv
All python3 packages are prefixed python-, whereas python2 packages are prefixed python2-.
  1. The package is called python-pip. First check if it's already installed: pacman -Qs python-pip

  2. 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

  3. In order to install virtualenv run: pip install virtualenv

  4. You also need to run sudo /usr/bin/easy_install virtualenv which puts it in /usr/local/bin/.

Source

Setting up Python3 virtual environment on Linx (Ubuntu) machine

How to install virtualenv

  1. Install pip first
    sudo apt-get install python3-pip
  1. Then install virtualenv using pip3
    sudo pip3 install virtualenv
  1. Now create a virtual environment
    virtualenv venv

you can use any name insted of venv

  1. You can also use a Python interpreter of your choice:
    virtualenv -p /usr/bin/python2.7 venv
  1. Active your virtual environment:
    source venv/bin/activate
  1. Using fish shell:
    source venv/bin/activate.fish
  1. To deactivate:
    deactivate
  1. Create virtualenv using Python3:
    virtualenv -p python3 myenv
  1. Instead of using virtualenv you can use this command in Python3:
    python3 -m venv myenv

Source

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.

  1. Open Terminal
  2. Run: \path\to\env\Scripts\activate

Source

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

Source

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

Source

Install Docker Engine on Ubuntu
  1. Older versions of Docker were called docker, docker.io, or docker-engine. If these are installed, uninstall them:
sudo apt-get remove docker docker-engine docker.io containerd runc
  1. Update the apt package index and install packages to allow apt 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
  1. 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
  1. 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"
  1. 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
  1. 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.

Source

Install Docker Compose on Linux systems

Step-by-step instructions are included below:

  1. 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
  1. Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
  1. Test the installation:
docker-compose --version

Source

error: RPC failed; curl 56 Recv failure: Connection was reset
  1. Open Git Bash
  2. Run: "git config --global http.postBuffer 157286400"

Source

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:

  1. Run tests from pytest using following arguments: -v --alluredir=allure-results
  2. Copy '.\allure-report\history' folder into '.\allure-results\history'
  3. Run: allure generate .\allure-results\ -o .\allure-report\ --clean
  4. Following output should appear: Report successfully generated to .\allure-report
  5. Run: allure open .\allure-report\

Source

Sphinx Documentation Set Up


Step by step:

  1. Create docs directory

  2. Open cmd > Go to docs directory

  3. cmd > Run: sphinx-quickstart. Note: run with default answers

  4. Go to docs/conf.py

  5. Uncomment following lines:

    import os
    import sys
    sys.path.insert(0, os.path.abspath('.'))
  1. Update extensions list as following:
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc']
  1. Update template as following:
html_theme = 'sphinx_rtd_theme'
  1. Update sys.path.insert as following:
sys.path.insert(0, os.path.abspath('..'))
  1. Go to docs/index.rst > add modules, see example below:
.. toctree::
   :maxdepth: 2
   :caption: Contents:

   modules
  1. Open cmd > run:
sphinx-apidoc -o . ..
  1. cmd > Run: make html
  2. Install html template:
pip install sphinx_rtd_theme

Video Tutorial Sphinx Documentation More Info

Auto-Generated Python Documentation with Sphinx


Step by step:

  1. Open CMD
  2. Go to docs directory
  3. Run: make clean
  4. Run: make html

Source

About

Create an advanced REST API with Python, Django REST Framework and Docker using Test Driven Development (TDD)

License:The Unlicense


Languages

Language:Python 97.9%Language:Dockerfile 2.1%