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 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 / 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()
...
atom dimension {
width: int?
height: int?
depth: int?
}
atom money {
amount: int
currency: string
}
@mariocesar
mariocesar / models.py
Created August 9, 2024 19:09
SearchableCharField in Django, GIN Index for case-insensitive search
from django.db import models
from django.contrib.postgres.indexes import GinIndex
from django.db.models.functions import Lower
class SearchableCharField(models.CharField):
def contribute_to_class(self, cls, name, **kwargs):
super().contribute_to_class(cls, name, **kwargs)
# Add a GIN index with trigram operations for fast search
@mariocesar
mariocesar / json_jinja2_decoder.py
Last active August 12, 2024 18:17
A JSON Decoder that will evaluate strings as jinja2 expressions
"""
TODO: Use an strict environment like SandboxedEnvironment
TODO: Create an "allowed list" of filters and functions to use in the expression
TODO: Make or check the context object is inmutable (Prevent thread-safe situations)
"""
import json
from datetime import datetime
from jinja2 import Environment, meta, sandbox
@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

This small snippet can be implemented in your Django project to handle locks. It is particularly useful for replacing Redis locks, reducing dependency overhead. To use this snippet, simply copy and adapted to your Django project.

The hash_string function is used to convert a string value into a numerical hash value, as PostgreSQL advisory lock mechanism requires an integer.

Tested on Python3.11 and Django4

Learn more about advisory locks in: