Skip to content

Instantly share code, notes, and snippets.

@balazs-endresz
balazs-endresz / gist:94d9a5439d692d15d5c4
Created April 22, 2015 15:03
Update fork with upstream
git remote add upstream https://github.com/whoever/whatever.git
git fetch upstream
git checkout master
git rebase upstream/master
@balazs-endresz
balazs-endresz / gist:c6fcd91eb705004fbcfa
Created April 22, 2015 12:08
List long running postgres queries
\pset format wrapped;
select now() - query_start as duration, query from pg_stat_activity order by duration desc;
@balazs-endresz
balazs-endresz / gist:5050df80015cdf443f01
Created April 7, 2015 14:34
Wagtail rich text editor paragraph issue fix
(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;
@balazs-endresz
balazs-endresz / gist:592913a3f425509d3a51
Created March 13, 2015 22:23
invalidate cached template fragments (tested with django 1.5 only)
from django.core.cache import cache
from django.utils.hashcompat import md5_constructor
from django.utils.http import urlquote
def invalidate_template_fragment(fragment_name, *variables):
args = md5_constructor(u':'.join([urlquote(var) for var in variables]))
cache_key = 'template.cache.{0}.{1}'.format(fragment_name, args.hexdigest())
cache.delete(cache_key)
@balazs-endresz
balazs-endresz / gist:f9b27514046506a7db75
Created December 2, 2014 09:34
Drop all tables and sequences in postgres
# 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';
@balazs-endresz
balazs-endresz / gist:fda4f55c6f47e57b40f7
Last active August 29, 2015 14:10
Do both rounding and float formatting in django templates
{{ value|floatformat|floatformat }}
or:
{{ value|floatformat:-1|floatformat:-1 }}
1.0 -> 1
1.01 -> 1 # this would be 1.0 with one floatformat filter
1.10 -> 1.1
1.123 -> 1.1
@balazs-endresz
balazs-endresz / gist:b29469823a1e40a85bd2
Last active August 29, 2015 14:10
Run command for all files in directory
find . -name \*.py -type f -exec sh -c 'command $0' {} \;
set ruler
set sm
set nocp
set bs=2
syn on
@balazs-endresz
balazs-endresz / gist:c4d5b342d0cd4c468a6d
Created July 29, 2014 12:03
Update the ID sequence for a given model
def fix_id_sequence(model_class):
from django.db import connection
next_val = model_class.objects.all().order_by("-id")[0].id + 1
cursor = connection.cursor()
cursor.execute("select setval('%s_id_seq', %d, True)" % (model_class._meta.db_table, next_val))
row = cursor.fetchone()
cursor.close()
return row[0]
@balazs-endresz
balazs-endresz / gist:19a816115900a123614f
Last active August 29, 2015 14:04
ssh through a remote server with fabric
fab pull_data_from_production --gateway [email protected]
# first it might ask for a password for the root user but that is actually for [email protected]
# then press Ctrl+D to continue if it opens an interactive ssh session on the production server
# alternatively use env.gateway = '[email protected]'