Skip to content

Instantly share code, notes, and snippets.

View archatas's full-sized avatar

Aidas Bendoraitis archatas

View GitHub Profile
@archatas
archatas / backup_db.sh
Created November 26, 2025 23:32
PostgreSQL Database Backup and Restoring using Database Template
#!/usr/bin/env bash
SECONDS=0
PROJECT_PATH=/var/webapps/myproject
CRON_LOG_FILE=${PROJECT_PATH}/logs/backup_db.log
WEEK_DATE=$(LC_ALL=en_US.UTF-8 date +"%w-%A")
BACKUP_PATH=${PROJECT_PATH}/db_backups/${WEEK_DATE}.backup
LATEST_PATH=${PROJECT_PATH}/db_backups/latest.backup
source ${PROJECT_PATH}/venv/bin/activate
cd ${PROJECT_PATH}/project/myproject || exit 1
@archatas
archatas / generators.py
Last active July 23, 2025 12:11
Generate django-imagekit versions on the fly
from imagekit import ImageSpec
from imagekit.processors import ResizeToFill, ResizeToFit
from PIL import Image
class Avatar80x80(ImageSpec):
processors = [ResizeToFill(80, 80)]
format = "PNG"
@archatas
archatas / benchmark_views.py
Last active July 24, 2025 14:21
A script to benchmark authenticated Django views
#!/usr/bin/env python3
"""
Django Views Benchmark Script
This script benchmarks Django views by measuring their response times.
It handles authentication via Django's login system and maintains session state.
Usage:
python benchmarks/benchmark_views.py [--host HOST] [--iterations N] [--warmup-requests N]
import requests
import base64
# Authentication - prompt for sensitive information
username = input("Enter your Bitbucket username: ")
app_password = input("Enter your Bitbucket app password (Generate this in Bitbucket settings: https://bitbucket.org/account/settings/app-passwords/): ")
auth_string = f"{username}:{app_password}"
encoded_auth = base64.b64encode(auth_string.encode()).decode()
headers = {
"Authorization": f"Basic {encoded_auth}",
@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