Ustawianie galerii inline.
[redakcja.git] / apps / api / handlers / library_handlers.py
index cbd3a0a..7e90e31 100755 (executable)
@@ -4,12 +4,8 @@ import os.path
 import logging
 log = logging.getLogger('platforma.api.library')
 
-__author__= "Ɓukasz Rekucki"
-__date__ = "$2009-09-25 15:49:50$"
-__doc__ = "Module documentation."
-
 from piston.handler import BaseHandler, AnonymousBaseHandler
-from django.http import HttpResponse
+from piston.utils import rc
 
 from datetime import date
 
@@ -19,10 +15,8 @@ from django.db import IntegrityError
 import librarian
 import librarian.html
 import difflib
-from librarian import dcparser, parser
 
 from wlrepo import *
-from api.models import PullRequest
 from explorer.models import GalleryForDocument
 
 # internal imports
@@ -31,6 +25,10 @@ import api.response as response
 from api.utils import validate_form, hglibrary, natural_order
 from api.models import PartCache, PullRequest
 
+from pygments import highlight
+from pygments.lexers import DiffLexer
+from pygments.formatters import HtmlFormatter
+
 #
 import settings
 
@@ -180,44 +178,27 @@ class LibraryHandler(BaseHandler):
 #
 # Document Handlers
 #
-class BasicDocumentHandler(AnonymousBaseHandler):
-    allowed_methods = ('GET',)
-
-    @hglibrary
-    def read(self, request, docid, lib):
-        try:    
-            doc = lib.document(docid)
-        except RevisionNotFound:
-            return rc.NOT_FOUND
-
-        result = {
-            'name': doc.id,
-            'html_url': reverse('dochtml_view', args=[doc.id]),
-            'text_url': reverse('doctext_view', args=[doc.id]),
-            'dc_url': reverse('docdc_view', args=[doc.id]),
-            'public_revision': doc.revision,
-        }
-
-        return result
-
-
 class DiffHandler(BaseHandler):
     allowed_methods = ('GET',)
     
     @hglibrary
-    def read(self, request, source_revision, target_revision, lib):
-        '''Return diff between source_revision and target_revision)'''
-        source_document = lib.document_for_revision(source_revision)
-        target_document = lib.document_for_revision(target_revision)
-        print source_document,
-        print target_document
+    def read(self, request, docid, lib):
+        '''Return diff between source_revision and target_revision)'''        
+        revision = request.GET.get('revision')
+        if not revision:
+            return ''
+        source_document = lib.document(docid)
+        target_document = lib.document_for_revision(revision)
+        print source_document, target_document
+        
         diff = difflib.unified_diff(
             source_document.data('xml').splitlines(True),
             target_document.data('xml').splitlines(True),
             'source',
             'target')
         
-        return ''.join(list(diff))
+        s =  ''.join(list(diff))
+        return highlight(s, DiffLexer(), HtmlFormatter(cssclass="pastie"))
 
 
 #
@@ -225,7 +206,6 @@ class DiffHandler(BaseHandler):
 #
 class DocumentHandler(BaseHandler):
     allowed_methods = ('GET', 'PUT')
-    anonymous = BasicDocumentHandler
 
     @validate_form(forms.DocumentRetrieveForm, 'GET')
     @hglibrary
@@ -305,7 +285,7 @@ class DocumentHTMLHandler(BaseHandler):
 
     @validate_form(forms.DocumentRetrieveForm, 'GET')
     @hglibrary
-    def read(self, request, form, docid, lib, stylesheet='partial'):
+    def read(self, request, form, docid, lib, stylesheet='full'):
         """Read document as html text"""
         try:
             revision = form.cleaned_data['revision']
@@ -328,7 +308,7 @@ class DocumentHTMLHandler(BaseHandler):
                 return error
 
             return librarian.html.transform(document.data('xml'), is_file=False, \
-                parse_dublincore=False, stylesheet='full',\
+                parse_dublincore=False, stylesheet=stylesheet,\
                 options={
                     "with-paths": 'boolean(1)',                    
                 })
@@ -348,7 +328,7 @@ class DocumentHTMLHandler(BaseHandler):
 #
 
 class DocumentGalleryHandler(BaseHandler):
-    allowed_methods = ('GET')
+    allowed_methods = ('GET', 'POST')
     
     
     def read(self, request, docid):
@@ -393,7 +373,27 @@ class DocumentGalleryHandler(BaseHandler):
 
         return galleries
 
-
+    def create(self, request, docid):
+        if not request.user.is_superuser:
+            return rc.FORBIDDEN
+        
+        new_path = request.POST.get('path')
+        
+        if new_path:
+            gallery, created = GalleryForDocument.objects.get_or_create(
+                document = docid,
+                defaults = {
+                    'subpath': new_path,
+                }
+            )
+
+            if not created:
+                gallery.subpath = new_path
+                gallery.save()
+
+            return rc.CREATED
+        
+        return rc.BAD_REQUEST
 
 #
 # Dublin Core handlers