Skip to content

Instantly share code, notes, and snippets.

View mariocesar's full-sized avatar

Mario-César mariocesar

View GitHub Profile
@mariocesar
mariocesar / README.md
Created March 10, 2025 16:11
Apagar/Encender/Reiniciar instancias EC2 con Policy. Util para gitlab CI

Comandos AWS CLI:

Apagar una instancia EC2 por ID y esperar a que se detenga:

aws ec2 stop-instances --instance-ids i-1234567890abcdef0
aws ec2 wait instance-stopped --instance-ids i-1234567890abcdef0

Encender una instancia EC2 por ID y esperar a que inicie:

@mariocesar
mariocesar / README.md
Created February 26, 2025 15:30
A django template tag that generates SVG icons. Useful to create svg favicons with emojis

Usage

{% load utils_tags %}
{% svgicon '🕶️' 'rounded square' '#ffccff' as icon %}
<link rel="icon" href="data:image/svg+xml,{{ icon | urlencode }}">

Will make a nice favicon of sunglases with a pink background.

@mariocesar
mariocesar / README.md
Last active February 22, 2025 20:20
React-style components for Django templates, but only on Django Template. Simple, familiar, and good enough for most projects

Why?

I built this because I wanted React's component patterns in Django templates without switching to a JavaScript frontend. When you use Jinja2 you have macros and that work similar. This will bring the Jinja2 macro experience.

No complex setup, no build process, just practical template components when you need them.

How It Works

Two template tags, that's it:

@mariocesar
mariocesar / README.md
Last active February 10, 2025 20:14
TimestampStateField custom Django field that creates a timestamp-boolean pair

The TimestampStateField creates two related fields in your Django models:

  1. A timestamp field (DateTimeField) that records when a state changed
  2. A corresponding boolean field that indicates the current state

The goal is to do state-tracking where you want to know both IF something is in a state and WHEN it entered that state. For example, tracking if a user is active and when they became active. And with that also gain the benefits of faster DB access and creating more efficient db indexes.

Here's how it works through an example:

@mariocesar
mariocesar / README.md
Last active February 4, 2025 03:35
Check network can resolve and access a host with port

Run me like this. Or maybe not! it's dangerous!

curl -sL "https://gist.github.com/mariocesar/b628c91e7b00cca0dca371ac2d54544c/raw/script.py?v=$(date +%s)" | python3 - google.com:443
@mariocesar
mariocesar / README.md
Last active January 31, 2025 21:20
Useful oneliners that I often forget. #terminal #python #shell

bash

Get a sha256sum hash for all the given files, similar to GitHub Action hashFiles function.

hashFiles() {
    local files=("$@")
    if [[ ${#files[@]} -eq 0 ]]; then
        echo "Error: No files provided" >&2
 return 1
@mariocesar
mariocesar / celery.py
Created December 18, 2024 14:30
Celery. Reduce risk of idle connections by closing connections.
from celery import Celery
from celery.signals import worker_shutdown, task_postrun
from django.db import connections
app = Celery()
...
@mariocesar
mariocesar / telegram-notify
Created April 5, 2017 04:22
Send telegram messages! send text, photos, videos, stickers, and documents within the console
#!/usr/bin/env python
import argparse
import os
import sys
import requests
apiurl = 'https://api.telegram.org/bot{token}/{method}'.format
@mariocesar
mariocesar / wait_for_signals.sh
Created April 8, 2018 21:48
Bash script that runs forever and wait for signals.
#!/usr/bin/env bash
catch_kill() {
echo "Caught SIGKILL signal!"
kill -KILL "$pid" 2>/dev/null
}
catch_term() {
echo "Caught SIGTERM signal!"
kill -TERM "$pid" 2>/dev/null
@mariocesar
mariocesar / storage.py
Last active November 6, 2024 16:57
Django custom storage backend that applies optimizations for png and jpg images
from django.contrib.staticfiles.storage import CachedFilesMixin, StaticFilesStorage
from pipeline.storage import PipelineMixin
from functools import wraps
import shutil
import re
import subprocess
def coroutine(func):
@wraps(func)