Skip to content

Instantly share code, notes, and snippets.

View jonashaag's full-sized avatar

Jonas Haag jonashaag

View GitHub Profile
@jonashaag
jonashaag / django_change_translation_fallback_language.py
Created September 24, 2018 11:18
Django change translation fallback language
# This is a hack so that French translation falls back to English translation,
# not German translation (which is the default locale and the original
# strings).
from django.utils.translation import trans_real
class MyDjangoTranslation(trans_real.DjangoTranslation):
def _add_fallback(self, localedirs=None):
if self._DjangoTranslation__language[:2] in {'de', 'en'}:
return super()._add_fallback(localedirs)
@jonashaag
jonashaag / celery_model_serializer.py
Last active March 28, 2022 23:14
Celery Django model serializer – pass Django model instances as arguments to Celery tasks
"""Our convenience Celery Task wrapper that allows us to conveniently pass
model instances as Task arguments.
It "serializes" model instances to IDs and "deserializes" these IDs to model
instances upon task execution.
Serialized representation of a model instance is (sentinel, app_name, model_name, pk).
"""
import celery
from django.apps import apps
@jonashaag
jonashaag / aws_fargate_docker_application_load_balancer_without_public_ip.md
Last active March 14, 2024 23:34
AWS Fargate Docker Application Load Balancer Howto (without public IP)

AWS Fargate Docker Simple Deployment Setup with SSL termination

How to:

  • create a Docker-based AWS Fargate/ECS deployment
  • without the Docker containers having a public IP
  • with an Application Load Balancer as reverse proxy / SSL termination proxy sitting in front of the containers

For Fargate/ECS to be able to access your Docker images hosted on ECR (or somewhere else) you'll have to allow outbound internet access to the Fargate subnets. Here's how you do it.

@jonashaag
jonashaag / subtest.py
Created February 5, 2018 11:14
Python subTest decorator
def subTests(params):
"""
@subTests(...)
def test_foo(self):
...
->
def test_foo(self):
for param in ...:
const alph = [
'a', '\'', '"', '&', '<', '>', '<!', '<!-', '<!--', ' ', '-', '--', '-->', '</script', '<script>', '/', '\\',
'< /script', '</ script', '< script>', '&quot', '&quot;', '&lt', '&lt;', '&gt', '&gt;'
]
const randint = (min, max) => parseInt(Math.random() * (max - min)) + min
const randletter = () => alph[randint(0, alph.length)]
const html4 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
@jonashaag
jonashaag / impl1_testcase.py
Last active November 3, 2017 09:11
Python unittest mock setUp without context manager, decorator or extra method arguments
class ClassMockTestCase(TestCase):
class_mocks = {}
def setUp(self):
super().setUp()
for attr_name, mock in self.class_mocks.items():
setattr(self, attr_name, mock.__enter__())
def tearDown(self):
super().tearDown()
@jonashaag
jonashaag / lazy-svg.js
Last active November 3, 2017 09:12
Lazy SVG placeholder
// Lazy image placeholder
$('img[data-original],.lazy-background')
.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) {
from django.middleware.locale import LocaleMiddleware
from django.urls import is_valid_path, translate_url, resolve, NoReverseMatch, Resolver404, reverse
from django.utils import translation
from django.conf import settings
from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit
def guess_translated_url(url, target_language):
parsed = urlsplit(url)
import django.views.debug
class ApiResponseLoggerMixin:
def initial(self, request, *args, **kwargs):
"""Logging helper"""
super().initial(request, *args, **kwargs)
try:
method = getattr(self, request.method.lower())
@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."),
}