Olemis Lang avatar Olemis Lang committed 04ba37b

BH RPC : Product RPC ... [untested]

Comments (0)

Files changed (6)

trunk/bhrpc/__init__.py

+# -*- coding: utf-8 -*-
+"""
+License: BSD
+
+(c) 2013      ::: Olemis Lang (olemis+trac@gmail.com)
+"""
+
+from bhrpc.product import *
+
+__author__ = 'Olemis Lang <olemis+trac@gmail.com>'
+__license__ = 'BSD'
+
+try:
+    __version__ = __import__('pkg_resources').get_distribution('BloodhoundRPC').version
+except (ImportError, pkg_resources.DistributionNotFound):
+    pass

trunk/bhrpc/product.py

+# -*- coding: utf-8 -*-
+"""
+License: BSD
+
+(c) 2013      ::: Olemis Lang (olemis+trac@gmail.com)
+"""
+
+from trac.core import Component, implements, TracError
+from trac.resource import ResourceNotFound, Neighborhood
+from trac.util.translation import _
+
+from multiproduct.env import ProductEnvironment
+from multiproduct.model import Product
+
+from tracrpc.api import IXMLRPCHandler
+
+__author__ = 'Olemis Lang <olemis+trac@gmail.com>'
+__license__ = 'BSD'
+
+__all__ = ['ProductRPC']
+
+
+class ProductRPC(Component):
+    """ An interface to Apache(TM) Bloodhound's multi-product system. """
+
+    implements(IXMLRPCHandler)
+
+    # IXMLRPCHandler methods
+    def xmlrpc_namespace(self):
+        return 'product'
+
+    def xmlrpc_methods(self):
+        yield (None, ((list,),
+                      (list, str)), self.getAll)
+        yield (None, ((list, str),), self.get)
+        yield (None, ((None, str,),), self.delete)
+        yield (None, ((None, str),
+                      (None, str, str),
+                      (None, str, str, str),
+                      (None, str, str, str, str),), self.update)
+        if not isinstance(self.env, ProductEnvironment):
+            yield ('PRODUCT_CREATE', ((None, str, str),), self.create)
+
+    # Exported methods
+    def getAll(self, req, owner=None):
+        """
+        Get a list of all product names .
+
+        :param owner: (optional) only return products owned by user.
+        :return: a list of unicode strings (prefixes) for available
+        products, filtered considering user permissions. 
+        """
+        filter = {'owner' : owner} if owner else None
+        return [p for p in Product.select(global_env, where=filter)
+                  if 'PRODUCT_VIEW' in req.perm(Neighborhood('product',
+                                                             p.prefix))]
+
+    def get(self, req, prefix):
+        """
+        Fetch a product.
+
+        :return: [prefix, name, description, owner] if product exists else None
+        """
+        try:
+            p = Product(self.env, {'prefix' : prefix})
+        except ResourceNotFound:
+            return None 
+        else:
+            req.perm(Neighborhood('product', prefix)).require('PRODUCT_VIEW')
+            return [p._data[x] for x in ('prefix', 'name', 
+                                         'description', 'owner')]
+
+    def delete(self, req, name):
+        """ Not supported. Will always raise not implemented error """
+
+        raise TracError(_('Product removal is not allowed!'))
+
+    def create(self, req, prefix, name, description='', owner=None):
+        """ Create a new product . """
+
+        owner = owner or req.authname
+        req.perm(Neighborhood('global', None)).require('PRODUCT_CREATE')
+        product = Product(self.env)
+        product.update_field_dict({'prefix':prefix, 'name':name, 'owner':owner,
+                              'description' : description})
+        product.insert()
+
+    def update(self, req, prefix, name=None, description=None, owner=None):
+        """ Update products with given values. """
+
+        req.perm(Neighborhood('product', prefix)).require('PRODUCT_MODIFY')
+        product = Product(self.env, {'prefix' : prefix})
+        l = locals()
+        changes = dict(l[k] for k in ('name', 'description', 'owner') if l[k])
+        product.update_field_dict(changes)
+        product.update()

trunk/bhrpc/tests/__init__.py

+# -*- coding: utf-8 -*-
+"""
+License: BSD
+
+(c) 2013      ::: Olemis Lang (olemis+trac@gmail.com)
+"""
+
+__author__ = 'Olemis Lang <olemis+trac@gmail.com>'
+__license__ = 'BSD'
+
+import unittest
+
+def test_suite():
+    suite = unittest.TestSuite()
+
+    import tracrpc.tests 
+    suite.addTest(tracrpc.tests.test_suite())
+
+    import bhrpc.tests.product
+    suite.addTest(bhrpc.tests.product.test_suite())
+
+    return suite

trunk/bhrpc/tests/product.py

+# -*- coding: utf-8 -*-
+"""
+License: BSD
+
+(c) 2013      ::: Olemis Lang (olemis+trac@gmail.com)
+"""
+
+__author__ = 'Olemis Lang <olemis+trac@gmail.com>'
+
+import unittest
+
+import xmlrpclib
+import os
+import datetime
+import time
+import urllib
+
+from tracrpc.tests import rpc_testenv, TracRpcTestCase
+
+from multiproduct.api import DEFAULT_PRODUCT
+
+class RpcProductTestCase(TracRpcTestCase):
+
+    def setUp(self):
+        TracRpcTestCase.setUp(self)
+        self.anon = xmlrpclib.ServerProxy(rpc_testenv.url_anon)
+        self.user = xmlrpclib.ServerProxy(rpc_testenv.url_user)
+        self.admin = xmlrpclib.ServerProxy(rpc_testenv.url_admin)
+
+    def tearDown(self):
+        TracRpcTestCase.tearDown(self)
+
+    def test_create(self):
+        raise NotImplementedError()
+
+def test_suite():
+    test_suite = unittest.TestSuite()
+    test_suite.addTest(unittest.makeSuite(RpcProductTestCase))
+    return test_suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
 else :
   test_deps = ['twill']
 
+if sys.version_info[:2] < (2, 7):
+    test_deps.append('unittest2')
+
 setup(
     name='BloodhoundRPC',
     version='1.1.2',
     author='Alec Thomas',
     author_email='alec@swapoff.org',
     maintainer='Odd Simon Simonsen & Olemis Lang',
-    maintainer_email='simon-code@bvnetwork.no , olemis@gmail.com',
+    maintainer_email='simon-code@bvnetwork.no , olemis+trac@gmail.com',
     #FIXME: Update project URL
     url='http://trac-hacks.org/wiki/XmlRpcPlugin',
     description='RPC interface to Apache(TM) Bloodhound',
     zip_safe=True,
-    test_suite = 'tracrpc.tests.test_suite',
+    test_suite = 'bhrpc.tests.test_suite',
     install_requires=['Trac>=1.0', 'BloodhoundMultiProduct>=0.6.0dev'],
     tests_require = test_deps,
     packages=find_packages(exclude=['*.tests']),

trunk/tracrpc/__init__.py

 __license__ = 'BSD'
 
 try:
-    __version__ = __import__('pkg_resources').get_distribution('TracXMLRPC').version
+    __version__ = __import__('pkg_resources').get_distribution('BloodhoundRPC').version
 except (ImportError, pkg_resources.DistributionNotFound):
     pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.