src/cm/views/export.py
changeset 455 33c7e20efcb7
parent 454 b7a092a52eae
child 460 2fdb7d095d5c
equal deleted inserted replaced
454:b7a092a52eae 455:33c7e20efcb7
     4 from django.shortcuts import render_to_response
     4 from django.shortcuts import render_to_response
     5 from django.template.loader import render_to_string
     5 from django.template.loader import render_to_string
     6 from django.template import RequestContext
     6 from django.template import RequestContext
     7 from django.utils.translation import ugettext as _, ugettext_lazy
     7 from django.utils.translation import ugettext as _, ugettext_lazy
     8 from django.contrib.auth.models import User
     8 from django.contrib.auth.models import User
       
     9 from django.conf import settings
     9 from cm.converters.pandoc_converters import pandoc_convert, do_tidy
    10 from cm.converters.pandoc_converters import pandoc_convert, do_tidy
    10 from cm.models import Text, TextVersion, Attachment, Comment
    11 from cm.models import Text, TextVersion, Attachment, Comment
    11 from cm.security import get_viewable_comments
    12 from cm.security import get_viewable_comments
    12 import mimetypes
    13 import mimetypes
    13 import simplejson
    14 import simplejson
       
    15 import imghdr
       
    16 import base64
       
    17 import re
    14 from cm.cm_settings import USE_ABI
    18 from cm.cm_settings import USE_ABI
    15 
    19 
    16 EXPORT2_INFOS = {
    20 EXPORT2_INFOS = {
    17 # key -> { mimetype, extension}
    21 # key -> { mimetype, extension}
    18 's5' :   {},
    22 's5' :   {},
    72                   from cm.converters.oo_converters import combine_css_body                
    76                   from cm.converters.oo_converters import combine_css_body                
    73                   fix_content = combine_css_body(content, '')
    77                   fix_content = combine_css_body(content, '')
    74             if USE_ABI:
    78             if USE_ABI:
    75               from cm.converters.abi_converters import AbiFileConverter
    79               from cm.converters.abi_converters import AbiFileConverter
    76               converter = AbiFileConverter()
    80               converter = AbiFileConverter()
    77 
       
    78               # replaces images url by their actual path
       
    79               from django.conf import settings
       
    80               site_url = settings.SITE_URL
       
    81               import re
       
    82               attach_re = r'/attach/(?P<attach_key>\w*)/'
       
    83               attach_str = r'%s/attach/%s/'
       
    84               for match in re.findall(attach_re, fix_content):
       
    85                 link = attach_str %(site_url, match)
       
    86                 attach = Attachment.objects.get(key=match)
       
    87                 fix_content = fix_content.replace(link, attach.data.path)
       
    88 
       
    89               export_content = converter.convert_from_html(fix_content, format)
    81               export_content = converter.convert_from_html(fix_content, format)
    90             else:
    82             else:
    91               from cm.converters.oo_converters import convert_html as oo_convert                
    83               from cm.converters.oo_converters import convert_html as oo_convert                
    92               export_content = oo_convert(fix_content, format)
    84               export_content = oo_convert(fix_content, format)
    93     
    85     
   149   if text_version.email:
   141   if text_version.email:
   150     template_dict['email'] = text_version.email
   142     template_dict['email'] = text_version.email
   151   else:
   143   else:
   152     template_dict['email'] = users[0].email
   144     template_dict['email'] = users[0].email
   153 
   145 
       
   146   # Attachments
       
   147   attachments = []
       
   148   template_dict['content'] = re.sub("%s" %settings.SITE_URL, '', template_dict['content']) # replaces absolute urls by relative urls
       
   149   attach_re = r'(?:/text/(?P<key>\w*))?/attach/(?P<attach_key>\w*)/'
       
   150   attach_str_textversion = r'/text/%s/attach/%s/'
       
   151   attach_str = r'/attach/%s/'
       
   152   for match in re.findall(attach_re, template_dict['content']):
       
   153     if match[0]: # removes text_version, attachements do not need it
       
   154       template_dict['content'] = template_dict['content'].replace(attach_str_textversion %match, attach_str %match[1])
       
   155 
       
   156     attach = Attachment.objects.get(key=match[1])
       
   157     img_fmt = imghdr.what(attach.data.path)
       
   158     img = open(attach.data.path, 'rb')
       
   159     attachments.append({'key': match[1], 'data': base64.b64encode(img.read())})
       
   160     img.close()
       
   161   template_dict['attachments'] = attachments
       
   162 
   154   # Renders template
   163   # Renders template
   155   export_content = render_to_string('site/export.xml', template_dict, context_instance=RequestContext(request))
   164   export_content = render_to_string('site/export.xml', template_dict, context_instance=RequestContext(request))
   156 
   165 
   157   # Returns HTTP response
   166   # Returns HTTP response
   158   export_infos = EXPORT2_INFOS['xml']
   167   export_infos = EXPORT2_INFOS['xml']