Skip to content

Instantly share code, notes, and snippets.

View cb109's full-sized avatar
💭
🍴 🐘

Christoph Bülter cb109

💭
🍴 🐘
View GitHub Profile
@cb109
cb109 / recover_from_lost_django_migration_files.md
Last active July 1, 2024 11:36
Recover from losing Django migration files while preserving the database (tested on Django 3.2)

Let me point out that losing migration files is a very bad situation and should be avoided at all costs, but if it happens, having a step-by-step way to preserve the database is a good thing to have. It is a terrible situation to be in if your database/models/migrations are a shared state across multiple team members, and that would require a coordinated halt and cleanup so everyone can move forward from the same base state again, with a proper migration files workflow from then on.

Let's assume you have models, any number of migration files, all of those applied to your existing database. Now for some reason, poof!, all the migration files are gone.

@cb109
cb109 / remove_django_model_field_without_actually_removing_it.md
Last active May 8, 2024 08:59
Partially unmanaged models in Django ORM aka ignore certain columns/fields

Partially ignore table fields on a Django model

There may come a day where your table includes a field that your Django app should ignore and not even be aware of, while other clients may still need to use that field in production through normal SQL commands. How to achieve this?

Removing a model field will give you a migration step like this:

        migrations.RemoveField(
 model_name="mymodel",
@cb109
cb109 / ffmpeg_overlay_progressbar.py
Created April 23, 2024 07:48
Overlay a simple growing/moving progressbar on the bottom of a video using ffmpeg
import subprocess
def add_progressbar_on_top_of_video(
input_video_filepath: str,
output_video_filepath: str,
width: int,
height: int,
duration: float,
fps: float = 25.0,
progressbar_height: int = 8,
@cb109
cb109 / class_attribute_knows_its_name_on_parent_class.py
Created April 5, 2024 08:41
Give class attributes knowledge about how they are used on their parent class
class Bar:
def __init__(self):
self.parent_class_attr_name = None
class LetClassAttributesKnowTheirName(type):
def __new__(mcs, name, bases, attributes):
for attr_name, attr_value in attributes.items():
if isinstance(attr_value, Bar):
attr_value.parent_class_attr_name = attr_name
@cb109
cb109 / base_site.html
Last active February 22, 2024 12:51
Django Admin custom clientside Search for Index (tested with Django 3.2)
{% extends "admin/base_site.html" %}
{% block usertools %}
{% url 'admin:index' as admin_index_url %}
{% if request.get_full_path == admin_index_url %}
<input
type="text"
id="admin_base_search"
placeholder="Search..."
autofocus
@cb109
cb109 / google_chrome_ignores_disable_web_security_flag.md
Created February 6, 2024 13:54
Google Chrome Launch Argument Order may matter

Google Chrome Launch Argument Order may matter

--disable-web-security VS --user-data-dir

Also see: https://peter.sh/experiments/chromium-command-line-switches/#disable-web-security

I came across an issue with running google-chrome to render some HTML file, where we are loading fonts from an external URL and to avoid CORS screaming at us we configured it like:

google-chrome --user-data-dir=/tmp/ --disable-web-security

@cb109
cb109 / django_migrate_m2m_add_through_model.py
Last active June 13, 2024 21:43
Fix: ValueError: Cannot alter field <model>.<field> into <model>.<field> - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
# Update: Turns out there is a specific tool for this kind of migration that should
# do a better job as my multistep-migration below, check out SeparateDatabaseAndState:
#
# https://docs.djangoproject.com/en/5.0/howto/writing-migrations/#changing-a-manytomanyfield-to-use-a-through-model
# Changing the through= part of a models.ManyToManyField() fails to migrate,
# as Django won't let us do this in a single step. We can manually workaround
# with several migration steps though, as shown below.
#
# Based on Django 3.2
@cb109
cb109 / gist:4039770a33ead38fb77789c107dd080f
Created September 1, 2023 10:53
Django FileField / File .path VS .name
# Django's models.FileField stores FieldFile objects. These have both .name
# and .path attributes, which however are not exactly intuitive:
my_instance.my_filefield.name
# E.g. whatever/123/foo.png
# Returns the path relative to MEDIA_ROOT, not the filename!
# For the filename do e.g. os.path.basename(my_instance.my_filefield.name)
my_instance.my_filefield.path
# E.g. /var/www/media/whatever/123/foo.png
@cb109
cb109 / mprof.sh
Created August 9, 2023 07:36
Plot memory usage of any python process
# https://pypi.org/project/memory-profiler/
pip install -U memory_profiler
mprof run --include-children --attach <pid>
mprof plot
@cb109
cb109 / fix_num_db_queries_from_test_output.py
Last active July 14, 2023 10:48
rewrite contextmanager arguments based on pytest output using redbaron
"""A script to help update many querycount assertions in code quickly.
Requirements:
pip install redbaron
Deprecation note:
redbaron (based on baron) is somewhat unmaintained and only supports
Python grammar u to version 3.7, so for newer Python versions we may