Commits

Anonymous committed eb5dffc

added zip export

  • Participants
  • Parent commits 7609d8d

Comments (0)

Files changed (2)

File couchit/views.py

 # limitations under the License.
 
 from datetime import datetime
+import os
 import random
 import re
 from time import asctime, gmtime, time
 import urllib2
 import uuid
+import zipfile
+from StringIO import StringIO
+    
+
 from jinja2.filters import do_truncate, do_striptags, escape
 from werkzeug import redirect
 from werkzeug.contrib.atom import AtomFeed
 from couchit.contrib import mimeparse
 from couchit.http import BCResponse
 from couchit.template import render_response, url_for, render_template, send_json, convert_markdown
-from couchit.utils import local, make_hash, datetime_tojson, to_str
+from couchit.utils import local, make_hash, datetime_tojson, to_str, smart_str, force_unicode
 from couchit.utils.mail import send_mail
 from couchit.utils.sioc import SiocWiki, send_sioc
 
 re_page = re.compile(r"^[\"\'\-\/ \w]+$", re.U)
 re_address = re.compile(r'^[-_\w]+$')
 
-
 def not_logged(f):
     def decorated(request, **kwargs):
         authenticated = request.session.get('%s_authenticated' % request.site.cname, False)
 
     return render_response('site/changes.html', changes=changes, pages=pages)
 
-
+@can_edit
+@login_required
 def site_export(request, feedtype="atom"):
     pages = all_pages(local.db, request.site.id)
     if pages:
             author=page.title.replace(' ', '_')
         )
         return feed.get_response()
-    json = {
-        'title': "%s: Latest changes" % request.site.title and request.site.title or request.site.cname,
-        'subtitle': request.site.subtitle,
-        'updated':datetime_tojson(pages[0].updated),
-        'pages': []
-    }
-    for page in pages:
-        url = url_for("show_page", 
-                    pagename=page.title.replace(' ', '_')
-        )
-        json['pages'].append({
-            'title': page.title,
-            'content': page.content,
-            'url':  url,
-            'updated':datetime_tojson(page.updated),
-            'id':page.title.replace(' ', '_')
-        })
-    return send_json(json)
+    elif feedtype == "json":
+        json = {
+            'title': "%s: Latest changes" % request.site.title and request.site.title or request.site.cname,
+            'subtitle': request.site.subtitle,
+            'updated':datetime_tojson(pages[0].updated),
+            'pages': []
+        }
+        for page in pages:
+            url = url_for("show_page", 
+                        pagename=page.title.replace(' ', '_')
+            )
+            json['pages'].append({
+                'title': page.title,
+                'content': page.content,
+                'url':  url,
+                'updated':datetime_tojson(page.updated),
+                'id':page.title.replace(' ', '_')
+            })
+        return send_json(json)
+    elif feedtype == "zip":
+        pages = all_pages(local.db, request.site.id)
+        zip_content = StringIO()
+        zfile = zipfile.ZipFile(zip_content, "w", zipfile.ZIP_DEFLATED)
+        import time
+        for page in pages:
+             zinfo = zipfile.ZipInfo()
+             zinfo.filename = smart_str(page.title.replace(" ", "_")) + ".html"
+             zinfo.compress_type = zipfile.ZIP_DEFLATED
+             zinfo.date_time = time.localtime()[:6]
+             zfile.writestr(zinfo, smart_str(page.content))
+        zfile.close()
+        response = BCResponse(zip_content.getvalue())
+        response.headers['content-type'] = "application/x-zip-compressed"
+        return response
 
 @can_edit    
 @login_required
     pages = all_pages(local.db, request.site.id)       
     return render_response('site/design.html', pages=pages)
     
+
+    
 def sitemap(request):
     pages = all_pages(local.db, request.site.id)
     return render_response("site/sitemap.xml", pages=pages)

File templates/site/settings.html

     <p>&nbsp;</p>
     <h5 class="settings">Export your content</h5>
     
-    <p>Export your content to <a href="{{ site_url }}/site/export.atom">Atom Syndication Format</a> or <a href="{{ site_url }}/site/export.json">JSON Format</a> (only the latest version of your pages is included).</p>
+    <p>Export your content to <a href="{{ site_url }}/site/export.atom">Atom Syndication Format</a>, <a href="{{ site_url }}/site/export.json">JSON Format</a> or <a href="{{ site_url }}/site/export.zip">ZIP Format</a> (only the latest version of your pages is included).</p>
     <h5 class="settings">Delete site</h5>
     <p><a href="{{ url_for('site_delete') }}">Delete this site.</a> Deleting this site will immediately take it down.</p>