snipt/snipt/snipts/api.py

85 lines
3.1 KiB
Python
Raw Normal View History

2011-10-03 17:42:00 -07:00
from tastypie.resources import ModelResource, ALL_WITH_RELATIONS
2011-10-01 16:09:59 -07:00
from django.contrib.auth.models import User
from snipts.models import Comment, Snipt
2011-10-02 15:57:41 -07:00
from tastypie.cache import SimpleCache
2011-10-02 15:38:20 -07:00
from django.db.models import Count
2011-10-01 16:09:59 -07:00
from tastypie import fields
2011-10-02 13:07:47 -07:00
from taggit.models import Tag
2011-10-01 10:23:05 -07:00
2011-10-02 15:38:20 -07:00
2011-10-01 16:09:59 -07:00
class PublicUserResource(ModelResource):
class Meta:
2011-10-02 13:07:47 -07:00
queryset = User.objects.all()
2011-10-01 16:09:59 -07:00
resource_name = 'user'
2011-10-02 13:07:47 -07:00
fields = ['username',]
2011-10-02 13:16:58 -07:00
include_absolute_url = True
2011-10-03 17:42:00 -07:00
allowed_methods = ['get']
list_allowed_methods = []
2011-10-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-01 16:09:59 -07:00
2011-10-03 17:42:00 -07:00
def dehydrate(self, bundle):
bundle.data['snipts'] = '/api/public/snipt/?user=%d' % bundle.obj.id
return bundle
2011-10-01 16:09:59 -07:00
class PublicTagResource(ModelResource):
class Meta:
2011-10-03 17:42:00 -07:00
queryset = Tag.objects.filter(snipt__public=True)
queryset = queryset.annotate(count=Count('taggit_taggeditem_items__id'))
queryset = queryset.order_by('-count')
2011-10-01 16:09:59 -07:00
resource_name = 'tag'
2011-10-02 13:16:58 -07:00
fields = ['name',]
2011-10-03 17:42:00 -07:00
allowed_methods = ['get']
2011-10-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-02 15:38:20 -07:00
def dehydrate(self, bundle):
bundle.data['absolute_url'] = '/public/tag/%s/' % bundle.obj.slug
2011-10-10 21:13:18 -07:00
bundle.data['snipts'] = '/api/public/snipt/?tag=%d' % bundle.obj.id
2011-10-03 17:42:00 -07:00
bundle.data['count'] = bundle.obj.taggit_taggeditem_items.filter(
snipt__public=True).count()
2011-10-02 15:38:20 -07:00
return bundle
2011-10-01 16:09:59 -07:00
class PublicCommentResource(ModelResource):
2011-10-02 13:07:47 -07:00
user = fields.ForeignKey(PublicUserResource, 'user')
2011-10-03 17:42:00 -07:00
snipt = fields.ForeignKey('snipts.api.PublicSniptResource', 'snipt')
2011-10-01 16:09:59 -07:00
class Meta:
2011-10-03 17:42:00 -07:00
queryset = Comment.objects.filter(snipt__public=True).order_by('-created')
2011-10-01 16:09:59 -07:00
resource_name = 'comment'
2011-10-02 13:07:47 -07:00
fields = ['user', 'snipt', 'comment', 'created', 'modified',]
2011-10-02 13:16:58 -07:00
include_absolute_url = True
2011-10-03 17:42:00 -07:00
allowed_methods = ['get']
2011-10-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-01 16:09:59 -07:00
class PublicSniptResource(ModelResource):
2011-10-03 17:42:00 -07:00
user = fields.ForeignKey(PublicUserResource, 'user', full=True)
tags = fields.ToManyField(PublicTagResource, 'tags', related_name='tag', full=True)
2011-10-02 15:38:20 -07:00
comments = fields.ToManyField(PublicCommentResource, 'comment_set',
2011-10-03 17:42:00 -07:00
related_name='comment', full=True)
2011-10-01 10:23:05 -07:00
class Meta:
2011-10-02 13:07:47 -07:00
queryset = Snipt.objects.filter(public=True).order_by('-created')
2011-10-01 10:23:05 -07:00
resource_name = 'snipt'
2011-10-03 17:42:00 -07:00
fields = ['title', 'slug', 'lexer', 'code', 'created', 'modified',]
2011-10-02 13:16:58 -07:00
include_absolute_url = True
2011-10-03 17:42:00 -07:00
allowed_methods = ['get']
filtering = { 'user': 'exact', }
2011-10-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-01 16:09:59 -07:00
def dehydrate(self, bundle):
2011-10-03 17:42:00 -07:00
bundle.data['embed_url'] = bundle.obj.get_embed_url()
2011-10-02 15:38:20 -07:00
bundle.data['stylized'] = bundle.obj.get_stylized()
2011-10-01 16:09:59 -07:00
return bundle
2011-10-02 15:38:20 -07:00
def build_filters(self, filters=None):
if filters is None:
filters = {}
orm_filters = super(PublicSniptResource, self).build_filters(filters)
if 'tag' in filters:
tag = Tag.objects.get(pk=filters['tag'])
tagged_items = tag.taggit_taggeditem_items.all()
orm_filters['pk__in'] = [i.object_id for i in tagged_items]
return orm_filters