← Back to team overview

launchpad-reviewers team mailing list archive

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

 

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

Commit message:
Convert GPGKey to Storm

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/436126
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:stormify-gpgkey into launchpad:master.
diff --git a/lib/lp/registry/interfaces/gpg.py b/lib/lp/registry/interfaces/gpg.py
index 7599337..2924a83 100644
--- a/lib/lp/registry/interfaces/gpg.py
+++ b/lib/lp/registry/interfaces/gpg.py
@@ -51,14 +51,13 @@ class IGPGKey(IHasOwner):
         title=_("Key can be used for encryption"), required=True
     )
     owner = Int(title=_("Person"), required=True, readonly=True)
-    ownerID = Int(title=_("Owner ID"), required=True, readonly=True)
 
 
 class IGPGKeySet(Interface):
     """The set of GPGKeys."""
 
     def new(
-        ownerID,
+        owner,
         keyid,
         fingerprint,
         keysize,
diff --git a/lib/lp/registry/model/gpgkey.py b/lib/lp/registry/model/gpgkey.py
index 8dbbb7d..2eaf688 100644
--- a/lib/lp/registry/model/gpgkey.py
+++ b/lib/lp/registry/model/gpgkey.py
@@ -3,41 +3,60 @@
 
 __all__ = ["GPGKey", "GPGKeySet"]
 
+from storm.locals import And, Bool, Int, Not, Reference, Select, Unicode
 from zope.component import getUtility
 from zope.interface import implementer
 
 from lp.registry.interfaces.gpg import IGPGKey, IGPGKeySet
 from lp.services.database.enumcol import DBEnum
-from lp.services.database.sqlbase import SQLBase, sqlvalues
-from lp.services.database.sqlobject import (
-    BoolCol,
-    ForeignKey,
-    IntCol,
-    StringCol,
-)
+from lp.services.database.interfaces import IStore
+from lp.services.database.stormbase import StormBase
 from lp.services.gpg.interfaces import GPGKeyAlgorithm, IGPGHandler
+from lp.services.verification.model.logintoken import LoginToken
 
 
 @implementer(IGPGKey)
-class GPGKey(SQLBase):
+class GPGKey(StormBase):
 
-    _table = "GPGKey"
-    _defaultOrder = ["owner", "keyid"]
+    __storm_table__ = "GPGKey"
+    __storm_order__ = ["owner", "keyid"]
 
-    owner = ForeignKey(dbName="owner", foreignKey="Person", notNull=True)
+    id = Int(primary=True)
 
-    keyid = StringCol(dbName="keyid", notNull=True)
-    fingerprint = StringCol(dbName="fingerprint", notNull=True)
+    owner_id = Int(name="owner", allow_none=False)
+    owner = Reference(owner_id, "Person.id")
 
-    keysize = IntCol(dbName="keysize", notNull=True)
+    keyid = Unicode(name="keyid", allow_none=False)
+    fingerprint = Unicode(name="fingerprint", allow_none=False)
+
+    keysize = Int(name="keysize", allow_none=False)
 
     algorithm = DBEnum(
         name="algorithm", allow_none=False, enum=GPGKeyAlgorithm
     )
 
-    active = BoolCol(dbName="active", notNull=True)
+    active = Bool(name="active", allow_none=False)
+
+    can_encrypt = Bool(name="can_encrypt", allow_none=True)
 
-    can_encrypt = BoolCol(dbName="can_encrypt", notNull=False)
+    def __init__(
+        self,
+        owner,
+        keyid,
+        fingerprint,
+        keysize,
+        algorithm,
+        active,
+        can_encrypt=False,
+    ):
+        super().__init__()
+        self.owner = owner
+        self.keyid = keyid
+        self.fingerprint = fingerprint
+        self.keysize = keysize
+        self.algorithm = algorithm
+        self.active = active
+        self.can_encrypt = can_encrypt
 
     @property
     def keyserverURL(self):
@@ -58,7 +77,7 @@ class GPGKey(SQLBase):
 class GPGKeySet:
     def new(
         self,
-        ownerID,
+        owner,
         keyid,
         fingerprint,
         keysize,
@@ -68,7 +87,7 @@ class GPGKeySet:
     ):
         """See `IGPGKeySet`"""
         return GPGKey(
-            owner=ownerID,
+            owner=owner,
             keyid=keyid,
             fingerprint=fingerprint,
             keysize=keysize,
@@ -80,7 +99,7 @@ class GPGKeySet:
     def activate(self, requester, key, can_encrypt):
         """See `IGPGKeySet`."""
         fingerprint = key.fingerprint
-        lp_key = GPGKey.selectOneBy(fingerprint=fingerprint)
+        lp_key = IStore(GPGKey).find(GPGKey, fingerprint=fingerprint).one()
         if lp_key:
             assert lp_key.owner == requester
             is_new = False
@@ -89,12 +108,11 @@ class GPGKeySet:
             lp_key.can_encrypt = can_encrypt
         else:
             is_new = True
-            ownerID = requester.id
             keyid = key.keyid
             keysize = key.keysize
             algorithm = key.algorithm
             lp_key = self.new(
-                ownerID,
+                requester,
                 keyid,
                 fingerprint,
                 keysize,
@@ -104,30 +122,37 @@ class GPGKeySet:
         return lp_key, is_new
 
     def deactivate(self, key):
-        lp_key = GPGKey.selectOneBy(fingerprint=key.fingerprint)
+        lp_key = IStore(GPGKey).find(GPGKey, fingerprint=key.fingerprint).one()
         lp_key.active = False
 
     def getByFingerprint(self, fingerprint, default=None):
         """See `IGPGKeySet`"""
-        result = GPGKey.selectOneBy(fingerprint=fingerprint)
+        result = IStore(GPGKey).find(GPGKey, fingerprint=fingerprint).one()
         if result is None:
             return default
         return result
 
     def getGPGKeysForPerson(self, owner, active=True):
+        clauses = []
         if active is False:
-            query = """
-                active = false
-                AND fingerprint NOT IN
-                    (SELECT fingerprint FROM LoginToken
-                     WHERE fingerprint IS NOT NULL
-                           AND requester = %s
-                           AND date_consumed is NULL
-                    )
-                """ % sqlvalues(
-                owner.id
+            clauses.extend(
+                [
+                    Not(GPGKey.active),
+                    Not(
+                        GPGKey.fingerprint.is_in(
+                            Select(
+                                LoginToken.fingerprint,
+                                where=And(
+                                    LoginToken.fingerprint != None,
+                                    LoginToken.requester == owner,
+                                    LoginToken.date_consumed == None,
+                                ),
+                            )
+                        )
+                    ),
+                ]
             )
         else:
-            query = "active=true"
-        query += " AND owner=%s" % sqlvalues(owner.id)
-        return list(GPGKey.select(query, orderBy="id"))
+            clauses.append(GPGKey.active)
+        clauses.append(GPGKey.owner == owner)
+        return list(IStore(GPGKey).find(GPGKey, *clauses).order_by(GPGKey.id))
diff --git a/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.rst b/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.rst
index e4e17d7..7731369 100644
--- a/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.rst
+++ b/lib/lp/registry/stories/gpg-coc/xx-gpg-coc.rst
@@ -147,8 +147,13 @@ Certify the key is imported:
 Verify that the key was imported with the "can encrypt" flag set:
 
     >>> from lp.registry.model.gpgkey import GPGKey
-    >>> key = GPGKey.selectOneBy(
-    ...     fingerprint="A419AE861E88BC9E04B9C26FBA2B9389DFD20543"
+    >>> from lp.services.database.interfaces import IStore
+    >>> key = (
+    ...     IStore(GPGKey)
+    ...     .find(
+    ...         GPGKey, fingerprint="A419AE861E88BC9E04B9C26FBA2B9389DFD20543"
+    ...     )
+    ...     .one()
     ... )
     >>> print(key.owner.name)
     name12
diff --git a/lib/lp/registry/tests/test_gpgkey.py b/lib/lp/registry/tests/test_gpgkey.py
index 82f8f52..22f93d4 100644
--- a/lib/lp/registry/tests/test_gpgkey.py
+++ b/lib/lp/registry/tests/test_gpgkey.py
@@ -24,7 +24,7 @@ class GPGKeySetTests(TestCaseWithFactory):
         person = self.factory.makePerson()
         fingerprint = "DEADBEEF12345678DEADBEEF12345678DEADBEEF"
         keyset.new(
-            person.id,
+            person,
             "F0A432C2",
             fingerprint,
             4096,
diff --git a/lib/lp/soyuz/doc/publishing.rst b/lib/lp/soyuz/doc/publishing.rst
index 8e0e42e..98e1dd8 100644
--- a/lib/lp/soyuz/doc/publishing.rst
+++ b/lib/lp/soyuz/doc/publishing.rst
@@ -27,7 +27,7 @@ to make sure verifyObject will work.
     >>> from lp.registry.interfaces.person import IPersonSet
     >>> from lp.registry.model.gpgkey import GPGKey
     >>> name16 = getUtility(IPersonSet).getByName("name16")
-    >>> fake_signer = GPGKey.selectOneBy(owner=name16)
+    >>> fake_signer = IStore(GPGKey).find(GPGKey, owner=name16).one()
     >>> spph.sourcepackagerelease.signing_key_owner = fake_signer.owner
     >>> spph.sourcepackagerelease.signing_key_fingerprint = (
     ...     fake_signer.fingerprint
diff --git a/lib/lp/soyuz/doc/soyuz-upload.rst b/lib/lp/soyuz/doc/soyuz-upload.rst
index f32f5f7..18858ea 100644
--- a/lib/lp/soyuz/doc/soyuz-upload.rst
+++ b/lib/lp/soyuz/doc/soyuz-upload.rst
@@ -226,7 +226,7 @@ Assign the loaded GPG key to the katie user.
 
     >>> key_set = getUtility(IGPGKeySet)
     >>> user_key = key_set.new(
-    ...     ownerID=user.id,
+    ...     owner=user,
     ...     keyid=key.keyid,
     ...     fingerprint=key.fingerprint,
     ...     algorithm=key.algorithm,
diff --git a/lib/lp/soyuz/stories/webservice/xx-builds.rst b/lib/lp/soyuz/stories/webservice/xx-builds.rst
index ddce608..203e503 100644
--- a/lib/lp/soyuz/stories/webservice/xx-builds.rst
+++ b/lib/lp/soyuz/stories/webservice/xx-builds.rst
@@ -18,8 +18,9 @@ source publications can be retrieved.
     >>> from zope.security.proxy import removeSecurityProxy
     >>> from lp.registry.interfaces.person import IPersonSet
     >>> from lp.registry.model.gpgkey import GPGKey
+    >>> from lp.services.database.interfaces import IStore
     >>> name16 = getUtility(IPersonSet).getByName("name16")
-    >>> fake_signer = GPGKey.selectOneBy(owner=name16)
+    >>> fake_signer = IStore(GPGKey).find(GPGKey, owner=name16).one()
     >>> ppa = getUtility(IPersonSet).getByName("cprov").archive
     >>> for pub in ppa.getPublishedSources():
     ...     pub = removeSecurityProxy(pub)
diff --git a/lib/lp/soyuz/stories/webservice/xx-source-package-publishing.rst b/lib/lp/soyuz/stories/webservice/xx-source-package-publishing.rst
index 95ad1f0..c90197f 100644
--- a/lib/lp/soyuz/stories/webservice/xx-source-package-publishing.rst
+++ b/lib/lp/soyuz/stories/webservice/xx-source-package-publishing.rst
@@ -16,10 +16,11 @@ We need to insert some dependent fake data before continuing.
     >>> from zope.security.proxy import removeSecurityProxy
     >>> from lp.registry.interfaces.person import IPersonSet
     >>> from lp.registry.model.gpgkey import GPGKey
+    >>> from lp.services.database.interfaces import IStore
     >>> from lp.services.webapp.interfaces import OAuthPermission
     >>> from lp.testing.pages import webservice_for_person
     >>> name16 = getUtility(IPersonSet).getByName("name16")
-    >>> fake_signer = GPGKey.selectOneBy(owner=name16)
+    >>> fake_signer = IStore(GPGKey).find(GPGKey, owner=name16).one()
     >>> cprov_db = getUtility(IPersonSet).getByName("cprov")
     >>> cprov_ppa = cprov_db.archive
     >>> for pub in cprov_ppa.getPublishedSources():
diff --git a/lib/lp/testing/factory.py b/lib/lp/testing/factory.py
index 9a672aa..a395a92 100644
--- a/lib/lp/testing/factory.py
+++ b/lib/lp/testing/factory.py
@@ -594,7 +594,7 @@ class LaunchpadObjectFactory(ObjectFactory):
         fingerprint = key_id + "A" * 32
         keyset = getUtility(IGPGKeySet)
         key = keyset.new(
-            owner.id,
+            owner,
             keyid=key_id,
             fingerprint=fingerprint,
             keysize=self.getUniqueInteger(),
diff --git a/lib/lp/testing/gpgkeys/__init__.py b/lib/lp/testing/gpgkeys/__init__.py
index 46b9581..42a42f7 100644
--- a/lib/lp/testing/gpgkeys/__init__.py
+++ b/lib/lp/testing/gpgkeys/__init__.py
@@ -60,7 +60,7 @@ def import_public_key(email_addr):
     # Insert the key into the database.
     keyset = getUtility(IGPGKeySet)
     key = keyset.new(
-        ownerID=person.id,
+        owner=person,
         keyid=key.keyid,
         fingerprint=key.fingerprint,
         keysize=key.keysize,
diff --git a/utilities/make-lp-user b/utilities/make-lp-user
index 8be2a16..c856fa3 100755
--- a/utilities/make-lp-user
+++ b/utilities/make-lp-user
@@ -170,7 +170,7 @@ def add_gpg_key(person, fingerprint):
 
     can_encrypt = True
     lpkey = gpgkeyset.new(
-        person.id,
+        person,
         key.keyid,
         fingerprint,
         key.keysize,