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
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 | |
""" |
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 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) |
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
gitk --first-parent --no-merges |
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 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) |
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
# 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 }' |
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
cat file | python -mjson.tool |
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
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 |
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
lsof -Pnl +M -i4|grep :80 |
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
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]' |
OlderNewer