Skip to content
Browse files

Hook up Bango API to register devs (bug 810283)

  • Loading branch information...
1 parent fac25ee commit 261339cf9df0e6daff08c2468d404b817cc63fad @kumar303 kumar303 committed Nov 13, 2012
View
0 lib/bango/__init__.py
No changes.
View
22 lib/bango/forms.py
@@ -0,0 +1,22 @@
+from django import forms
+
+
+class PackageForm(forms.Form):
+ adminEmailAddress = forms.CharField()
+ supportEmailAddress = forms.CharField()
+ financeEmailAddress = forms.CharField()
+ paypalEmailAddress = forms.CharField()
+ vendorName = forms.CharField()
+ companyName = forms.CharField()
+ address1 = forms.CharField()
+ address2 = forms.CharField(required=False)
+ addressCity = forms.CharField()
+ addressState = forms.CharField()
+ addressZipCode = forms.CharField()
+ addressPhone = forms.CharField()
+ addressFax = forms.CharField(required=False)
+ vatNumber = forms.CharField(required=False)
+ countryIso = forms.CharField()
+ currencyIso = forms.CharField()
+ homePageURL = forms.CharField(required=False)
+ eventNotificationURL = forms.CharField(required=False)
View
75 lib/bango/resources.py
@@ -0,0 +1,75 @@
+from django.conf import settings
+from django.core.urlresolvers import reverse
+
+from suds import client as sudsclient
+
+from lib.sellers.models import Seller, SellerBango
+from solitude.base import Cached, Resource as BaseResource
+
+from .forms import PackageForm
+
+
+class Resource(BaseResource):
+
+ class Meta(BaseResource.Meta):
+ object_class = Cached
+
+ def get_resource_uri(self, bundle):
+ return reverse('api_dispatch_detail',
+ kwargs={'api_name': 'bango',
+ 'resource_name': self._meta.resource_name,
+ 'pk': bundle.obj.pk})
+
+ def populate(self, obj, data):
+ # TODO(Kumar) refactor this to use BangoProxy (bug 812661).
+ obj.username = settings.BANGO_USERNAME
+ obj.password = settings.BANGO_PASSWORD
+ for k, v in data.iteritems():
+ setattr(obj, k, v)
+
+ def client(self):
+ return sudsclient.Client(self.wsdl) # Cached internally by suds.
+
+
+class ExporterResource(Resource):
+ wsdl = settings.BANGO_EXPORTER_WSDL
+
+
+class PackageResource(ExporterResource):
+
+ class Meta(Resource.Meta):
+ resource_name = 'package'
+ list_allowed_methods = ['post']
+
+ def obj_create(self, bundle, request, **kw):
+ form = PackageForm(bundle.data)
+ if not form.is_valid():
+ raise self.form_errors(form)
+
+ client = self.client()
+ package = client.factory.create('CreatePackageRequest')
+ self.populate(package, bundle.data)
+ resp = client.service.CreatePackage(package)
+
+ data = {'seller_pk': None,
+ 'seller_bango_pk': None,
+ 'response_code': resp.responseCode,
+ 'response_message': resp.responseMessage}
+
+ if data['response_code'] == 'OK':
+ seller = Seller.objects.create(uuid='bango:%s' % resp.packageId)
+ seller_bango = SellerBango.objects.create(
+ seller=seller,
+ package_id=resp.packageId,
+ admin_person_id=resp.adminPersonId,
+ support_person_id=resp.supportPersonId,
+ finance_person_id=resp.financePersonId)
+ data['seller_pk'] = seller.pk
+ data['seller_bango_pk'] = seller_bango.pk
+
+ bundle.data = data
+ return bundle
+
+
+class BillingConfigResource(Resource):
+ wdsl = settings.BANGO_BILLING_CONFIG_WSDL
View
0 lib/bango/tests/__init__.py
No changes.
View
83 lib/bango/tests/test_api.py
@@ -0,0 +1,83 @@
+import json
+
+import mock
+from mock import Mock
+from nose.tools import eq_
+
+from lib.sellers.models import Seller, SellerBango
+from solitude.base import APITest
+
+
+class TestPackageResource(APITest):
+
+ def setUp(self):
+ super(APITest, self).setUp()
+ self.api_name = 'bango'
+ self.list_url = self.get_list_url('package')
+
+ p = mock.patch('lib.bango.resources.sudsclient')
+ self.sudscli = p.start()
+ self.addCleanup(p.stop)
+
+ def data_from_post(self, **kw):
+ res = self.client.post(self.list_url, **kw)
+ return json.loads(res.content)
+
+ def mock_service(self):
+ client = Mock()
+ self.sudscli.Client.return_value = client
+ return client.service
+
+ def mock_response(self):
+ bango = Mock()
+ bango.responseCode = 'OK'
+ bango.responseMessage = ''
+ bango.packageId = 1
+ bango.adminPersonId = 2
+ bango.supportPersonId = 3
+ bango.financePersonId = 4
+ self.mock_service().CreatePackage.return_value = bango
+ return bango
+
+ def good_data(self):
+ return {'adminEmailAddress': 'admin@place.com',
+ 'supportEmailAddress': 'support@place.com',
+ 'financeEmailAddress': 'finance@place.com',
+ 'paypalEmailAddress': 'paypal@place.com',
+ 'vendorName': 'Some Company',
+ 'companyName': 'Some Company, LLC',
+ 'address1': '111 Somewhere',
+ 'addressCity': 'Pleasantville',
+ 'addressState': 'CA',
+ 'addressZipCode': '11111',
+ 'addressPhone': '4445551111',
+ 'countryIso': 'USA',
+ 'currencyIso': 'USD'}
+
+ def test_list_allowed(self):
+ self.allowed_verbs(self.list_url, ['post'])
+
+ def test_create(self):
+ bango = self.mock_response()
+ data = self.data_from_post(data=self.good_data())
+ seller = Seller.objects.get()
+ eq_(data['seller_pk'], seller.pk)
+ eq_(data['seller_bango_pk'], seller.bango.pk)
+
+ def test_missing_field(self):
+ bango = self.mock_response()
+ # Submit missing fields:
+ data = {'adminEmailAddress': 'admin@place.com',
+ 'supportEmailAddress': 'support@place.com'}
+ data = self.data_from_post(data=data)
+ eq_(data['companyName'], ['This field is required.'])
+
+ def test_bango_fail(self):
+ bango = self.mock_response()
+ bango.responseCode = 'FAIL'
+ bango.responseMessage = 'something happened'
+ data = self.data_from_post(data=self.good_data())
+ eq_(Seller.objects.all().count(), 0)
+ eq_(SellerBango.objects.all().count(), 0)
+ eq_(data['response_code'], bango.responseCode)
+ eq_(data['response_message'], bango.responseMessage)
View
6 lib/bango/urls.py
@@ -0,0 +1,6 @@
+from tastypie.api import Api
+
+from .resources import PackageResource
+
+bango = Api(api_name='bango')
+bango.register(PackageResource())
View
11 lib/sellers/models.py
@@ -61,3 +61,14 @@ class SellerProduct(Model):
class Meta(Model.Meta):
db_table = 'seller_product'
+
+
+class SellerBango(Model):
+ seller = models.OneToOneField(Seller, related_name='bango')
+ package_id = models.IntegerField(unique=True)
+ admin_person_id = models.IntegerField()
+ support_person_id = models.IntegerField()
+ finance_person_id = models.IntegerField()
+
+ class Meta(Model.Meta):
+ db_table = 'seller_bango'
View
13 migrations/14-add-seller-bango.sql
@@ -0,0 +1,13 @@
+CREATE TABLE `seller_bango` (
+ `id` int(11) unsigned AUTO_INCREMENT NOT NULL PRIMARY KEY,
+ `created` datetime NOT NULL,
+ `modified` datetime NOT NULL,
+ `seller_id` int(11) unsigned NOT NULL UNIQUE,
+ `package_id` int(11) NOT NULL UNIQUE,
+ `admin_person_id` int(11) NOT NULL,
+ `support_person_id` int(11) NOT NULL,
+ `finance_person_id` int(11) NOT NULL
+) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+
+ALTER TABLE `seller_bango` ADD CONSTRAINT `seller_id_refs_id_c6c7badb`
+ FOREIGN KEY (`seller_id`) REFERENCES `seller` (`id`);
View
1 requirements/prod.txt
@@ -21,6 +21,7 @@ schematic==0.2
simplejson==2.6.2
six==1.2.0
statsd==1.0.0
+suds==0.4
tower==0.3.4
# not on pypi
View
3 settings_test.py
@@ -29,3 +29,6 @@
HMAC_KEYS = {'2011-01-01': 'cheesecake'}
from django_sha2 import get_password_hashers
PASSWORD_HASHERS = get_password_hashers(BASE_PASSWORD_HASHERS, HMAC_KEYS)
+
+BANGO_EXPORTER_WSDL = 'https://nowhere/mozillaexporter/?WSDL'
+BANGO_BILLING_CONFIG_WSDL = 'https://nowhere/billingconfiguration/?WSDL'
View
6 solitude/settings/base.py
@@ -124,3 +124,9 @@
# A mapping of the keys and secrets that will be used to encode the JWT
# for any server talking to this server.
CLIENT_JWT_KEYS = {}
+
+# Bango API settings.
+BANGO_USERNAME = 'Mozilla'
+BANGO_PASSWORD = ''
+BANGO_EXPORTER_WSDL = 'https://webservices.bango.com/mozillaexporter/?WSDL'
+BANGO_BILLING_CONFIG_WSDL = 'https://webservices.bango.com/billingconfiguration/?WSDL'
View
2 solitude/urls.py
@@ -3,6 +3,7 @@
from tastypie.api import Api
+from lib.bango.urls import bango
from lib.bluevia.urls import bluevia
from lib.buyers.resources import BuyerResource, BuyerPaypalResource
from lib.buyers.views import check_pin
@@ -39,6 +40,7 @@
url(r'^', include(api.urls)),
url(r'^', include(paypal.urls)),
url(r'^', include(bluevia.urls)),
+ url(r'^', include(bango.urls)),
url(r'^', include(service.urls)),
url(r'^buyer/check_pin', check_pin, name='check-pin'),
url(r'^$', 'solitude.views.home', name='home'),

0 comments on commit 261339c

Please sign in to comment.
Something went wrong with that request. Please try again.