src/cm/views/create.py
author Simon Descarpentries <sid@sopinspace.com>
Thu, 13 Mar 2014 18:19:43 +0100
changeset 613 a0b695aace0a
parent 499 805e08ea57b1
permissions -rw-r--r--
File field of Upload from file is now mandatory. Integrates 1sts IRI improvements in JavaScript test-suite. Continue testing error messages of mandatory fields, up to 704 tests.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     1
from cm.cm_settings import VALID_EMAIL_FOR_PUBLISH, SITE_NAME
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     2
from cm.converters import convert_from_mimetype
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     3
from cm.converters.pandoc_converters import pandoc_convert
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
     4
from cm.models import Text, TextVersion, Comment, Attachment
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     5
from cm.utils.files import remove_extension
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     6
from cm.utils.mail import EmailMessage
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     7
from cm.views import get_text_by_keys_or_404
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     8
from django import forms
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     9
from cm.message import display_message
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    10
from django.conf import settings
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    11
from django.core.urlresolvers import reverse
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    12
from django.forms import ModelForm
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    13
from django.forms.util import ErrorList
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    14
from django.http import HttpResponse, HttpResponseRedirect
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    15
from django.shortcuts import render_to_response
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    16
from django.template import RequestContext
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    17
from django.template.loader import render_to_string
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    18
from django.utils.translation import ugettext as _, ugettext_lazy
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    19
from django.db import connection, transaction
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    20
from mimetypes import guess_type
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    21
from cm.activity import register_activity
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    22
from cm.security import has_global_perm
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    23
import os
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    24
from BeautifulSoup import BeautifulStoneSoup
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    25
import re
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    26
from base64 import b64decode
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    27
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    28
class CreateTextUploadForm(ModelForm):
613
a0b695aace0a File field of Upload from file is now mandatory.
Simon Descarpentries <sid@sopinspace.com>
parents: 499
diff changeset
    29
    file = forms.FileField(required=True,
a0b695aace0a File field of Upload from file is now mandatory.
Simon Descarpentries <sid@sopinspace.com>
parents: 499
diff changeset
    30
                           label=ugettext_lazy("Upload file"),
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    31
                           help_text=ugettext_lazy("Upload a file from your computer instead of using the direct input above"),)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    32
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    33
    title = forms.CharField(required=False,
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    34
                                  label=ugettext_lazy("Title"))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    35
    class Meta:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    36
        model = TextVersion
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    37
        fields = ('title', 'format', 'tags') #, 'note'
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    38
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    39
    def clean(self):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    40
        cleaned_data = self.cleaned_data
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    41
        if not cleaned_data.get('file', None) :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    42
            msg = _("You should specify a file to upload.")
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    43
            self._errors["file"] = ErrorList([msg])
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    44
              
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    45
        return cleaned_data
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    46
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    47
class CreateTextImportForm(ModelForm):
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    48
    file = forms.FileField(required=True,
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    49
                           label=ugettext_lazy("Upload XML file"),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    50
                           help_text=ugettext_lazy("Upload a previously exported XML file from your computer"),)
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    51
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    52
    class Meta:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    53
        model = TextVersion
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    54
        fields = ()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    55
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    56
    def clean(self):
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    57
        cleaned_data = self.cleaned_data
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    58
        if not cleaned_data.get('file', None) :
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    59
          msg = _("You should specify a file to upload.")
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    60
          self._errors["file"] = ErrorList([msg])
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    61
          return cleaned_data
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    62
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    63
        uploaded_file = self.cleaned_data['file']
