Skip to content

Instantly share code, notes, and snippets.

Avatar
Focusing

Mario César mariocesar

Focusing
View GitHub Profile
@mariocesar
mariocesar / admin.py
Last active May 3, 2021
Django admin decorator to create a confirmation form action, like the default delete action works
View admin.py
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())
@admin.register(Post)
@mariocesar
mariocesar / README.md
Last active Apr 30, 2021
Useful shell function to load .env files in the terminal session
View README.md

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"
    oldenv=$(env|sort)
    export $(grep -v '^#' .env | xargs -d '\n')
    diff <(echo "$oldenv") <(env | sort)
@mariocesar
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)
end
function check_password(password, encoded_password)
local bcrypt = require 'bcrypt'
return bcrypt.verify(password, encoded_password)
end
@mariocesar
mariocesar / README.md
Created Mar 30, 2021
Annoying things to remember about Node.js, npm, yarn, etc and the whole ecosystem
View README.md

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
mariocesar / README.md
Last active Feb 16, 2021
Psycopg2 utils / PostgreSQL with Python
View README.md

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
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
mariocesar / index.html
Last active Jan 30, 2021
Example Python + Websockets to emulate tail -f
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Tail </title>
<style>
body {font-family: monospace;}
p{ margin: 0;}
ul{padding: 0;}
@mariocesar
mariocesar / telegram.py
Created Jun 13, 2017
Send a telegram message with python requests
View telegram.py
import requests
api_url = 'https://api.telegram.org/bot{token}/{method}'.format
TELEGRAM_ACCESS_TOKEN = os.environ.get('TELEGRAM_ACCESS_TOKEN', None)
def telegram_command(name, data):
url = api_url(token=TELEGRAM_ACCESS_TOKEN, method=name)
return requests.post(url=url, json=data)
@mariocesar
mariocesar / password_hasher.py
Created Dec 14, 2020
Some notes for twitch.tv/melkeydev to make a simple/secure password hasher
View password_hasher.py
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
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 =
icons:
mkdir -p icons