Skip to content

Instantly share code, notes, and snippets.

import base64
from tastypie.fields import FileField
from django.core.files.uploadedfile import SimpleUploadedFile
class Base64FileField(FileField):
"""
A django-tastypie field for handling file-uploads through raw post data.
It uses base64 for en-/decoding the contents of the file.
>>> from taggit.models import Tag
>>> Tag.objects.all()
[<Tag: Administrative>, <Tag: Philantrophy>, <Tag: Fundraising>, <Tag: Management>, <Tag: Food Preparation/Cooking>, <Tag: Labor>, <Tag: Food Service>, <Tag: Musician>, <Tag: Emergency Services>, <Tag: Medical Services>, <Tag: Leadership>, <Tag: Teaching>, <Tag: Manual Labor>, <Tag: Cleaning>, <Tag: Clerical>, <Tag: Math>, <Tag: Science>, <Tag: Law>, <Tag: Spanish>, <Tag: Social Justice>, '...(remaining elements truncated)...']
>>> OpportunityNeed.objects.filter(skills__name='Leadership').query
<django.db.models.sql.query.Query object at 0x2b2ad10>
>>> print OpportunityNeed.objects.filter(skills__name='Leadership').query
SELECT "opportunities_opportunityneed"."id", "opportunities_opportunityneed"."created", "opportunities_opportunityneed"."modified", "opportunities_opportunityneed"."kind", "opportunities_opportunityneed"."number", "opportunities_opportunityneed"."location_id", "opportunities_opportunityneed"."event_id" FROM "opportunities_opportunityneed
@issackelly
issackelly / pleaseread.markdown
Created May 27, 2012 23:44
What you can do about the Tastypie perms branch

Hey.

I know you'd like the perms branch merged into master. Here's what would be helpful:

  • Try it. Work with it in its current state and document any trouble you have. If there is a bug, the best thing you can do is write a test for it. Beyond that, writing a test and then a patch is great.
  • Use other methods. There's nothing in the perms branch that is groundbreaking, and you can apply permissions at several different spots already. Tastypie was designed to be extended, so you should be able to make the changes you need in your code. You should be able to get your work done without it.
  • Fork it, use something else, write your own.

What not to do:

def check_strange_installation():
default_exists = set([
'admin',
'displayRules',
'favicon.ico',
'index.php',
'resources',
'templates',
'variables.php',
'apple-touch-icon-72x72.png',
@issackelly
issackelly / signing.coffee
Created September 17, 2011 14:31
Experimental implementation of Django Signing in JavaScript
base64 = require 'base64'
crypto = require 'crypto'
gzip = require 'gzip'
class BadSignature extends Error
constructor: (msg="Signature does not match") ->
super msg
class SignatureExpired extends BadSignature
constructor: (msg="Signature timestamp is older than required max_age") ->
@issackelly
issackelly / api.py
Created September 2, 2011 19:51
Project Model
class ProjectResource(ModelResource):
class Meta:
queryset = Project.objects.all()
@issackelly
issackelly / views.py
Created August 30, 2011 15:07
method_decorator around a Django Class Based View for login_required
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views.generic.base import TemplateView
import datetime
from accounts.forms import NewAccountForm
>>> def funkyTon(foo, bar, baz=None):
... return [i*i for i in range(foo, bar)]
...
>>> funkyTon(1, baz=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: funkyTon() takes at least 2 arguments (2 given)
>>>
{
"run_list": [
"main::default",
"main::python",
"main::nginx",
"main::munin",
"main::security",
"main::postgresql",
"main::postgis"
],
@issackelly
issackelly / gist:1075119
Created July 11, 2011 00:12
Show method_decorator on a Class Based View
from django.utils.decorators import method_decorator
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
class MaiView(TemplateView):
template_name = "mai/view.html"
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):