Skip to content

Instantly share code, notes, and snippets.

View bohde's full-sized avatar

Rowan Bohde bohde

View GitHub Profile
@bohde
bohde / example.js
Created February 23, 2011 00:47
Example of using curl and jquery with Tastypie
var data = JSON.stringify({
"user": "/api/v1/user/1",
"title": "My New Blog Post",
"body": "Something interesting should be here..."
});
$.ajax({
url: '/api/v1/entry/',
type: 'POST',
contentType: 'application/json',
class HiddenFieldResource(ModelResource):
secret = fields.CharField(help_text="Public lookup key.")
def hydrate_secret(self, bundle):
lookup_key = bundle.obj.secret
bundle.obj.secret = lookup_secret(lookup_key)
return bundle
def dehydrate_secret(self, bundle):
bundle.data['secret'] = bundle.obj.secret.lookup_key
@bohde
bohde / models.py
Created March 3, 2011 17:55
Auto Create a User Profile
from django.contrib.auth.models import User
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(User)
silly_field = models.CharField(max_length=255, default="Silly")
def auto_create_profile(sender, instance, created, **kwargs):
if created:
user.profile, created = Profile.objects.get_or_create(user=instance)
<!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
@bohde
bohde / embedded_docs.json
Created April 5, 2011 23:53
sample implementation of embedded resources
{
"name": "foo",
"resource_uri": "/api/v1/list/1",
"items": {
"meta": {
"resource_uri": "/api/v1/list/1/items/",
"count": 5
},
"objects": [
@bohde
bohde / resources.py
Created April 13, 2011 15:52
Dispatch to a different resource if the pk is 'self'
class UserResource(ModelResource):
def dispatch_detail(request, **kwargs):
if kwargs.get('pk') == "self":
# This assumes that self.is_authenticated sets request.user
self.is_authenticated(request)
kwargs['pk'] = request.user.id
return CurrentUserResource.dispatch_detail(request, **kwargs)
return super(UserResource, self).dispatch_detail(request, **kwargs)
@bohde
bohde / tastypie.log
Created April 14, 2011 21:15
#tastypie chat log
07:10 < bedspax> uhm
07:10 < bedspax> i'm confused :)
07:16 < danielzilla> DanGer: Hard to say without seeing your code. If the template has inheritance, the context will act as a list, so you might need to try ``resp.context[-1]``.
07:16 < bedspax> danielzilla, hi
07:16 < bedspax> for my doubts, what you advice to me?
07:17 < danielzilla> bedspax: I have no clue what you're asking about.
07:18 < bedspax> i would have in the same resource this three resources, that are related to the model User: http://dpaste.com/hold/531887/
07:19 < danielzilla> I'm sorry, I don't understand. Are you trying to combine them into one resource, or just get the ``ForeignKey`` fields working.
07:20 < bedspax> ok np
07:20 < bedspax> the first
@bohde
bohde / api.py
Created May 18, 2011 14:19
Example of ToMany resources in Django Tastypie
v1_api = Api(api_name='v1')
v1_api.register(BookResource())
# Intentionally left out PageResource
@bohde
bohde / app.coffee
Created June 4, 2011 17:13
Simple GET Param to CouchDB app
_ = require 'underscore'
express = require 'express'
cradle = require 'cradle'
class Queue
# Class to store the queue documents to store in Couch.
constructor: ->
_.bindAll @, 'flush'
@reqs = []
@bohde
bohde / resources.py
Created July 6, 2011 13:07
Tastypie support for Geodjango distance filtering
class MyGeoResource(Resource):
def apply_sorting(self, objects, options=None):
if options and "longitude" in options and "latitude" in options:
return objects.distance(Point(options['latitude'], options['longitude'])).order_by('distance')
return super(MyGeoResource, self).apply_sorting(objects, options)