diff -r 000000000000 -r 40c8f766c9b8 src/cm/views/texts.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/cm/views/texts.py Mon Nov 23 15:14:29 2009 +0100 @@ -0,0 +1,850 @@ +from cm.activity import register_activity +from cm.client import jsonize, get_filter_datas, edit_comment, remove_comment, \ + add_comment, RequestComplexEncoder, comments_thread +from cm.cm_settings import NEW_TEXT_VERSION_ON_EDIT +from cm.diff import text_diff as _text_diff, text_history as inner_text_history, \ + get_colors +from cm.exception import UnauthorizedException +from cm.message import * +from cm.models import * +from cm.models_base import generate_key +from cm.security import get_texts_with_perm, has_perm, get_viewable_comments, \ + has_perm_on_text +from cm.utils import get_among, get_among, get_int +from cm.utils.comment_positioning import compute_new_comment_positions, \ + insert_comment_markers +from cm.utils.html import cleanup_textarea +from cm.utils.spannifier import spannify +from cm.views import get_keys_from_dict, get_text_by_keys_or_404, redirect +from cm.views.export import content_export2 +from cm.views.user import AnonUserRoleForm, cm_login +from difflib import unified_diff +from django import forms +from django.conf import settings +from django.contrib.auth import login as django_login +from django.contrib.auth.forms import AuthenticationForm +from django.contrib.auth.models import User +from django.core.urlresolvers import reverse +from django.db.models import Q +from django.forms import ModelForm +from django.forms.models import BaseModelFormSet, modelformset_factory +from django.http import HttpResponse, HttpResponseRedirect, Http404 +from django.shortcuts import render_to_response +from django.template import RequestContext +from django.template.loader import render_to_string +from django.utils.translation import ugettext as _, ugettext_lazy +from django.views.generic.list_detail import object_list +import difflib +import logging +import mimetypes +import simplejson +import sys + + +def get_text_and_admin(key, adminkey, assert_admin = False): + """ + assert_admin => redirect to unauthorized if not admin + """ + admin = False + if adminkey: + text = Text.objects.get(key = key, adminkey = adminkey) + if text: + admin = True + else: + text = Text.objects.get(key=key) + if assert_admin and not admin: + raise UnauthorizedException('Is not admin') + return text, admin + + + +ACTIVITY_PAGINATION = 10 +RECENT_TEXT_NB = 5 +RECENT_COMMENT_NB = RECENT_TEXT_NB + +MODERATE_NB = 5 + +def dashboard(request): + request.session.set_test_cookie() + if request.user.is_authenticated(): + act_view = { + 'view_texts' : get_int(request.GET,'view_texts',1), + 'view_comments' : get_int(request.GET,'view_comments',1), + 'view_users' : get_int(request.GET,'view_users',1), + } + + paginate_by = get_int(request.GET,'paginate',ACTIVITY_PAGINATION) + + # texts with can_view_unapproved_comment perms + moderator_texts = get_texts_with_perm(request, 'can_view_unapproved_comment') + viewer_texts = get_texts_with_perm(request, 'can_view_approved_comment') + all_texts_ids = [t.id for t in moderator_texts] + [t.id for t in viewer_texts] + + span = get_among(request.GET,'span',('day','month','week',),'week') + template_dict = { + 'span' : span, + 'last_texts' : get_texts_with_perm(request, 'can_view_text').order_by('-modified')[:RECENT_TEXT_NB], + 'last_comments' : Comment.objects.filter(text_version__text__in=all_texts_ids).order_by('-created')[:RECENT_COMMENT_NB],# TODO: useful? + #'last_users' : User.objects.all().order_by('-date_joined')[:5], + } + template_dict.update(act_view) + + all_activities = { + 'view_comments' : ['comment_created','comment_removed'], + 'view_users' : ['user_created', 'user_activated', 'user_suspended','user_enabled',], + 'view_texts' : ['text_created','text_removed', 'text_edited', 'text_edited_new_version'], + } + + selected_activities = [] + [selected_activities.extend(all_activities[k]) for k in act_view.keys() if act_view[k]] + + activities = Activity.objects.filter(type__in = selected_activities) + if not has_perm(request,'can_manage_workspace'): + texts = get_texts_with_perm(request, 'can_view_text') + activities = activities.filter(Q(text__in=texts)) + + comments = [] + [comments.extend(get_viewable_comments(request, t.last_text_version.comment_set.all(), t)) for t in texts] + + activities = activities.filter(Q(comment__in=comments) | Q(comment=None) ) + template_dict['to_mod_profiles'] = [] + else: + template_dict['to_mod_profiles'] = UserProfile.objects.filter(user__is_active=False).filter(is_suspended=True).order_by('-user__date_joined')[:MODERATE_NB] + template_dict['to_mod_comments'] = Comment.objects.filter(state='pending').filter(text_version__text__in=moderator_texts).order_by('-modified')[:MODERATE_NB-len(template_dict['to_mod_profiles'])] + + activities = activities.order_by('-created') + return object_list(request, activities, + template_name = 'site/dashboard.html', + paginate_by = paginate_by, + extra_context = template_dict, + ) + + else: + if request.method == 'POST': + form = AuthenticationForm(request, request.POST) + if form.is_valid(): + user = form.get_user() + user.backend = 'django.contrib.auth.backends.ModelBackend' + cm_login(request, user) + display_message(request, _(u"You're logged in!")) + return HttpResponseRedirect(reverse('index')) + else: + form = AuthenticationForm() + + + public_texts = get_texts_with_perm(request, 'can_view_text').order_by('-modified') + + template_dict = { + 'form' : form, + 'texts' : public_texts, + } + return render_to_response('site/non_authenticated_index.html', template_dict, context_instance=RequestContext(request)) + +TEXT_PAGINATION = 10 +# security check inside view +# TODO: set global access perm +def text_list(request): + paginate_by = get_int(request.GET,'paginate',TEXT_PAGINATION) + + order_by = get_among(request.GET,'order',('title','author','modified','-title','-author','-modified'),'-modified') + + if request.method == 'POST': + action = request.POST.get('action',None) + text_keys = get_keys_from_dict(request.POST, 'check-').keys() + if action == 'delete': + for text_key in text_keys: + text = Text.objects.get(key=text_key) + if has_perm(request, 'can_delete_text', text=text): + text.delete() + else: + raise UnauthorizedException('No perm can_delete_text on comment') + display_message(request, _(u'%(nb_texts)i text(s) deleted') %{'nb_texts':len(text_keys)}) + return HttpResponseRedirect(reverse('text')) + + texts = get_texts_with_perm(request, 'can_view_text').order_by(order_by) + return object_list(request, texts, + template_name = 'site/text_list.html', + paginate_by = paginate_by, + ) + +@has_perm_on_text('can_view_text') +def text_view(request, key, adminkey=None): + + text = get_text_by_keys_or_404(key) + register_activity(request, "text_view", text=text) + + text_version = text.get_latest_version() + template_dict = { 'text' : text, 'text_version' : text_version, 'title' : text_version.title, 'content' : text_version.get_content()} + return render_to_response('site/text_view.html', template_dict, context_instance=RequestContext(request)) + +@has_perm_on_text('can_delete_text') +def text_delete(request, key): + text = Text.objects.get(key=key) + if request.method != 'POST': + raise UnauthorizedException('Unauthorized') + display_message(request, _(u'Text %(text_title)s deleted') %{'text_title':text.title}) + register_activity(request, "text_removed", text=text) + text.delete() + return HttpResponse('') # no redirect because this is called by js + +@has_perm_on_text('can_view_text') # only protected by text_view / comment filtering done in view +def text_view_comments(request, key, adminkey=None): + text = get_text_by_keys_or_404(key) + #TODO: stupid why restrict to latest ? + text_version = text.get_latest_version() + comments = get_viewable_comments(request, text_version.comment_set.filter(reply_to__isnull=True),text) + filter_datas = get_filter_datas(request, text_version, text) + + get_params = simplejson.dumps(request.GET) + + wrapped_text_version, _ , _ = spannify(text_version.get_content()) + template_dict = {'text' : text, + 'text_version' : text_version, + 'title' : text_version.title, # TODO use it ... + 'get_params' : get_params, + 'json_comments':jsonize(comments, request), + 'json_filter_datas':jsonize(filter_datas, request), + 'content' : wrapped_text_version, + 'client_date_fmt' : settings.CLIENT_DATE_FMT, + } + return render_to_response('site/text_view_comments.html', + template_dict, + context_instance=RequestContext(request)) +def client_exchange(request): + ret = None + function_name = request.POST['fun']# function called from client + user = request.user + if function_name == 'experiment' : + ret = experiment() + elif function_name == 'warn' : +# TODO: (RBE to RBA) send mail withinfos + ret = "warn test" + #print request.POST + elif request.POST: + key = request.POST['key'] + + text = Text.objects.get(key=key) ; + #TODO: stupid why restrict to latest ? + text_version = text.get_latest_version() + + if (text != None) : + if function_name in ('editComment', 'addComment', 'removeComment',) : + if function_name == 'editComment' : + ret = edit_comment(request=request, key=key, comment_key=request.POST['comment_key']) + elif function_name == 'addComment' : + ret = add_comment(request=request, key=key) + elif function_name == 'removeComment' : + ret = remove_comment(request=request, key=key, comment_key=request.POST['comment_key']) + + ret['filterData'] = get_filter_datas(request, text_version, text) + #ret['tagCloud'] = get_tagcloud(key) + if ret : + if type(ret) != HttpResponseRedirect : + ret = HttpResponse(simplejson.dumps(ret, cls=RequestComplexEncoder, request=request)) + else : + ret = HttpResponse(simplejson.dumps({})) + ret.status_code = 403 + + return ret + + +#NOTE : some arguments like : withcolor = "yes" + format = "markdown" are incompatible +#http://localhost:8000/text/text_key_1/export/pdf/1/all/1 +def text_export(request, key, format, download, whichcomments, withcolor, adminkey=None): + text, admin = get_text_and_admin(key, adminkey) + text_version = text.get_latest_version() + original_content = text_version.content + original_format = text_version.format # BD : html or markdown for now ... + + download_response = download == "1" + with_color = withcolor == "1" + + comments = [] # whichcomments=="none" + + if whichcomments == "filtered" or whichcomments == "all": + #comments = text_version.comment_set.filter(reply_to__isnull=True)# whichcomments=="all" + #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" + comments = get_viewable_comments(request, text_version.comment_set.all(), text, order_by=('start_wrapper','start_offset','end_wrapper','end_offset'))# whichcomments=="all" + + if whichcomments == "filtered" : + if request.method == 'POST' : + ll = request.POST.get('filteredIds',[]).split(",") + filteredIds = [ int(l) for l in ll if l] + comments = comments.filter(id__in=filteredIds) # security ! TODO CROSS PERMISSIONS WITH POST CONTENT + else : + comments = [] + + if len(comments) == 0 : #want to bypass html conversion in this case + return content_export2(request, original_content, text_version.title, original_format, format, False, download_response) + else : # case comments to be added + #comments = comments.order_by('start_wrapper','start_offset','end_wrapper','end_offset') + html = text_version.get_content() + wrapped_text_version, _ , _ = spannify(html) + with_markers = True + marked_content = insert_comment_markers(wrapped_text_version, comments, with_markers, with_color) + + viewable_comments = comments_thread(request, text_version, text) +# viewable_commentsnoreply = get_viewable_comments(request, commentsnoreply, text, order_by = ('start_wrapper','start_offset','end_wrapper','end_offset')) +# viewable_comments = [] +# for cc in viewable_commentsnoreply : +# viewable_comments += list_viewable_comments(request, [cc], text) + + # numerotation{ id --> numbered as a child} + extended_comments = {} + nb_children = {} + for cc in viewable_comments : + id = 0 #<-- all top comments are children of comment with id 0 + if cc.is_reply() : + id = cc.reply_to_id + + nb_children[id] = nb_children.get(id, 0) + 1 + + cc.num = "%d"%nb_children[id] + + extended_comments[cc.id] = cc + + if cc.is_reply() : + cc.num = "%s.%s"%(extended_comments[cc.reply_to_id].num, cc.num) + +# viewable_comments += list_viewable_comments(request, viewable_commentsnoreply, text) + html_comments=render_to_string('site/macros/text_comments.html',{'comments':viewable_comments }, context_instance=RequestContext(request)) + + content = "%s%s"%(marked_content, html_comments) + content_format = "html" + # impossible to satisfy because of color then no colors instead: + if with_color and format in ('markdown', 'tex') : #TODO : add S5 + with_color = False + + # decide to use pandoc or not + if with_color : + use_pandoc = False # pandoc wouldn't preserve comments scope background colors + else : + if format in ('markdown', 'tex') : + use_pandoc = True + elif format in ('pdf', 'odt') : + use_pandoc = (original_format == "markdown") + elif format in ('doc', 'html') : + use_pandoc = False + + return content_export2(request, content, text_version.title, content_format, format, use_pandoc, download_response) + +def text_print(request, key, adminkey=None): + text, admin = get_text_and_admin(key, adminkey) + + text_version = text.get_latest_version() + +# chosen default url behaviour is export all comments + bckcolor + pdf + comments = Comment.objects.filter(text_version=text_version, reply_to__isnull=True) + + with_markers = True + with_colors = True + download_requested = True + action = 'export' # client origine dialog + requested_format = 'pdf' # requested output format + + if request.method == 'POST': + # colors or not ? + with_colors = (u'yes' == request.POST.get('p_color', u'no')) + + # restrict comments to ones that should be exported / printed + p_comments = request.POST.get('p_comments') + if p_comments == "filtered" or p_comments == "none" : + filteredIds = [] # "none" case + if p_comments == "filtered" : + ll = request.POST.get('filteredIds').split(",") + filteredIds = [ int(l) for l in ll if l] + + comments = comments.filter(id__in=filteredIds) + + # print or export ? + action = request.POST.get('print_export_action') + requested_format = request.POST.get('p_method') + + comments = comments.order_by('start_wrapper','start_offset','end_wrapper','end_offset') + + download_requested = (action == 'export') or (action == 'print' and requested_format != 'html') + + ori_format = text_version.format # BD : html or markdown for now ... + src_format = ori_format # as expected by convert functions ... + src = text_version.content + + if len(comments) > 0 and (with_markers or with_colors) : + html = text_version.get_content() + wrapped_text_version, _ , _ = spannify(html) + marked_text_version = insert_comment_markers(wrapped_text_version, comments, with_markers, with_colors) + + src_format = 'html' + src = marked_text_version + html_comments=render_to_string('site/macros/text_comments.html',{'comments':comments}, context_instance=RequestContext(request)) + src += html_comments + + if download_requested : + use_pandoc = (requested_format == 'html' or requested_format == 'markdown') + return content_export(request, src, text_version.title, src_format, requested_format, use_pandoc) + else : # action == 'print' and requested_format == 'html' (no colors) + template_dict = {'text' : text, + 'text_version' : text_version, + 'title' : text_version.title, # TODO use it ... + 'comments': comments, + 'content' : marked_text_version, + 'client_date_fmt' : settings.CLIENT_DATE_FMT + } + if admin: + template_dict['adminkey'] = text.adminkey + template_dict['admin'] = True + return render_to_response('site/text_print.html', + template_dict, + context_instance=RequestContext(request)) + +@has_perm_on_text('can_view_text') +def text_view_frame(request, key, adminkey=None): + text = get_text_by_keys_or_404(key) + + text_version = text.get_latest_version() + template_dict = {'text' : text} + return render_to_response('site/text_view_frame.html', + template_dict, + context_instance=RequestContext(request)) + + +@has_perm_on_text('can_view_text') +def text_history(request, key, v1_nid=None, v2_nid=None, adminkey=False): + text = get_text_by_keys_or_404(key) + text_versions = text.get_versions() + author_colors = get_colors([t.get_name() for t in text.get_inversed_versions()]) + + if v1_nid: + v1_nid = int(v1_nid) + else: + v1_nid = text.get_versions_number() + + v1 = text.get_version(v1_nid) + + v1_id = v1.id + + v2_id = None + v2 = None + if v2_nid: + v2_nid = int(v2_nid) + v2 = text.get_version(v2_nid) + v2_id = v2.id + + versions = text.get_inversed_versions() + paired_versions = [] + colors_dict = dict(author_colors) + for index in range(len(versions)): + vv1 = versions[index] + if index + 1 < len(versions): + vv2 = versions[index + 1] + else: + vv2 = None + paired_versions.append((vv1, vv2, colors_dict.get(vv1.get_name(), '#D9D9D9'))) + + if v1_nid and not v2_nid: + content = v1.get_content() + else: + content = get_uniffied_inner_diff_table(cleanup_textarea(v1.content), cleanup_textarea(v2.content)) + + template_dict = {'paired_versions' : paired_versions, + 'text' : text, + 'v1_nid' : v1_nid, + 'v2_nid' : v2_nid, + 'v1_id' : v1_id, + 'v2_id' : v2_id, + 'version1': v1, + 'version2': v2, + 'content' : content, + 'author_colors' : author_colors, + } + return render_to_response('site/text_history.html', template_dict, context_instance=RequestContext(request)) + +# taken from trac +def _get_change_extent(str1, str2): + """ + Determines the extent of differences between two strings. Returns a tuple + containing the offset at which the changes start, and the negative offset + at which the changes end. If the two strings have neither a common prefix + nor a common suffix, (0, 0) is returned. + """ + start = 0 + limit = min(len(str1), len(str2)) + while start < limit and str1[start] == str2[start]: + start += 1 + end = -1 + limit = limit - start + while - end <= limit and str1[end] == str2[end]: + end -= 1 + return (start, end + 1) + +def diff_decorate(minus, plus): + return minus, plus + +def get_uniffied_inner_diff_table(text1, text2): + """ + Return the inner of the html table for text1 vs text2 diff + """ + gen = unified_diff(text1.split('\n'), text2.split('\n'), n=3) + index = 0 + res = ['
| Line %s | Line %s | |||
| %s | %s | |||
| - | %s | + | %s | |