Skip to content

Instantly share code, notes, and snippets.

View archatas's full-sized avatar

Aidas Bendoraitis archatas

View GitHub Profile
@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
@archatas
archatas / settings.py
Created July 29, 2024 16:45
get_secret() function that uses a JSON file for Django project secrets
import os
import json
from django.core.exceptions import ImproperlyConfigured
with open(os.path.join(os.path.dirname(__file__), "secrets.json"), "r") as f:
secrets = json.loads(f.read())
def get_secret(setting):
@archatas
archatas / example.html
Created July 16, 2024 23:28
Implementation of a field reset based on a default value from a URL path
<div class="form-field">
<label for="title">Title</label>
<input type="text" id="title" />
<button type="button"
class="reset_field"
data-url="http://example.com/default-title/"
data-target="#title"
>Reset title</button>
</div>
@archatas
archatas / dynamic_inclusion_tag.py
Created July 14, 2024 12:36
A Django template tag decorator for including a dynamically set template with context variables
from functools import wraps
from inspect import getfullargspec, unwrap
from django import template
register = template.Library()
def dynamic_inclusion_tag(filename=None, func=None, takes_context=None, name=None):
"""
@archatas
archatas / validators.py
Last active May 18, 2024 23:31
CSS Color Validator for Django models and forms
import re
from django.core.exceptions import ValidationError
def validate_css_color(value):
"""
Custom validation function for background color field.
Args:
value (str): The value entered for the background color field.