Skip to content

Instantly share code, notes, and snippets.

@powellc
powellc / copy_db.sh
Created February 14, 2013 17:44
A simple shell script to drop a staging database and clone the production database in it's place
sudo -u postgres dropdb <staging_db>
sudo -u postgres createdb -O <staging_user> -T <production_db> <staging_db>
@powellc
powellc / dj_ci_boostrap.sh
Created February 25, 2013 06:11
A building block for django app integrations with Jenkins
#!/bin/bash -ex
cd $WORKSPACE
virtualenv -q --no-site-packages --distribute venv
source venv/bin/activate
pip install -r etc/requirements.txt
ln -fs `pwd`/local_settings_template.py `pwd`/local_settings.py
python manage.py syncdb --noinput
python manage.py migrate
@powellc
powellc / queryset-troubles-django-cbv.md
Last active December 16, 2015 15:09
In Django, class-based views are neat and all, but there be monsters out there too.

Queryset Troubles in Django Class-based Views

So in a recent project I had a very strange publishing bug where two models out of more than a dozen or so, were tossing out 404s where a detail page should be. We had just instituted a lot of caching pieces to the project, which turned out to be red-herrings and probably had something to do with how long it took to track down this bug. But for the sake of the internet and anyone else who finds their model's publish datetimes strangely cached despite their best efforts to get them to show up, here's some good advice:

Don't use the "queryset" model attribute for your view class, override the "get_queryset" function.

In code:

@powellc
powellc / johnny-cache-management-commands.md
Created May 7, 2013 21:31
A nice big caveat for using Johnny Cache to speed up django ORM queries when you also have to use management tasks.

Johnny Cache & Management Commands

Discovered a pretty huge caveat to using Johnny Cache to speed up Django ORM queries.

Johnny Cache is a really great library that can speed up Django sites that are slowing down with large joins and complex data models. Django has a built in ORM caching mechanism, but Johnny Cache takes it one step further.

That said, there are some monsters in the undiscovered ocean. One of the big ones is,

@powellc
powellc / backup.sh
Created May 28, 2013 12:31
Shell script to use duplicity and AWS S3 to backup files on a server, hooray. Not the most secure way to handle your password and AWS key, but couldn't get easier, giving you no reason to have some kind of backup of your server.
#!/bin/bash
export PASSPHRASE=your_gpg_passphrase
export AWS_ACCESS_KEY_ID=your_s3_access_key
export AWS_SECRET_ACCESS_KEY=your_s3_secret
export MYSQL_PASSWORD=your_mysql_password
for DB in app_1 app_2 app_3; do
mysqldump -h localhost -u backup_user -p${MYSQL_PASSWORD} ${DB} | gzip > /var/dbdumps/${DB}.sql
@powellc
powellc / save_as-with-unique-slugs
Created January 5, 2014 06:01
ModelAdmin code to make save_as work for models with unique slugs
def add_view(self, request, form_url='', extra_context=None):
save_as_new = request.POST.get("_saveasnew", '')
if extra_context is None:
extra_context = {}
# keep the save as new in case of validation errors
extra_context['save_as_new'] = save_as_new
# but if it is a real new addition, remove _saveasnew from POST
if not save_as_new and '_saveasnew' in request.POST:
del request.POST['_saveasnew']
@powellc
powellc / gogs.conf
Created October 28, 2014 14:29
Gogs Upstart Script
#
# upstart script for gogs (Go Git Service)
#
author "tsv"
description "gogs upstart script"
start on (local-filesystems and net-device-up)
stop on shutdown
@powellc
powellc / keybase.md
Last active February 10, 2016 21:28

Keybase proof

I hereby claim:

  • I am powellc on github.
  • I am secstate (https://keybase.io/secstate) on keybase.
  • I have a public key ASBxgRKwG3tnw7tfdz9pf0b2oIvtQrkboSA3EbFL7aDObQo

To claim this, I am signing this object:

@powellc
powellc / gist:de1b1a73cf814b16e39f
Created October 5, 2015 15:20
Toggl-Button-Todoist
// ==UserScript==
// @name Toggl-Button GitHub
// @namespace https://github.com/jurgenhaas/toggl-button-greasemonkey
// @version 1.3
// @include http*://github.com/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant GM_getResourceText
// @grant GM_getValue
// @grant GM_setValue
@powellc
powellc / zshrc
Last active February 10, 2016 17:22
My zshrc file
# Path to your oh-my-zsh installation.
export ZSH=~/.oh-my-zsh
# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
ZSH_THEME="agnoster"
# Uncomment the following line to use case-sensitive completion.