Skip to content

Instantly share code, notes, and snippets.

@balazs-endresz
balazs-endresz / .py
Last active February 12, 2024 15:15
Order queryset by list of IDs
# 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:
from itertools import islice, chain
class QuerySetChain(object):
"""
Chains multiple subquerysets (possibly of different models) and behaves as
one queryset. Supports minimal methods needed for use with
django.core.paginator.
http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view#answer-432666
"""
@balazs-endresz
balazs-endresz / gist:dd31cc5e2dee45829e72
Last active August 29, 2015 14:01
Find duplicates in queryset
from django.db.models import Count
def duplicates_by(model, field):
values = model.objects\
.values(field)\
.annotate(Count(field))\
.order_by(field)\
.filter(**{'%s__count__gt' % field: 1})\
.values_list(field, flat=True)
@balazs-endresz
balazs-endresz / gist:00e47a6d05485bdbcbd1
Created May 21, 2014 14:07
View commits on the current branch only
gitk --first-parent --no-merges
@balazs-endresz
balazs-endresz / gist:0944f36aa38241fd25f8
Last active August 29, 2015 14:01
Inclusive daterange generator
from datetime import timedelta
def daterange(start_date, end_date):
for n in range(1 + int((end_date - start_date).days)):
yield start_date + timedelta(n)
@balazs-endresz
balazs-endresz / gist:ba2f9f8cb1d01424b2b1
Last active August 29, 2015 14:01
Find duplicates in LDAP
# might need to set olcSizeLimit: 10000
ldapsearch -x -h localhost -b dc=domain,dc=org,dc=uk mail | grep '^mail:'|sort|uniq -c|sort -n|awk '$1 > 1 { print }'
cat file | python -mjson.tool
@balazs-endresz
balazs-endresz / gist:2fec55cccf7dd541328c
Created June 2, 2014 13:35
Convert supported files to an RGB image with Wand/ImageMagick
import subprocess
from django.core.mail import mail_admins
def convert_to_rgb(local_file):
"""
Returns an RGB Wand image or one with the original colourspace if the conversion fails.
Colourspace conversion is currently not working with the Wand API: https://github.com/dahlia/wand/issues/110
"""
in_filename = '%s[0]' % local_file.name
out_filename = '%s_rgb.jpg' % local_file.name
@balazs-endresz
balazs-endresz / gist:4b1f8730db672d8e8afc
Created July 9, 2014 09:26
Processes listening on port
lsof -Pnl +M -i4|grep :80
@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]'