Skip to content

Instantly share code, notes, and snippets.

# Copyright (c) 2012, Bin Tan
# This file is distributed under the BSD Licence. See python-epub-builder-license.txt for details.
# James Outterside - Modified code from http://code.google.com/p/python-epub-builder/source/browse/trunk/epub.py to use
# use django templates instead of genshi
import itertools
import mimetypes
import os
import shutil
@airtonix
airtonix / authorization.py
Last active December 16, 2015 17:58
An authorization class for dango-tastypie that limits queryset results to those whose designated field points at the current user or when the designated field is None, when the User instance is the same as the request.user
from tastypie.authorization import DjangoAuthorization, Authorization
class UserObjectsOnlyAuthorization(Authorization):
"""
:user_field: specify object owner field on the model as `user_field`
class Something(models.Model):
the_owner = models.ForeignKey('auth.User')
@airtonix
airtonix / authorization.py
Created April 28, 2013 09:54
A django-tastypie Authorization Class that uses django-guardian for row level permission checking.
import logging
from tastypie.authorization import DjangoAuthorization
from tastypie.exceptions import Unauthorized
from guardian.shortcuts import get_objects_for_user
from guardian.core import ObjectPermissionChecker
logger = logging.getLogger(__name__)
@airtonix
airtonix / admin.py
Last active December 16, 2015 19:38
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
from tastypie.models import ApiAccess, ApiKey
from models import UserProfile
try:
admin.site.unregister(User)
admin.site.unregister(ApiKey)
@airtonix
airtonix / slugify
Last active December 16, 2015 22:19
slugify for dash (the bash like interpreter on Ubuntu)
#!/bin/sh
#
# Installation
# mkdir ~/bin
# cd ~/bin
# wget http://gist.github.com/raw//5506188/slugify
# chmod +x ~/bin/slugify
#
# Usage
# $ lspci > ~/Desktop/pci-devices-$(slugify `date`).report
@airtonix
airtonix / discovery.py
Created May 7, 2013 20:57
And example of how I deal with automatically registering tastypie resources for the urlconf
# PROJECT/base.lib.discovery
#
from functools import wraps
import traceback
import sys
registry = {}
@airtonix
airtonix / webserver
Last active December 17, 2015 05:59
improved simple python local webserver.
#!/usr/bin/env python
"""
Installation
------------
mkdir -p ~/bin
wget https://gist.github.com/airtonix/5562266 -o ~/bin/webserver
chmod +x ~/bin/webserver
@airtonix
airtonix / service.tastypie.js
Created May 14, 2013 03:06
Tasty Pie Model Service. Handles apikey authentication (storing the key in html5 local storage)
/*
Angular Tasty Pie Service
-------------------------
Requires
--------
- https://github.com/grevory/angular-local-storage
Features
@airtonix
airtonix / angular.zurb.js
Created June 14, 2013 11:00
The beginnings of a angular-zurb frankenstein.
angular.module('reveal.directives', [])
.directive('zurbReveal', function($revealService, $log){
return {
restrict: 'A',
link: function postLink(scope, element, attrs) {
scope.$watch(attrs.zurbReveal, function(url){
if(typeof(url)=='undefined') url = attrs.zurbReveal;
element.bind( "click", function(){
scope.$emit("event:modal-request", url, scope, attrs.revealOptions);
});