Fix import for use with api (with co_ment Drupal module for eg.).
authorgibus
Fri, 14 Sep 2012 09:50:05 +0200
changeset 463 9c7de6dd1723
parent 462 189be4b3d712
child 464 5a02bfc8aae8
Fix import for use with api (with co_ment Drupal module for eg.).
src/cm/api/handlers.py
src/cm/api/urls.py
src/cm/views/create.py
--- a/src/cm/api/handlers.py	Fri Sep 14 09:49:27 2012 +0200
+++ b/src/cm/api/handlers.py	Fri Sep 14 09:50:05 2012 +0200
@@ -7,7 +7,7 @@
     has_perm_on_text_api
 from cm.security import get_viewable_comments
 from cm.utils.embed import embed_html
-from cm.views.create import CreateTextContentForm, create_text
+from cm.views.create import CreateTextContentForm, create_text, CreateTextImportForm, _text_create_import
 from cm.views.texts import client_exchange, text_view_frame, text_view_comments, text_export
 from cm.views.feeds import text_feed
 from piston.utils import validate
@@ -209,7 +209,7 @@
         return text_pre_edit(request, key=key)
 
 from cm.views.texts import text_edit
-    
+
 class TextEditHandler(BaseHandler):
     allowed_methods = ('POST', )    
     type = "Text methods"
@@ -430,6 +430,25 @@
     def create(self, request, key, format, download, whichcomments, withcolor):
         return text_export(request, key, format, download, whichcomments, withcolor, adminkey=None)
 
+class ImportHandler(BaseHandler):
+    allowed_methods = ('POST', )    
+    type = "Text methods"
+    title = "Import text and comments"
+    desc = "Import a previously exported text, along with comments and attachments in XML format."
+    args = """<br />
+`xml`: Previously exported XML file of text, comments and attachments<br />
+    """ 
+    
+    @staticmethod
+    def endpoint():
+        return URL_PREFIX + '/import/'
+    
+    
+    def create(self, request):
+      text, res = _text_create_import(request, CreateTextImportForm)
+      text_version = text.last_text_version
+      return {'key' : text.key , 'version_key' : text.last_text_version.key, 'html': text_version.content}
+
 class AnonymousCommentsHandler(AnonymousBaseHandler):
     allowed_methods = ('GET',)    
     type = "Comment methods"
--- a/src/cm/api/urls.py	Fri Sep 14 09:49:27 2012 +0200
+++ b/src/cm/api/urls.py	Fri Sep 14 09:50:05 2012 +0200
@@ -18,6 +18,7 @@
 tv_delete_handler = Resource(handler=TextVersionDeleteHandler, authentication=auth)
 
 text_export_handler = Resource(handler=TextExportHandler, authentication=auth)
+import_handler = Resource(handler=ImportHandler, authentication=auth)
 
 comments_handler = Resource(handler=CommentsHandler, authentication=auth)
 
@@ -43,6 +44,7 @@
    url(r'^text/(?P<key>\w*)/comments/(?P<version_key>\w*)/$', comment_handler),
    
    url(r'^text/(?P<key>\w*)/export/(?P<format>\w*)/(?P<download>\w*)/(?P<whichcomments>\w*)/(?P<withcolor>\w*)/$', text_export_handler),
+   url(r'^import/$', import_handler),
 
    url(r'^text/(?P<key>\w*)/feed/$', text_feed_handler),
    url(r'^text/(?P<key>\w*)/delete/$', text_delete_handler),
--- a/src/cm/views/create.py	Fri Sep 14 09:49:27 2012 +0200
+++ b/src/cm/views/create.py	Fri Sep 14 09:50:05 2012 +0200
@@ -61,7 +61,7 @@
           return cleaned_data
 
         uploaded_file = self.cleaned_data['file']
-        if (uploaded_file.content_type != 'text/xml'):
+        if (uploaded_file.content_type != 'text/xml' and (uploaded_file.content_type != 'application/octet-stream' or cleaned_data.get('mime', 'application/xml') != 'application/xml')):
           msg = _("The imported file should be an XML file generated by co-ment when exporting a text and comments.")
           self._errors["file"] = ErrorList([msg])
           return cleaned_data
@@ -179,6 +179,7 @@
       # Replaces attachements keys in content.
       for old_key in attachments_keys_map.keys():
         form.cleaned_data['content'] = re.sub(old_key, attachments_keys_map[old_key], form.cleaned_data['content'])
+      form.cleaned_data['content'] = re.sub(r'src="/attach/', 'src="' + settings.SITE_URL + '/attach/', form.cleaned_data['content'])
 
       # Creates text.
       text = create_text(request.user, form.cleaned_data)
@@ -242,7 +243,7 @@
       # Logs on activity.
       register_activity(request, "text_imported", text)
       display_message(request, _(u'Text "%(text_title)s" has been imported')%{"text_title":text.get_latest_version().title})
-      return None, HttpResponseRedirect(reverse('text-view', args=[text.key]))
+      return text, HttpResponseRedirect(reverse('text-view', args=[text.key]))
   else:
     form = createForm()