Edgewall Software

Ticket #454: comment_edit.patch

File comment_edit.patch, 10.7 KB (added by David Sorber <baranovich@…>, 3 years ago)
  • trac/ticket/api.py

     
    354354    def get_permission_actions(self): 
    355355        return ['TICKET_APPEND', 'TICKET_CREATE', 'TICKET_CHGPROP', 
    356356                'TICKET_VIEW', 'TICKET_EDIT_CC', 'TICKET_EDIT_DESCRIPTION', 
     357                'TICKET_EDIT_COMMENT', 
    357358                ('TICKET_MODIFY', ['TICKET_APPEND', 'TICKET_CHGPROP']), 
    358359                ('TICKET_ADMIN', ['TICKET_CREATE', 'TICKET_MODIFY', 
    359360                                  'TICKET_VIEW', 'TICKET_EDIT_CC', 
    360                                   'TICKET_EDIT_DESCRIPTION'])] 
     361                                  'TICKET_EDIT_DESCRIPTION', 
     362                                  'TICKET_EDIT_COMMENT'])] 
    361363 
    362364    # IWikiSyntaxProvider methods 
    363365 
  • trac/ticket/web_ui.py

     
    3939from trac.timeline.api import ITimelineEventProvider 
    4040from trac.util import get_reporter_id 
    4141from trac.util.compat import any 
    42 from trac.util.datefmt import format_datetime, to_timestamp, utc 
     42from trac.util.datefmt import parse_date, pretty_timedelta, to_datetime, \ 
     43                              format_datetime, to_timestamp, utc 
    4344from trac.util.text import CRLF, shorten_line, obfuscate_email_address, \ 
    4445                           exception_to_unicode 
    4546from trac.util.presentation import separated 
     
    474475                return self._render_history(req, ticket, data, text_fields) 
    475476            elif action == 'diff': 
    476477                return self._render_diff(req, ticket, data, text_fields) 
    477         elif req.method == 'POST': # 'Preview' or 'Submit' 
     478        # Comment edit preview is a special case, similar to 'View' action 
     479        elif (req.method == 'POST' and 'preview_comment' in req.args and  
     480              'TICKET_EDIT_COMMENT' in req.perm(ticket.resource)): 
     481              data['comment_preview'] = req.args.get('edited_comment') 
     482              data['cnum_edit'] = req.args.get('comment_num') 
     483              field_changes = {}        
     484        elif req.method == 'POST': # 'Preview' or 'Submit' or Comment Edit     
     485            # Cancel comment edit 
     486            if 'cancel_comment' in req.args: 
     487                req.redirect(req.href.ticket(ticket.id)) 
     488            # Edit a ticket comment 
     489            elif ('edit_comment' in req.args and  
     490                  'TICKET_EDIT_COMMENT' in req.perm(ticket.resource)): 
     491                author = req.args.get('comment_author') 
     492                if (author == req.authname or  
     493                   'TRAC_ADMIN' in req.perm(ticket.resource)): 
     494                    comment = req.args.get('edited_comment') 
     495                    comment += "^^%s&%s^^" % (to_timestamp(datetime.now(utc)),  
     496                                              get_reporter_id(req, 'author')) 
     497                    when = req.args.get('edit_comment_when') 
     498                    when_ts = to_timestamp(parse_date(when, req.tz)) 
     499                    ticket.edit_comment(comment, when_ts) 
     500                    req.redirect(req.href.ticket(ticket.id)) 
     501 
    478502            # Do any action on the ticket? 
    479503            actions = TicketSystem(self.env).get_available_actions( 
    480504                req, ticket) 
     
    12141238        """Insert ticket data into the template `data`""" 
    12151239        replyto = req.args.get('replyto') 
    12161240        data['replyto'] = replyto 
     1241        if req.args.get('cnum_edit'): 
     1242            data['cnum_edit'] = req.args.get('cnum_edit') 
    12171243        data['version'] = ticket.resource.version 
    12181244        data['description_change'] = None 
    12191245 
     
    14421468                    current['cnum'] = autonum 
    14431469            # some common processing for fields 
    14441470            if field == 'comment': 
     1471                # find comments that have been edited and create pretty 
     1472                # "edited by" messages for them 
     1473                m = re.search('\^\^(.+?)\&(.+?)\^\^$', new) 
     1474                if m: 
     1475                    ts = int(m.group(1)) 
     1476                    user = m.group(2) 
     1477                    verb = 'Edited' 
     1478                    if re.search('^\^\^(.+?)\&(.+?)\^\^$', new): 
     1479                        verb = 'Comment removed' # Comment is empty! 
     1480                    current['comment_edited'] = ("\n\n''%s %s ago by %s.''"  
     1481                        % (verb, pretty_timedelta(to_datetime(ts)), user)) 
     1482                    new = new[:m.start(0)] 
    14451483                current['comment'] = new 
    14461484                if old: 
    14471485                    if '.' in old: # retrieve parent.child relationship 
  • trac/ticket/model.py

     
    363363 
    364364        for listener in TicketSystem(self.env).change_listeners: 
    365365            listener.ticket_deleted(self) 
     366         
     367    def edit_comment(self, cmt, ts, db=None): 
     368        db, handle_ta = self._get_db_for_write(db) 
     369        cursor = db.cursor() 
    366370 
     371        cursor.execute("UPDATE ticket_change SET newvalue=%s " 
     372                       "WHERE ticket=%s AND time=%s AND field='comment'", 
     373                       (cmt, self.id, ts)) 
     374        if handle_ta: 
     375            db.commit()   
    367376 
    368377def simplify_whitespace(name): 
    369378    """Strip spaces and remove duplicate spaces within names""" 
  • trac/ticket/templates/ticket.html

     
    4848      <a href="#comment:$cnum">$prefix$cnum</a> 
    4949    </py:def> 
    5050 
    51     <py:def function="display_change(change)"> 
     51    <py:def function="display_change(change, edit_cnum=0)"> 
    5252      <ul py:if="change.fields" class="changes"> 
    5353        <li py:for="field_name, field in change.fields.items()"> 
    5454          <strong>${field_name}</strong> 
     
    6969          </py:choose> 
    7070        </li> 
    7171      </ul> 
    72       <div py:if="'comment' in change" class="comment searchable" xml:space="preserve"> 
    73         ${wiki_to_html(context, change.comment, escape_newlines=preserve_newlines)} 
     72      <div py:if="'comment' in change and (str(change.cnum) != cnum_edit or comment_preview)" class="comment searchable ${(comment_preview and cnum_edit == str(change.cnum)) and 'ticketdraft'}" xml:space="preserve"> 
     73        <py:choose test="comment_preview and cnum_edit == str(change.cnum)"> 
     74          <py:when>${wiki_to_html(context, comment_preview, escape_newlines=preserve_newlines)}</py:when> 
     75          <py:otherwise>${wiki_to_html(context, change.comment, escape_newlines=preserve_newlines)}</py:otherwise> 
     76        </py:choose> 
     77        <py:if test="change.comment_edited and not (comment_preview and cnum_edit == str(change.cnum))"> 
     78          ${wiki_to_html(context, change.comment_edited, escape_newlines=preserve_newlines)} 
     79        </py:if> 
    7480      </div> 
    7581    </py:def> 
    7682 
     
    204210        <py:if test="ticket.exists and changes"> 
    205211          <h2>Change History</h2> 
    206212          <div id="changelog"> 
    207             <form py:for="change in changes" method="get" action="#comment" class="printableform"> 
     213            <py:for each="change in changes" class="printableform"> 
    208214              <div class="change"> 
    209215                <h3 class="change"> 
    210216                  <span class="threading" py:if="'cnum' in change" 
     
    223229                  </span> 
    224230                  Changed ${dateinfo(change.date)} ago by ${authorinfo(change.author)} 
    225231                </h3> 
    226                 <div py:if="'cnum' in change and 'TICKET_APPEND' in perm(ticket.resource)" class="inlinebuttons"> 
    227                   <input type="hidden" name="replyto" value="${change.cnum}" /> 
    228                   <input type="submit" value="${_('Reply')}" title="Reply to comment ${change.cnum}" /> 
     232                <div class="inlinebuttons"> 
     233                  <form py:if="'cnum' in change and 'TICKET_APPEND' in perm(ticket.resource)"  
     234                        method="get" action="#comment" class="changelogbuttons"> 
     235                    <input type="hidden" name="replyto" value="${change.cnum}" /> 
     236                    <input type="submit" value="${_('Reply')}" title="Reply to comment ${change.cnum}" /> 
     237                  </form> 
     238                  <form py:if="'TICKET_EDIT_COMMENT' in perm(ticket.resource) and  
     239                               (authname == change.author or 'TRAC_ADMIN' in perm(ticket.resource))"  
     240                        method="get" action="#comment:${change.cnum}" class="changelogbuttons"> 
     241                    <input type="hidden" name="cnum_edit" value="${change.cnum}" /> 
     242                    <input type="submit" value="${_('Edit')}" title="Edit comment ${change.cnum}" /> 
     243                  </form>               
    229244                </div> 
    230                 ${display_change(change)} 
     245                ${display_change(change, ('TICKET_EDIT_COMMENT' in perm(ticket.resource) and cnum_edit or 0))} 
     246                <form method="post" py:if="'TICKET_EDIT_COMMENT' in perm(ticket.resource) and str(change.cnum) == cnum_edit"> 
     247                  <p> 
     248                    <py:choose test="comment_preview"> 
     249                      <py:when><textarea name="edited_comment" class="wikitext" rows="10" cols="78">${comment_preview}</textarea></py:when> 
     250                      <py:otherwise><textarea name="edited_comment" class="wikitext" rows="10" cols="78">${change.comment}</textarea></py:otherwise> 
     251                    </py:choose> 
     252                  </p> 
     253                  <input type="hidden" name="edit_comment_when" value="${change.date.isoformat()}" /> 
     254                  <input type="hidden" name="comment_author" value="${change.author}" /> 
     255                  <input type="hidden" name="comment_num" value="${change.cnum}" /> 
     256                  <input type="submit" name="preview_comment" value="${_('Preview')}" title='Preview changes to comment ${change.cnum}' /> 
     257                  <input type="submit" name="edit_comment" value="${_('Submit changes')}" title="Submit changes to comment ${change.cnum}" /> 
     258                  <input type="submit" name="cancel_comment" value="${_('Cancel')}" title="Cancel comment edit" /> 
     259                </form> 
    231260              </div> 
    232             </form> 
     261            </py:for> 
    233262          </div> 
    234263        </py:if> 
    235264      </py:if> 
  • trac/htdocs/css/ticket.css

     
    7676} 
    7777 
    7878#changelog { border: 1px outset #996; padding: 1em } 
     79#changelog .changelogbuttons { 
     80 display: inline; 
     81} 
    7982#preview { border: 1px solid #d7d7d7; padding: 1em } 
    8083#preview h3, #changelog h3 { 
    8184 border-bottom: 1px solid #d7d7d7;