Skip to content

Instantly share code, notes, and snippets.

View oliverhaas's full-sized avatar

Oliver Haas oliverhaas

View GitHub Profile
@oliverhaas
oliverhaas / convert_ai.py
Created October 30, 2025 10:21
Convert AI rules/instructions
"""Small script to convert different kind of AI rules/instructions, starting from Cursor rules. Useful for teams which use mixed AI tools."""
import logging
import re
from pathlib import Path
logging.basicConfig(level=logging.INFO, format="%(message)s")
logger = logging.getLogger(__name__)
@oliverhaas
oliverhaas / lazy_prefetch.py
Created October 26, 2025 17:20
Django ORM: "Lazy" prefetching or "smartly filling in the gaps" with prefetch_related_objects
"""
Django prefetch_related_objects() - Smart Gap Filling
Intelligently prefetches only the missing related objects without re-fetching already loaded data.
"""
from django.db.models import prefetch_related_objects, Prefetch
def process_books_mixed_sources():
# Load books from different sources with different prefetches
@oliverhaas
oliverhaas / dirty_tracker.py
Last active October 26, 2025 17:13
DirtyTracker - Efficient Django Model Field Change Tracking ("is dirty")
"""
DirtyTracker - Efficient Django Model Field Change Tracking
Tracks which fields changed on a model instance to enable efficient updates.
Similar to django-dirtyfields (see https://pypi.org/project/django-dirtyfields/),
but I was just wondering if a context manager is maybe cleaner
and more focused than modifying the model class. Basically just an idea.
Usage:
@oliverhaas
oliverhaas / htmx_multi_swap_and_hx_select_oob.html
Last active January 1, 2025 11:41
HTMX multi-swap extension and hx-select-oob
<!-- Up until now, I thought the use of multi-swap like this here was very convenient.-->
<div hx-post=""
hx-swap="multi:#foo:outerHTML,#bar:outerHTML">
<!-- But for this we do not really need multi-swap. HTMX (since a couple of years at least) can do the above without an extension like this-->
<div hx-post=""
hx-swap="none"
hx-select-oob="#foo:outerHTML,#bar:outerHTML">
@oliverhaas
oliverhaas / settings.json
Created December 1, 2024 11:51
Settings for django-components & tailwind extension intellisense
{
"tailwindCSS.classAttributes": [
"class",
"className",
"ngClass",
"class:list",
"css_classes",
"[a-z]*_css_classes"
],
}
@oliverhaas
oliverhaas / example.html
Created November 29, 2024 18:26
Django-cotton-like syntax for django-components
<!-- A simple button component which normally could look like this with django-components -->
{% component "button" icon="upload" css_classes"btn-primary" type="submit" %}
Upload
{% endcomponent %}
<!-- can now be written like this -->
<c-button icon="upload"
class="btn-primary"
type="submit">
Upload
@oliverhaas
oliverhaas / components_html_tag_loader.py
Created September 13, 2024 21:06
HTML tag syntax for django-components (similar to django-cotton)
import random
import re
import string
from bs4 import BeautifulSoup
from django.conf import settings
from django.template import TemplateDoesNotExist
from django.template.loaders.app_directories import Loader as AppDirectoriesLoader
from django.template.loaders.base import Loader as BaseLoader
from django.template.loaders.filesystem import Loader as FilesystemLoader