blog api
authorJan Szejko <janek37@gmail.com>
Tue, 17 Jul 2018 08:50:41 +0000 (10:50 +0200)
committerJan Szejko <janek37@gmail.com>
Tue, 17 Jul 2018 08:50:41 +0000 (10:50 +0200)
requirements/requirements.txt
src/api/handlers.py
src/api/urls.py

index 2913380..7d22d7c 100644 (file)
@@ -69,6 +69,6 @@ requests
 paypalrestsdk
 
 django-haystack<2.8.0
-django-migdal>=0.7.6
+django-migdal>=0.8.1
 
 python-slugify
\ No newline at end of file
index ab1a44a..830f0a9 100644 (file)
@@ -9,6 +9,7 @@ from django.core.urlresolvers import reverse
 from django.http.response import HttpResponse
 from django.utils.functional import lazy
 from django.db import models
+from migdal.models import Entry
 from piston.handler import AnonymousBaseHandler, BaseHandler
 from piston.utils import rc
 from sorl.thumbnail import default
@@ -714,3 +715,30 @@ class UserShelfHandler(BookDetailHandler):
         if count:
             books = books[:count]
         return books
+
+
+class BlogEntryHandler(BaseHandler):
+    model = Entry
+    fields = ('title', 'lead', 'body', 'place', 'time', 'image_url', 'gallery_urls', 'type', 'key')
+
+    def read(self, request):
+        after = request.GET.get('after')
+        count = int(request.GET.get('count', 20))
+        entries = Entry.published_objects.filter(in_stream=True).order_by('-first_published_at')
+        if after:
+            entries = entries.filter(first_published_at__lt=after)
+        if count:
+            entries = entries[:count]
+        return entries
+
+    @classmethod
+    def image_url(cls, entry):
+        return entry.image.url if entry.image else None
+
+    @classmethod
+    def gallery_urls(cls, entry):
+        return [photo.url() for photo in entry.photo_set.all()]
+
+    @classmethod
+    def key(cls, entry):
+        return entry.first_published_at
index c5ede8f..4bbd5f5 100644 (file)
@@ -36,6 +36,8 @@ fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
 
 picture_resource = CsrfExemptResource(handler=handlers.PictureHandler, authentication=auth)
 
+blog_resource = Resource(handler=handlers.BlogEntryHandler)
+
 
 tags_re = r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})'
 paginate_re = r'(?:before/(?P<before>[a-z0-9-]+)/)?(?:after/(?P<after>[a-z0-9-]+)/)?(?:count/(?P<count>[0-9]+)/)?$'
@@ -109,6 +111,8 @@ urlpatterns = [
 
     url(r'^pictures/$', picture_resource),
 
+    url(r'^blog/$', blog_resource),
+
     # fragments by book, tags, themes
     # this should be paged
     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),