Blog


Secret Key generation for Django and Flask

Both Django and Flask rely on SECRET_KEY to generate things like session IDs, cookies etc. Here is a safe way to generate them. Note that this relies on the secrets module introduced in Python 3.6 and onwards. From the Python docs: The secrets module is used for generating cryptographically strong random numbers suitable for managing […]




Git Stash — way to safely split your commits

  • April 24, 2020
  • Git

A junior dev in my team was regularly committing 15 to 20+ files in one go, with useless messages like “added files”, “changed files” or even better “change 1” etc. Figured out that the reason was, “how to commit until everything is working”. Turns out, he was treating Git as SVN or IBM-clearcase. If you […]




Flask + SQLAlchemy tips

First and foremost: this : https://flask-sqlalchemy.palletsprojects.com/en/2.x/ This is the old Flask website built in flask. Some patterns are outdated. But still pretty nice to gloss over with a cup of tea and some buiscuits. 🙂 https://github.com/pallets/flask-website/tree/master/flask_website scoped_session(sessionmaker()) or plain sessionmaker() ? Well, the recommended pattern is to use scoped_session as it is considered thread safe. […]




Install psycopg2 on Ubuntu 18.04+

I was trying to install psycopg2 database adapter for PostgreSQL on Ubuntu 18.04. And was always getting an error when doing “pipenv install psycopg2”, complaining about “Python.h” missing. After much head-scratching, I found the issue. Multiple Python distributions confuse psycopg2 installation. Its like this. psycopg2 has two main dependencies libpq-dev python3-dev. On Ubuntu, you install […]




Configure Pycharm Community Edition to run Flask

Setup Environment Step 1: pipenv install –python 3.8 Step 2: pipenv shell Step 3: pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy flask-migrate psycopg2 flask-sqlalchemy is the sql-alchemy with flask bindings. flask-marshmallow is like the Serializer in Django Rest Framework, marshmallow-sqlalchemy is common binding between marshmallow and sqlalchemy (for things like ModelSerializer in Django Rest Framework).flask-migrate is […]




SQL Alchemy tutorial

This describes the basic overview of SQL Alchemy. Notice the color theme. It will be related to the next onion-ring overview of SQL Alchemy. SQL Alchemy is divided into Core and ORM. You can use Core and not use ORM. ORM is more geared towards mapping model objects to Python data types. Core on the […]




PostgreSQL Basics

Schema PostgreSQL Schema: Exactly like namespaces in SQL Server (dbo in SQL Server is equivalent to public in PostgreSQL. The default namespace.) Logical container of tables and other database objects You can have multiple schemas in a database Schemas (or namespaces) are useful in production environment in that we can assign users to them and […]




PostgreSQL installation and configuration on ubuntu 18.04

Installation: Bad way First install postgress from ubuntu repos (note: this will lag behind the latest version directly from postgreSQL official page but far easier to install and uninstall). sudo apt update sudo apt install postgresql postgresql-contrib Installation complete. contrib package contains additional postgres utilities. Installation: Latest Version : Right Way Create the file /etc/apt/sources.list.d/pgdg.list […]




Basic Rest API in Flask and SqlAlchemy

Step 1: pipenv install –python 3.8 Step 2: pipenv shell Step 3: pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy flask-migrate psycopg2 flask-sqlalchemy is the sql-alchemy with flask bindings. flask-marshmallow is like the Serializer in Django Rest Framework, marshmallow-sqlalchemy is common binding between marshmallow and sqlalchemy (for things like ModelSerializer in Django Rest Framework).flask-migrate is migration tool […]




Django and Django Rest Framework Permissioning system

What are Permissions In Django Permissions can apply to Individual Users Groups of Users Permissions are all about Access – Who can see what? Control – Who can do what In django docs, Permissions are defined like so: Permissions [are] binary (yes/no) flags designating whether a user may perform a certain task So, the above […]