Skip to content

Instantly share code, notes, and snippets.

View beniwohli's full-sized avatar

Benjamin Wohlwend beniwohli

View GitHub Profile
@beniwohli
beniwohli / README.md
Created September 13, 2018 14:22
How to keep Django Rest Framework viewset actions separate on Elastic APM

How to keep Django Rest Framework viewset actions separate on Elastic APM

Elastic APM by default uses the name of the Django view as the identifier of a transaction, combined with the request method. While this works well in general, it leads to confusing results with view sets from Django Rest Framework. It cobbles list and detail views together, as well as any custom actions you might have defined in your viewset.

You can work around this by adding the action of the viewset to the transaction name. Simply add the provided mixin to any view set:

class BooksViewSet(ViewSetTransactionNameMixin, viewsets.ModelViewSet):

your code here

@beniwohli
beniwohli / timer.py
Last active February 17, 2016 20:10
Timer decorator / context manager
import time
import functools
class timer(object):
"""
Usage
As decorator:
@beniwohli
beniwohli / keybase.md
Last active September 25, 2017 11:33

Keybase proof

I hereby claim:

  • I am beniwohli on github.
  • I am piquadrat (https://keybase.io/piquadrat) on keybase.
  • I have a public key ASAAy1hyA09WpNUHd9eW89Ox1Blx6qb1km6J1yz8Jc-bUgo

To claim this, I am signing this object:

@beniwohli
beniwohli / test.py
Last active October 20, 2015 13:02
override template debug in Django 1.8+
with override_settings(
TEMPLATES=[
{
'BACKEND': settings.TEMPLATES[0]['BACKEND'],
'DIRS': settings.TEMPLATES[0]['DIRS'],
'OPTIONS': {
'context_processors': settings.TEMPLATES[0]['OPTIONS']['context_processors'],
'loaders': settings.TEMPLATES[0]['OPTIONS']['loaders'],
'debug': True,
},
import logging
from django.conf import settings as django_settings
from opbeat.contrib.django.models import client
def _is_ignorable_404(uri):
"""
Returns True if the given request *shouldn't* notify the site managers.
import django
from django.conf import settings
import celery
class Celery(celery.Celery):
def on_configure(self):
django.setup()
from opbeat.contrib.django.models import client, logger, register_handlers
from opbeat.contrib.celery import register_signal
@beniwohli
beniwohli / utils.py
Created March 27, 2014 09:02
linkify/hashtagify
# -*- coding: utf-8 -*-
import re
_linkify_re = re.compile(
r"""(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))""")
_hashtag_re = re.compile(r'(?: |^)#(\w{2,})', re.UNICODE)
def _link_replace(match):
href = match.groups(1)[0]
@beniwohli
beniwohli / copy_site.py
Created March 3, 2014 12:35
Management command to copy a whole CMS site
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.contrib.sites.models import Site
from django.db import transaction
from cms.models import Page
class Command(BaseCommand):
@beniwohli
beniwohli / solr.xml
Created September 24, 2013 13:07
template for schema.xml that is compatible with newer Solr versions
<?xml version="1.0" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
@beniwohli
beniwohli / forms.py
Created November 9, 2012 10:47
A form that validates that the user doesn't change (some) hidden inputs
from django import forms
from django.core import signing
from django.utils.translation import ugettext_lazy as _
class SignedForm(forms.Form):
bad_signature_error = _("You've been a naughty boy!")
signed_fields = None
signature = forms.CharField(widget=forms.HiddenInput, required=True)