Edgewall Software
Modify

Opened 2 years ago

Last modified 18 months ago

#12414 new enhancement

Improve API for getting comment number

Reported by: anonymous Owned by:
Priority: normal Milestone:
Component: ticket system Version:
Severity: normal Keywords:
Cc:
Release Notes:
API Changes:

Description (last modified by Jun Omae)

E.g. in ITicketManipulator.validate_ticket it is surprisingly difficult to get the comment number of the comment to be validated.

ticket.get_comment_number(ticket['changetime']) often gives the previous comment number, but fails e.g. when the a comment was edited. (th:#12714)

Am I missing something or could the API be improved somehow? Thanks.

Attachments (0)

Change History (6)

comment:1 Changed 2 years ago by Jun Omae

Component: generalticket system
Description: modified (diff)

You could use TicketModule.grouped_changelog_entries().

    def _get_ticket_cnum(self, ticket, modtime):
        for change in TicketModule(self.env) \
                      .grouped_changelog_entries(ticket, when=modtime):
            return change['cnum']

See tags/trac-1.0.10/trac/ticket/notification.py@:181-182,191#L165.

comment:2 Changed 18 months ago by anonymous

Unfortunately that also seems to fail when the last ticket modification was a comment edit. Or what should modtime be?

comment:3 Changed 18 months ago by anonymous

Could something like this be added to Ticket?

    def get_ticket_max_comment_number(self):
        rows = self.env.db_query("""
            SELECT CAST(oldvalue AS INTEGER) as cnum
            FROM ticket_change
            WHERE ticket=%s AND field='comment'
            ORDER BY cnum DESC
            LIMIT 1
            """, (self.id,))
        return rows[0][0] if rows else 0

comment:4 in reply to:  2 ; Changed 18 months ago by Jun Omae

Replying to anonymous:

Unfortunately that also seems to fail when the last ticket modification was a comment edit. Or what should modtime be?

The modtime is ticket_change.time column of the comment, not ticket['changetime'].

See when arguments of TicketModule.grouped_changelog_entries and Ticket.get_changelog.

comment:5 Changed 18 months ago by Jun Omae

Your logic wouldn't work if the ticket is concurrently modified by several users. I think you should search comment matched with author by TicketModule.grouped_changelog_entries. I don't think it is needed to add that.

comment:6 in reply to:  4 Changed 18 months ago by anonymous

The modtime is ticket_change.time column of the comment, not ticket['changetime'].

OK, but how do I get ticket_change.time in validate_ticket? If that was possible I could also simply use ticket.get_comment_number(ticket_change.time), no?

That's basically my problem: I can't find the latest comment_number because I can't find the latest ticket_change.time.

Your logic wouldn't work if the ticket is concurrently modified by several users.

The logic inside get_ticket_max_comment_number? Or the idea of "previous-comment-number + 1 = validation-comment-number"?

I think you should search comment matched with author by TicketModule.grouped_changelog_entries.

I'm afraid I don't understand. The author of the new comment?

def validate_ticket(self, req, ticket):
    author = get_reporter_id(req, 'author')    
    comment = req.args.get('comment')

    # Doesn't work after comment edits:
    comment_number_wrong1 = (ticket.get_comment_number(ticket['changetime']) or 0) + 1
    comment_number_wrong2 = self._get_ticket_cnum(ticket, ticket['changetime']) + 1

    # Seems to me this would work (except for concurrent modifications):
    comment_number_ok = ticket.get_ticket_max_comment_number() + 1

    # Your proposal?
    comment_number_by_author = self._search_comment_matched_with_author(ticket, author)

def _get_ticket_cnum(self, ticket, modtime):
    for change in TicketModule(self.env) \
                  .grouped_changelog_entries(ticket, when=modtime):
        return change['cnum']

def _search_comment_matched_with_author(self, ticket, author):
    for change in TicketModule(self.env) \
                  .grouped_changelog_entries(ticket):
        # ...?
        if change['author'] == author:
            return change['cnum']

Thanks you for your time.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The ticket will remain with no owner.
The ticket will be disowned.
as The resolution will be set.
The owner will be changed from (none) to anonymous.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.