diff options
Diffstat (limited to 'src/mailman/model/pending.py')
| -rw-r--r-- | src/mailman/model/pending.py | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/src/mailman/model/pending.py b/src/mailman/model/pending.py index 17513015c..49b12c16a 100644 --- a/src/mailman/model/pending.py +++ b/src/mailman/model/pending.py @@ -31,7 +31,9 @@ import random import hashlib from lazr.config import as_timedelta -from storm.locals import DateTime, Int, RawStr, ReferenceSet, Unicode +from sqlalchemy import ( + Column, DateTime, ForeignKey, Integer, LargeBinary, Unicode) +from sqlalchemy.orm import relationship from zope.interface import implementer from zope.interface.verify import verifyObject @@ -49,31 +51,35 @@ from mailman.utilities.modules import call_name class PendedKeyValue(Model): """A pended key/value pair, tied to a token.""" + __tablename__ = 'pendedkeyvalue' + + id = Column(Integer, primary_key=True) + key = Column(Unicode) + value = Column(Unicode) + pended_id = Column(Integer, ForeignKey('pended.id'), index=True) + def __init__(self, key, value): self.key = key self.value = value - id = Int(primary=True) - key = Unicode() - value = Unicode() - pended_id = Int() - @implementer(IPended) class Pended(Model): """A pended event, tied to a token.""" + __tablename__ = 'pended' + + id = Column(Integer, primary_key=True) + token = Column(LargeBinary) + expiration_date = Column(DateTime) + key_values = relationship('PendedKeyValue') + def __init__(self, token, expiration_date): super(Pended, self).__init__() self.token = token self.expiration_date = expiration_date - id = Int(primary=True) - token = RawStr() - expiration_date = DateTime() - key_values = ReferenceSet(id, PendedKeyValue.pended_id) - @implementer(IPendable) @@ -105,7 +111,7 @@ class Pendings: token = hashlib.sha1(repr(x)).hexdigest() # In practice, we'll never get a duplicate, but we'll be anal # about checking anyway. - if store.find(Pended, token=token).count() == 0: + if store.query(Pended).filter_by(token=token).count() == 0: break else: raise AssertionError('Could not find a valid pendings token') @@ -114,10 +120,10 @@ class Pendings: token=token, expiration_date=now() + lifetime) for key, value in pendable.items(): - if isinstance(key, str): - key = unicode(key, 'utf-8') - if isinstance(value, str): - value = unicode(value, 'utf-8') + if isinstance(key, bytes): + key = key.decode('utf-8') + if isinstance(value, bytes): + value = value.decode('utf-8') elif type(value) is int: value = '__builtin__.int\1%s' % value elif type(value) is float: @@ -129,7 +135,7 @@ class Pendings: value = ('mailman.model.pending.unpack_list\1' + '\2'.join(value)) keyval = PendedKeyValue(key=key, value=value) - pending.key_values.add(keyval) + pending.key_values.append(keyval) store.add(pending) return token @@ -137,7 +143,7 @@ class Pendings: def confirm(self, store, token, expunge=True): # Token can come in as a unicode, but it's stored in the database as # bytes. They must be ascii. - pendings = store.find(Pended, token=str(token)) + pendings = store.query(Pended).filter_by(token=str(token)) if pendings.count() == 0: return None assert pendings.count() == 1, ( @@ -146,31 +152,32 @@ class Pendings: pendable = UnpendedPendable() # Find all PendedKeyValue entries that are associated with the pending # object's ID. Watch out for type conversions. - for keyvalue in store.find(PendedKeyValue, - PendedKeyValue.pended_id == pending.id): + entries = store.query(PendedKeyValue).filter( + PendedKeyValue.pended_id == pending.id) + for keyvalue in entries: if keyvalue.value is not None and '\1' in keyvalue.value: type_name, value = keyvalue.value.split('\1', 1) pendable[keyvalue.key] = call_name(type_name, value) else: pendable[keyvalue.key] = keyvalue.value if expunge: - store.remove(keyvalue) + store.delete(keyvalue) if expunge: - store.remove(pending) + store.delete(pending) return pendable @dbconnection def evict(self, store): right_now = now() - for pending in store.find(Pended): + for pending in store.query(Pended).all(): if pending.expiration_date < right_now: # Find all PendedKeyValue entries that are associated with the # pending object's ID. - q = store.find(PendedKeyValue, - PendedKeyValue.pended_id == pending.id) + q = store.query(PendedKeyValue).filter( + PendedKeyValue.pended_id == pending.id) for keyvalue in q: - store.remove(keyvalue) - store.remove(pending) + store.delete(keyvalue) + store.delete(pending) |
