Skip to content

Instantly share code, notes, and snippets.

View seddonym's full-sized avatar

David Seddon seddonym

View GitHub Profile
@seddonym
seddonym / durability.py
Last active September 13, 2024 09:24
Durable decorator
import functools
from django.conf import settings
from django.db import transaction, utils
def durable(func):
"""
Decorator to ensure that a function is not being called within an atomic block.
@seddonym
seddonym / django_forms_forms.py
Last active March 10, 2017 17:48
Form signals idea
from . import signals
# ...
class BaseForm:
# ...
def _clean_form(self):
try:
signals.pre_clean.dispatch(sender=self.__class__, form=self)
cleaned_data = self.clean()
signals.post_clean.dispatch(sender=self.__class__, form=self, cleaned_data)
except ValidationError as e:
@seddonym
seddonym / my_test.py
Last active September 9, 2020 17:05
Django test class for asserting the connection of a receiver to a given signal
from django.test import TestCase
class ReceiverConnectionTestCase(TestCase):
"""TestCase that allows asserting that a given receiver is connected to a signal.
Important: this will work correctly providing you:
1. Do not import or patch anything in the module containing the receiver in any django.test.TestCase.
2. Do not import (except in the context of a method) the module containing the receiver in any test module.
@seddonym
seddonym / myapp.highlighting.py
Created August 19, 2015 09:09
How to override faulty behaviour of Haystack highlighting
import re
from haystack.utils.highlighting import Highlighter as FaultyHighlighter
class Highlighter(FaultyHighlighter):
"""Highlighter that fixes the issue with highlighting parts of words:
https://github.com/django-haystack/django-haystack/issues/378
"""
def find_highlightable_words(self):
@seddonym
seddonym / models.py
Created March 19, 2015 16:43
Clarification of behaviour of Django exclude() filters that span relationships
from django.db import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __unicode__(self): # __unicode__ on Python 2
return self.name
class Author(models.Model):