Changed base resource to use prepend_urls

This commit is contained in:
Fergal Moran
2012-12-04 20:50:33 +00:00
parent 9ea6a331dd
commit 9380aec2f8
3 changed files with 44 additions and 2 deletions

View File

@@ -1,7 +1,28 @@
from django.conf.urls import url
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from tastypie import fields
from tastypie.http import HttpGone, HttpMultipleChoices
from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash
class BackboneCompatibleResource(ModelResource):
pass
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/children%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_children'), name="api_get_children"),
]
"""
def override_urls(self):
urls = []
for name, field in self.fields.items():
if isinstance(field, fields.ToManyField):
resource = r"^(?P<resource_name>{resource_name})/(?P<{related_name}>.+)/{related_resource}/$".format(
resource_name=self._meta.resource_name,
related_name=field.related_name,
related_resource=field.attribute,
)
resource = url(resource, field.to_class().wrap_view('get_list'), name="api_dispatch_detail")
urls.append(resource)
return urls
"""

View File

@@ -1,16 +1,20 @@
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.template.loader import render_to_string
from tastypie import fields
from tastypie.authorization import Authorization
from tastypie.constants import ALL_WITH_RELATIONS
from tastypie.http import HttpMultipleChoices, HttpGone
from core.serialisers import json
from spa.api.v1.BackboneCompatibleResource import BackboneCompatibleResource
from spa.api.v1.CommentResource import CommentResource
from spa.models import Genre
from spa.models.Mix import Mix
class MixResource(BackboneCompatibleResource):
comments = fields.ToManyField('spa.api.v1.CommentResource.CommentResource', 'comments', 'mix', null=True, full=True)
#comments = fields.ToManyField('spa.api.v1.CommentResource.CommentResource', 'comments', 'mix', null=True, full=True)
comments = fields.ToManyField('spa.api.v1.CommentResource.CommentResource', 'comments', 'mix', null=True)
class Meta:
queryset = Mix.objects.filter(is_active=True)
@@ -33,6 +37,17 @@ class MixResource(BackboneCompatibleResource):
return ret
def get_children(self, request, **kwargs):
try:
obj = self.cached_obj_get(request=request, **self.remove_api_resource_names(kwargs))
except ObjectDoesNotExist:
return HttpGone()
except MultipleObjectsReturned:
return HttpMultipleChoices("More than one resource is found at this URI.")
child_resource = CommentResource()
return child_resource.get_detail(request, parent_id=obj.pk)
def _unpackGenreList(self, bundle, genres):
genre_list = self._parseGenreList(genres)
bundle.obj.genres = genre_list

View File

@@ -170,6 +170,12 @@ window.MixView = Backbone.View.extend({
$('.mix-listing', this.el).append(item.el);
$('#mix-description', this.el).html(this.model.get("description"));
/*
var comments = this.model.get("comments");
var content = new CommentListView({collection:comments}).render();
$('#mix-comments', el).html(content.el);
*/
var comments = new CommentCollection();
comments.url = com.podnoms.settings.urlRoot + this.model.get("item_url") + "/comments/";
comments.mix_id = this.model.id;