| author | Simon Descarpentries <sid@sopinspace.com> |
| Thu, 13 Mar 2014 18:19:43 +0100 | |
| changeset 613 | a0b695aace0a |
| parent 499 | 805e08ea57b1 |
| permissions | -rw-r--r-- |
| 0 | 1 |
from cm.cm_settings import VALID_EMAIL_FOR_PUBLISH, SITE_NAME |
2 |
from cm.converters import convert_from_mimetype |
|
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 | 5 |
from cm.utils.files import remove_extension |
6 |
from cm.utils.mail import EmailMessage |
|
7 |
from cm.views import get_text_by_keys_or_404 |
|
8 |
from django import forms |
|
9 |
from cm.message import display_message |
|
10 |
from django.conf import settings |
|
11 |
from django.core.urlresolvers import reverse |
|
12 |
from django.forms import ModelForm |
|
13 |
from django.forms.util import ErrorList |
|
14 |
from django.http import HttpResponse, HttpResponseRedirect |
|
15 |
from django.shortcuts import render_to_response |
|
16 |
from django.template import RequestContext |
|
17 |
from django.template.loader import render_to_string |
|
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 | 20 |
from mimetypes import guess_type |
21 |
from cm.activity import register_activity |
|
22 |
from cm.security import has_global_perm |
|
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 | 27 |
|
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 | 31 |
help_text=ugettext_lazy("Upload a file from your computer instead of using the direct input above"),) |
32 |
||
33 |
title = forms.CharField(required=False, |
|
34 |
label=ugettext_lazy("Title")) |
|
35 |
class Meta: |
|
36 |
model = TextVersion |
|
37 |
fields = ('title', 'format', 'tags') #, 'note' |
|
38 |
||
39 |
def clean(self): |
|
40 |
cleaned_data = self.cleaned_data |
|
41 |
if not cleaned_data.get('file', None) : |
|
42 |
msg = _("You should specify a file to upload.") |
|
43 |
self._errors["file"] = ErrorList([msg]) |
|
44 |
||
45 |
return cleaned_data |
|
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 | 82 |
class CreateTextContentForm(ModelForm): |
83 |
title = forms.CharField(required=True, |
|
84 |
label=ugettext_lazy("Title"), |
|
85 |
help_text=ugettext_lazy("The title of your text"), |
|
86 |
widget=forms.TextInput) |
|
87 |
||
88 |
class Meta: |
|
89 |
model = TextVersion |
|
90 |
fields = ('title', 'format', 'content','tags') #, 'note' |
|
91 |
||
92 |
@has_global_perm('can_create_text') |
|
93 |
def text_create_content(request): |
|
| 216 | 94 |
text, rep = _text_create_content(request, CreateTextContentForm) |
95 |
return rep |
|
| 0 | 96 |
|
97 |
def redirect_post_create(text) : |
|
98 |
return HttpResponseRedirect(reverse('text-view', args=[text.key])) |
|
99 |
||
100 |
def _text_create_content(request, createForm): |
|
101 |
document = "" |
|
102 |
||
103 |
if request.method == 'POST': |
|
104 |
form = createForm(request.POST) |
|
105 |
if form.is_valid(): |
|
106 |
text = create_text(request.user, form.cleaned_data) |
|
107 |
||
108 |
register_activity(request, "text_created", text) |
|
| 106 | 109 |
display_message(request, _(u'Text "%(text_title)s" has been created') %{"text_title":text.get_latest_version().title}) |
| 216 | 110 |
return text, redirect_post_create(text) |
| 0 | 111 |
else: |
112 |
form = createForm() |
|
113 |
||
| 216 | 114 |
return None, render_to_response('site/text_create_content.html', {'document':document, 'form' : form}, context_instance=RequestContext(request)) |
| 0 | 115 |
|
116 |
def _text_create_upload(request, createForm): |
|
117 |
||
118 |
if request.method == 'POST': |
|
119 |
form = createForm(request.POST, request.FILES) |
|
120 |
if form.is_valid(): |
|
121 |
# should convert? |
|
122 |
if form.cleaned_data['file']: |
|
123 |
try: |
|
124 |
uploaded_file = form.cleaned_data['file'] |
|
125 |
content, attachs = convert_from_mimetype(uploaded_file.temporary_file_path(), |
|
126 |
uploaded_file.content_type, |
|
127 |
format=form.cleaned_data['format'], |
|
128 |
) |
|
129 |
form.cleaned_data['content'] = content |
|
130 |
form.cleaned_data['attachs'] = attachs |
|
131 |
||
132 |
# set title if not present |
|
133 |
if not form.cleaned_data.get('title', None): |
|
134 |
form.cleaned_data['title'] = remove_extension(uploaded_file.name) |
|
135 |
||
136 |
del form.cleaned_data['file'] |
|
137 |
except: |
|
138 |
raise |
|
139 |
||
140 |
text = create_text(request.user, form.cleaned_data) |
|
141 |
||
142 |
register_activity(request, "text_created", text) |
|
143 |
||
| 106 | 144 |
display_message(request, _(u'Text "%(text_title)s" has been created')%{"text_title":text.get_latest_version().title}) |
| 216 | 145 |
return text, redirect_post_create(text) |
| 0 | 146 |
|
147 |
else: |
|
148 |
form = createForm() |
|
149 |
||
| 216 | 150 |
return None, render_to_response('site/text_create_upload.html', {'form' : form}, context_instance=RequestContext(request)) |
| 0 | 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 | 255 |
@has_global_perm('can_create_text') |
256 |
def text_create_upload(request): |
|
| 216 | 257 |
text, rep = _text_create_upload(request, CreateTextUploadForm) |
258 |
return rep |
|
| 0 | 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 | 264 |
def create_text(user, data): |
265 |
text = Text.objects.create_text(title=data['title'], |
|
266 |
format=data['format'], |
|
| 175 | 267 |
content=data['content'], |
| 0 | 268 |
note=data.get('note', None), |
269 |
name=data.get('name', None), |
|
270 |
email=data.get('email', None), |
|
271 |
tags=data.get('tags', None), |
|
272 |
user=user |
|
273 |
) |
|
274 |
text.update_denorm_fields() |
|
275 |
text_version = text.get_latest_version() |
|
| 175 | 276 |
text_content = text_version.content |
| 0 | 277 |
|
278 |
for attach_file in data.get('attachs', []): |
|
279 |
attach_data = file(attach_file, 'rb').read() |
|
280 |
filename = os.path.basename(attach_file) |
|
281 |
attachment = Attachment.objects.create_attachment(filename=filename, data=attach_data, text_version=text_version) |
|
282 |
attach_url = reverse('text-attach', args=[text.key, attachment.key]) |
|
283 |
text_content = text_content.replace(filename, attach_url) |
|
284 |
||
285 |
# save updated (attach links) text content |
|
286 |
text_version.content = text_content |
|
287 |
text_version.save() |
|
|
460
2fdb7d095d5c
Added import from XML file, including text, comments and attachments.
gibus
parents:
216
diff
changeset
|
288 |
return text |