463
9c7de6dd1723 Fix import for use with api (with co_ment Drupal module for eg.).
gibus
parents: 460
diff changeset
    64
        if (uploaded_file.content_type != 'text/xml' and (uploaded_file.content_type != 'application/octet-stream' or cleaned_data.get('mime', 'application/xml') != 'application/xml')):
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    65
          msg = _("The imported file should be an XML file generated by co-ment when exporting a text and comments.")
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    66
          self._errors["file"] = ErrorList([msg])
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    67
          return cleaned_data
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    68
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    69
        soup = BeautifulStoneSoup(uploaded_file)
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    70
        if not soup.co_ment_text:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    71
          msg = _("No co_ment_text node found in XML.")
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    72
          self._errors["file"] = ErrorList([msg])
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    73
          return cleaned_data
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    74
        for mandatory_child in ['title', 'created', 'modified', 'name', 'email', 'format', 'content']:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    75
          if not getattr(soup.co_ment_text, mandatory_child):
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    76
            msg = _('No %(tag)s node found in XML.' %{"tag":mandatory_child})
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    77
            self._errors["file"] = ErrorList([msg])
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    78
            return cleaned_data
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    79
        cleaned_data['soup'] = soup
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    80
        return cleaned_data
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
    81
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    82
class CreateTextContentForm(ModelForm):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    83
    title = forms.CharField(required=True,
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    84
                            label=ugettext_lazy("Title"),
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    85
                            help_text=ugettext_lazy("The title of your text"),
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    86
                            widget=forms.TextInput)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    87
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    88
    class Meta:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    89
        model = TextVersion
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    90
        fields = ('title', 'format', 'content','tags') #, 'note'
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    91
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    92
@has_global_perm('can_create_text')
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    93
def text_create_content(request):
216
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
    94
    text, rep = _text_create_content(request, CreateTextContentForm)
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
    95
    return rep
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    96
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    97
def redirect_post_create(text) :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    98
    return HttpResponseRedirect(reverse('text-view', args=[text.key]))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    99
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   100
def _text_create_content(request, createForm):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   101
    document = ""
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   102
        
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   103
    if request.method == 'POST':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   104
        form = createForm(request.POST)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   105
        if form.is_valid():
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   106
                text = create_text(request.user, form.cleaned_data)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   107
                
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   108
                register_activity(request, "text_created", text)
106
rbernard
parents: 0
diff changeset
   109
                display_message(request, _(u'Text "%(text_title)s" has been created') %{"text_title":text.get_latest_version().title})
216
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
   110
                return text, redirect_post_create(text)
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   111
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   112
        form = createForm()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   113
        
216
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
   114
    return None, render_to_response('site/text_create_content.html', {'document':document, 'form' : form}, context_instance=RequestContext(request))
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   115
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   116
def _text_create_upload(request, createForm):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   117
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   118
    if request.method == 'POST':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   119
        form = createForm(request.POST, request.FILES)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   120
        if form.is_valid():
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   121
            # should convert?
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   122
            if form.cleaned_data['file']:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   123
                try:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   124
                    uploaded_file = form.cleaned_data['file']
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   125
                    content, attachs = convert_from_mimetype(uploaded_file.temporary_file_path(),
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   126
                                                    uploaded_file.content_type,
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   127
                                                    format=form.cleaned_data['format'],
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   128
                                                    )
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   129
                    form.cleaned_data['content'] = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   130
                    form.cleaned_data['attachs'] = attachs
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   131
                    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   132
                    # set title if not present
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   133
                    if not form.cleaned_data.get('title', None):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   134
                        form.cleaned_data['title'] = remove_extension(uploaded_file.name)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   135
                        
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   136
                    del form.cleaned_data['file']
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   137
                except:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   138
                    raise
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   139
                
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   140
            text = create_text(request.user, form.cleaned_data)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   141
            
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   142
            register_activity(request, "text_created", text)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   143
            
106
rbernard
parents: 0
diff changeset
   144
            display_message(request, _(u'Text "%(text_title)s" has been created')%{"text_title":text.get_latest_version().title})
216
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
   145
            return text, redirect_post_create(text)
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   146
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   147
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   148
        form = createForm()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   149
        
