Skip to content

Instantly share code, notes, and snippets.

View toastdriven's full-sized avatar

Daniel Lindsley toastdriven

View GitHub Profile
from tastypie.resources import ModelResource
from yourapp.api.cache import CustomCache
from yourapp.models import YourModel
class SampleResource(ModelResource):
class Meta:
queryset = YourModel.objects.all()
resource_name = 'sample'
class MyResource(ModelResource):
class Meta:
queryset = MyModel.objects.all()
resource_name = 'myresource'
def override_urls(self):
return [
url(r"^(?P<resource_name>%s)/compare%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_compare'), name="api_get_compare"),
]
@toastdriven
toastdriven / setZeroTimeout.html
Created March 24, 2011 16:42
This is amazingly fast. Don't want to lose it.
<!DOCTYPE HTML>
<!-- zero-timeout.html, L. David Baron <dbaron@dbaron.org>, 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
@toastdriven
toastdriven / user1.py
Created April 12, 2011 17:27
First example uses a ``OneToOne`` profile, the second uses the traditional Django profile bits.
from django.contrib.auth.models import User
# from tastypie.authentication import ApiKeyAuthentication
from tastypie.authorization import DjangoAuthorization
from tastypie import fields
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from core.api.authentication import OpenReadApiKeyAuthentication
class UserResource(ModelResource):
bio = fields.CharField(attribute='profile__bio', null=True)
[04/13/2011 -:- 10:19:17 AM] daniellindsley (~daniellin@gateway.the-worldco.com) joined the channel.
[04/13/2011 -:- 10:19:17 AM] Topic is Tastypie: Creating delicious APIs for Django | Google Group @ http://groups.google.com/group/django-tastypie/ | Submit issues @ http://github.com/toastdriven/django-tastypie/issues
[04/13/2011 -:- 10:19:17 AM] Set by daniellindsley on November 8, 2010 1:54:47 PM CST
[04/13/2011 -:- 10:23:04 AM] Disconnected
[04/13/2011 -:- 10:23:13 AM] daniellindsley (~daniellin@gateway.the-worldco.com) joined the channel.
[04/13/2011 -:- 10:23:13 AM] Topic is Tastypie: Creating delicious APIs for Django | Google Group @ http://groups.google.com/group/django-tastypie/ | Submit issues @ http://github.com/toastdriven/django-tastypie/issues
[04/13/2011 -:- 10:23:14 AM] Set by daniellindsley on November 8, 2010 1:54:47 PM CST
[04/13/2011 -:- 10:42:36 AM] joshbohde: gudmundur: You could probably overwrite dispatch to check if the pk is "self" and if so grab the id of the current use
@toastdriven
toastdriven / hackday1.txt
Created April 20, 2011 03:54
Mediaphormedia Hack Day #1
matt_c:
* Ellington Story & Section inlines
* Redis PubSub + Node + Whitelist = real-time activity on site
codysoyland:
* Gevent + 0MQ multi-test runner
fxdgear:
* More improvements on SocialBeer.me + Portal 2
import datetime
from haystack import indexes
from haystack import site
from myapp.models import Note
class NoteIndex(indexes.SearchIndex):
text = indexes.CharField(document=True, use_template=True)
author = indexes.CharField(model_attr='user')
pub_date = indexes.DateTimeField(model_attr='pub_date')
class MyResource(ModelResource):
def obj_create(self, bundle, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_create``.
"""
bundle.obj = self._meta.object_class()
for key, value in kwargs.items():
setattr(bundle.obj, key, value)
class ParentResource(ModelResource):
children = fields.ToManyField(ChildResource, 'children')
def override_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/children%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_children'), name="api_get_children"),
]
def get_children(self, request, **kwargs):
try:
class SampleResource(ModelResource):
# ... then ...
def dehydrate(self, bundle):
bundle = super(SampleResource, self).dehydrate(bundle)
bundle.data['the_field'] = call_method_that_returns_the_dict()
return bundle
# Or...