diff --git a/spa/api/v1/BackboneCompatibleResource.py b/spa/api/v1/BackboneCompatibleResource.py index 9ae8086..8308ec5 100644 --- a/spa/api/v1/BackboneCompatibleResource.py +++ b/spa/api/v1/BackboneCompatibleResource.py @@ -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 \ No newline at end of file + def prepend_urls(self): + return [ + url(r"^(?P%s)/(?P\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})/(?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 + """ \ No newline at end of file diff --git a/spa/api/v1/MixResource.py b/spa/api/v1/MixResource.py index 04fa9da..fd10b97 100644 --- a/spa/api/v1/MixResource.py +++ b/spa/api/v1/MixResource.py @@ -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 diff --git a/static/js/app/views/mix.js b/static/js/app/views/mix.js index a4ae453..c91bb0d 100644 --- a/static/js/app/views/mix.js +++ b/static/js/app/views/mix.js @@ -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;