# HG changeset patch # User gibus # Date 1336374645 -7200 # Node ID bacf162c7b58195eb213fdcbee04ec39b2638728 # Parent ef8b166a099381488e8ced57ff5ba16334e53f58 Adds api/convert to return HTML document from legacy format + Saves attached images when just converting to HTML, without creating a text + Returns nb of detached and removed commentswhen updating text. diff -r ef8b166a0993 -r bacf162c7b58 src/cm/api/handlers.py --- a/src/cm/api/handlers.py Wed Apr 25 09:25:39 2012 +0200 +++ b/src/cm/api/handlers.py Mon May 07 09:10:45 2012 +0200 @@ -1,7 +1,7 @@ from piston.handler import AnonymousBaseHandler, BaseHandler from piston.utils import rc -from cm.models import Text,TextVersion, Role, UserRole, Comment +from cm.models import Text,TextVersion, Role, UserRole, Comment, Attachment from cm.views import get_keys_from_dict, get_textversion_by_keys_or_404, get_text_by_keys_or_404, get_textversion_by_keys_or_404, redirect from cm.security import get_texts_with_perm, has_perm, get_viewable_comments, \ has_perm_on_text_api @@ -134,6 +134,35 @@ else: resp = rc.BAD_REQUEST return resp + +from cm.converters import _convert_from_mimetype +import os +from django.core.urlresolvers import reverse + +class ConvertHandler(BaseHandler): + type = "Text methods" + allowed_methods = ('POST', ) + title = "Convert a legacy file" + desc = "Returns the HTLM file." + args = """
+`file`: the file in legacy format
+ """ + + @staticmethod + def endpoint(): + return URL_PREFIX + '/convert/' + + def create(self, request): + mime = request.POST.get('mime', None) + the_file = request.FILES['file']; + html, attachs = _convert_from_mimetype(the_file.read(), mime, 'html') + for attach_file in attachs: + attach_data = file(attach_file, 'rb').read() + filename = os.path.basename(attach_file) + attachment = Attachment.objects.create_attachment(filename=filename, data=attach_data, text_version=None) + attach_url = reverse('notext-attach', args=[attachment.key]) + html = html.replace(filename, settings.SITE_URL + attach_url) + return {'html' : html} from cm.exception import UnauthorizedException from cm.views.texts import text_delete @@ -202,10 +231,16 @@ def create(self, request, key): + prev_text = get_text_by_keys_or_404(key) + prev_text_version = prev_text.last_text_version + prev_comments = prev_text_version.get_comments() + prev_scope_removed = [c for c in prev_comments if c.is_scope_removed()] res = text_edit(request, key=key) text = get_text_by_keys_or_404(key) text_version = text.last_text_version - return {'version_key' : text_version.key , 'created': text_version.created} + comments = text_version.get_comments() + scope_removed = [c for c in comments if c.is_scope_removed()] + return {'version_key' : text_version.key , 'created': text_version.created, 'nb_deleted' : len(prev_comments) - len(comments), 'nb_scope_removed' : len(scope_removed) - len(prev_scope_removed)} class AnonymousTextFeedHandler(AnonymousBaseHandler): @@ -452,7 +487,7 @@ if limit: query = query[:int(limit)] return query - + from piston.doc import documentation_view from piston.handler import handler_tracker diff -r ef8b166a0993 -r bacf162c7b58 src/cm/api/urls.py --- a/src/cm/api/urls.py Wed Apr 25 09:25:39 2012 +0200 +++ b/src/cm/api/urls.py Mon May 07 09:10:45 2012 +0200 @@ -21,6 +21,8 @@ comments_handler = Resource(handler=CommentsHandler, authentication=auth) +convert_handler = Resource(handler=ConvertHandler, authentication=auth) + client_handler = Resource(handler=ClientHandler, authentication=auth) jsi8n_handler = Resource(handler=JSI18NHandler, authentication=None) @@ -48,6 +50,7 @@ url(r'^text/(?P\w*)/edit/$', text_edit_handler), url(r'^text/(?P\w*)/(?P\w*)/$', textversion_handler), url(r'^comments/$', comments_handler), + url(r'^convert/$', convert_handler), url(r'^client/$', client_handler), url(r'^jsi18n/$', jsi8n_handler), url(r'^doc/$', documentation), diff -r ef8b166a0993 -r bacf162c7b58 src/cm/urls.py --- a/src/cm/urls.py Wed Apr 25 09:25:39 2012 +0200 +++ b/src/cm/urls.py Mon May 07 09:10:45 2012 +0200 @@ -77,6 +77,9 @@ url(r'^text/(?P\w*)/history/$', text_history, name="text-history"), #url(r'^text/(?P\w*)/diff/(?P\w*)/(?P\w*)/$', text_diff, name="text-diff"), # url(r'^text/(?P\w*)/version/(?P\w*)/$', text_version, name="text-version"), + + # detached attachements! (used for saving images when just converting to HTML, without creating a text) + url(r'^attach/(?P\w*)/$', notext_attach, name="notext-attach"), # main client frame url(r'^text/(?P\w*)/comments_frame/$', text_view_frame, name="text-view-comments-frame"), diff -r ef8b166a0993 -r bacf162c7b58 src/cm/views/texts.py --- a/src/cm/views/texts.py Wed Apr 25 09:25:39 2012 +0200 +++ b/src/cm/views/texts.py Mon May 07 09:10:45 2012 +0200 @@ -790,9 +790,13 @@ response = HttpResponse(content, mimetype) return response +def notext_attach(request, attach_key): + attach = Attachment.objects.get(key=attach_key) + content = file(attach.data.path).read() + mimetype, _encoding = mimetypes.guess_type(attach.data.path) + response = HttpResponse(content, mimetype) + return response - - def fix_anon_in_formset(formset): # fix role choice in formset for anon (not all role are allowed) role_field = [f.fields['role'] for f in formset.forms if f.instance.user == None][0]