Skip to content

Instantly share code, notes, and snippets.

View obeattie's full-sized avatar

Oliver Beattie obeattie

View GitHub Profile
from cStringIO import StringIO
from decimal import Decimal
from PIL import Image
from urllib2 import urlopen
from django.db import transaction
from django.core.files.base import ContentFile
from ilovephotos.blacktip import models
from ilovephotos.blacktip.util.parse_help import verify_args, get_list_ids
@obeattie
obeattie / db_utils.py
Created October 14, 2009 12:51
Exposes SQLAlchemy's sessions and transactions as context managers (so they will be managed automatically inside blocks), and also provides a transaction decorator, which wraps an entire function in a transaction
"""Utilities for managing database sessions."""
from __future__ import with_statement
import contextlib
import functools
@contextlib.contextmanager
def temp_session(session_cls, **kwargs):
"""Quick and dirty context manager that provides a temporary Session object
to the nested block. The session is always closed at the end of the block.
@obeattie
obeattie / datetime_range.py
Created October 19, 2009 12:31
A datetime range class for Python, for (obviously) dealing with ranges of datetimes.
"""Provides a `DateTimeRange` class, which is used for managing ranges of datetimes."""
import datetime
class DateTimeRange(object):
"""Represents a range of datetimes, with a start and (optionally) an end.
Basically implements most of the methods on a standard sequence data type to provide
some lovely syntactic sugar. Specifically, you can iterate on this, index it, slice it,
use the in operator, reverse it, and use it in a boolean context to see if there is any
time in between the start and end."""
[color]
ui = auto
[user]
name = Oliver Beattie
email = [email protected]
[alias]
stat = status
co = checkout
br = branch
[core]
@obeattie
obeattie / basecamp-custom.css
Created November 17, 2009 12:18
A user stylesheet to make Basecamp a little bit prettier. Custom colors, a bit of jiggery-pokery with the header spacing, hide the milestones calendar from the dashboard page, hide the company logo (if you have one).
/* Basic color replacements */
a:link, a:visited {
color: #039 !important;
}
a:hover {
color: #fff !important;
background-color: #039 !important;
}
Element.implement({
closest: function(selector){
/* Matches the nearest element with the passed selector -- much like
* Element.getParent, but with the starting element included */
return (this.match(selector)) ? this : this.getParent(selector);
},
addLiveEvent: function(eventType, selector, handler){
/* Binds a 'live event' to all the element. Whenever the event is allowed to bubble
* up to this element, event.target is checked to see whether it matches the passed
* selector. If it does, the handler will be fired */
@obeattie
obeattie / ignorant_paginator.py
Created April 2, 2010 06:34
A Django Paginator implementation that is stupid. It only knows whether it can navigate forward or back, not how many pages are available. Useful for object lists of an unknown length.
from django.core.paginator import EmptyPage, InvalidPage, PageNotAnInteger, Page, Paginator
class IgnorantPage(Page):
"""The just-as-ignorant stepchild of IgnorantPaginator and Page."""
def __init__(self, *args, **kwargs):
ret = super(IgnorantPage, self).__init__(*args, **kwargs)
# Now, we need to know if we have a first and last dealio
self.__has_next = (len(self.object_list) > self.paginator.per_page)
self.object_list = self.object_list[:self.paginator.per_page]
return ret
@obeattie
obeattie / hide-blogger-warning.user.js
Created April 11, 2010 08:39
Automatically skips the obnoxious "Content warning" on Blogger sites (only those with blogspot.com domains) — and doesn't require a page reload to do so. Winnah. To install using Google Chrome or Grasemonkey, click raw and you'll be prompted to install th
// ==UserScript==
// @name Blogger Content Warning Skip
// @description Automatically skips the content warning on blogspot.com sites without reloading the page
// @match http://*.blogspot.com/*
// ==/UserScript==
var fireEvent = function(obj,evt){
var fireOnThis = obj;
if (document.createEvent) {
var evObj = document.createEvent('MouseEvents');