216
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
   150
    return None, render_to_response('site/text_create_upload.html', {'form' : form}, context_instance=RequestContext(request))
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   151
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   152
def _text_create_import(request, createForm):
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   153
  if request.method == 'POST':
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   154
    form = createForm(request.POST, request.FILES)
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   155
    if form.is_valid():
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   156
      soup = form.cleaned_data['soup']
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   157
      if not soup.co_ment_text:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   158
        raise Exception('Bad Soup')
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   159
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   160
      # Process attachments first to create new keys.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   161
      attachments_keys_map = {}
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   162
      if soup.co_ment_text.attachments:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   163
        for imported_attachement in soup.co_ment_text.attachments.findAll('attachment'):
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   164
          # Creates attachment object.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   165
          filename = 'imported_attachment'
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   166
          attachment = Attachment.objects.create_attachment(filename=filename, data=b64decode(imported_attachement.data.renderContents()), text_version=None)
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   167
          # Stores key mapping.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   168
          attachments_keys_map[imported_attachement.key.renderContents()] = attachment.key
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   169
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   170
      # Process text.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   171
      form.cleaned_data['title'] = soup.co_ment_text.title.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   172
      form.cleaned_data['format'] = soup.co_ment_text.format.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   173
      form.cleaned_data['content'] = re.sub(r'^<!\[CDATA\[|\]\]>$', '', soup.co_ment_text.content.renderContents())
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   174
      form.cleaned_data['name'] = soup.co_ment_text.find('name').renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   175
      form.cleaned_data['email'] = soup.co_ment_text.email.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   176
      if soup.co_ment_text.tags:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   177
        form.cleaned_data['tags'] = soup.co_ment_text.tags.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   178
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   179
      # Replaces attachements keys in content.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   180
      for old_key in attachments_keys_map.keys():
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   181
        form.cleaned_data['content'] = re.sub(old_key, attachments_keys_map[old_key], form.cleaned_data['content'])
463
9c7de6dd1723 Fix import for use with api (with co_ment Drupal module for eg.).
gibus
parents: 460
diff changeset
   182
      form.cleaned_data['content'] = re.sub(r'src="/attach/', 'src="' + settings.SITE_URL + '/attach/', form.cleaned_data['content'])
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   183
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   184
      # Creates text.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   185
      text = create_text(request.user, form.cleaned_data)
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   186
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   187
      # Brute updates of dates (cannot do it through django models since fields are set with auto_now or auto_now_add).
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   188
      created = soup.co_ment_text.created.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   189
      modified = soup.co_ment_text.modified.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   190
      cursor = connection.cursor()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   191
      cursor.execute("UPDATE cm_textversion SET created = %s, modified = %s WHERE id = %s", [created, modified, text.last_text_version_id])
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   192
      cursor.execute("UPDATE cm_text SET created = %s, modified = %s WHERE id = %s", [created, modified, text.id])
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   193
      transaction.commit_unless_managed()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   194
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   195
      # Process comments.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   196
      if soup.co_ment_text.comments:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   197
        comments_ids_map = {}
499
805e08ea57b1 Fixes crash when editing imported text with orphan comment.
gibus
parents: 463
diff changeset
   198
        all_comments = soup.co_ment_text.comments.findAll('comment')
805e08ea57b1 Fixes crash when editing imported text with orphan comment.
gibus
parents: 463
diff changeset
   199
        # Sort by id in order to have parent processed before reply_to
