Skip to content

Commit

Permalink
Enable sort by recently tapped (#198)
Browse files Browse the repository at this point in the history
* Add ability to sort beers by most recently added to a tap

This looks across *all* taps, so depending on how STA handles
changing kegs, this might make Monkeynaut always seem new.

* Validate that we don't add extra queries with the annotation
  • Loading branch information
drewbrew authored Aug 14, 2019
1 parent cf3fdfa commit 575845b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions beers/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BeerFilterSet(FilterSet):
fields=[
'name', 'abv', 'ibu', 'style__name',
'style__alternate_names__name', 'manufacturer__name',
'most_recently_added',
],
)

Expand Down
31 changes: 31 additions & 0 deletions beers/test/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from dateutil.relativedelta import relativedelta
from django.utils.timezone import now
from django.urls import reverse
from nose.tools import eq_
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -161,3 +163,32 @@ def test_on_tap_no_dupes(self):
eq_(response.status_code, 200)
eq_(len(response.data['results']), 1, response.data)
eq_(response.data['results'][0]['name'], self.beer.name, response.data)

def test_sort_by_time_added(self):
# create a tap for a second beer in between the two
other_beer = BeerFactory()
TapFactory(
beer=other_beer,
time_added=now() - relativedelta(hours=5, minutes=2),
)
# create another beer on tap with no time added, so therefore it's now
third_beer = TapFactory(beer=BeerFactory()).beer

# create two taps for the first beer
TapFactory(beer=self.beer, time_added=now() - relativedelta(hours=1))
TapFactory(beer=self.beer, time_added=now() - relativedelta(hours=16))

# 5 queries:
# 1. count of results
# 2. beers
# 3. taps
# 4. alt names
# 5. prices
with self.assertNumQueries(5):
response = self.client.get(f'{self.url}?o=-most_recently_added')
eq_(response.status_code, 200)
# expected order is third_beer, self.beer, other_beer
eq_(len(response.data['results']), 3, response.data)
eq_(response.data['results'][0]['name'], third_beer.name, response.data)
eq_(response.data['results'][1]['name'], self.beer.name, response.data)
eq_(response.data['results'][2]['name'], other_beer.name, response.data)
4 changes: 3 additions & 1 deletion beers/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from django.db import transaction
from django.db.utils import IntegrityError
from django.db.models import Prefetch, Count
from django.db.models import Prefetch, Count, Max
from django.http import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
Expand Down Expand Up @@ -73,6 +73,8 @@ class BeerViewSet(CachedListMixin, ModerationMixin, ModelViewSet):
'venue', 'serving_size',
)
),
).annotate(
most_recently_added=Max('taps__time_added'),
).order_by('manufacturer__name', 'name')
filterset_class = filters.BeerFilterSet

Expand Down

0 comments on commit 575845b

Please sign in to comment.