Skip to content

Instantly share code, notes, and snippets.

@charettes
charettes / ticket-34597-sqlite.diff
Created May 30, 2023 04:36
SQL changes when running the test suite for django#34597
--- test_add (many_to_many.tests.ManyToManyTests):sqlite:556fa4bbba
+++ test_add (many_to_many.tests.ManyToManyTests):sqlite:bbf141bcdc
@@ -41,7 +41,7 @@
INNER JOIN "many_to_many_article_publications" ON ("many_to_many_publication"."id" = "many_to_many_article_publications"."publication_id")
WHERE "many_to_many_article_publications"."article_id" = %s
ORDER BY "many_to_many_publication"."title" ASC
-ROLLBACK TO SAVEPOINT "s4705965568_x3"
+ROLLBACK TO SAVEPOINT "s4422178304_x3"
INSERT INTO "many_to_many_publication" ("title")
VALUES (%s)
@charettes
charettes / compare.py
Created November 11, 2022 01:29
Django test suite SQL output compare
import argparse
import difflib
import os
import sys
from itertools import chain
import yaml
file_name_format = "{sha}:{vendor}.yml"
@charettes
charettes / prefetch_manager_gfk.py
Created April 18, 2022 14:24
Generic foreign key that allows for a particular related manager to be used during prefetch
class PrefetchedManagerGenericForeignKey(GenericForeignKey):
def __init__(self, manager_name, *args, **kwargs):
self.manager_name = manager_name
super(PrefetchedManagerGenericForeignKey, self).__init__(*args, **kwargs)
def get_content_type_manager(self, content_type):
model_class = content_type.model_class()
return getattr(model_class, self.manager_name, model_class._base_manager)
# This is mostly a copy-paste of the GenericForeignKey implementation
@charettes
charettes / alias_module.py
Created February 12, 2020 20:57
Allow module aliasing
from __future__ import unicode_literals
import re
import sys
from importlib import import_module
class _AliasLoader(object):
def __init__(self, alias, aliased):
self.alias = alias
@charettes
charettes / operation.py
Created March 28, 2019 19:00
import_model_operation.py
class ImportForeignModel(Operation):
"""Import a model from a foreign app to this one."""
def __init__(self, app_label, model_name):
self.app_label = app_label
self.model_name = model_name.lower()
def state_forwards(self, app_label, state):
imported_model_state = state.models[self.app_label, self.model_name].clone()
# Change the app label.
#!/usr/bin/env python
"""
This script assumes you have both flake8 and flake8-commas packages
installed in the Python environment you use to run this script.
"""
import subprocess
import sys
from collections import defaultdict
diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py
index b8d7c99..8c67a18 100644
--- a/tests/test_client_regress/tests.py
+++ b/tests/test_client_regress/tests.py
@@ -467,6 +467,10 @@ class AssertRedirectsTests(SimpleTestCase):
self.assertEqual(response.request.get('wsgi.url_scheme'), 'https')
self.assertEqual(response.request.get('SERVER_NAME'), 'otherserver')
self.assertEqual(response.request.get('SERVER_PORT'), '8443')
+ response = self.client.get('/redirect_other_host/', follow=False)
+ self.assertRedirects(
# Add the common tests module as an application to allow its models
# table to be created even if they are not part of the common migations.
app_config = AppConfig.create(__package__)
app_config.label = 'common_tests'
app_config.import_models(apps.all_models[app_config.label])
apps.app_configs[app_config.label] = app_config
apps.clear_cache()
from django.db import router
from django.db.models import deletion
def ALLOW_MIGRATE_CASCADE(collector, field, sub_objs, using):
opts = sub_objs[0]._meta
if router.allow_migrate(using, opts.app_label, opts.model_name):
deletion.CASCADE(collector, field, sub_objs, using)
@charettes
charettes / test.py
Created December 23, 2015 23:12
Python 3.5 OrderedDict bug
import collections
import gc
import weakref
def create_reference():
class A(object):
pass
class B(A):
parents = collections.OrderedDict([