src/cm/views/texts.py
changeset 231 e71ea24ff34c
parent 230 fb1a2dfb473d
child 232 a6c6b046a9c0
equal deleted inserted replaced
230:fb1a2dfb473d 231:e71ea24ff34c
    38 import difflib
    38 import difflib
    39 import logging
    39 import logging
    40 import mimetypes
    40 import mimetypes
    41 import simplejson
    41 import simplejson
    42 import sys
    42 import sys
       
    43 import re
    43 from django.db.models.sql.datastructures import EmptyResultSet
    44 from django.db.models.sql.datastructures import EmptyResultSet
    44 
    45 
    45 def get_text_and_admin(key, adminkey, assert_admin = False):
    46 def get_text_and_admin(key, adminkey, assert_admin = False):
    46     """
    47     """
    47     assert_admin => redirect to unauthorized if not admin 
    48     assert_admin => redirect to unauthorized if not admin 
   291         ret = HttpResponse()
   292         ret = HttpResponse()
   292         ret.status_code = 403 
   293         ret.status_code = 403 
   293     return ret 
   294     return ret 
   294 
   295 
   295 
   296 
       
   297 def from_html_links_to_abs_links(content):
       
   298     """
       
   299     Replaces (html) links to attachs with real file path on server
       
   300     """
       
   301     attach_re = r'/text/(?P<key>\w*)/attach/(?P<attach_key>\w*)/'
       
   302     attach_str = r'/text/%s/attach/%s/'
       
   303     for match in re.findall(attach_re, content):
       
   304         link = attach_str %match
       
   305         attach = Attachment.objects.get(key=match[1], text_version__text__key=match[0])                
       
   306         content = content.replace(link, attach.data.path)
       
   307     return content
       
   308 
   296 #NOTE : some arguments like : withcolor = "yes" + format = "markdown" are incompatible
   309 #NOTE : some arguments like : withcolor = "yes" + format = "markdown" are incompatible
   297 #http://localhost:8000/text/text_key_1/export/pdf/1/all/1
   310 #http://localhost:8000/text/text_key_1/export/pdf/1/all/1
   298 def text_export(request, key, format, download, whichcomments, withcolor, adminkey=None):
   311 def text_export(request, key, format, download, whichcomments, withcolor, adminkey=None):
   299     text, admin = get_text_and_admin(key, adminkey)
   312     text, admin = get_text_and_admin(key, adminkey)
   300     text_version = text.get_latest_version()
   313     text_version = text.get_latest_version()
   326     else :
   339     else :
   327         if format in ('markdown', 'latex') : 
   340         if format in ('markdown', 'latex') : 
   328             use_pandoc = True
   341             use_pandoc = True
   329         elif format in ('pdf', 'odt') : 
   342         elif format in ('pdf', 'odt') : 
   330             use_pandoc = (original_format == "markdown")
   343             use_pandoc = (original_format == "markdown")
   331         elif format in ('doc', 'html') : 
   344         elif format in ('doc', 'html') :
   332             use_pandoc = False
   345             use_pandoc = False
       
   346 
       
   347     # correct attach path => real path
       
   348     if format in ('pdf','odt') :        
       
   349         original_content = from_html_links_to_abs_links(original_content)
       
   350             
   333     if len(comments) == 0 : #want to bypass html conversion in this case
   351     if len(comments) == 0 : #want to bypass html conversion in this case
   334         return content_export2(request, original_content, text_version.title, original_format, format, use_pandoc, download_response)
   352         return content_export2(request, original_content, text_version.title, original_format, format, use_pandoc, download_response)
   335     else : # case comments to be added  
   353     else : # case comments to be added  
   336         #comments = comments.order_by('start_wrapper','start_offset','end_wrapper','end_offset')
   354         #comments = comments.order_by('start_wrapper','start_offset','end_wrapper','end_offset')
   337         html = text_version.get_content()
   355         html = text_version.get_content()