Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / pg_chg_table_owner.sh
Last active December 11, 2015 17:48
Change owner of all tables in a PG database, great for those times when your dev user doesn't match production or whatever.
echo "Your database name [Enter]: "
read YOUR_DB
echo "New owner name [Enter]: "
read NEW_OWNER
# First alter tables
for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" $YOUR_DB` ; do
psql -c "alter table $tbl owner to $NEW_OWNER" $YOUR_DB ;
done
@powellc
powellc / msql_backup_all.sh
Last active December 11, 2015 17:08
MySQL backup script for nightly dumps that clean up after a certain number of days and keep a symlink to the latest one so you can easily setup a webserver to provide dumps for development purposes
#!/bin/bash
DB_BACKUP="/var/backups/databases/"
LATEST_DIR="/var/backups/databases/latest/"
DB_USER="root"
DB_PASSWD="secretttt"
HN=`hostname | awk -F. '{print $1}'`
# Remove backups older than 10 days
@powellc
powellc / pg_backup_all.sh
Last active October 1, 2023 19:14
Bash script to backup all postgresql databases on a server, run with cron once a day or 5 times a day, whatever. Just updated it so it ignores your postgres db, and also bzips the backups and adds a symlink to a latest directory. Sweet.
#!/bin/bash
# Location to place backups.
backup_dir="/var/backups/databases/"
nightly_dir="/var/backups/databases/latest/"
#String to append to the name of the backup files
backup_date=`date +%d-%m-%Y`
#Numbers of days you want to keep copie of your databases
number_of_days=15
databases=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
for i in $databases; do if [ "$i" != "postgres" ] && [ "$i" != "template0" ] && [ "$i" != "template1" ] && [ "$i" != "template_postgis" ]; then
@powellc
powellc / run.py
Created March 7, 2011 20:21
A development script to run django with gevent instead of flup
#!/usr/bin/python
import sys
sys.path.append('<path_to_virtualenv>')
from gevent import monkey; monkey.patch_all()
from gevent.wsgi import WSGIServer
import os
import traceback
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import call_command
diff --git a/src/adzone/managers.py b/src/adzone/managers.py
index 347e85e..271e36d 100644
--- a/src/adzone/managers.py
+++ b/src/adzone/managers.py
@@ -8,8 +8,15 @@ class AdManager(models.Manager):
Returns a random advert that belongs to the specified category and zone
"""
- try:
- ad = self.get_query_set().filter(category__slug=ad_category, zone__slug=ad_zone).order_by('?')[0]
#!/bin/bash
###
### This script simply parses ifconfig and loads a box's main IP into a shell variable
###
OS=`uname`
IP="" # store IP
case $OS in
Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' | grep 24 `;;