Skip to content

Instantly share code, notes, and snippets.

View darkpixel's full-sized avatar

Aaron C. de Bruyn darkpixel

View GitHub Profile
@darkpixel
darkpixel / gist:1418860
Created December 1, 2011 18:38
Tweaked django csv export helper from http://djangosnippets.org/snippets/790/
def GenericCSVExport(qs, fields=None):
from django.db.models.loading import get_model
from django.http import HttpResponse, HttpResponseForbidden
from django.template.defaultfilters import slugify
import csv
model = qs.model
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % slugify(model.__name__)
writer = csv.writer(response)
@darkpixel
darkpixel / gist:3473470
Last active February 25, 2023 22:34
Don't lose model association with a Session object when logging in
def cycle_key(self):
#TODO: Errors here will tank the system, probably need some better handling...
old_session_key = self.session_key
old_session = Session.objects.get(session_key=old_session_key)
try:
cart = Cart.objects.get(session=old_session)
super(SessionStore, self).cycle_key()
new_session_key = self.session_key
new_session = Session.objects.get(session_key=new_session_key)
cart.session = new_session
@darkpixel
darkpixel / gist:30da06041cd339eecbb2
Last active April 26, 2016 17:29
Restore cryptolocker encrypted files if you have ZFS
# If you have a box running ZFS serving your Windows file shares, and someone gets hit with CryptoLocker, here's an easy way to restore individually encrypted files.
# Assuming your infected dataset is named 'tank/officeshare'
# Find a snapshot of your data before cryptolocker infected it and clone it (something like zfs clone tank/officeshare@good tank/officesharegood
# Find a snapshot of your data after cryptolocker infected everything, but *after* the infected machines were removed from the network and clone it (something like zfs clone tank/officeshare@bad tank/officesharebad)
#Go into the tank/officesharebad directory and run the following command to scan through the bad clone for the 'DECRYPT_INSTRUCTION.TXT' file left behind by cryptolocker, gather 'bad' directories, then grab all the files from them to restore to the current 'officeshare'.
#This command works on FreeNAS or BSD-ish boxes
find . -type f -name 'DECRYPT_INSTRUCTION.TXT' | sed 's/\/DECRYPT_INSTRUCTION.TXT//' | sed 's/^\.\///' | grep -v
### Keybase proof
I hereby claim:
* I am darkpixel on github.
* I am darkpixel (https://keybase.io/darkpixel) on keybase.
* I have a public key whose fingerprint is 3ADF CE15 BA18 FA79 DB4A 4C9B F11F 3E8D DE4B A04C
To claim this, I am signing this object:
@darkpixel
darkpixel / gist:02accb7529ba20c09998
Created February 25, 2015 16:58
Fun with sample address data
self.address_list = [
'221B Baker St.//London/OH', # Sherlock Holmes
'1313 Mockingbird Lane//Mockingbird Heights/CA', # The Munsters
'0001 Cemetery Lane//Chicago/IL', # The Adams Family
'344 Clinton St./Apt 3B/Metropolis/NY', # Clark Kent
'112.5 Beacon St.//Boston/MA', # Cheers
'742 Evergreen Terrace//Springfield/OR', # The Simpsons
'42 Wallaby Way//Sydney/WA', # Clownfish (Finding Nemo)
'508 Saint Cloud Road//Bel Air/CA', # Fresh Prince
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use JSON;
my $webhook_url = 'your-webhook-url';
my $default_channel = '#your-channel';
my $ua = LWP::UserAgent->new;
$ua->timeout(15);
@darkpixel
darkpixel / login.bat
Created August 17, 2015 19:35
The worst login file I have ever seen...
This is the worst login file I have ever seen.
I redacted a few things with '---' to protect the identity of the MCP who created it, and the idiots who kept updating it, and the unfortunate client who just ditched them because nothing ever worked right...
REM @echo off
REM universal login batch file
REM Put individual user settings below in the user section.
REM Modified by --- on 5/21/03 for new server
REM Modified by --- on 11/11/03 (added check for term server)
REM Modified by --- on 9/9/008 to reflect fact that server2 is now ntserver.
@darkpixel
darkpixel / gist:e8f494038d5192225901
Last active February 29, 2016 19:59
ProgrammingError: column "company.owner_id" must appear in the GROUP BY clause or be used in the aggregate function
Please ignore the horrible DB schema. It's not mine. It's an introspection of a horrible Windows ticket app.
I upgraded from Django 1.8.x to 1.9 and the following query that I use when gathering stats (Top ticket creators) broke:
>>> Company.objects.all().annotate(ticketcount=Count('srservice'))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/models/query.py", line 234, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/aaron/.virtualenvs/intranet/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
# Django
11:57:29 [aaron@ender:~/code/mspdna-receiver] master(+3/-3)* ± ab -s 60 -r -n 1000 -c 1000 -p test.json -T application/json http://127.0.0.1:8000/hook/v2/31038d0b2f2e01083001905ad22a759c67b0b908/a52b6347-37c3-49d4-b7f8-dd389c4dd0b8/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
@darkpixel
darkpixel / iterdrives.py
Created June 25, 2016 17:43
iterdrives.py
import probstat, os, sys, popen2
# List of partitions possibly used in the array
li = ['/dev/hde1', '/dev/hdg1', '/dev/hdi1', '/dev/hdk1', '/dev/sdc1', '/dev/sdd1', '/dev/sde1', '/dev/sdf1']
# Assemble = Number of Devices in Array - Number of Parity Drives
# RAID 6 uses 2 parity drives. So for an array of 10 partitions in RAID 6
# you would set assemble to 8
assemble = 5