Done with v1 of public API
parent
d229d4f057
commit
c62d7fbfb8
|
@ -1,4 +1,4 @@
|
||||||
from tastypie.resources import ModelResource
|
from tastypie.resources import ModelResource, ALL_WITH_RELATIONS
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from snipts.models import Comment, Snipt
|
from snipts.models import Comment, Snipt
|
||||||
from tastypie.cache import SimpleCache
|
from tastypie.cache import SimpleCache
|
||||||
|
@ -13,74 +13,61 @@ class PublicUserResource(ModelResource):
|
||||||
resource_name = 'user'
|
resource_name = 'user'
|
||||||
fields = ['username',]
|
fields = ['username',]
|
||||||
include_absolute_url = True
|
include_absolute_url = True
|
||||||
|
allowed_methods = ['get']
|
||||||
|
list_allowed_methods = []
|
||||||
cache = SimpleCache()
|
cache = SimpleCache()
|
||||||
|
|
||||||
class PublicCommentSniptResource(ModelResource):
|
def dehydrate(self, bundle):
|
||||||
class Meta:
|
bundle.data['snipts'] = '/api/public/snipt/?user=%d' % bundle.obj.id
|
||||||
queryset = Snipt.objects.filter(public=True).order_by('-created')
|
return bundle
|
||||||
resource_name = 'snipt'
|
|
||||||
fields = ['id',]
|
|
||||||
include_absolute_url = True
|
|
||||||
cache = SimpleCache()
|
|
||||||
|
|
||||||
class PublicTagResource(ModelResource):
|
class PublicTagResource(ModelResource):
|
||||||
class Meta:
|
class Meta:
|
||||||
tags = Tag.objects.filter(snipt__public=True)
|
queryset = Tag.objects.filter(snipt__public=True)
|
||||||
annotated = tags.annotate(count=Count('taggit_taggeditem_items__id'))
|
queryset = queryset.annotate(count=Count('taggit_taggeditem_items__id'))
|
||||||
queryset = annotated.order_by('-count')
|
queryset = queryset.order_by('-count')
|
||||||
resource_name = 'tag'
|
resource_name = 'tag'
|
||||||
fields = ['name',]
|
fields = ['name',]
|
||||||
|
allowed_methods = ['get']
|
||||||
cache = SimpleCache()
|
cache = SimpleCache()
|
||||||
|
|
||||||
def dehydrate(self, bundle):
|
def dehydrate(self, bundle):
|
||||||
bundle.data['absolute_url'] = '/public/tag/%s/' % bundle.obj.slug
|
bundle.data['absolute_url'] = '/public/tag/%s/' % bundle.obj.slug
|
||||||
bundle.data['snipts'] = '/api/public/snipt/?tag=%d' % bundle.obj.id
|
bundle.data['snipts'] = '/api/public/snipt/?tag=%d' % bundle.obj.id
|
||||||
bundle.data['count'] = bundle.obj.taggit_taggeditem_items.filter(snipt__public=True).count()
|
bundle.data['count'] = bundle.obj.taggit_taggeditem_items.filter(
|
||||||
|
snipt__public=True).count()
|
||||||
return bundle
|
return bundle
|
||||||
|
|
||||||
class PublicCommentResource(ModelResource):
|
class PublicCommentResource(ModelResource):
|
||||||
user = fields.ForeignKey(PublicUserResource, 'user')
|
user = fields.ForeignKey(PublicUserResource, 'user')
|
||||||
snipt = fields.ForeignKey(PublicCommentSniptResource, 'snipt')
|
snipt = fields.ForeignKey('snipts.api.PublicSniptResource', 'snipt')
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
queryset = Comment.objects.all()
|
queryset = Comment.objects.filter(snipt__public=True).order_by('-created')
|
||||||
resource_name = 'comment'
|
resource_name = 'comment'
|
||||||
fields = ['user', 'snipt', 'comment', 'created', 'modified',]
|
fields = ['user', 'snipt', 'comment', 'created', 'modified',]
|
||||||
include_absolute_url = True
|
include_absolute_url = True
|
||||||
|
allowed_methods = ['get']
|
||||||
cache = SimpleCache()
|
cache = SimpleCache()
|
||||||
|
|
||||||
class PublicSniptResource(ModelResource):
|
class PublicSniptResource(ModelResource):
|
||||||
|
user = fields.ForeignKey(PublicUserResource, 'user', full=True)
|
||||||
|
tags = fields.ToManyField(PublicTagResource, 'tags', related_name='tag', full=True)
|
||||||
comments = fields.ToManyField(PublicCommentResource, 'comment_set',
|
comments = fields.ToManyField(PublicCommentResource, 'comment_set',
|
||||||
related_name='comment')
|
related_name='comment', full=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
queryset = Snipt.objects.filter(public=True).order_by('-created')
|
queryset = Snipt.objects.filter(public=True).order_by('-created')
|
||||||
resource_name = 'snipt'
|
resource_name = 'snipt'
|
||||||
fields = ['user', 'title', 'slug', 'tags', 'lexer', 'code', 'created',
|
fields = ['title', 'slug', 'lexer', 'code', 'created', 'modified',]
|
||||||
'modified',]
|
|
||||||
include_absolute_url = True
|
include_absolute_url = True
|
||||||
|
allowed_methods = ['get']
|
||||||
|
filtering = { 'user': 'exact', }
|
||||||
cache = SimpleCache()
|
cache = SimpleCache()
|
||||||
|
|
||||||
def dehydrate(self, bundle):
|
def dehydrate(self, bundle):
|
||||||
bundle.data['user'] = {
|
bundle.data['embed_url'] = bundle.obj.get_embed_url()
|
||||||
'username': bundle.obj.user.username,
|
|
||||||
'resource_uri': '/api/public/user/%d/' % bundle.obj.user.id,
|
|
||||||
'absolute_url': bundle.obj.user.get_absolute_url(),
|
|
||||||
}
|
|
||||||
|
|
||||||
bundle.data['embed_url'] = bundle.obj.embed_url
|
|
||||||
bundle.data['stylized'] = bundle.obj.get_stylized()
|
bundle.data['stylized'] = bundle.obj.get_stylized()
|
||||||
|
|
||||||
bundle.data['tags'] = []
|
|
||||||
for tag in bundle.obj.tags.all():
|
|
||||||
bundle.data['tags'].append({
|
|
||||||
'name': tag.name,
|
|
||||||
'count': tag.taggit_taggeditem_items.filter(snipt__public=True).count(),
|
|
||||||
'absolute_url': '/public/tag/%s/' % tag.slug,
|
|
||||||
'resource_uri': '/api/public/tag/%d/' % tag.id,
|
|
||||||
'snipts': '/api/public/snipt/?tag=%d' % tag.id,
|
|
||||||
})
|
|
||||||
|
|
||||||
return bundle
|
return bundle
|
||||||
|
|
||||||
def build_filters(self, filters=None):
|
def build_filters(self, filters=None):
|
||||||
|
|
|
@ -30,7 +30,7 @@ class Snipt(models.Model):
|
||||||
key = models.CharField(max_length=100)
|
key = models.CharField(max_length=100)
|
||||||
public = models.BooleanField(default=False)
|
public = models.BooleanField(default=False)
|
||||||
|
|
||||||
# TODO Set auto_now_add back to True for production!
|
# TODO Set back to True for production!
|
||||||
created = models.DateTimeField(auto_now_add=False, editable=False)
|
created = models.DateTimeField(auto_now_add=False, editable=False)
|
||||||
modified = models.DateTimeField(auto_now=False, editable=False)
|
modified = models.DateTimeField(auto_now=False, editable=False)
|
||||||
|
|
||||||
|
@ -57,8 +57,7 @@ class Snipt(models.Model):
|
||||||
else:
|
else:
|
||||||
return self.stylized
|
return self.stylized
|
||||||
|
|
||||||
@property
|
def get_embed_url(self):
|
||||||
def embed_url(self):
|
|
||||||
return 'http%s://%s/embed/%s/' % ('s' if settings.USE_HTTPS else '',
|
return 'http%s://%s/embed/%s/' % ('s' if settings.USE_HTTPS else '',
|
||||||
site.domain,
|
site.domain,
|
||||||
self.key)
|
self.key)
|
||||||
|
@ -71,9 +70,12 @@ class Comment(models.Model):
|
||||||
|
|
||||||
comment = models.TextField()
|
comment = models.TextField()
|
||||||
|
|
||||||
# TODO Set auto_now_add back to True for production!
|
# TODO Set back to True for production!
|
||||||
created = models.DateTimeField(auto_now_add=False, editable=False)
|
created = models.DateTimeField(auto_now_add=False, editable=False)
|
||||||
modified = models.DateTimeField(auto_now=False, editable=False)
|
modified = models.DateTimeField(auto_now=False, editable=False)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return u'%s on %s' %(self.user, self.snipt)
|
return u'%s on %s' %(self.user, self.snipt)
|
||||||
|
|
||||||
|
def get_absolute_url(self):
|
||||||
|
return '%s#comment-%d' % (self.snipt.get_absolute_url(), self.id)
|
||||||
|
|
Loading…
Reference in New Issue