Fixed comments (kinda)

This commit is contained in:
Fergal Moran
2013-12-16 22:31:35 +00:00
parent 234c36be5c
commit cbb88e0b3a
59 changed files with 18643 additions and 12768 deletions

View File

@@ -2,9 +2,9 @@ from tastypie import fields
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.http import HttpForbidden, HttpBadRequest
from tastypie.http import HttpBadRequest, HttpMethodNotAllowed, HttpUnauthorized, HttpApplicationError, HttpNotImplemented
from spa.api.v1.BackboneCompatibleResource import BackboneCompatibleResource
from spa.models import Mix
from spa.models import Mix, UserProfile
from spa.models.comment import Comment
@@ -21,32 +21,34 @@ class CommentResource(BackboneCompatibleResource):
authentication = Authentication()
always_return_data = True
def dehydrate(self, bundle):
if bundle.obj.user is not None:
bundle.data['avatar_image'] = bundle.obj.user.get_profile().get_avatar_image()
bundle.data['user_url'] = bundle.obj.user.get_profile().get_absolute_url()
bundle.data['user_name'] = bundle.obj.user.get_profile().get_nice_name()
else:
bundle.data['avatar_image'] = UserProfile.get_default_avatar_image()
bundle.data['user_url'] = "/"
bundle.data['user_name'] = "Anonymouse"
return bundle
def obj_create(self, bundle, **kwargs):
bundle.data['user'] = bundle.request.user
del bundle.data['avatar_image']
del bundle.data['user_url']
del bundle.data['user_name']
try:
if 'mix_id' in bundle.data:
mix = Mix.objects.get(pk=bundle.data['mix_id'])
mix = Mix.objects.get_by_id_or_slug(bundle.data['mix_id'])
if mix is not None:
return super(CommentResource, self).obj_create(bundle, user=bundle.request.user, mix=mix)
if bundle.request.user.is_authenticated():
return super(CommentResource, self).obj_create(bundle, user=bundle.request.user or None, mix=mix)
else:
return super(CommentResource, self).obj_create(bundle, mix=mix)
else:
return HttpBadRequest("Unable to find mix for supplied mix_id (candidate fields are slug & id).")
return HttpBadRequest("Missing mix_id field.")
except ImmediateHttpResponse, e:
self.logger.error("Error creating comment (%s)" % e.message)
return HttpUnauthorized("Git tae fuck!")
except Exception, e:
self.logger.error("Error creating comment (%s)" % e.message)
pass
raise ImmediateHttpResponse(
HttpBadRequest("Unable to hydrate comment from supplied data.")
)
def obj_update(self, bundle, skip_errors=False, **kwargs):
del bundle.data['avatar_image']
del bundle.data['user_url']
del bundle.data['user_name']
return super(CommentResource, self).obj_update(bundle, bundle.request)
def dehydrate(self, bundle):
bundle.data['avatar_image'] = bundle.obj.user.get_profile().get_small_profile_image()
bundle.data['user_url'] = bundle.obj.user.get_absolute_url()
bundle.data['user_name'] = bundle.obj.user.get_profile().get_nice_name() or bundle.obj.user.get_profile().display_name
return bundle
return HttpApplicationError("Unable to hydrate comment from supplied data.")