From: Jan Szejko Date: Tue, 17 Jul 2018 08:50:41 +0000 (+0200) Subject: blog api X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/b4b9ab771461b422c88e34e23e6816ddac626d76?hp=0a02421dd593f0466226b9defc33337cdbf672ef blog api --- diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 2913380c6..7d22d7c50 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -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 diff --git a/src/api/handlers.py b/src/api/handlers.py index ab1a44a52..830f0a99a 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -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 diff --git a/src/api/urls.py b/src/api/urls.py index c5ede8fcb..4bbd5f578 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -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(?:(?:[a-z0-9-]+/){2}){0,6})' paginate_re = r'(?:before/(?P[a-z0-9-]+)/)?(?:after/(?P[a-z0-9-]+)/)?(?:count/(?P[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(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),