Skip to content

Instantly share code, notes, and snippets.

View pauloxnet's full-sized avatar
🐍
https://www.paulox.net

Paolo Melchiorre pauloxnet

🐍
https://www.paulox.net
View GitHub Profile
@apollo13
apollo13 / chromelogger.py
Last active May 12, 2022 01:09
Django logging to Firefox/Chrome devtools
"""
License: MIT - https://opensource.org/licenses/MIT
ChromeLogger is a protocol which allows sending logging messages to the Browser.
This module implements simple support for Django. It consists of two components:
* `LoggingMiddleware` which is responsible for sending all log messages
associated with the request to the browser.
* `ChromeLoggerHandler` a python logging handler which collects all messages.
@lockie
lockie / trigram_migration.py
Last active January 14, 2018 11:45
A Django migration to add PostgreSQL trigram search, aka pg_trgm
# -*- coding: utf-8 -*-
# XXX this is actually useful for very special use cases, e.g. misspel suggestions:
# https://www.postgresql.org/docs/9.6/static/pgtrgm.html#AEN180626
from __future__ import unicode_literals
from django.db import migrations
from django.contrib.postgres.operations import TrigramExtension
class Migration(migrations.Migration):
operations = [
@pylover
pylover / a2dp.py
Last active March 11, 2024 03:06
Fixing bluetooth stereo headphone/headset problem in ubuntu 16.04, 16.10 and also debian jessie, with bluez5.
#! /usr/bin/env python3
"""Fixing bluetooth stereo headphone/headset problem in debian distros.
Workaround for bug: https://bugs.launchpad.net/ubuntu/+source/indicator-sound/+bug/1577197
Run it with python3.5 or higher after pairing/connecting the bluetooth stereo headphone.
This will be only fixes the bluez5 problem mentioned above .
Licence: Freeware
@raprasad
raprasad / test_unmanaged_models.md
Last active November 1, 2023 00:04
Ignoring migrations during Django testing (unmanaged databases, legacy databases, etc)

Scenario

  • Django 1.9 application with two databases:
    • Legacy database with readonly access via unmanaged models. Both Django models (models.py) and related migrations have "managed" set to False
      • 'managed': False
    • Default database holding django specific tables (e.g. auth_user, django_content_type, etc)

Testing Woes

@pauloxnet
pauloxnet / signals.py
Last active April 1, 2016 08:45 — forked from jefftriplett/signals.py
Django management command to trace / list all signals. My fork is just a few cleanups where CommandError wasn't referenced, etc.
# coding:utf-8
import gc
import inspect
import weakref
from django.core.management.base import BaseCommand, CommandError
from django.dispatch import Signal
from django.dispatch.weakref_backports import WeakMethod
from optparse import make_option
@jmichalicek
jmichalicek / PrimaryKeyInObjectOutRelatedField.py
Created February 23, 2016 18:19
Django Rest Framework RelatedField allows a PK for input but returns a fully serialized model in response
class PrimaryKeyInObjectOutRelatedField(relations.PrimaryKeyRelatedField):
"""
Django Rest Framework RelatedField which takes the primary key as input to allow setting relations,
but takes an optional `output_serializer_class` parameter, which if specified, will be used to
serialize the data in responses.
Usage:
class MyModelSerializer(serializers.ModelSerializer):
related_model = PrimaryKeyInObjectOutRelatedField(
queryset=MyOtherModel.objects.all(), output_serializer_class=MyOtherModelSerializer)
@arthuralvim
arthuralvim / permissions.py
Created September 10, 2014 14:27
Django Rest Framework - Only ajax requests are permitted.
from rest_framework.permissions import BasePermission
class IsAjaxPermission(BasePermission):
"""
Check is request is ajax.
"""
def has_object_permission(self, request, view, obj):
return request.is_ajax()
@diyan
diyan / uwsgi_json_access_log.md
Last active October 29, 2018 14:44
uWSGI logging configuration that could be used with central logging servers such as Logstash, Fluentd, etc.

uWSGI logging configuration that could be used with central logging servers such as Logstash, Fluentd, etc.

[uwsgi]
project = some_project
...

# file: prefix is required for req-logger
req-logger = file:/var/log/%(project)/%(project)_access.log
log-format = "method": "%(method)", "uri": "%(uri)", "proto": "%(proto)", "status": %(status), "referer": "%(referer)", "user_agent": "%(uagent)", "remote_addr": "%(addr)", "http_host": "%(host)", "pid": %(pid), "worker_id": %(wid), "core": %(core), "async_switches": %(switches), "io_errors": %(ioerr), "rq_size": %(cl), "rs_time_ms": %(msecs), "rs_size": %(size), "rs_header_size": %(hsize), "rs_header_count": %(headers)
CREATE TEXT SEARCH CONFIGURATION fr ( COPY = french );
ALTER TEXT SEARCH CONFIGURATION fr ALTER MAPPING
FOR hword, hword_part, word WITH unaccent, french_stem;
CREATE TEXT SEARCH CONFIGURATION en ( COPY = english );
ALTER TEXT SEARCH CONFIGURATION en ALTER MAPPING
FOR hword, hword_part, word WITH unaccent, english_stem;
CREATE TEXT SEARCH CONFIGURATION de ( COPY = german );
ALTER TEXT SEARCH CONFIGURATION de ALTER MAPPING
@cobusc
cobusc / postgresql_date_function_index_howto.md
Last active March 12, 2024 12:10
Short explanation of the issues faced when trying to create a PostgreSQL index using the date() function and how to resolve it.