fix stats api
[redakcja.git] / src / cover / views.py
index 25663a0..39b12f9 100644 (file)
@@ -7,7 +7,7 @@ import os.path
 import PIL.Image
 from django.conf import settings
 from django.contrib.auth.decorators import permission_required
 import PIL.Image
 from django.conf import settings
 from django.contrib.auth.decorators import permission_required
-from django.http import HttpResponse, HttpResponseRedirect, Http404
+from django.http import HttpResponse, HttpResponseRedirect, Http404, JsonResponse
 from django.shortcuts import get_object_or_404, render
 from django.views.decorators.csrf import csrf_exempt
 from django.views.decorators.http import require_POST
 from django.shortcuts import get_object_or_404, render
 from django.views.decorators.csrf import csrf_exempt
 from django.views.decorators.http import require_POST
@@ -22,7 +22,7 @@ from cover import forms
 from cover.utils import get_import_data
 
 
 from cover.utils import get_import_data
 
 
-PREVIEW_SIZE = (216, 300)
+PREVIEW_SIZE = (212, 300)
 
 
 def preview(request, book, chunk=None, rev=None):
 
 
 def preview(request, book, chunk=None, rev=None):
@@ -31,7 +31,14 @@ def preview(request, book, chunk=None, rev=None):
     If chunk and rev number are given, use version from given revision.
     If rev is not given, use publishable version.
     """
     If chunk and rev number are given, use version from given revision.
     If rev is not given, use publishable version.
     """
-    chunk = Chunk.get(book, chunk)
+    try:
+        chunk = Chunk.get(book, chunk)
+    except Chunk.DoesNotExist:
+        raise Http404
+
+    if chunk.book.cover and rev is None and not request.GET.get('width') and not request.GET.get('height'):
+        return HttpResponseRedirect(chunk.book.cover.url)
+
     if rev is not None:
         try:
             revision = chunk.at_revision(rev)
     if rev is not None:
         try:
             revision = chunk.at_revision(rev)
@@ -96,6 +103,22 @@ def preview_from_xml(request):
 def image(request, pk):
     img = get_object_or_404(Image, pk=pk)
 
 def image(request, pk):
     img = get_object_or_404(Image, pk=pk)
 
+    if not request.accepts('text/html') and request.accepts('application/json') or request.GET.get('format') == 'json':
+        return JsonResponse({
+            'title': img.title,
+            'author': img.author,
+            'license_name': img.license_name,
+            'license_url': img.license_url,
+            'source_url': img.source_url,
+            'attribution': img.attribution,
+            'cut_left': img.cut_left,
+            'cut_right': img.cut_right,
+            'cut_top': img.cut_top,
+            'cut_bottom': img.cut_bottom,
+            'file': img.file.url,
+            'use_file': img.use_file.url,
+        })
+
     if request.user.has_perm('cover.change_image'):
         if request.method == "POST":
             form = forms.ImageEditForm(request.POST, request.FILES, instance=img)
     if request.user.has_perm('cover.change_image'):
         if request.method == "POST":
             form = forms.ImageEditForm(request.POST, request.FILES, instance=img)