From d7f2d1d7fee932ef8083a240db27ce38d24840c2 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Mon, 22 Jul 2013 12:07:43 +0100 Subject: [PATCH] Fixed no mix for unauthenticated user --- spa/api/v1/MixResource.py | 49 ++++------------------- spa/management/commands/debugRelations.py | 2 + 2 files changed, 10 insertions(+), 41 deletions(-) diff --git a/spa/api/v1/MixResource.py b/spa/api/v1/MixResource.py index f593514..feed707 100755 --- a/spa/api/v1/MixResource.py +++ b/spa/api/v1/MixResource.py @@ -1,7 +1,7 @@ from django.conf.urls import url from django.core.exceptions import ObjectDoesNotExist from django.core.paginator import Paginator, InvalidPage -from django.db.models import Count, Q +from django.db.models import Count from django.http import Http404 from django.template.loader import render_to_string from tastypie import fields @@ -21,8 +21,8 @@ from spa.models.mix import Mix class MixResource(BackboneCompatibleResource): comments = fields.ToManyField('spa.api.v1.CommentResource.CommentResource', 'comments', null=True) - #downloads = fields.ToManyField('spa.api.v1.ActivityResource.ActivityResource', 'downloads') - favourites = fields.ToManyField('spa.api.v1.UserResource.UserResource', 'favourites', related_name='favourites', full=True) + favourites = fields.ToManyField('spa.api.v1.UserResource.UserResource', 'favourites', + related_name='favourites', full=True) class Meta: queryset = Mix.objects.filter(is_active=True) @@ -133,47 +133,13 @@ class MixResource(BackboneCompatibleResource): return obj_list - """ - def build_filters(self, filters=None): - if filters is None: - filters = {} - - # TODO(Ferg@@lMoran.me): Yet another code smell - # I'm doing this shit everywhere in tastypie - here is my canonical rant about it - - # Either I'm completely missing something or tastypie was the wrong horse to back here - # There has to be a more prescriptive way to do this shit!!! - # I'm seriously considering swapping out tastpie for a more sane REST framework - # Simple stuff like deciding on - # your own url pattern - # ORM level rather than Resource level filtering - # are extremely difficult/undocumented - - - orm_filters = super(MixResource, self).build_filters(filters) - #find the non-resource filters that were stripped but the super's build_filters - #and re add them, will probably need to perform some checks here against Meta.filters - #so we can't be filtered willy-nilly - #also, have no idea how filters became a QueryDict?? - for f in filters: - if f not in ['format', 'order_by', 'sort']: - orm_filters.update({'custom': Q(f=filters.get(f))}) - - return orm_filters - """ def apply_filters(self, request, applicable_filters): semi_filtered = super(MixResource, self) \ .apply_filters(request, applicable_filters) \ .filter(waveform_generated=True) - f_type = request.GET.get('type', None) f_user = request.GET.get('user', None) - """ - if f_type == 'favourites': - semi_filtered = semi_filtered.filter(favourites__user=request.user.get_profile()) - elif f_type == 'likes': - semi_filtered = semi_filtered.filter(likes__user=request.user.get_profile()) - """ + if f_user is not None: semi_filtered = semi_filtered.filter(user__slug=f_user) else: @@ -200,17 +166,18 @@ class MixResource(BackboneCompatibleResource): bundle.data['download_count'] = bundle.obj.activity_downloads.count() bundle.data['like_count'] = bundle.obj.activity_likes.count() - bundle.data['tooltip'] = render_to_string('inc/player_tooltip.html', {'item': bundle.obj}) bundle.data['comment_count'] = bundle.obj.comments.count() bundle.data['genre-list'] = json.to_ajax(bundle.obj.genres.all(), 'description', 'slug') bundle.data['liked'] = bundle.obj.is_liked(bundle.request.user) - bundle.data['favourited'] = bundle.obj.favourites.filter(user=bundle.request.user).count() != 0 + if bundle.request.user.is_authenticated(): + bundle.data['favourited'] = bundle.obj.favourites.filter(user=bundle.request.user).count() != 0 + else: + bundle.data['favourited'] = False return bundle - def get_search(self, request, **kwargs): self.method_check(request, allowed=['get']) self.is_authenticated(request) diff --git a/spa/management/commands/debugRelations.py b/spa/management/commands/debugRelations.py index c1cfeec..bf29653 100644 --- a/spa/management/commands/debugRelations.py +++ b/spa/management/commands/debugRelations.py @@ -6,6 +6,8 @@ class Command(NoArgsCommand): def handle_noargs(self, **options): try: list = Mix.objects.filter(slug='dss-on-deepvibes-radio-17th-july-jamie-o-sullivan')[0] + for fav in list.favourites.all(): + print fav.slug pass except Exception, ex: print "Debug exception: %s" % ex.message \ No newline at end of file