Skip to content

Instantly share code, notes, and snippets.


Mario César mariocesar

View GitHub Profile
mariocesar /
Last active May 3, 2021
Django admin decorator to create a confirmation form action, like the default delete action works
from .models import Post, Category
from .decorators import action_form
class PostCategoryForm(forms.Form):
title = 'Update category for the selected posts'
category = forms.ModelChoiceField(queryset=Category.objects.all())
mariocesar /
Last active Apr 30, 2021
Useful shell function to load .env files in the terminal session

Load environment variables using .env files

You can add the following functions for your shell, loadenv will search for .env file if found will export all to the shell session with unloadenv will unset the variables so you can start again, reloadenv will reread the .env file.

function loadenv {
    test -f .env || echo "No .env file in the working directory"
    export $(grep -v '^#' .env | xargs -d '\n')
    diff <(echo "$oldenv") <(env | sort)
mariocesar / access.lua
Last active Apr 13, 2021
Nginx Lua script redis based for Basic user authentication
View access.lua
function password_encode(password)
local bcrypt = require 'bcrypt'
return bcrypt.digest(password, 12)
function check_password(password, encoded_password)
local bcrypt = require 'bcrypt'
return bcrypt.verify(password, encoded_password)
mariocesar /
Created Mar 30, 2021
Annoying things to remember about Node.js, npm, yarn, etc and the whole ecosystem

Most packages still fail due peer dependency version resolve. Packages like expo-cli. To be able to install it make sure you have the legacy mode for resolving versions is enabled

npm config --global set legacy-peer-deps true
mariocesar /
Last active Feb 16, 2021
Psycopg2 utils / PostgreSQL with Python

Create a connection using a database url from Heroku or Digital Ocean App platform

from urllib.parse import urlparse

import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

def create_connection():
    url = urlparse(os.environ["DATABASE_URL"])
mariocesar / api.js
Created Sep 26, 2017
Axios single configured instance
View api.js
import axios from "axios";
const singleton = Symbol();
const singletonEnforcer = Symbol();
function readCookie(name) {
const match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
return (match ? decodeURIComponent(match[3]) : null);
mariocesar / index.html
Last active Jan 30, 2021
Example Python + Websockets to emulate tail -f
View index.html
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Tail </title>
body {font-family: monospace;}
p{ margin: 0;}
ul{padding: 0;}
mariocesar /
Created Jun 13, 2017
Send a telegram message with python requests
import requests
api_url = '{token}/{method}'.format
def telegram_command(name, data):
url = api_url(token=TELEGRAM_ACCESS_TOKEN, method=name)
return, json=data)
mariocesar /
Created Dec 14, 2020
Some notes for to make a simple/secure password hasher
import base64
import hashlib
import secrets
from typing import NoReturn
def pbkdf2(password: str, salt: str, iterations: int, dklen: int=0, digest=None):
"""Return the hash of password using pbkdf2."""
if digest is None:
digest = hashlib.sha256
mariocesar / Makefile
Last active Nov 25, 2020
Build all icons and favicon needed for a webpage, using a svg input file
View Makefile
INKSCAPE_BIN ?= flatpak run org.inkscape.Inkscape
iconsizes = 16 32 48 57 60 72 76 96 120 144 152 180 192 256 300
iconnames = $(addsuffix .png, $(addprefix icons/icon-, $(iconsizes)))
all: icons icons/favicon.ico $(iconnames)
@echo = Done =
mkdir -p icons