mirror of
https://github.com/fergalmoran/dss.git
synced 2025-12-26 03:27:33 +00:00
Initial commit after change to backbone/SPA
This commit is contained in:
36
spa/api/v1/CommentResource.py
Normal file
36
spa/api/v1/CommentResource.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import datetime
|
||||
import humanize
|
||||
from tastypie import fields
|
||||
from tastypie.authentication import Authentication
|
||||
from tastypie.authorization import Authorization
|
||||
from spa.api.v1.BackboneCompatibleResource import BackboneCompatibleResource
|
||||
from spa.models import Comment
|
||||
|
||||
class CommentResource(BackboneCompatibleResource):
|
||||
mix = fields.ToOneField('spa.api.v1.MixResource.MixResource', 'mix')
|
||||
|
||||
class Meta:
|
||||
queryset = Comment.objects.all().order_by('-date_created')
|
||||
resource_name = 'comments'
|
||||
filtering = {
|
||||
"mix": ('exact',),
|
||||
}
|
||||
authorization = Authorization()
|
||||
authentication = Authentication()
|
||||
always_return_data = True
|
||||
|
||||
def obj_create(self, bundle, request, **kwargs):
|
||||
bundle.data['user'] = {'pk': request.user.pk}
|
||||
return super(CommentResource, self).obj_create(bundle, request, user=request.user)
|
||||
|
||||
def dehydrate_date_created(self, bundle):
|
||||
if (datetime.datetime.now() - bundle.obj.date_created) <= datetime.timedelta(days=1):
|
||||
return humanize.naturaltime(bundle.obj.date_created)
|
||||
else:
|
||||
return humanize.naturalday(bundle.obj.date_created)
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
bundle.data['avatar_image'] = bundle.obj.user.get_profile().get_avatar_image(150)
|
||||
bundle.data['user_url'] = bundle.obj.user.get_absolute_url()
|
||||
bundle.data['user_name'] = bundle.obj.user.get_profile().nice_name()
|
||||
return bundle
|
||||
Reference in New Issue
Block a user