Skip to content

Instantly share code, notes, and snippets.

View archatas's full-sized avatar

Aidas Bendoraitis archatas

View GitHub Profile
@archatas
archatas / django_silent_migrations.py
Created February 27, 2025 18:22
Silent Django Migrations
from django.db.migrations.operations.base import Operation
class SilentOperationWrapper(Operation):
"""
A wrapper for Django migration operations that handles errors silently.
This wrapper executes the wrapped operation and catches any exceptions.
If an exception occurs, it can either fail silently or mark the operation
as "faked" in the migration history without actually performing the operation.
@archatas
archatas / django_no_5_song.md
Last active February 21, 2025 08:47
Django No 5

Django No. 5

(Parody of Mambo No. 5 by Lou Bega, rewritten by ChatGPT for Django development.)

[Intro]

Ha-ha-ha, Django devs, are you ready?
Let's build some apps!

[Verse 1]

One, two, three, four, five,
Time to spin up Django, let’s go live!

@archatas
archatas / token_counter.py
Created February 19, 2025 23:28
Count OpenAI tokens for text
import tiktoken
encoding = tiktoken.get_encoding("o200k_base")
def num_tokens_from_messages(messages):
entire_input = ""
for message in messages:
entire_input += message["content"] + " "
tokens = encoding.encode(entire_input)
return len(tokens)
@archatas
archatas / check_image_bombs.py
Last active December 6, 2024 10:33
Check the media directory for image decompression bombs
import os
import imghdr
import warnings
from PIL import Image, ImageFile
def configure_image_safety():
"""
Configure image processing safety settings to prevent decompression bomb issues.
"""
# Increase the maximum allowed pixels to prevent warnings
@archatas
archatas / pagination.html
Last active November 27, 2024 00:06
Bootstrap 4 pagination using only https://pypi.org/project/django-query-params/
{% load query_params_tags %}
{% if page_obj.has_other_pages %}
<nav aria-label="Pagination">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a href="{% modify_query page=page_obj.previous_page_number %}" class="page-link">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
@archatas
archatas / hls.sh
Created November 11, 2024 13:37 — forked from stenuto/hls.sh
HLS ffmpeg script
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 /path/to/input.mp4 [ /path/to/output_directory ]"
exit 1
}
# Check if at least one argument (input file) is provided
if [ $# -lt 1 ]; then
@archatas
archatas / file_utils.py
Created September 11, 2024 22:15
A utility function that compares two files in the default Django storage (compatible with boto3)
def storage_file_compare(file1_path, file2_path, chunk_size=8192):
"""
Compare two files stored in Django's default_storage
:param file1_path: Path to the first file in the storage system
:param file2_path: Path to the second file in the storage system
:param chunk_size: Size of chunks to read at a time (default is 8192 bytes)
:return: True if files are identical, False otherwise
"""
import hashlib
@archatas
archatas / example.py
Last active September 3, 2024 22:06
Utility function to retrieve all the editable field names of a model form
from utils import get_modelform_fields
from myapp.forms import MyModelForm
print(get_modelform_fields(MyModelForm))
@archatas
archatas / performance.py
Last active August 21, 2024 20:29
Performance Logging Decorator
import time
from functools import wraps
import logging
import inspect
from django.conf import settings
SHOULD_LOG_PERFORMANCE = getattr(settings, "SHOULD_LOG_PERFORMANCE", False)
PERFORMANCE_LOGGER_NAME = "django.performance"
@archatas
archatas / download_chromedriver.py
Created July 31, 2024 11:29
Django Management Command that Downloads and Extracts the Latest Stable ChromeDriver
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = "Downloads the latest stable ChromeDriver"
JSON_URL = "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json"
def get_platform(self):
import sys