snipt/snipts/api.py

98 lines
3.4 KiB
Python
Raw Normal View History

2011-10-01 10:23:05 -07:00
from tastypie.resources import ModelResource
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-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-01 16:09:59 -07:00
class PublicCommentSniptResource(ModelResource):
class Meta:
2011-10-02 13:07:47 -07:00
queryset = Snipt.objects.filter(public=True).order_by('-created')
2011-10-01 16:09:59 -07:00
resource_name = 'snipt'
2011-10-02 13:07:47 -07:00
fields = ['id',]
2011-10-02 13:16:58 -07:00
include_absolute_url = True
2011-10-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-01 16:09:59 -07:00
class PublicTagResource(ModelResource):
class Meta:
2011-10-02 15:54:24 -07:00
tags = Tag.objects.filter(snipt__public=True)
2011-10-02 15:38:20 -07:00
annotated = tags.annotate(count=Count('taggit_taggeditem_items__id'))
queryset = annotated.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-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
bundle.data['snipts'] = '/api/public/snipt/?tag=%d' % bundle.obj.id
2011-10-02 15:54:24 -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-01 16:09:59 -07:00
snipt = fields.ForeignKey(PublicCommentSniptResource, 'snipt')
class Meta:
2011-10-02 13:07:47 -07:00
queryset = Comment.objects.all()
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-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-01 16:09:59 -07:00
class PublicSniptResource(ModelResource):
2011-10-02 15:38:20 -07:00
comments = fields.ToManyField(PublicCommentResource, 'comment_set',
related_name='comment')
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-02 15:38:20 -07:00
fields = ['user', 'title', 'slug', 'tags', 'lexer', 'code', 'created',
'modified',]
2011-10-02 13:16:58 -07:00
include_absolute_url = True
2011-10-02 15:57:41 -07:00
cache = SimpleCache()
2011-10-01 16:09:59 -07:00
def dehydrate(self, bundle):
2011-10-02 13:16:58 -07:00
bundle.data['user'] = {
'username': bundle.obj.user.username,
'resource_uri': '/api/public/user/%d/' % bundle.obj.user.id,
'absolute_url': bundle.obj.user.get_absolute_url(),
}
2011-10-02 15:38:20 -07:00
bundle.data['embed_url'] = bundle.obj.embed_url
bundle.data['stylized'] = bundle.obj.get_stylized()
2011-10-02 13:16:58 -07:00
bundle.data['tags'] = []
for tag in bundle.obj.tags.all():
bundle.data['tags'].append({
'name': tag.name,
2011-10-02 15:54:24 -07:00
'count': tag.taggit_taggeditem_items.filter(snipt__public=True).count(),
2011-10-02 15:38:20 -07:00
'absolute_url': '/public/tag/%s/' % tag.slug,
2011-10-02 13:16:58 -07:00
'resource_uri': '/api/public/tag/%d/' % tag.id,
2011-10-02 15:38:20 -07:00
'snipts': '/api/public/snipt/?tag=%d' % tag.id,
2011-10-02 13:16:58 -07:00
})
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