[Zope3-checkins] CVS: Products3/bugtracker - xmlrpc.py:1.1 tracker.py:1.4

Stephan Richter srichter@cosmos.phy.tufts.edu
Mon, 28 Jul 2003 06:21:42 -0400


Update of /cvs-repository/Products3/bugtracker
In directory cvs.zope.org:/tmp/cvs-serv8367

Modified Files:
	tracker.py 
Added Files:
	xmlrpc.py 
Log Message:
- Since it takes a lot of code to setup bug tracker tests, I moved that
  initialization to placelessetup and corrected the tests for it.

- Implemented XML-RPC methods for possible mail-in feature. Wouldn't it be 
  neat if you could send an E-mail as follows:

Title: Bug 231 - Add Comment

This is the comment...


=== Added File Products3/bugtracker/xmlrpc.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""XML-RPC methods for the Bug Tracker and Bugs

XML-RPC might be the best method to support mail-input. 

$Id: xmlrpc.py,v 1.1 2003/07/28 10:21:06 srichter Exp $
"""
import base64
from zope.app.content.file import File
from zope.app.content.image import Image
from zope.app.event import publish
from zope.app.event.objectevent import ObjectCreatedEvent, ObjectModifiedEvent
from zope.component import getAdapter
from zope.publisher.xmlrpc import XMLRPCView
from zope.schema.vocabulary import getVocabularyRegistry

from zopeproducts.bugtracker.bug import Bug
from zopeproducts.bugtracker.comment import Comment
from zopeproducts.bugtracker.interfaces import IComment, IBugDependencies


class UnknownEncoding(Exception):
    """We specify encodings for the attachment data, since it is expected that
    they are often binary data. This exception is raised, if none of the
    available encodings was chosen."""
    

class BugTrackerMethods(XMLRPCView):
    """XML-RPC methods for the Bug Tracker object."""

    def getBugNames(self):
        """Get a list of all bugs."""
        return list(self.context.keys())
  
    def addBug(self, title, description, type=u'bug', status=u'new',
               priority=u'normal', release=u'None', owners=None,
               dependencies=None):
        """Add a message. Returns the id of the bug."""
        bug = Bug()
        bug.title = title
        bug.description = description
        bug.type = type
        bug.status = status
        bug.priority = priority
        bug.release = release
        if owners is not None:
            registry = getVocabularyRegistry()
            vocab = registry.get(self.context, 'Users')
            owner_ids = []
            for term in vocab:
                if term.principal['login'] in owners:
                    owner_ids.append(term.value)
            bug.owners = owner_ids
        if dependencies is not None:
            bug.dependencies = dependencies
        publish(self.context, ObjectCreatedEvent(bug))
        return self.context.setObject('', bug)
  
    def deleteBug(self, name):
        """Delete a bug. Return True, if successful."""
        self.context.__delitem__(name)
        return True 


class BugMethods(XMLRPCView):

    def getProperties(self):
        registry = getVocabularyRegistry()
        vocab = registry.get(self.context, 'Users')
        owners = map(lambda o: vocab.getTerm(o).principal['login'],
                     self.context.owners)        
        deps = getAdapter(self.context, IBugDependencies)
        return {'title' : self.context.title,
                'description' : self.context.description,
                'type' : self.context.type,
                'status' : self.context.status,
                'priority' : self.context.priority,
                'release' : self.context.release,
                'owners' : owners,
                'dependencies' : deps.dependencies
                }

    def setProperties(self, title=None, description=None, type=None,
                      status=None, priority=None, release=None,
                      owners=None, dependencies=None):
        """Set the properties of the bug."""
        bug = self.context
        if title is not None:
            bug.title = title
        if description is not None:
            bug.description = description
        if type is not None:
            bug.type = type
        if status is not None:
            bug.status = status
        if priority is not None:
            bug.priority = priority
        if release is not None:
            bug.release = release
        if owners is not None:
            registry = getVocabularyRegistry()
            vocab = registry.get(self.context, 'Users')
            owner_ids = []
            for term in vocab:
                if term.principal['login'] in owners:
                    owner_ids.append(term.value)
            bug.owners = owner_ids
        if dependencies is not None:
            deps = getAdapter(bug, IBugDependencies)
            deps.dependencies = dependencies
        publish(self.context, ObjectModifiedEvent(bug))
        return True

    def getCommentNames(self):
        """Get the names (ids) of the comments for this bug."""
        names = []
        for name, obj in self.context.items():
            if IComment.isImplementedBy(obj):
                names.append(name)
        return names

    def addComment(self, body):
        """Add a comment to the bug."""
        comment = Comment()
        comment.body = body
        names = filter(lambda n: n.startswith('comment'), self.context.keys())
        int_names = map(lambda n: int(n[7:]), names)
        name = 'comment1'
        if int_names:
            name = 'comment' + str(max(int_names)+1)
        publish(self.context, ObjectCreatedEvent(comment))
        return self.context.setObject(name, comment)

    def deleteComment(self, name):
        """Delete a Comment. Return True, if successful."""
        self.context.__delitem__(name)
        return True 

    def getAttachmentNames(self):
        """Get the names (ids) of the attachments for this bug."""
        namess = []
        for name, obj in self.context.items():
            if not IComment.isImplementedBy(obj):
                names.append(name)
        return names

    def addAttachment(self, name, data, type="File", encoding="base64"):
        """Add an attachment to the bug."""
        if type == 'Image':
            attach = Image()
        else:
            attach = File()
        if encoding == 'base64':
            attach.data = base64.decodestring(data)
        else:
            raise UnknownEncoding, 'The encoding is not known: %s' %encoding 
        return self.context.setObject(name, attach)

    def deleteAttachment(self, name):
        """Delete an Attachment. Return True, if successful."""
        self.context.__delitem__(name)
        return True 


class CommentMethods(XMLRPCView):
    """XML-RPC Methods for a Bug Comment object."""

    def getBody(self):
        """Get the contents/body of the comment."""
        return self.context.body

    def setBody(self, body):
        """Give the comment new contents.""" 
        self.context.body = body
        return True


class AttachmentMethods(XMLRPCView):
    """XML-RPC Methods for Bug Attachments."""

    def getData(self, encoding='base64'):
        """Return the data of the attachment in the specified encoding."""
        if encoding == 'base64':
            return base64.encodestring(self.context.data)
        else:
            raise UnknownEncoding, 'The encoding is not known: %s' %encoding 

    def setData(self, data, encoding='base64'):
        """Set the data of the attachment converting from the specified
        encoding."""
        if encoding == 'base64':
            self.context.data = base64.decodestring(data)
        else:
            raise UnknownEncoding, 'The encoding is not known: %s' %encoding 


=== Products3/bugtracker/tracker.py 1.3 => 1.4 ===
--- Products3/bugtracker/tracker.py:1.3	Sat Jul 26 16:13:30 2003
+++ Products3/bugtracker/tracker.py	Mon Jul 28 06:21:06 2003
@@ -44,6 +44,14 @@
         items.sort(compare)
         return items
 
+    def setObject(self, name, object):
+        if not self.keys():
+            name = '1'
+        else:
+            int_names = map(lambda name: int(name), self.keys())
+            name = str(max(int_names)+1)
+        return super(BugTracker, self).setObject(name, object)
+
 
 def compare(obj1, obj2):
     try: