tags:

views:

162

answers:

1

OK - Python newbie here - I assume I am doing something really stupid, could you please tell me what it is so we can all get on with our lives?

I get the error NameError: global name 'has_no_changeset' is not defined in the line 55 (where I try calling the function has_no_changeset).

from genshi.builder import tag

from trac.core import implements,Component
from trac.ticket.api import ITicketManipulator
from trac.ticket.default_workflow import ConfigurableTicketWorkflow
from trac.perm import IPermissionRequestor
from trac.config import Option, ListOption
import re

revision = "$Rev$"
url = "$URL$"

class CloseActionController(Component):
    """Support for close checking.

    If a ticket is closed, it is NOT allowed if ALL the following conditions apply: 
     a) ticket is 'bug' ticket
     b) resolution status is 'fixed'
     c) none of the ticket's changes include a comment containing a changeset, i.e. regex "\[\d+\]"
     d) the ticket does not have the keyword 'web'
    """

    implements(ITicketManipulator)

    # ITicketManipulator methods

    def prepare_ticket(req, ticket, fields, actions):
        """Not currently called, but should be provided for future
        compatibility."""
        return


    def has_no_changeset(ticket):
        db = self.env.get_db_cnx()
        cursor = db.cursor()

        cursor.execute("SELECT newvalue FROM ticket_change WHERE ticket=%s AND field='comment'", (str(ticket.id).encode('ascii','replace'),))

        for newvalue, in cursor:
            if re.search("\[\d{5,}\]", newvalue):
                return False

        return True

    def validate_ticket(me, req, ticket):
        """Validate a ticket after it's been populated from user input.

        Must return a list of `(field, message)` tuples, one for each problem
        detected. `field` can be `None` to indicate an overall problem with the

        ticket. Therefore, a return value of `[]` means everything is OK."""

        if ticket['type'] == 'bug' and ticket['resolution'] == 'fixed':
          if ticket['keywords'] == None or ticket['keywords'].find('web') == -1:
            if has_no_changeset(ticket):
              return [(None, 'You can only close a bug ticket as "fixed" if you refer to a changeset somewhere within the ticket, e.g. with [12345]!')]

        return[]
+2  A: 

You need to explicitly specify self (or in your case, me) when referring to a method of the current class:

if me.has_no_changeset(ticket):

You're using me instead of self - that's legal but strongly discouraged. The first parameter of member functions should be called self:

def validate_ticket(self, req, ticket):
    # [...]
    if self.has_no_changeset(ticket):
RichieHindle
thanks, that was the problem...
Epaga