Adds api/convert to return HTML document from legacy format + Saves attached images when just converting to HTML, without creating a text + Returns nb of detached and removed commentswhen updating text.
authorgibus
Mon, 07 May 2012 09:10:45 +0200
changeset 415 bacf162c7b58
parent 414 ef8b166a0993
child 416 5573b959131d
Adds api/convert to return HTML document from legacy format + Saves attached images when just converting to HTML, without creating a text + Returns nb of detached and removed commentswhen updating text.
src/cm/api/handlers.py
src/cm/api/urls.py
src/cm/urls.py
src/cm/views/texts.py
--- a/src/cm/api/handlers.py	Wed Apr 25 09:25:39 2012 +0200
+++ b/src/cm/api/handlers.py	Mon May 07 09:10:45 2012 +0200
@@ -1,7 +1,7 @@
 from piston.handler import AnonymousBaseHandler, BaseHandler
 from piston.utils import rc
 
-from cm.models import Text,TextVersion, Role, UserRole, Comment
+from cm.models import Text,TextVersion, Role, UserRole, Comment, Attachment
 from cm.views import get_keys_from_dict, get_textversion_by_keys_or_404, get_text_by_keys_or_404, get_textversion_by_keys_or_404, redirect
 from cm.security import get_texts_with_perm, has_perm, get_viewable_comments, \
     has_perm_on_text_api
@@ -134,6 +134,35 @@
         else:
             resp = rc.BAD_REQUEST
         return resp
+    
+from cm.converters import _convert_from_mimetype
+import os
+from django.core.urlresolvers import reverse
+
+class ConvertHandler(BaseHandler):    
+  type = "Text methods"
+  allowed_methods = ('POST', )    
+  title = "Convert a legacy file"    
+  desc = "Returns the HTLM file."
+  args = """<br />
+`file`: the file in legacy format<br />        
+    """ 
+
+  @staticmethod
+  def endpoint():
+    return URL_PREFIX + '/convert/'
+    
+  def create(self, request):
+    mime = request.POST.get('mime', None)
+    the_file = request.FILES['file'];
+    html, attachs = _convert_from_mimetype(the_file.read(), mime, 'html')
+    for attach_file in attachs:
+      attach_data = file(attach_file, 'rb').read()
+      filename = os.path.basename(attach_file)
+      attachment = Attachment.objects.create_attachment(filename=filename, data=attach_data, text_version=None)
+      attach_url = reverse('notext-attach', args=[attachment.key])
+      html = html.replace(filename, settings.SITE_URL + attach_url)
+    return {'html' : html}
 
 from cm.exception import UnauthorizedException
 from cm.views.texts import text_delete
@@ -202,10 +231,16 @@
     
     
     def create(self, request, key):
+        prev_text = get_text_by_keys_or_404(key)
+        prev_text_version = prev_text.last_text_version
+        prev_comments = prev_text_version.get_comments()
+        prev_scope_removed = [c for c in prev_comments if c.is_scope_removed()]
         res = text_edit(request, key=key)
         text = get_text_by_keys_or_404(key)
         text_version = text.last_text_version
-        return {'version_key' : text_version.key , 'created': text_version.created}
+        comments = text_version.get_comments()
+        scope_removed = [c for c in comments if c.is_scope_removed()]
+        return {'version_key' : text_version.key , 'created': text_version.created, 'nb_deleted' : len(prev_comments) - len(comments), 'nb_scope_removed' : len(scope_removed) - len(prev_scope_removed)}
 
 
 class AnonymousTextFeedHandler(AnonymousBaseHandler):
@@ -452,7 +487,7 @@
         if limit:
             query = query[:int(limit)]
         return query
-    
+
 from piston.doc import documentation_view
 
 from piston.handler import handler_tracker
--- a/src/cm/api/urls.py	Wed Apr 25 09:25:39 2012 +0200
+++ b/src/cm/api/urls.py	Mon May 07 09:10:45 2012 +0200
@@ -21,6 +21,8 @@
 
 comments_handler = Resource(handler=CommentsHandler, authentication=auth)
 
+convert_handler = Resource(handler=ConvertHandler, authentication=auth)
+
 client_handler = Resource(handler=ClientHandler, authentication=auth)
 
 jsi8n_handler = Resource(handler=JSI18NHandler, authentication=None)
@@ -48,6 +50,7 @@
    url(r'^text/(?P<key>\w*)/edit/$', text_edit_handler),
    url(r'^text/(?P<key>\w*)/(?P<version_key>\w*)/$', textversion_handler),
    url(r'^comments/$', comments_handler),
+   url(r'^convert/$', convert_handler),
    url(r'^client/$', client_handler),
    url(r'^jsi18n/$', jsi8n_handler),
    url(r'^doc/$', documentation),
--- a/src/cm/urls.py	Wed Apr 25 09:25:39 2012 +0200
+++ b/src/cm/urls.py	Mon May 07 09:10:45 2012 +0200
@@ -77,6 +77,9 @@
      url(r'^text/(?P<key>\w*)/history/$', text_history, name="text-history"),
      #url(r'^text/(?P<key>\w*)/diff/(?P<id_v1>\w*)/(?P<id_v2>\w*)/$', text_diff, name="text-diff"),
 #     url(r'^text/(?P<key>\w*)/version/(?P<id_version>\w*)/$', text_version, name="text-version"),
+
+     # detached attachements! (used for saving images when just converting to HTML, without creating a text)
+     url(r'^attach/(?P<attach_key>\w*)/$', notext_attach, name="notext-attach"),
      
      # main client frame
      url(r'^text/(?P<key>\w*)/comments_frame/$', text_view_frame, name="text-view-comments-frame"),
--- a/src/cm/views/texts.py	Wed Apr 25 09:25:39 2012 +0200
+++ b/src/cm/views/texts.py	Mon May 07 09:10:45 2012 +0200
@@ -790,9 +790,13 @@
     response = HttpResponse(content, mimetype)
     return response
 
+def notext_attach(request, attach_key):
+    attach = Attachment.objects.get(key=attach_key)
+    content = file(attach.data.path).read()
+    mimetype, _encoding = mimetypes.guess_type(attach.data.path)
+    response = HttpResponse(content, mimetype)
+    return response
     
-
-
 def fix_anon_in_formset(formset):
     # fix role choice in formset for anon (not all role are allowed)
     role_field = [f.fields['role'] for f in formset.forms if f.instance.user == None][0]