Skip to content

Instantly share code, notes, and snippets.

View cb109's full-sized avatar
💭
🍴 🐘

Christoph Bülter cb109

💭
🍴 🐘
View GitHub Profile
@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 January 15, 2024 11:38
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)
# 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
# 1) Initial state of models.py
from django.db import models
@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
@cb109
cb109 / closeVuetifyDialog.js
Created June 1, 2023 13:12
Close opened Vuetify Dialog from outside any Vue app (Vuetify 1.5.x)
document.querySelector('.v-dialog--active').__vue__.$options._renderChildren[0].context.isActive = false
@cb109
cb109 / 1_dropzone.html
Last active January 10, 2023 13:12
Integrate Dropzone UI with jquery AJAX POST and django-filer FilerImageField
<head>
<link rel="stylesheet" href="/static/css/dropzone.css">
<script type="text/javascript" src="/static/js/dropzone.js"></script>
</head>
<form
id="my-dropzone-form"
class="dropzone"
action="/cannot-be-empty-but-is-not-used-instead-see-submit.js"
></form>
@cb109
cb109 / django_admin_autocomplete_field_label_as_link.js
Last active January 6, 2023 13:33
Turn Django admin autocomplete_field labels into links to the chosen model instance