web/hosts/issues/detectors sanitycheck.py,NONE,1.1

Tobias Hunger <[email protected]> Mon, 25 Aug 2003 14:04:35 -0500
Newsgroups gmane.comp.video.fresco.cvs
Message-ID <[email protected]>
Update of /cvs/fresco/web/hosts/issues/detectors
In directory purcel:/tmp/cvs-serv22047

Added Files:
	sanitycheck.py 
Log Message:
Forgot to add this file:-(


--- NEW FILE: sanitycheck.py ---
from roundup import roundupdb, hyperdb

#
# PERMISSIONS:
#

def check_bug_permissions(db, cl, newvalues):
    ''' Check wether the user tried to change something he shouldn't have.
    '''
    if not db.security.hasPermission('Process', db.getuid(), cl.classname):
        if newvalues.has_key('title') or newvalues.has_key('assignedto') or newvalues.has_key('bug_type') or newvalues.has_key('severity') or newvalues.has_key('topics') or newvalues.has_key('status') or newvalues.has_key('resolution') or newvalues.has_key('dependson') or newvalues.has_key('superseeder'):
            raise ValueError, "You don't have permission to edit one of the selected fields."

def check_task_permissions(db, cl, newvalues):
    ''' Check wether the user tried to change something he shouldn't have.
    '''
    # Only developers may edit tasks and they may edit any field.
    return

def check_milestone_permissions(db, cl, newvalues):
    ''' Check wether the user tried to change something he shouldn't have.
    '''

    if not db.security.hasPermission('Process', db.getuid(), cl.classname):
        if newvalues.has_key('bugs') or newvalues.has_key('tasks') or newvalues.has_key('status') or newvalues.has_key('release_date'):
	    raise ValueError, "You don't have permission to edit one of the selected fields."

#
# SET:
#

def set_bug(db, cl, nodeid, newvalues):
    ''' Ren sanitychecks on bugs
    '''
    check_bug_permissions(db, cl, newvalues)    

    # Check resolution and related values:
    if newvalues.has_key('resolution'):
        if not newvalues.has_key('status'):
            newvalues['status'] = db.status.lookup('closed')
        elif not newvalues['status'] == db.status.lookup('closed'):
           raise ValueError, "Status has to be closed when providing a resolution."
	
        if newvalues['resolution'] == db.resolution.lookup('duplicate'):
            if not newvalues.has_key('superseeder'):
                raise ValueError, "Please provide a superseeder when setting resolution to duplicate"

        if newvalues['resolution'] == db.resolution.lookup('invalid'):
	    if not newvalues.has_key('messages'):
	        raise ValueError, "Please provide a message explaining why this bug is invalid."

    # Check status and related values: 
    if newvalues.has_key('status'):
        if newvalues['status'] == db.status.lookup('open') and not newvalues.has_key('assignedto'):
	    raise ValueError, "You need to provide an assignee when setting the status to open or closed."
       
        if newvalues['status'] == db.status.lookup('closed'):
	    # Do we have a assignedto?
            if not newvalues.has_key('assignedto') and not cl.get(nodeid, 'assignedto'):
	        raise ValueError, "You need to provide an assignee when setting the status to open or closed." 

	    # Do we have a resolution?
	    if not newvalues.has_key('resolution'):
	        raise ValueError, "You must provide a resolution when setting status to closed."

    # Check assignee and related values:
    if newvalues.has_key('assignedto'):
        if not db.security.hasPermission('Process', newvalues['assignedto'], cl.classname):
	    raise ValueError, "The assignee is not a developer."
	    
        if newvalues.has_key('status'):
	    if newvalues['status'] != db.status.lookup('open') and newvalues['status'] != db.status.lookup('closed'):
	        raise ValueError, "When assigning someone to a bug the status must be open or closed."
        else:
	    newvalues['status'] = db.status.lookup('open')


def set_task(db, cl, nodeid, newvalues):
    ''' Ren sanitychecks on tasks
    '''
    check_task_permissions(db, cl, newvalues)
    
    # Check result and related values:
    if newvalues.has_key('result'):
        if not newvalues.has_key('status'):
            newvalues['status'] = db.status.lookup('closed')
        elif not newvalues['status'] == db.status.lookup('closed'):
            raise ValueError, "Status has to be closed when providing a result."

    # Check status and related values: 
    if newvalues.has_key('status'):
        if newvalues['status'] == db.status.lookup('open'):
            # Do we have a assignedto?
            if not newvalues.has_key('assignedto'):
                raise ValueError, "You need to provide an assignee when setting the status to open or closed."                                                     
        if newvalues['status'] == db.status.lookup('closed'):
            # Do we have a assignedto?
            if not newvalues.has_key('assignedto'):
                raise ValueError, "You need to provide an assignee when setting the status to open or closed." 
            # Do we have a resolution?
            if not newvalues.has_key('result'):
                raise ValueError, "You must provide a result when setting status to closed."

def set_milestone(db, cl, nodeid, newvalues):
    ''' Run sanitychecks on milestones
    '''
    check_milestone_permissions(db, cl, newvalues)


#
# CRREATE:
#

def init_bug(db, cl, nodeid, newvalues):
    ''' initialize the unselected attributes of a new bug
    '''
    check_bug_permissions(db, cl, newvalues)
    
    if not newvalues.has_key('status'):
        newvalues['status'] = db.status.lookup('new')
    if not newvalues.has_key('severity'):
        newvalues['severity'] = db.severity.lookup('normal')
    if not newvalues.has_key('priority'):
        newvalues['priority'] = db.priority.lookup('normal')

def init_task(db, cl, nodeid, newvalues):
    ''' initialize the unselected attributes of a new task
    '''
    check_task_permissions(db, cl, newvalues)

    if not newvalues.has_key('status'):
        newvalues['status'] = db.status.lookup('new')
    if not newvalues.has_key('priority'):
        newvalues['priority'] = db.priority.lookup('normal')

def init_milestone(db, cl, nodeid, newvalues):
    ''' initialize the unselected attributes of a new milestone
    '''
    check_milestone_permissions(db, cl, newvalues)

    if not newvalues.has_key('status'):
        newvalues['status'] = db.status.lookup('new')

#
# SETUP OF THE DETECTOR:
#

def init(db):
    # fire before changes are made
    db.bug.audit('create', init_bug)
    db.task.audit('create', init_task)
    db.milestone.audit('create', init_milestone)
    db.bug.audit('set', set_bug)
    db.task.audit('set', set_task)
    db.milestone.audit('set', set_milestone)