← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:stormify-commercialsubscription into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:stormify-commercialsubscription into launchpad:master.

Commit message:
Convert CommercialSubscription to Storm

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/387108
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:stormify-commercialsubscription into launchpad:master.
diff --git a/lib/lp/registry/model/commercialsubscription.py b/lib/lp/registry/model/commercialsubscription.py
index 7e5ff65..b9b0b7d 100644
--- a/lib/lp/registry/model/commercialsubscription.py
+++ b/lib/lp/registry/model/commercialsubscription.py
@@ -9,9 +9,12 @@ __all__ = ['CommercialSubscription']
 import datetime
 
 import pytz
-from sqlobject import (
-    ForeignKey,
-    StringCol,
+from storm.locals import (
+    DateTime,
+    Int,
+    Reference,
+    Store,
+    Unicode,
     )
 from zope.interface import implementer
 
@@ -21,27 +24,46 @@ from lp.registry.interfaces.commercialsubscription import (
     )
 from lp.registry.interfaces.person import validate_public_person
 from lp.services.database.constants import UTC_NOW
-from lp.services.database.datetimecol import UtcDateTimeCol
-from lp.services.database.sqlbase import SQLBase
+from lp.services.database.stormbase import StormBase
 
 
 @implementer(ICommercialSubscription)
-class CommercialSubscription(SQLBase):
-
-    product = ForeignKey(
-        dbName='product', foreignKey='Product', notNull=True)
-    date_created = UtcDateTimeCol(notNull=True, default=UTC_NOW)
-    date_last_modified = UtcDateTimeCol(notNull=True, default=UTC_NOW)
-    date_starts = UtcDateTimeCol(notNull=True, default=UTC_NOW)
-    date_expires = UtcDateTimeCol(notNull=True, default=UTC_NOW)
-    registrant = ForeignKey(
-        dbName='registrant', foreignKey='Person', default=None,
-        storm_validator=validate_public_person)
-    purchaser = ForeignKey(
-        dbName='purchaser', foreignKey='Person', default=None,
-        storm_validator=validate_public_person)
-    sales_system_id = StringCol(notNull=True)
-    whiteboard = StringCol(default=None)
+class CommercialSubscription(StormBase):
+
+    __storm_table__ = 'CommercialSubscription'
+
+    id = Int(primary=True)
+
+    product_id = Int(name='product', allow_none=False)
+    product = Reference(product_id, 'Product.id')
+
+    date_created = DateTime(tzinfo=pytz.UTC, allow_none=False, default=UTC_NOW)
+    date_last_modified = DateTime(
+        tzinfo=pytz.UTC, allow_none=False, default=UTC_NOW)
+    date_starts = DateTime(tzinfo=pytz.UTC, allow_none=False, default=UTC_NOW)
+    date_expires = DateTime(tzinfo=pytz.UTC, allow_none=False, default=UTC_NOW)
+
+    registrant_id = Int(
+        name='registrant', allow_none=False, validator=validate_public_person)
+    registrant = Reference(registrant_id, 'Person.id')
+
+    purchaser_id = Int(
+        name='purchaser', allow_none=False, validator=validate_public_person)
+    purchaser = Reference(purchaser_id, 'Person.id')
+
+    sales_system_id = Unicode(allow_none=False)
+    whiteboard = Unicode(default=None)
+
+    def __init__(self, product, date_starts, date_expires, registrant,
+                 purchaser, sales_system_id, whiteboard):
+        super(CommercialSubscription, self).__init__()
+        self.product = product
+        self.date_starts = date_starts
+        self.date_expires = date_expires
+        self.registrant = registrant
+        self.purchaser = purchaser
+        self.sales_system_id = sales_system_id
+        self.whiteboard = whiteboard
 
     @property
     def is_active(self):
@@ -54,4 +76,4 @@ class CommercialSubscription(SQLBase):
         if self.is_active:
             raise CannotDeleteCommercialSubscription(
                 "This CommercialSubscription is still active.")
-        self.destroySelf()
+        Store.of(self).remove(self)
diff --git a/lib/lp/registry/model/person.py b/lib/lp/registry/model/person.py
index ad5885b..8009cfd 100644
--- a/lib/lp/registry/model/person.py
+++ b/lib/lp/registry/model/person.py
@@ -1153,7 +1153,7 @@ class Person(
                 Product, TeamParticipation.teamID == Product._ownerID),
             Join(
                 CommercialSubscription,
-                CommercialSubscription.productID == Product.id)
+                CommercialSubscription.product_id == Product.id)
             ).find(
                 Person,
                 CommercialSubscription.date_expires > datetime.now(
diff --git a/lib/lp/registry/model/product.py b/lib/lp/registry/model/product.py
index 8cdd446..3a0acf0 100644
--- a/lib/lp/registry/model/product.py
+++ b/lib/lp/registry/model/product.py
@@ -801,7 +801,8 @@ class Product(SQLBase, BugTargetBase, MakesAnnouncements,
 
     @cachedproperty
     def commercial_subscription(self):
-        return CommercialSubscription.selectOneBy(product=self)
+        return IStore(CommercialSubscription).find(
+            CommercialSubscription, product=self).one()
 
     @property
     def has_current_commercial_subscription(self):
@@ -984,9 +985,9 @@ class Product(SQLBase, BugTargetBase, MakesAnnouncements,
             lp_janitor = getUtility(ILaunchpadCelebrities).janitor
             now = datetime.datetime.now(pytz.UTC)
             date_expires = now + datetime.timedelta(days=30)
-            sales_system_id = 'complimentary-30-day-%s' % now
+            sales_system_id = u'complimentary-30-day-%s' % now
             whiteboard = (
-                "Complimentary 30 day subscription. -- Launchpad %s" %
+                u"Complimentary 30 day subscription. -- Launchpad %s" %
                 now.date().isoformat())
             subscription = CommercialSubscription(
                 product=self, date_starts=now, date_expires=date_expires,
@@ -1602,8 +1603,8 @@ def get_precached_products(products, need_licences=False,
 
     for subscription in IStore(CommercialSubscription).find(
         CommercialSubscription,
-        CommercialSubscription.productID.is_in(product_ids)):
-        cache = caches[subscription.productID]
+        CommercialSubscription.product_id.is_in(product_ids)):
+        cache = caches[subscription.product_id]
         cache.commercial_subscription = subscription
     if need_licences:
         for license in IStore(ProductLicense).find(
@@ -1939,7 +1940,7 @@ class ProductSet:
             subscription_expr = Exists(Select(
                 1, tables=[CommercialSubscription],
                 where=And(*
-                    [CommercialSubscription.productID == Product.id]
+                    [CommercialSubscription.product == Product.id]
                     + subscription_conditions)))
             if has_subscription is False:
                 subscription_expr = Not(subscription_expr)
diff --git a/lib/lp/registry/model/productjob.py b/lib/lp/registry/model/productjob.py
index 4213786..009645a 100644
--- a/lib/lp/registry/model/productjob.py
+++ b/lib/lp/registry/model/productjob.py
@@ -376,7 +376,7 @@ class CommericialExpirationMixin:
             )
         conditions = [
             Product.active == True,
-            CommercialSubscription.productID == Product.id,
+            CommercialSubscription.product == Product.id,
             CommercialSubscription.date_expires >= earliest_date,
             CommercialSubscription.date_expires < latest_date,
             Not(Product.id.is_in(Select(
diff --git a/lib/lp/registry/tests/test_product.py b/lib/lp/registry/tests/test_product.py
index d8c02e1..f34be9d 100644
--- a/lib/lp/registry/tests/test_product.py
+++ b/lib/lp/registry/tests/test_product.py
@@ -1582,7 +1582,7 @@ class ProductLicensingTestCase(TestCaseWithFactory):
         product = self.factory.makeProduct()
         self.factory.makeCommercialSubscription(product)
         with celebrity_logged_in('admin'):
-            product.commercial_subscription.sales_system_id = 'testing'
+            product.commercial_subscription.sales_system_id = u'testing'
             date_expires = product.commercial_subscription.date_expires
         with person_logged_in(product.owner):
             product.licenses = [License.OTHER_PROPRIETARY]
diff --git a/lib/lp/testing/factory.py b/lib/lp/testing/factory.py
index ac23612..0c9dca4 100644
--- a/lib/lp/testing/factory.py
+++ b/lib/lp/testing/factory.py
@@ -4675,9 +4675,10 @@ class BareLaunchpadObjectFactory(ObjectFactory):
         return fileupload
 
     def makeCommercialSubscription(self, product, expired=False,
-                                   voucher_id='new'):
+                                   voucher_id=u'new'):
         """Create a commercial subscription for the given product."""
-        if CommercialSubscription.selectOneBy(product=product) is not None:
+        if IStore(CommercialSubscription).find(
+                CommercialSubscription, product=product).one() is not None:
             raise AssertionError(
                 "The product under test already has a CommercialSubscription.")
         if expired:
@@ -4691,7 +4692,7 @@ class BareLaunchpadObjectFactory(ObjectFactory):
             registrant=product.owner,
             purchaser=product.owner,
             sales_system_id=voucher_id,
-            whiteboard='')
+            whiteboard=u'')
         del get_property_cache(product).commercial_subscription
         return commercial_subscription
 
@@ -4699,7 +4700,7 @@ class BareLaunchpadObjectFactory(ObjectFactory):
         """Give 'person' a commercial subscription."""
         product = self.makeProduct(owner=person)
         self.makeCommercialSubscription(
-            product, voucher_id=self.getUniqueString())
+            product, voucher_id=self.getUniqueUnicode())
 
     def makeLiveFS(self, registrant=None, owner=None, distroseries=None,
                    name=None, metadata=None, require_virtualized=True,