805e08ea57b1 Fixes crash when editing imported text with orphan comment.
gibus
parents: 463
diff changeset
   200
        for imported_comment in sorted(all_comments, key=lambda cid: cid.id.renderContents()):
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   201
          # Creates each comment.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   202
          comment = Comment.objects.create(
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   203
              text_version=text.get_latest_version(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   204
              title=imported_comment.title.renderContents(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   205
              state=imported_comment.state.renderContents(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   206
              name=imported_comment.find('name').renderContents(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   207
              email=imported_comment.email.renderContents(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   208
              format=imported_comment.format.renderContents(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   209
              content=re.sub(r'^<!\[CDATA\[|\]\]>$', '', imported_comment.content.renderContents()),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   210
              content_html=re.sub(r'^<!\[CDATA\[|\]\]>$', '', imported_comment.content_html.renderContents()),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   211
              )
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   212
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   213
          # Stores id for reply_to mapping.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   214
          comments_ids_map[imported_comment.id.renderContents()] = comment
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   215
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   216
          # Process boolean and potentially null integer/foreign key attributes.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   217
          save = False
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   218
          if imported_comment.deleted.renderContents() == 'True':
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   219
            comment.deleted = True
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   220
            save = True
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   221
          if imported_comment.start_wrapper.renderContents() != 'None':
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   222
            comment.start_wrapper = imported_comment.start_wrapper.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   223
            save = True
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   224
          if imported_comment.end_wrapper.renderContents() != 'None':
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   225
            comment.end_wrapper = imported_comment.end_wrapper.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   226
            save = True
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   227
          if imported_comment.start_offset.renderContents() != 'None':
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   228
            comment.start_offset = imported_comment.start_offset.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   229
            save = True
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   230
          if imported_comment.end_offset.renderContents() != 'None':
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   231
            comment.end_offset = imported_comment.end_offset.renderContents()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   232
            save = True
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   233
          if imported_comment.find('parent'):
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   234
            comment.reply_to = comments_ids_map.get(imported_comment.find('parent').renderContents())
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   235
            save = True
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   236
          if save:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   237
            comment.save()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   238
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   239
          # Brute updates of dates (cannot do it through django models since fields are set with auto_now or auto_now_add).
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   240
          created=imported_comment.created.renderContents(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   241
          modified=imported_comment.modified.renderContents(),
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   242
          cursor.execute("UPDATE cm_comment SET created = %s, modified = %s WHERE id = %s", [created, modified, comment.id])
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   243
          transaction.commit_unless_managed()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   244
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   245
      # Logs on activity.
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   246
      register_activity(request, "text_imported", text)
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   247
      display_message(request, _(u'Text "%(text_title)s" has been imported')%{"text_title":text.get_latest_version().title})
463
9c7de6dd1723 Fix import for use with api (with co_ment Drupal module for eg.).
gibus
parents: 460
diff changeset
   248
      return text, HttpResponseRedirect(reverse('text-view', args=[text.key]))
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   249
  else:
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   250
    form = createForm()
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   251
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   252
  return None, render_to_response('site/text_create_import.html', {'form' : form}, context_instance=RequestContext(request))
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   253
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   254
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   255
@has_global_perm('can_create_text')
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   256
def text_create_upload(request):
216
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
   257
    text, rep = _text_create_upload(request, CreateTextUploadForm)
6f635c8e98c3 add text to monkey patchable text creation function
raph
parents: 175
diff changeset
   258
    return rep
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   259
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   260
def text_create_import(request):
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   261
    text, rep = _text_create_import(request, CreateTextImportForm)
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   262
    return rep
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   263
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   264
def create_text(user, data):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   265
    text = Text.objects.create_text(title=data['title'],
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   266
                                    format=data['format'],
175
4f072edc51a1 BUG FIX : handling html
rbernard
parents: 106
diff changeset
   267
                                    content=data['content'],
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   268
                                    note=data.get('note', None),
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   269
                                    name=data.get('name', None),
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   270
                                    email=data.get('email', None),
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   271
                                    tags=data.get('tags', None),
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   272
                                    user=user
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   273
                                    )
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   274
    text.update_denorm_fields()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   275
    text_version = text.get_latest_version()
175
4f072edc51a1 BUG FIX : handling html
rbernard
parents: 106
diff changeset
   276
    text_content = text_version.content
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   277
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   278
    for attach_file in data.get('attachs',  []):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   279
        attach_data = file(attach_file, 'rb').read()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   280
        filename = os.path.basename(attach_file)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   281
        attachment = Attachment.objects.create_attachment(filename=filename, data=attach_data, text_version=text_version)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   282
        attach_url = reverse('text-attach', args=[text.key, attachment.key])
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   283
        text_content = text_content.replace(filename, attach_url)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   284
            
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   285
    # save updated (attach links) text content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   286
    text_version.content = text_content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   287
    text_version.save()
460
2fdb7d095d5c Added import from XML file, including text, comments and attachments.
gibus
parents: 216
diff changeset
   288
    return text