Skip to content

Commit

Permalink
Fix 500 error on /api/v1/venues/byslug/my-bar/beers/ (#237)
Browse files Browse the repository at this point in the history
Addresses #236.
  • Loading branch information
drewbrew authored and mjcarroll committed Aug 21, 2019
1 parent 575845b commit cb5fc6d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
22 changes: 21 additions & 1 deletion venues/test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from faker import Faker

from hsv_dot_beer.users.test.factories import UserFactory
from beers.models import Beer
from venues.models import Venue, VenueAPIConfiguration
from beers.test.factories import BeerFactory
from beers.test.factories import BeerFactory, ManufacturerFactory
from taps.models import Tap
from taps.test.factories import TapFactory
from .factories import VenueFactory

Expand Down Expand Up @@ -98,6 +100,24 @@ def test_get_ok(self):
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data['id'], self.venue.id)

def test_beers(self):
mfg = ManufacturerFactory()
beers = Beer.objects.bulk_create(BeerFactory.build(
manufacturer=mfg
) for dummy in range(10))
taps = Tap.objects.bulk_create(
Tap(
beer=beer,
tap_number=index + 1,
venue=self.venue,
) for index, beer in enumerate(beers)
)
response = self.client.get(f'{self.url}beers/')
self.assertEqual(len(taps), len(beers))
self.assertEqual(Beer.objects.count(), len(beers))
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(len(response.data['results']), len(beers))


class TestVenueDetailTestCase(APITestCase):
"""
Expand Down
20 changes: 20 additions & 0 deletions venues/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ class VenueBySlugViewSet(VenueViewSet):
def list(self, request, *args, **kwargs):
raise NotFound()

@method_decorator(cache_page(60 * 5))
@action(detail=True, methods=['GET'])
def beers(self, request, slug):
from beers.views import BeerViewSet
from beers.filters import BeerFilterSet

queryset = BeerViewSet.queryset.filter(
**{f'taps__venue__{self.lookup_field}': slug}
).distinct()

# let the user use all the beer filters just for kicks
queryset = BeerFilterSet(request.query_params, queryset=queryset).qs

page = self.paginate_queryset(queryset)
if page is not None:
serializer = BeerViewSet.serializer_class(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = BeerViewSet.serializer_class(queryset, many=True)
return Response(serializer.data)

lookup_field = 'slug'
serializer_class = serializers.VenueBySlugSerializer

Expand Down

0 comments on commit cb5fc6d

Please sign in to comment.