Skip to content

Instantly share code, notes, and snippets.

View jonashaag's full-sized avatar

Jonas Haag jonashaag

View GitHub Profile
diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
index 2103788..14b46f1 100644
--- a/django/contrib/admin/widgets.py
+++ b/django/contrib/admin/widgets.py
@@ -255,7 +255,6 @@ class RelatedFieldWidgetWrapper(forms.Widget):
can_change_related=False, can_delete_related=False):
self.needs_multipart_form = widget.needs_multipart_form
self.attrs = widget.attrs
- self.choices = widget.choices
self.widget = widget
6855871 function calls (6855838 primitive calls) in 12.051 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
346704 1.364 0.000 1.364 0.000 {method 'astype' of 'numpy.generic' objects}
12 1.049 0.087 1.864 0.155 encoder.py:204(iterencode)
2 1.031 0.516 1.031 0.516 decoder.py:345(raw_decode)
35488 0.542 0.000 3.842 0.000 properties.py:809(properties_with_values)
115516 0.509 0.000 0.509 0.000 {built-in method numpy.core.multiarray.array}
@jonashaag
jonashaag / django-elb-allowed-hosts_settings.py
Last active July 17, 2017 08:43
Python 3 ELB Django ALLOWED_HOSTS
# See: https://stackoverflow.com/questions/27720254/django-allowed-hosts-with-elb-healthcheck
def get_ELB_hostname():
import urllib.request
import urllib.error
try:
fileobj = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/local-ipv4', timeout=0.1)
return fileobj.read().decode('ascii')
except urllib.error.URLError:
return None
@jonashaag
jonashaag / remove_slash_middleware.py
Created August 15, 2017 17:07
Django remove slash middleware
from django.conf import settings
from django.urls import is_valid_path
from django.middleware.common import CommonMiddleware
class RemoveSlashCommonMiddleware(CommonMiddleware):
"""REMOVE_SLASH works like APPEND_SLASH, but reversed."""
def process_request(self, request):
# Check if a slash should be removed
if self.should_redirect_with_no_slash(request):
@jonashaag
jonashaag / csp_exempt_middleware.py
Created August 21, 2017 20:20
CSP exempt middleware for django-csp
from django.conf import settings
try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
class MiddlewareMixin(object):
"""
If this middleware doesn't exist, this is an older version of django
and we don't need it.
@jonashaag
jonashaag / 1fast_email_backend.py
Last active June 14, 2021 14:10
Fast Django development email backend (socket.getfqdn() slowness)
from django.core.mail.message import make_msgid
class FastMailBackendMixin:
"""Work around slow development email backend due to slow socket.getfqdn() call.
This simply uses "example.com" instead of your local machine's hostname.
"""
def send_messages(self, messages):
for message in messages:
@jonashaag
jonashaag / lazy.js
Last active August 25, 2017 09:10
jquery-lazyload placeholder images
$('img[data-original]')
.lazyload()
.each(function (_, e) {
// Empty placeholder image so that page does not "jump" while real images are being loaded
e = $(e)
if (!e.loaded) {
const w = e.attr('data-width')
const h = e.attr('data-height')
if (w && h) {
e.attr('src', `data:image/svg+xml;charset=utf-8,%3Csvg width%3D'${w}' height%3D'${h}' xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg' %2F%3E`)
@jonashaag
jonashaag / admin_custom_manager.py
Created September 13, 2017 10:43
Django admin manager override
class CustomManagerMixin:
"""Hack to override the manager used by the admin interface.
Set ``model_manager`` in subclasses to specify a custom manager.
"""
def get_queryset(self, *args, **kwargs):
default_manager = self.model._meta.default_manager
try:
self.model._meta.default_manager = self.model_manager
@jonashaag
jonashaag / suppress_exceptions.py
Last active November 30, 2017 14:00
Suppress Python exceptions in production (with statement/context manager)
class suppress_exceptions_in_production:
"""Allow exceptions in production; propagate exceptions during development.
Usage:
with suppress_exceptions_in_production() as result:
...
Exceptions that happen in the ... block are suppressed but logged using the
``logger`` given (or a default logger if none is given). Exceptions are
@jonashaag
jonashaag / require_password_mixin.py
Created September 28, 2017 08:57
Django require password confirmation for submit mixin
import django.contrib.auth
import django.core.exceptions
from django import forms
from django.utils.translation import ugettext_lazy as _
class RequiresPasswordMixin(forms.Form):
error_messages = {
'password_incorrect': _("Your old password was entered incorrectly. Please enter it again."),
}