This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connections | |
from django.db.models.query import QuerySet | |
def explain(self): | |
cursor = connections[self.db].cursor() | |
cursor.execute('set enable_seqscan = off') | |
query, params = self.query.sql_with_params() | |
print(query % params) | |
cursor.execute('EXPLAIN ANALYZE %s' % query, params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/4916851/django-get-a-queryset-from-array-of-ids-in-specific-order/37648265#37648265 | |
from django.db.models import Case, When | |
pk_list = [10, 2, 1] | |
preserved = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(pk_list)]) | |
queryset = MyModel.objects.filter(pk__in=pk_list).order_by(preserved) | |
# pre django 1.8: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tables for owned by 'tableowner' | |
select 'drop table if exists "' || tablename || '" cascade;' from pg_tables where tableowner='tableowner'; | |
# all sequences | |
select 'drop sequence if exists "' || relname || '" cascade;' from pg_class where relkind = 'S'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function throttleDebouncePromise( | |
inner, | |
{ throttle, debounce, cache, immediateIfCached }, | |
) { | |
// Ordinary debounce functions cause issues with promises and react-select but this works well. | |
// Based on: https://stackoverflow.com/questions/35228052/debounce-function-implemented-with-promises | |
const resolveCallbacks = [] | |
const store = {} | |
let timeout = null | |
let time = new Date() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage | |
class ManifestStaticFilesStorageWithSourceMaps(ManifestStaticFilesStorage): | |
""" | |
Adds js and css source map support for ManifestStaticFilesStorage. | |
""" | |
patterns = ( | |
("*.css", ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generator_from_queryset(queryset, chunksize=2000, enumerate=False): | |
assert queryset.query.order_by is not None | |
offset = 0 | |
i = 0 | |
finished = False | |
while not finished: | |
finished = True | |
for row in queryset[offset:offset + chunksize]: | |
finished = False | |
if enumerate: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models import ExpressionWrapper, FloatField, Func | |
class Cast(Func): | |
function = 'CAST' | |
template = '%(function)s(%(expressions)s AS %(db_type)s)' | |
def __init__(self, expression, db_type): | |
# convert second positional argument to kwarg to be used in function template | |
super(Cast, self).__init__(expression, db_type=db_type) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
// hallo.js plugin to use paragraphs by default | |
// https://github.com/bergie/hallo/issues/157 | |
// https://github.com/torchbox/wagtail/issues/559 | |
function getLastChildElement(el){ | |
var lc = el.lastChild; | |
while(lc && lc.nodeType != 1) { | |
if(lc.previousSibling) | |
lc = lc.previousSibling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Hide status bar on Slack | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://*.slack.com/* | |
// @grant none | |
// ==/UserScript== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# http://serverfault.com/questions/24428/monitoring-dmesg-output | |
MAILTO=root | |
LOG=/var/log/kern.log | |
OFFSET_FILE=$0.offset | |
NewerOlder