Skip to content

Commit

Permalink
Merge branch 'master' into favorite-beer-model
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbrew authored Aug 14, 2019
2 parents 528d3b8 + 575845b commit 105d243
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
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
19 changes: 19 additions & 0 deletions beers/migrations/0030_auto_20190807_2142.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.4 on 2019-08-07 21:42

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('beers', '0029_merge_20190519_1259'),
]

operations = [
migrations.AlterField(
model_name='beerprice',
name='venue',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='beer_prices', to='venues.Venue'),
),
]
2 changes: 1 addition & 1 deletion beers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def __str__(self):
class BeerPrice(models.Model):
beer = models.ForeignKey(Beer, models.CASCADE, related_name='prices')
venue = models.ForeignKey(
'venues.Venue', models.DO_NOTHING, related_name='beer_prices',
'venues.Venue', models.CASCADE, related_name='beer_prices',
)
serving_size = models.ForeignKey(
ServingSize, models.DO_NOTHING, related_name='beer_prices',
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
11 changes: 10 additions & 1 deletion taps/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

from . import models

admin.site.register(models.Tap)

class TapAdmin(admin.ModelAdmin):

list_display = ('tap_number', 'venue', 'beer', 'id')
list_filter = ('venue', )
list_select_related = ('venue', 'beer')
search_fields = ('beer__name', 'venue__name', 'beer__manufacturer__name')


admin.site.register(models.Tap, TapAdmin)

0 comments on commit 105d243

Please sign in to comment.