Skip to content

Instantly share code, notes, and snippets.

View prestontimmons's full-sized avatar

Preston Timmons prestontimmons

View GitHub Profile
@prestontimmons
prestontimmons / testcase.py
Created October 24, 2011 22:19
Lazy man's Django testcase
"""
Decrease the verbosity of writing view tests.
Old way:
self.client.get(reverse("my-view"))
self.client.post(reverse("my-view"), data={"key": "value"})
self.client.login("username", "password")
self.client.get(reverse("my-other-view"))
self.client.logout()
@prestontimmons
prestontimmons / gist:1207189
Created September 9, 2011 20:08
Install ssh-copy-id on mac
sudo curl "http://phildawson.co.uk/ssh-copy-id" -o /usr/bin/ssh-copy-id
sudo chmod +x /usr/bin/ssh-copy-id
@prestontimmons
prestontimmons / gist:1198079
Created September 6, 2011 16:34
slugify model on save
from django.template.defaultfilters import slugify as django_slugify
class MyModel(models.Model):
def save(*args, **kwargs):
# Build our slug from the name, keeping it short for
# linkability, but still guarantee uniqueness
if not self.pk and self.slug:
self.slug = self.slugify(self.slug)
@prestontimmons
prestontimmons / redis-server
Created June 29, 2011 04:14 — forked from tessro/redis-server
A CentOS initscript for Redis
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
@prestontimmons
prestontimmons / gist:952217
Created May 2, 2011 19:37
Sample mod_wsgi Django file
"""
WSGI application configuration.
Read http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango to learn
more about integrating with Django.
"""
import os
import site
import sys
@prestontimmons
prestontimmons / gist:945264
Created April 27, 2011 21:31
Saving an image to a Django model from a url
try:
contents = urllib2.urlopen(url).read()
except urllib2.HTTPError, exc:
# handle exception
pass
mymodel.image.save(file_name, ContentFile(contents))
@prestontimmons
prestontimmons / gist:920546
Created April 14, 2011 21:05
Matching template variables in Django
try:
variable_name = re.search("as (\w+)", token.contents).group(1)
except AttributeError:
raise template.TemplateSyntaxError, \
"%r tag had invalid arguments." % token.split_contents()[0]
@prestontimmons
prestontimmons / gist:885958
Created March 24, 2011 21:44
Posting data with curl

Post data to url:

curl -d "k1=v1&k2=v2" http://example.com/

Post multipart form data to url:

curl -F "photo=@test_image.jpg" -F "k1=v1" -F "k2=v2" http://example.com/
<!DOCTYPE HTML>
<!-- zero-timeout.html, L. David Baron <[email protected]>, 2010-03-07, 2010-03-09 -->
<!--
Copyright (c) 2010, The Mozilla Foundation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
@prestontimmons
prestontimmons / gist:855795
Created March 4, 2011 22:10
Carousel with javascript
<!-- Carousel -->
<div id="carousel" class="feature autorotate slidelinks group">
<!-- Slides -->
<div class="carousel-slides">
<div class="slide">
<a href="/bundles/greatest-need/"><img src="//www.gfamedia.org/images/bundles-slide/greatest-need.jpg" alt=""></a>
</div>
<div class="slide">