Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Merge 51e10a7 into 5ee1f5b
Browse files Browse the repository at this point in the history
  • Loading branch information
mparent61 committed Sep 2, 2016
2 parents 5ee1f5b + 51e10a7 commit ad5b6f8
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 141 deletions.
60 changes: 0 additions & 60 deletions bulbs/liveblog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,66 +84,6 @@

`liveblog=123` Conditional filtering to match entries for specific `LiveBlog`

## Get All Entries (PUBLIC)

**GET:**

```
/api/v1/liveblog/public/entry/
```

### Body

```json
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"liveblog": 123,
"headline": "Something Really Funny",
"authors": [50, 57],
"body": "Why are you reading this? Stop it.",
"recirc_content": [501, 1801, 17203],
"published": "2015-01-01T01:01:00.000Z",
"responses": [
{
"author": 85,
"internal_name": "Secret Name",
"body": "Some more really interesting stuff you should read."
}
]
},
{
"liveblog": 123,
"headline": "Something Really Funny",
"authors": [50, 57],
"body": "Why are you reading this? Stop it.",
"recirc_content": [501, 1801, 17203],
"published": "2015-01-01T01:01:00.000Z",
"responses": [
{
"author": 85,
"internal_name": "Secret Name",
"body": "Some more really interesting stuff you should read."
}
]
}
]
}
```

### Filters

`liveblog=123` Conditional filtering to match entries for specific `LiveBlog`
`if_modified_since=2015-01-01T01:01:00.000Z` Conditional filtering to return all entries published after specified ISO 8601 date/time.

### Status Codes

* `200` if successful (modified entries found)
* `304` no modified entries

## Get single entry

### GET
Expand Down
12 changes: 2 additions & 10 deletions bulbs/liveblog/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.conf.urls import url

from rest_framework import routers

from bulbs.liveblog.viewsets import LiveBlogEntryViewSet, PublicLiveBlogEntryViewSet
from bulbs.liveblog.viewsets import LiveBlogEntryViewSet

api_v1_router = routers.DefaultRouter()
api_v1_router.register(
Expand All @@ -11,10 +9,4 @@
base_name="liveblog-entry"
)

urlpatterns = [
url(r"api/v1/liveblog/public/entry/?$",
PublicLiveBlogEntryViewSet.as_view({'get': 'list'}),
name="public-liveblog-entry"),
]

urlpatterns += api_v1_router.urls
urlpatterns = api_v1_router.urls
21 changes: 0 additions & 21 deletions bulbs/liveblog/filters.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
from rest_framework import filters
from rest_framework.exceptions import ParseError

from django.utils import dateparse


logger = __import__('logging').getLogger(__name__)


class IfModifiedSinceFilterBackend(filters.BaseFilterBackend):
"""
Optionally exclude older requests.
Expects ISO 8601-formatted "if_modified_since" query param.
"""
def filter_queryset(self, request, queryset, view):
if_modified_since = request.GET.get('if_modified_since')
if if_modified_since:
when = dateparse.parse_datetime(if_modified_since)
if when:
return queryset.filter(published__gt=when)
else:
raise ParseError('Bad "if_modified_since" date: {}'.format(if_modified_since))
else:
return queryset


class LiveBlogFilterBackend(filters.BaseFilterBackend):
"""
Optionally include only entries belonging to specified LiveBlog.
Expand Down
15 changes: 2 additions & 13 deletions bulbs/liveblog/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from rest_framework import filters, viewsets
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from rest_framework.permissions import AllowAny, IsAdminUser
from rest_framework.permissions import IsAdminUser

from bulbs.api.permissions import CanEditContent
from .filters import IfModifiedSinceFilterBackend, LiveBlogFilterBackend
from .filters import LiveBlogFilterBackend
from .models import LiveBlogEntry
from .serializers import LiveBlogEntrySerializer

Expand All @@ -24,13 +23,3 @@ class LiveBlogEntryViewSet(viewsets.ModelViewSet):

# Avoid immediate need for FE pagination
paginate_by = 500


class PublicLiveBlogEntryViewSet(CacheResponseMixin, LiveBlogEntryViewSet):

permission_classes = (AllowAny,)
filter_backends = (
filters.OrderingFilter,
IfModifiedSinceFilterBackend,
LiveBlogFilterBackend,
)
38 changes: 1 addition & 37 deletions tests/liveblog/test_liveblog_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime, timedelta
from datetime import datetime
import json

from model_mommy import mommy
import six
from six.moves.urllib.parse import urlencode

from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -174,38 +173,3 @@ def test_liveblog_filter(self):
six.assertCountEqual(self,
[e['id'] for e in resp.data['results']],
[e.id for e in entries[0]])


class TestPublicLiveBlogEntryApi(BaseAPITestCase):

def test_read_only(self):
for method in [self.api_client.post,
self.api_client.put]:
self.assertEquals(405, method(reverse('public-liveblog-entry'),
content_type="application/json").status_code)

def test_list_if_modified_since(self):
now = timezone.now()
liveblog = mommy.make(TestLiveBlog)
entries = [mommy.make(LiveBlogEntry, liveblog=liveblog, published=(now + timedelta(days=i)))
for i in range(5)]

def check(days, expected_entries, expected_status_code=200):
when = now + timedelta(days=days)
resp = self.api_client.get(
reverse('public-liveblog-entry') + '?{}'.format(
urlencode({'if_modified_since': when.isoformat()})))

self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data['count'], len(expected_entries))
self.assertEqual([e['id'] for e in resp.data['results']],
[e.id for e in expected_entries])

check(days=-1, expected_entries=entries)
check(days=0, expected_entries=entries[1:])
check(days=3, expected_entries=entries[4:])
check(days=4, expected_entries=[], expected_status_code=304)

def test_list_invalid_if_modified_since(self):
resp = self.api_client.get(reverse('public-liveblog-entry') + '?if_modified_since=ABC')
self.assertEqual(resp.status_code, 400)

0 comments on commit ad5b6f8

Please sign in to comment.