14 from cm.utils.html import on_content_receive |
14 from cm.utils.html import on_content_receive |
15 from cm.utils.comment_positioning import compute_new_comment_positions, \ |
15 from cm.utils.comment_positioning import compute_new_comment_positions, \ |
16 insert_comment_markers |
16 insert_comment_markers |
17 from cm.utils.spannifier import spannify |
17 from cm.utils.spannifier import spannify |
18 from cm.views import get_keys_from_dict, get_textversion_by_keys_or_404, get_text_by_keys_or_404, redirect |
18 from cm.views import get_keys_from_dict, get_textversion_by_keys_or_404, get_text_by_keys_or_404, redirect |
19 from cm.views.export import content_export2, content_export, xml_export |
19 from cm.views.export import content_export2, xml_export |
20 from cm.views.user import AnonUserRoleForm, cm_login |
20 from cm.views.user import AnonUserRoleForm, cm_login |
21 from difflib import unified_diff |
21 from difflib import unified_diff |
22 from django import forms |
22 from django import forms |
23 from django.conf import settings |
23 from django.conf import settings |
24 from django.contrib.auth import login as django_login |
24 from django.contrib.auth import login as django_login |
322 with_color = withcolor == "1" |
322 with_color = withcolor == "1" |
323 |
323 |
324 comments = [] # whichcomments=="none" |
324 comments = [] # whichcomments=="none" |
325 |
325 |
326 if whichcomments == "filtered" or whichcomments == "all": |
326 if whichcomments == "filtered" or whichcomments == "all": |
327 #comments = text_version.comment_set.filter(reply_to__isnull=True)# whichcomments=="all" |
|
328 #comments = get_viewable_comments(request, text_version.comment_set.filter(reply_to__isnull=True), text, order_by=('start_wrapper','start_offset','end_wrapper','end_offset'))# whichcomments=="all" |
|
329 |
|
330 _comments = text_version.comment_set.all() |
327 _comments = text_version.comment_set.all() |
331 if whichcomments == "filtered" : |
328 if whichcomments == "filtered" : |
332 filteredIds = [] |
329 filteredIds = [] |
333 if request.method == 'POST' : |
330 if request.method == 'POST' : |
334 ll = request.POST.get('filteredIds',[]).split(",") |
331 ll = request.POST.get('filteredIds',[]).split(",") |
336 _comments = text_version.comment_set.filter(id__in=filteredIds) # security ! TODO CROSS PERMISSIONS WITH POST CONTENT |
333 _comments = text_version.comment_set.filter(id__in=filteredIds) # security ! TODO CROSS PERMISSIONS WITH POST CONTENT |
337 |
334 |
338 comments = get_viewable_comments(request, _comments, text, order_by=('start_wrapper','start_offset','end_wrapper','end_offset'))# whichcomments=="all" |
335 comments = get_viewable_comments(request, _comments, text, order_by=('start_wrapper','start_offset','end_wrapper','end_offset'))# whichcomments=="all" |
339 |
336 |
340 # decide to use pandoc or not |
337 # decide to use pandoc or not |
341 if with_color : |
338 if format in ('markdown', 'latex', 'epub') : |
342 use_pandoc = False # pandoc wouldn't preserve comments scope background colors |
339 use_pandoc = True |
343 else : |
340 else: |
344 if format in ('markdown', 'latex', 'epub') : |
341 use_pandoc = (original_format == 'markdown' or original_format == 'rst') |
345 use_pandoc = True |
|
346 elif format in ('pdf', 'odt') : |
|
347 use_pandoc = (original_format == "markdown") |
|
348 elif format in ('docx', 'doc', 'html', 'xml') : |
|
349 use_pandoc = False |
|
350 |
342 |
351 # correct attach path => real path |
343 # correct attach path => real path |
352 if format in ('pdf','odt') : |
344 if format in ('pdf', 'odt', 'doc', 'docx') : |
353 original_content = from_html_links_to_abs_links(original_content) |
345 original_content = from_html_links_to_abs_links(original_content) |
354 |
346 |
355 if len(comments) == 0 : #want to bypass html conversion in this case |
347 if len(comments) == 0 : #want to bypass html conversion in this case |
356 return content_export2(request, original_content, text_version.title, original_format, format, use_pandoc, download_response) |
348 # Prepends title |
|
349 if original_format == 'html': |
|
350 original_content = "<h1>%s</h1>%s" %(text_version.title, original_content) |
|
351 elif original_format == 'markdown': |
|
352 original_content = "%s\n======\n%s" %(text_version.title, original_content) |
|
353 elif original_format == 'rst': |
|
354 underline = '=' * len(text_version.title) |
|
355 original_content = "%s\n%s\n%s" %(text_version.title, underline, original_content) |
|
356 |
|
357 return content_export2(request, original_content, text_version.title, original_format, format, use_pandoc, download_response) |
357 else : # case comments to be added |
358 else : # case comments to be added |
358 #comments = comments.order_by('start_wrapper','start_offset','end_wrapper','end_offset') |
|
359 html = pandoc_convert(original_content, original_format, 'html') |
359 html = pandoc_convert(original_content, original_format, 'html') |
360 wrapped_text_version, _ , _ = spannify(html) |
360 wrapped_text_version, _ , _ = spannify(html) |
361 with_markers = True |
361 with_markers = True |
362 marked_content = insert_comment_markers(wrapped_text_version, comments, with_markers, with_color) |
362 marked_content = insert_comment_markers(wrapped_text_version, comments, with_markers, with_color) |
363 |
363 # Prepends title |
|
364 marked_content = "<h1>%s</h1>%s" %(text_version.title, marked_content) |
364 viewable_comments = comments_thread(request, text_version, text) |
365 viewable_comments = comments_thread(request, text_version, text) |
365 # viewable_commentsnoreply = get_viewable_comments(request, commentsnoreply, text, order_by = ('start_wrapper','start_offset','end_wrapper','end_offset')) |
|
366 # viewable_comments = [] |
|
367 # for cc in viewable_commentsnoreply : |
|
368 # viewable_comments += list_viewable_comments(request, [cc], text) |
|
369 |
|
370 # numerotation{ id --> numbered as a child} |
|
371 extended_comments = {} |
366 extended_comments = {} |
372 nb_children = {} |
367 nb_children = {} |
373 for cc in viewable_comments : |
368 for cc in viewable_comments : |
374 id = 0 #<-- all top comments are children of comment with id 0 |
369 id = 0 #<-- all top comments are children of comment with id 0 |
375 if cc.is_reply() : |
370 if cc.is_reply() : |
382 extended_comments[cc.id] = cc |
377 extended_comments[cc.id] = cc |
383 |
378 |
384 if cc.is_reply() : |
379 if cc.is_reply() : |
385 cc.num = "%s.%s"%(extended_comments[cc.reply_to_id].num, cc.num) |
380 cc.num = "%s.%s"%(extended_comments[cc.reply_to_id].num, cc.num) |
386 |
381 |
387 # viewable_comments += list_viewable_comments(request, viewable_commentsnoreply, text) |
|
388 html_comments=render_to_string('site/macros/text_comments.html',{'comments':viewable_comments }, context_instance=RequestContext(request)) |
382 html_comments=render_to_string('site/macros/text_comments.html',{'comments':viewable_comments }, context_instance=RequestContext(request)) |
389 |
383 |
390 content = "%s%s"%(marked_content, html_comments) |
384 content = "%s%s"%(marked_content, html_comments) |
391 content_format = "html" |
385 content_format = "html" |
392 # impossible to satisfy because of color then no colors instead: |
|
393 if with_color and format in ('markdown', 'tex') : #TODO : add S5 |
|
394 with_color = False |
|
395 |
386 |
396 return content_export2(request, content, text_version.title, content_format, format, use_pandoc, download_response) |
387 return content_export2(request, content, text_version.title, content_format, format, use_pandoc, download_response) |
397 |
|
398 def text_print(request, key, adminkey=None): |
|
399 text, admin = get_text_and_admin(key, adminkey) |
|
400 |
|
401 text_version = text.get_latest_version() |
|
402 |
|
403 # chosen default url behaviour is export all comments + bckcolor + pdf |
|
404 comments = Comment.objects.filter(text_version=text_version, reply_to__isnull=True) |
|
405 |
|
406 with_markers = True |
|
407 with_colors = True |
|
408 download_requested = True |
|
409 action = 'export' # client origine dialog |
|
410 requested_format = 'pdf' # requested output format |
|
411 |
|
412 if request.method == 'POST': |
|
413 # colors or not ? |
|
414 with_colors = (u'yes' == request.POST.get('p_color', u'no')) |
|
415 |
|
416 # restrict comments to ones that should be exported / printed |
|
417 p_comments = request.POST.get('p_comments') |
|
418 if p_comments == "filtered" or p_comments == "none" : |
|
419 filteredIds = [] # "none" case |
|
420 if p_comments == "filtered" : |
|
421 ll = request.POST.get('filteredIds').split(",") |
|
422 filteredIds = [ int(l) for l in ll if l] |
|
423 |
|
424 comments = comments.filter(id__in=filteredIds) |
|
425 |
|
426 # print or export ? |
|
427 action = request.POST.get('print_export_action') |
|
428 requested_format = request.POST.get('p_method') |
|
429 |
|
430 comments = comments.order_by('start_wrapper','start_offset','end_wrapper','end_offset') |
|
431 |
|
432 download_requested = (action == 'export') or (action == 'print' and requested_format != 'html') |
|
433 |
|
434 ori_format = text_version.format # BD : html or markdown for now ... |
|
435 src_format = ori_format # as expected by convert functions ... |
|
436 src = text_version.content |
|
437 |
|
438 if len(comments) > 0 and (with_markers or with_colors) : |
|
439 html = text_version.get_content() |
|
440 wrapped_text_version, _ , _ = spannify(html) |
|
441 marked_text_version = insert_comment_markers(wrapped_text_version, comments, with_markers, with_colors) |
|
442 |
|
443 src_format = 'html' |
|
444 src = marked_text_version |
|
445 html_comments=render_to_string('site/macros/text_comments.html',{'comments':comments}, context_instance=RequestContext(request)) |
|
446 src += html_comments |
|
447 |
|
448 if download_requested : |
|
449 use_pandoc = (requested_format == 'html' or requested_format == 'markdown') |
|
450 return content_export(request, src, text_version.title, src_format, requested_format, use_pandoc) |
|
451 else : # action == 'print' and requested_format == 'html' (no colors) |
|
452 template_dict = {'text' : text, |
|
453 'text_version' : text_version, |
|
454 'title' : text_version.title, # TODO use it ... |
|
455 'comments': comments, |
|
456 'content' : marked_text_version, |
|
457 'client_date_fmt' : settings.CLIENT_DATE_FMT |
|
458 } |
|
459 if admin: |
|
460 template_dict['adminkey'] = text.adminkey |
|
461 template_dict['admin'] = True |
|
462 return render_to_response('site/text_print.html', |
|
463 template_dict, |
|
464 context_instance=RequestContext(request)) |
|
465 |
388 |
466 @has_perm_on_text('can_view_text') |
389 @has_perm_on_text('can_view_text') |
467 def text_view_frame(request, key, version_key=None, adminkey=None): |
390 def text_view_frame(request, key, version_key=None, adminkey=None): |
468 text = get_text_by_keys_or_404(key) |
391 text = get_text_by_keys_or_404(key) |
469 |
392 |