Skip to content

Commit

Permalink
Merge pull request #66 from Qabel/m/trafficfx
Browse files Browse the repository at this point in the history
query DB only once
  • Loading branch information
audax committed Dec 7, 2016
2 parents 805579d + 4f69680 commit 4833f77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion drop_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Drop(ExportModelOperationsMixin('Drop'), models.Model):
class Meta:
# Force un-prefixed table name for compatibility
db_table = 'drops'
get_latest_by = 'created_at'
ordering = ['created_at']

def __repr__(self):
return u'<{0} for {1} created at {2} >'.format(self.drop_id, self.message, self.created_at)
15 changes: 9 additions & 6 deletions drop_service/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ def _get_drops(self, drop_id):
return error('Invalid drop id'), None

drops = Drop.objects.filter(drop_id=drop_id)
if not drops:
return HttpResponse(status=status.HTTP_204_NO_CONTENT), None

try:
have_since, since = self.get_if_modified_since()
Expand All @@ -45,10 +43,15 @@ def _get_drops(self, drop_id):

if have_since:
drops = drops.filter(created_at__gt=since)
if have_since and not drops:
return HttpResponseNotModified(), None

return None, drops

if not drops.exists():
if have_since:
return HttpResponseNotModified(), None
else:
return HttpResponse(status=status.HTTP_204_NO_CONTENT), None

return None, list(drops)

def get(self, request, drop_id):
response, drops = self._get_drops(drop_id)
Expand All @@ -61,7 +64,7 @@ def get(self, request, drop_id):
body = self.generate_body(drops, boundary)
response = HttpResponse(body, content_type=content_type)
if drops:
self.set_latest(response, drops.latest())
self.set_latest(response, drops[-1])
return response

def head(self, request, drop_id):
Expand Down

0 comments on commit 4833f77

Please sign in to comment.