From 4990c983d7b6ac80bd45b78809faa18cce1be1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Marti=CC=81n?= Date: Sun, 5 Apr 2015 21:12:20 +0200 Subject: [PATCH] Added feed api url --- shelfzilla/apps/account/api/__init__.py | 0 shelfzilla/apps/account/api/serializers.py | 12 ++++++ shelfzilla/apps/account/api/urls.py | 12 ++++++ shelfzilla/apps/account/api/views.py | 43 ++++++++++++++++++++++ shelfzilla/urls.py | 5 +-- shelfzilla/urls_api.py | 19 ++++++++++ 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 shelfzilla/apps/account/api/__init__.py create mode 100644 shelfzilla/apps/account/api/serializers.py create mode 100644 shelfzilla/apps/account/api/urls.py create mode 100644 shelfzilla/apps/account/api/views.py create mode 100644 shelfzilla/urls_api.py diff --git a/shelfzilla/apps/account/api/__init__.py b/shelfzilla/apps/account/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shelfzilla/apps/account/api/serializers.py b/shelfzilla/apps/account/api/serializers.py new file mode 100644 index 0000000..87cb4c2 --- /dev/null +++ b/shelfzilla/apps/account/api/serializers.py @@ -0,0 +1,12 @@ +# coding: utf-8 + +# django + +# third party +from rest_framework import serializers + +# own + + +class FeedSerializer(serializers.Serializer): + pass diff --git a/shelfzilla/apps/account/api/urls.py b/shelfzilla/apps/account/api/urls.py new file mode 100644 index 0000000..6f744d7 --- /dev/null +++ b/shelfzilla/apps/account/api/urls.py @@ -0,0 +1,12 @@ +# coding: utf-8 + +# third +from rest_framework.routers import DefaultRouter + +# own +from .views import FeedViewSet + + +router = DefaultRouter(trailing_slash=False) +router.register(r'feed', FeedViewSet, base_name='feed') +urlpatterns = router.urls diff --git a/shelfzilla/apps/account/api/views.py b/shelfzilla/apps/account/api/views.py new file mode 100644 index 0000000..cfdc114 --- /dev/null +++ b/shelfzilla/apps/account/api/views.py @@ -0,0 +1,43 @@ +# coding: utf-8 + +# python +from itertools import chain +import json + +# third +from rest_framework import viewsets +from rest_framework.response import Response +from rest_framework.permissions import IsAuthenticated + +# own +from shelfzilla.apps.manga.models import ( + UserReadVolume, UserHaveVolume, UserWishlistVolume +) + + +class FeedViewSet(viewsets.ViewSet): + """ + """ + permission_classes = (IsAuthenticated,) + + def list(self, request): + owned_list = UserHaveVolume.objects.filter(user=request.user) + wishlisted_list = UserWishlistVolume.objects.filter(user=request.user) + read_list = UserReadVolume.objects.filter(user=request.user) + + timeline = sorted( + chain(owned_list, wishlisted_list, read_list), + key=lambda model: model.date, + reverse=True + )[:20] + + result = [] + for item in timeline: + event = { + 'date': item.date, + 'message': item.timeline_message, + 'type': item.event_type, + } + result.append(event) + + return Response(result) diff --git a/shelfzilla/urls.py b/shelfzilla/urls.py index c4ec46f..7b498d3 100644 --- a/shelfzilla/urls.py +++ b/shelfzilla/urls.py @@ -2,7 +2,7 @@ from django.conf.urls import patterns, include, url from django.conf import settings from django.contrib import admin -from .views import MessagesView, BlockedView +from .views import MessagesView admin.autodiscover() @@ -28,8 +28,7 @@ urlpatterns = patterns( # API urlpatterns += patterns( '', - url(r'^api/v1/auth/register/', BlockedView.as_view()), - url(r'^api/v1/auth/', include('djoser.urls', namespace='api')), + url(r'^api/v1/', include('shelfzilla.urls_api', namespace='api')), ) if settings.DEBUG: diff --git a/shelfzilla/urls_api.py b/shelfzilla/urls_api.py new file mode 100644 index 0000000..9ec0f7e --- /dev/null +++ b/shelfzilla/urls_api.py @@ -0,0 +1,19 @@ +# coding: utf-8 + +# django +from django.conf.urls import patterns, include, url + +# app +from .views import BlockedView + + +# API +urlpatterns = patterns( + '', + # Manually blocked API endpoints + url(r'^auth/register/', BlockedView.as_view()), + # /auth + url(r'^auth/', include('djoser.urls')), + # /feed + url(r'^', include('shelfzilla.apps.account.api.urls')), +)