Skip to content

Commit

Permalink
User favorites route (#38)
Browse files Browse the repository at this point in the history
* User favorites route

* ‘favorite’ -> ‘bookmark’

* Bookmark view tests
  • Loading branch information
alanmoo committed Jan 20, 2017
1 parent af78c9e commit e030e6e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pulseapi/entries/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,16 @@ def test_put_bookmark_entry_with_login(self):
# verify bookmark count is now zero
bookmarks = entry.bookmarked_by.count()
self.assertEqual(bookmarks, 0)

def test_bookmarked_entries_view(self):
"""
Verify that authenticated users can see a list of bookmarks.
"""
postresponse = self.client.put('/entries/1/bookmark')

# verify bookmark count is now one
bookmarkResponse = self.client.get('/entries/bookmarks/')
self.assertEqual(bookmarkResponse.status_code, 200)

bookmarkJson = json.loads(str(bookmarkResponse.content, 'utf-8'))
self.assertEqual(len(bookmarkJson), 1)
4 changes: 3 additions & 1 deletion pulseapi/entries/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from pulseapi.entries.views import (
EntriesListView,
EntryView,
toggle_bookmark
toggle_bookmark,
BookmarkedEntries,
)

urlpatterns = [
url('^$', EntriesListView.as_view(), name='entries-list'),
url('bookmarks/', BookmarkedEntries.as_view(), name='user-bookmarks'),
url(r'^(?P<entryid>[0-9]+)/bookmark/?', toggle_bookmark, name='bookmark'),
url(r'^(?P<pk>[0-9]+)/', EntryView.as_view(), name='entry'),
]
13 changes: 12 additions & 1 deletion pulseapi/entries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import django_filters
from rest_framework import (filters, status)
from rest_framework.decorators import detail_route, api_view
from rest_framework.generics import ListCreateAPIView, RetrieveAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveAPIView, ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

Expand Down Expand Up @@ -117,6 +117,17 @@ class EntryView(RetrieveAPIView):
pagination_class = None


class BookmarkedEntries(ListAPIView):

def get_queryset(self):
entries = set()
user = self.request.user.id
for bookmark in UserBookmarks.objects.filter(user=user).select_related('entry'):
entries.add(bookmark.entry)
return entries

serializer_class = EntrySerializer

class EntriesListView(ListCreateAPIView):
"""
A view that permits a GET to allow listing all the entries
Expand Down

0 comments on commit e030e6e

Please sign in to comment.