import documents from Hg
[redakcja.git] / apps / wiki / views.py
index 45ded5b..b303542 100644 (file)
@@ -1,7 +1,10 @@
 import os
 import os
+from StringIO import StringIO
 import logging
 logger = logging.getLogger("fnp.wiki")
 
 import logging
 logger = logging.getLogger("fnp.wiki")
 
+from lxml import etree
+
 from django.conf import settings
 
 from django.views.generic.simple import direct_to_template
 from django.conf import settings
 
 from django.views.generic.simple import direct_to_template
@@ -21,6 +24,8 @@ from django.utils.translation import ugettext_lazy as _
 from django.utils.decorators import decorator_from_middleware
 from django.middleware.gzip import GZipMiddleware
 
 from django.utils.decorators import decorator_from_middleware
 from django.middleware.gzip import GZipMiddleware
 
+import librarian.html
+import librarian.text
 
 #
 # Quick hack around caching problems, TODO: use ETags
 
 #
 # Quick hack around caching problems, TODO: use ETags
@@ -201,7 +206,7 @@ def upload(request):
 @decorator_from_middleware(GZipMiddleware)
 def text(request, slug, chunk=None):
     try:
 @decorator_from_middleware(GZipMiddleware)
 def text(request, slug, chunk=None):
     try:
-        doc = Chunk.get(slug, chunk).doc
+        doc = Chunk.get(slug, chunk)
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
 
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
 
@@ -247,21 +252,45 @@ def text(request, slug, chunk=None):
 
 
 @never_cache
 
 
 @never_cache
-def compiled(request, slug):
-    text = get_object_or_404(Book, slug=slug).materialize()
+def book_xml(request, slug):
+    xml = get_object_or_404(Book, slug=slug).materialize()
     
     
-    response = http.HttpResponse(text, content_type='application/xml', mimetype='application/wl+xml')
+    response = http.HttpResponse(xml, content_type='application/xml', mimetype='application/wl+xml')
     response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
     return response
 
 
     response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
     return response
 
 
+@never_cache
+def book_txt(request, slug):
+    xml = get_object_or_404(Book, slug=slug).materialize()
+    output = StringIO()
+    # errors?
+    librarian.text.transform(StringIO(xml), output)
+    text = output.getvalue()
+    response = http.HttpResponse(text, content_type='text/plain', mimetype='text/plain')
+    response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
+    return response
+
+
+@never_cache
+def book_html(request, slug):
+    xml = get_object_or_404(Book, slug=slug).materialize()
+    output = StringIO()
+    # errors?
+    librarian.html.transform(StringIO(xml), output, parse_dublincore=False,
+                             flags=['full-page'])
+    html = output.getvalue()
+    response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
+    return response
+
+
 @never_cache
 @require_POST
 def revert(request, slug, chunk=None):
     form = DocumentTextRevertForm(request.POST, prefix="textrevert")
     if form.is_valid():
         try:
 @never_cache
 @require_POST
 def revert(request, slug, chunk=None):
     form = DocumentTextRevertForm(request.POST, prefix="textrevert")
     if form.is_valid():
         try:
-            doc = Chunk.get(slug, chunk).doc
+            doc = Chunk.get(slug, chunk)
         except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
             raise Http404
 
         except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
             raise Http404
 
@@ -327,7 +356,7 @@ def diff(request, slug, chunk=None):
         revB = None
 
     try:
         revB = None
 
     try:
-        doc = Chunk.get(slug, chunk).doc
+        doc = Chunk.get(slug, chunk)
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
     docA = doc.at_revision(revA).materialize()
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
     docA = doc.at_revision(revA).materialize()
@@ -340,7 +369,7 @@ def diff(request, slug, chunk=None):
 @never_cache
 def revision(request, slug, chunk=None):
     try:
 @never_cache
 def revision(request, slug, chunk=None):
     try:
-        doc = Chunk.get(slug, chunk).doc
+        doc = Chunk.get(slug, chunk)
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
     return http.HttpResponse(str(doc.revision()))
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
     return http.HttpResponse(str(doc.revision()))
@@ -350,7 +379,7 @@ def revision(request, slug, chunk=None):
 def history(request, slug, chunk=None):
     # TODO: pagination
     try:
 def history(request, slug, chunk=None):
     # TODO: pagination
     try:
-        doc = Chunk.get(slug, chunk).doc
+        doc = Chunk.get(slug, chunk)
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
 
     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
         raise Http404
 
@@ -373,6 +402,14 @@ def history(request, slug, chunk=None):
     return JSONResponse(changes)
 
 
     return JSONResponse(changes)
 
 
+def book(request, slug):
+    book = get_object_or_404(Book, slug=slug)
+
+    return direct_to_template(request, "wiki/book_detail.html", extra_context={
+        "book": book,
+    })
+
+
 
 """
 import wlapi
 
 """
 import wlapi